Upper filter device driver for a mouse

…and for suggestion of a good driver class to take. Obviously, OSR’s :wink:

Bonne chance!

– pa

“…figure out which pointing device an event came from…” This hardly needs any more than 5 additional lines of code in the WDK source code sample moufiltr. Then a few more lines of code in a low level mouse hook WH_MOUSE_LL solve the whole problem:

Mouse filter driver can populate the ExtraInformation member of MOUSE_INPUT_DATA
WH_MOUSE_LL hook can look at dwExtraInfo member of MSLLHOOKSTRUCT

PS: Careful! ExtraInformation values need to be compatible with existing predefined ones like MOUSEEVENTF_FROMTOUCH

Marcel Ruedinger
datronicsoft

…oooops - only now I noticed another completely wrong statement which needs to be corrected…

Couldn’t be more wrong: “…because proper multipointer support requires changes on a level lower than what you have access to on Windows. You would need to change Window’s UI model to have more than one pointer, which isn’t possible.”

The truth is: Microsoft offers a “MultiPoint Mouse SDK” for exactly this purpose.

Marcel Ruedinger

datronicsoft

@Pavel : I tried first using the raw API but it didn’t work as I couldn’t intercept and block some of the mouse messages. Please refer to this link for more information (I feel like I’m repeating myself too much ^^): https://stackoverflow.com/questions/44970592/using-two-mice-to-perform-completely -different-actions-in-windows

I had a look at the seminars (if it’s what you mean by “class”) proposed by osr but they are way way too expensive to afford for a student :wink:

@Marcel Ruedinger : as for everything, I’m sure it’s easy to do once you know how ! :wink: The problem being that I still have a lot to understand and I have difficulties finding relevant information on the internet to solve the problems I’m currently facing. That’s why I would appreciate if you could take the time to answer some of the questions I asked there :

https://stackoverflow.com/questions/45196112/upper-filter-device-driver-for-a-mouse

As I’m doubting that some of you have actually read the post, I will copy/past it there :

"
I’ve just started diving into driver related stuffs as what I want to do (cf. here) seems undoable with a “regular” application. For now, I’m just trying to install a Kernel Mode Driver (KMDF) as an upper filter for the mouse I want to modify the behaviour. The source files are not empty as they are filled in according to the Visual Studio KMDF template. The only modification I did so far in those source files was adding those two lines of code in the CreateDevice routine :

WdfFdoInitSetFilter(DeviceInit);

WdfDeviceInitSetDeviceType(DeviceInit, FILE_DEVICE_MOUSE);

to specify that the driver is a mouse filter driver.

However, I’m having trouble to install it as I would like on my virtual machine. Here is the INF file (don’t pay too much attention to the comments, I just added them to better understand what’s going on as it’s the first time I’m confronted to such a file):

;
; EGMC_filter.inf
;

[Version]
Signature=“$WINDOWS NT$” ;Operating system for which the INF file is valid
Class=Mouse ; The class is mouse
ClassGuid={4d36e96f-e325-11ce-bfc1-08002be10318} ; Mouse class GUID
Provider=%ManufacturerName% ; is specified in the Strings section (bottom of this file)
CatalogFile=EGMC_filter.cat ; for signature
DriverVer= ; TODO: set DriverVer in stampinf property pages

; The DestinationDirs section specifies the target destination directory for all copy, delete and/or rename operations
; on files referenced by name elsewhere in the INF file

[DestinationDirs]
DefaultDestDir = 12 ; directory is \Drivers on WinNT platforms
EGMC_filter_Device_CoInstaller_CopyFiles = 11 ; Directory is \system32 on WinNt platforms

[SourceDisksNames]
1 = %DiskName%,“”

[SourceDisksFiles]
EGMC_filter.sys = 1,
WdfCoInstaller$KMDFCOINSTALLERVERSION$.dll=1 ; make sure the number matches with SourceDisksNames

;*****************************************
; Install Section
;*****************************************

[Manufacturer] ; This section is used to list all devices handled by the driver
%ManufacturerName%=Standard,NT$ARCH$ ;stands for architecture. Is modified depending on x64 or x86

