I am writing a small C# app to disable a device (my laptop touchpad) whenever another mouse device is detected, and enable the touchpad again if a mouse is not detected. I am not even able to disable the touchpad in device manager (it is running on the default mouse class driver).
I am getting into device driver development so I thought maybe I could write a little filter driver that would just accept IOCTLs to enable and disable passing mouse event messages up the device stack, and get messages from user mode via a raw PDO. However, I asked that question and somebody has suggested that I can do this in usermode via the SetupDi.. functions. That would be really good, because this raw PDO communication method is a PITA to work with.
I have only used SetupDiGetClassDevs before, and there are so many of them, can someone with more experience with this part of the Win32 API just tell me quickly what one I should call to stop/disable a mouse device or its interface or if there is something somewhere in the dark corners of the framework that will do this (maybe in WMI?).
Update (24/9/09) I figured out how to do this with a filter driver and posted how I did it on my original question. I still want to know if it is possible to enable or disable devices directly from Win32 and if so, how – so I will leave this question open.
You can enable/disable devices from Win32 (and hence from C# via P/Invoke) using the SetupDi APIs but not all devices are "disable-able" in this way.
The problem you’ll run into trying to disable your touchpad from Win32 (or WMI or any other API which calls down into the
SetupDi*family of functions) is that the default mouse driver which is in most laptops with a touchpad ("PS/2 compatible mouse") doesn’t support being disabled usingSetupDiAPI’s. I suspect this may be because actual old mice using PS/2 connectors can’t be hot-detached without hosing the hardware.To verify that you can’t disable, go into Device Manager and right-click on your mouse driver. If you see a disable option, you can use SetupDi to disable it. If no disable option, you’re out of luck… welcome to IOCTL-land!
If you do see a disable option, then the code below (ported to C# from a VB sample I found here) should let you disable and re-enable the device.
Here’s the code to call the library:
Here’s the library itself, adapted from here.
Note that when you get an Index-Out-Of-Bounds exception on the line
int index = GetIndexOfInstance(diSetHandle, diData, instanceId);, you might have used the wrong classGuid for the device or the wrong instanceId.Also note that when you run this code on a 64-bit Windows platform, you should target the 64-bit platform when you build your application. Otherwise – i.e. when running your application as a 32-bit process on a 64-bit Windows platform – you will get an SetupAPI error InWow64 (ERROR_IN_WOW64).
When targetting a 64-bit Windows platform, you might also have to make changes to other parts of your application, e.g. when doing pointer arithmetic, in order to prevent overflows.