[Standard.NT$ARCH$] ; This is a model section
%EGMC_filter.DeviceDesc%=EGMC_filter_Device, USB\VID_093A&PID_2510; Specifying the Hardware ID for which to install the driver. No revision nb specified

[EGMC_filter_Device.NT] ; This is a DDInstall section
CopyFiles=Drivers_Dir ; CopyFiles directive that specify a copyfiles section (Drivers_Dir)
AddReg=EGMC_filter.AddReg

[EGMC_filter.AddReg]
HKR,“UpperFilters”,0x0010008,“EGMC_filter” ; REG_MULTI_SZ value

[Drivers_Dir] ; This is a CopyFile section.
EGMC_filter.sys ; All the files in this section are copied to the specified directory in the DestinationDirs section.

;-------------- Service installation

[EGMC_filter_Device.NT.Services]
AddService = EGMC_filter,%SPSVCINST_ASSOCSERVICE%, EGMC_filter_Service_Inst ; create a service installation section

; -------------- EGMC_filter driver install sections
[EGMC_filter_Service_Inst]
DisplayName = %EGMC_filter.SVCDESC%
ServiceType = 1 ; SERVICE_KERNEL_DRIVER
StartType = 3 ; SERVICE_DEMAND_START
ErrorControl = 1 ; SERVICE_ERROR_NORMAL
ServiceBinary = %12%\EGMC_filter.sys ; create a value in the registry with the fully qualified path of the driver’s file

;
;— EGMC_filter_Device Coinstaller installation ------
;

[EGMC_filter_Device.NT.CoInstallers]
AddReg=EGMC_filter_Device_CoInstaller_AddReg ;Add a registry section
CopyFiles=EGMC_filter_Device_CoInstaller_CopyFiles

[EGMC_filter_Device_CoInstaller_AddReg]
HKR,CoInstallers32,0x00010000, “WdfCoInstaller$KMDFCOINSTALLERVERSION$.dll,WdfCoInstaller”

[EGMC_filter_Device_CoInstaller_CopyFiles]
WdfCoInstaller$KMDFCOINSTALLERVERSION$.dll

[EGMC_filter_Device.NT.Wdf]
KmdfService = EGMC_filter, EGMC_filter_wdfsect
[EGMC_filter_wdfsect]
KmdfLibraryVersion = $KMDFVERSION$

[Strings]
SPSVCINST_ASSOCSERVICE= 0x00000002
ManufacturerName=“EGMC” ; My manufacturer name
ClassName=“Mouse” ; ClassName of the device
DiskName = “EGMC_filter Installation Disk” ; the disk name. DON’T KNOW WHERE IT COMES FROM !!!
EGMC_filter.DeviceDesc = “EGMC Filter Device”
EGMC_filter.SVCDESC = “EGMC_filter Service”

I’m trying to install my driver in a virtual machine running Windows 10. The VM is configured to support USB 2.0 and I’ve added a USB device filter in order for the VM to detect my particular mouse (otherwise, the VM couldn’t distinguish the different mice connected).

First I tried deploying it via Visual Studio using the “Install/Reinstall and Verify” option in the project properties (configuration properties -> Driver Install -> Deployment). Note that remove previous driver versions before deployment is selected.

It didn’t work and I got the following error messages :

[17:32:01:625]: ERROR: Task “Default Driver Package Installation Task” failed to complete successfully. Look at the logs in the driver test group explorer for more details on the failure.
[17:32:02:348]: Driver Post Install Actions
[17:32:02:348]: Removing any existing files from test execution folder.
[17:32:02:375]: Copying required files for “Driver Post Install Actions”.
[17:32:02:934]: [Driver Post Install Actions] Command Line:
$KitRoot$\Testing\Runtimes\TAEF\te.exe “%SystemDrive%\DriverTest\Run\DriverTestTasks.dll” /select:“@Name=‘DriverTestTasks::_DriverPostInstall’” /rebootStateFile:%SystemDrive%\DriverTest\Run\DriverTestReboot.xml /enableWttLogging /wttDeviceString:$LogFile:file=“%SystemDrive%\DriverTest\Run\Driver_Post_Install_Actions_00011.wtl”,writemode=append,encoding=unicode,nofscache=true,EnableLvl=“WexStartTest|WexEndTest|WexXml|WexProperty|WexCreateContext|WexCloseContext|*” /runas:Elevated
[17:32:05:387]: Result Summary: Total=1, Passed=1, Failed=0, Blocked=0, Warned=0, Skipped=0
[17:32:05:388]: Task “Driver Post Install Actions” completed successfully
Driver Deployment Task Failed: Default Driver Package Installation Task

It may sound stupid but I was unable to locate the logs in the driver test group explorer to get more info about the error.

Secondly, I tried using the “Hardware ID Driver” option instead of the “Install/Reinstall and Verify” one with the following Hardware ID “USB\VID_093A&PID_2510” (same as the one specified in the INF). It worked this time. However, I’m not sure to understand exactly why. So now that you got a better picture of what I’m doing, here are my questions :

  1. I did understand that the SourceDisksNames section is used to specify the name of a disk (CD-rom, DVD) containing the .sys file of the driver. However, I don’t understand how to install a driver from such a disk practically. Imagine I buy a specific hardware and it comes with a CD with the corresponding driver files. Once the CD is in the player, what should I do ? Should I run the INF file in order to install it ? From what I remember the only time I had to install a driver from a disk it was a .exe file.

  2. What if I want to release my driver package via Internet ? What’s the point of this section then ?

  3. By default when I created the project, VS filled the disk name with “EGMC_filter Installation Disk”. What does it correspond to ? Should I modify it ?

  4. I don’t understand how the deployment process works. I guess that VS first transfers the driver package (containing the .INF, .cat and .sys) to the VM and then launches the .INF file in the VM. Is that right ? If yes, what does the DiskName correspond to in the VM ? Does VS create some kind of ISO containing the package ? Where can I find where the package is stored in the VM ?

  5. Does the deployment process differ when using the “Hardware ID Driver Update” and the “Install/Reinstall [?]” options ?

  6. Any idea why I can’t install my driver using the “Install/Reinstall and Verify” option ? What’s wrong with the INF file ? Has it something to do with the Disk name ?

  7. Despite having read this page, I’m confused about why I can deploy my driver with the “Hardware ID” option considering that I didn’t respect the form Root\xxx (I saw this page afterwards). However, it seems clear that for what I want to do, I must install my driver using the other option. Any idea why it still seems to work (at least partially) ?

  8. After the “incorrect” installation is completed, a “EGMC Filter Device” entry is added to the Device Manager under Mice and other pointer devices even when the mouse of interest is unplugged. Why is this ? When I plug it, another entry appears.

  9. When my driver is installed, the mouse doesn’t work anymore (it doesn’t even light up !). However, as the upper filter doesn’t do anything for now, the mouse should work normally. The only explanation I’ve found so far is that the driver is installed as a function driver and not as a filter one. Could it be the case ? In the registry of the VM, I’m unable to find the UpperFilters keys with the “EGMC_filter” value in the section “Ordinateur\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class{4d36e96f-e325-11ce-bfc1-08002be10318}”. The only one I found is : “UpperFilters : VboxMouse mouclass”. It comforts me in my idea that the driver is installed as a function one? Could the VM interferes with my driver ?

  10. The driver details of the mouse of interest (left) and the regular one (right) also confuse me as the mouhid is missing from the former. Driver details

  11. Lastly, what’s the point of specifying in the driver code that it’s a filter driver ? What does it change concretely ?

I know it’s a long post and that I’m asking quite specific questions but I hope you won’t mind and hopefully some experts right there will be able to answer them.

I’m looking forward to your replies.

Guillaume."

Please, for question 10, refer to the actual post to see the pictures.

Thanks all.

First you ask: “I am tuning a car engine adding more horse powers (tune Windows adding different mouse functionality). How can I do it?”.

Then after somebody told you how to do it, you ask: “What exactly is a piston (Filter Driver) and how does a spark-plug (INF file setup) work and what is its significance?”

Just in case it helps, here are the answers to the questions:
1)2)3) Just a name and just needs to be present (no further significance for this purpose here)
4)5)7) Don’t even try to understand Visual Studio Filter Driver deployment before you understood MANUAL Filter Driver deployment WITHOUT Visual Studio. You might get away without understanding when installing a Function Driver, but never when installing a Filter Driver.
6)9)10) The posted INF file is not even remotely seeming to install a filter driver. It has so many issues that I wouldn’t even know where to start explaining in a public forum. However it has nothing to do with the disk name.
8) Then the driver is still installed on the Root bus. INF Hardware ID is ignored when installing on Root bus.
11) The whole IRP/Request processing is different in a filter driver. Power management is different as well. Filter driver does not manage power policy for the device stack (mapping from S-Power-States to D-Power-States).

Marcel Ruedinger
datronicsoft

Marcel Ruedinger wrote

The truth is: Microsoft offers a “MultiPoint Mouse SDK” for exactly this
purpose.

The download link for this kit redirects to the Multipoint Server, it is a different product by itself, it definitely has custom kernel-side layer. It isn’t clear whether this SDK can work on a normal client OS.

Anyway… IIRC several years ago one poor soul tried to make something like OP wanted, using mouse filters, then USB simulation framework; he smashed his head endlessly on e wall… I don’t remember what happened to him. Life is short, there’s so many exciting projects. Learning how to write a mouse filter from scratch is not one of them.

– pa

Surprise surprise - the download link of “Microsoft MultiPoint Mouse SDK” is currently pointing to “Microsoft MultiPoint Server”, indeed.
Wrong link (accidental name confusion)?
Or is “MultiPoint Mouse SDK” meanwhile integrated into “MultiPoint Server”?
Last time I used “Microsoft Multipoint Mouse SDK” was in 2007.
At that time, it was a standalone msi running on regular Windows OS.
Did “Microsoft MultiPoint Server” product even exist at that time?

Great comment by the way:

This assignment has huge potential to turn the developer into a poor soul smashing the head endlessly to the wall. Taking weeks of being “almost done”, then taking months of still being “almost done” and no one will ever hear or remember the end of the story.

On the other hand, life is short and learning how to write a mouse filter from the scratch is not fun. There are so many other exciting projects. Much easier to hire a consultant with the right expertise who can do the assignment within a few days only using very few lines of code.

Marcel Ruedinger
datronicsoft

@Pavel A
I almost overlooked one topic: What makes you think that “…Multipoint Server…definitely has custom kernel-side layer.”?

Marcel Ruedinger
datronicsoft

@ the OP

The %SPSVCINST_ASSOCSERVICE% property is not valid for a filter driver service. Replace the following line:

AddService = EGMC_filter,%SPSVCINST_ASSOCSERVICE%, EGMC_filter_Service_Inst ; create a service installation section

with this line:

AddService = EGMC_filter, EGMC_filter_Service_Inst ; create a service installation section, no %SPSVCINST_ASSOCSERVICE% for a filter driver

With your deployement options, your driver is deployed as a PNP function driver and not as a PNP filter driver so that your driver replaces the original driver for the device with hardware ID:
USB\VID_093A&PID_2510

That is why your device does not function properly once your driver is deployed.

In your driver project property page, go to ‘Driver Install’ -> ‘Deployement’, then make sure the ‘Do not install’ button is checked.

With this setup, VS will only copy the driver package to the target machine. You should do this for (NDIS, FS, PNP) filter drivers.

PNP filter drivers are installed with a setup application that opens the INF files and ‘executes’ some INF sections. Typically these sections are the CopyFiles, AddService and AddReg sections.

So you should add a console application to your driver solution and deploy this app with your driver. The app will perform the installation of the filter driver this way:

  1. The app uses SetupApi functions to enumerate all devices with hardware ID USB\VID_093A&PID_2510. You could enumerate the devices of the Mouse setup class and then query the hardware IDs.
  2. If at least one device is found then:
    2a) Open the driver INF file.
    2b) Execute the CopyFiles section (don’t forget to commit the file queue with SetupCommitFileQueue).
    2c) Execute the Addservice section.
    2d) For each device, open the device’s registry key and execute the AddReg section of the INF file with this registry key (key handle) as the root key (HKR).
    2e) Disable and enable the device stack so that the new filter is loaded.

This is explained here:

https://docs.microsoft.com/en-us/windows-hardware/drivers/install/installing-a-filter-driver

Good luck.

@ W.D.

Good idea to suggest checking ‘Do not install’ in Visual Studio. But why suggest such a cumbersome, error prone and ineffective approach for installing a HID mouse device filter driver?

A professional HID mouse device filter driver is installed by reinstalling mouhis.sys using the original operating system supplied mouse INF file using a few INF sections like the one below:
[EGMC_filter_Device.NT]
Include=msmouse.inf
Needs=HID_Mouse_Inst

Then you don’t need to deliver any console app for installing the driver. Then you can install the filter driver like a regular Plug’n’Play driver by “Right Mouse Click Update” in Device Manager or by DifX API or by DPINST or even using Setup API, but a much less complicated sequence.

PS: You missed quite a few more mistakes in the INF file. E.g. the “UpperFilter” AddReg was in the wrong section [xxx.NT]. Correct section is [xxx.NT.HW]

Marcel Ruedinger
datronicsoft

>But why suggest such a cumbersome, error prone and ineffective approach for installing a HID
mouse device filter driver?

Because that is what is suggested in the MSDN link provided later in my post.

Anyway, I started the message with:

@ the OP

@ W.D. Valid answer to my question - no argue :wink:

However, still a nice example how even just following the documentation can quickly contribute to “…smashing the head endlessly to the wall.” in this case.

PS: The INF file technique I suggested can easily be seen and understood in Microsoft WDK sample input\moufiltr It just needs to be changed from PS2 mouse to HID mouse.

Marcel Ruedinger
datronicsoft

Marcel’s guidance with the inf Israel prescribed microsoft guidance

Bent from my phone


From: xxxxx@lists.osr.com on behalf of xxxxx@datronic.de xxxxx@lists.osr.com
Sent: Saturday, July 22, 2017 10:23:56 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Upper filter device driver for a mouse

@ W.D. Valid answer to my question - no argue :wink:

However, still a nice example how even just following the documentation can quickly contribute to “…smashing the head endlessly to the wall.” in this case.

PS: The INF file technique I suggested can easily be seen and understood in Microsoft WDK sample input\moufiltr It just needs to be changed from PS2 mouse to HID mouse.

Marcel Ruedinger
datronicsoft


NTDEV is sponsored by OSR

Visit the list online at: https:

MONTHLY seminars on crash dump analysis, WDF, Windows internals and software drivers!
Details at https:

To unsubscribe, visit the List Server section of OSR Online at https:</https:></https:></https:>

Yes, good job Mr Ruedinger. Here is a link for a good template file to start with :

http://contents.driverguide.com/content.php?id=1257354&path=logitech.inf

This is a better option, because, if the user unplugs the mouse and plugs it in a different USB port, and a brand new device is created with a new registry configuration, the driver would need a companion app that would wait for PNP notifications to handle this case.

With this kind of inf file you could avoid using apps. Hope this is correct.

Sorry, this inf file template is probably easier to work with :

https://github.com/Microsoft/Windows-driver-samples/blob/master/input/moufiltr/moufiltr.inx

Thanks to your help, I was finally able to install the driver as an upper filter one in the stack of interest.

Still a long long way to go as I’m now getting a “code 10” error. I will keep looking to figure out what’s going on. I guess it has now something do to with the code itself and not the .inf file.

I do partially agree to this comment :

“This assignment has huge potential to turn the developer into a poor soul smashing the head endlessly to the wall [?] There are so many other exciting projects. Much easier to hire a consultant with the right expertise who can do the assignment within a few days only using very few lines of code.”

As I’m sometimes wondering whether it’s really worth to learn how to code this driver by myself as I won’t certainly ever have to use this knowlege later in my life. This doubt is even more nourished considering the difficulty of the task. However, I consider my idea/project exciting and picturing myself using the end product to control my computer helps me stay motivated.

I wish I could hire a consultant to do it for me but as a student, I?clearly don’t have the money for that. In fact, I would really like to find a Windows Driver Developer who would be willing to spend for free a couple hours with me (either irl or using skype for instance) to brief me on driver development and teach me the basis for what I want to accomplish so I could start being productive way faster. But I guess I’m just being a stargazer there? Anyway, if you do know one, just let me know :slight_smile:

Code 10 means a failure to start the device. So look at your power up and self managed io routines

Bent from my phone


From: xxxxx@lists.osr.com on behalf of xxxxx@gmail.com xxxxx@lists.osr.com
Sent: Sunday, July 23, 2017 9:09:13 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Upper filter device driver for a mouse

Thanks to your help, I was finally able to install the driver as an upper filter one in the stack of interest.

Still a long long way to go as I’m now getting a “code 10” error. I will keep looking to figure out what’s going on. I guess it has now something do to with the code itself and not the .inf file.

I do partially agree to this comment :

“This assignment has huge potential to turn the developer into a poor soul smashing the head endlessly to the wall [?] There are so many other exciting projects. Much easier to hire a consultant with the right expertise who can do the assignment within a few days only using very few lines of code.”

As I’m sometimes wondering whether it’s really worth to learn how to code this driver by myself as I won’t certainly ever have to use this knowlege later in my life. This doubt is even more nourished considering the difficulty of the task. However, I consider my idea/project exciting and picturing myself using the end product to control my computer helps me stay motivated.

I wish I could hire a consultant to do it for me but as a student, I?clearly don’t have the money for that. In fact, I would really like to find a Windows Driver Developer who would be willing to spend for free a couple hours with me (either irl or using skype for instance) to brief me on driver development and teach me the basis for what I want to accomplish so I could start being productive way faster. But I guess I’m just being a stargazer there? Anyway, if you do know one, just let me know :slight_smile:


NTDEV is sponsored by OSR

Visit the list online at: https:

MONTHLY seminars on crash dump analysis, WDF, Windows internals and software drivers!
Details at https:

To unsubscribe, visit the List Server section of OSR Online at https:</https:></https:></https:>

Marcel R. wrote

@Pavel A
I almost overlooked one topic: What makes you think that “…Multipoint
Server…definitely has custom kernel-side layer.”?

Many moons ago I was at interview in a company that needed custom setup based on the Multipoint server, they discussed some internals. IIRC there are (were?) filters or virtual USB hubs that protect USB devices of one user from other users, and also something to assign displays attached to multi-head video cards to users. Maybe all this has changed in the last version, I have not looked again.

Regards,
P.

OK, if you call a simple USB filter/redirector driver a “custom kernel-side layer” in the context of discussing “normal client OS” architecture, then I might possibly have a slightly different opinion about terminology, but I don’t want to start nitpicking here.

Much more interesting is Multipoint Server graphics adapter and display assignment to users. To me it only seemed like each locally attached display Monitor was used by a separate RDP client running in the local console session. I didn’t see any indicator nor necessity for any kernel mode layer or component to assign displays to users. If there was, I would be very much interested to hear and learn about it.

Marcel Ruedinger
datronicsoft

Hey,
I was discussing with a friend this afternoon about my project and the difficulties I was having developing this driver and he told me that I could probably achieve the same functionnalities using this :

https://msdn.microsoft.com/en-us/library/windows/hardware/dn376885(v=vs.85).aspx

instead of writing an upper filter device driver.

What do you think ? To me, it seems indeed to be a good solution and would remove a lot of the burden that I would get developing a driver. (the app would be running in the background, communicating to the mouse device and sending messages to the other apps?).

Guillaume.