Home >Backend Development >C++ >How Can I Programmatically Enable or Disable Devices in Win32, and What are the Limitations?
Windows provides APIs in the SetupDi family for managing devices programmatically, including enabling and disabling them. However, not all devices support being disabled this way.
In the case of mouse devices, the default PS/2-compatible mouse driver does not support disablement via SetupDi APIs due to concerns about destabilizing the hardware when hot-detaching actual old mice using PS/2 connectors.
To determine if a device can be disabled using SetupDi APIs, check the Device Manager for a "Disable" option. If present, you can use the SetupDi APIs. If absent, you're left with IOCTL communication options.
If your mouse driver supports disablement via SetupDi APIs, the following code snippet demonstrates how to disable and re-enable it using C# and P/Invoke:
public static void EnableMouse(bool enable) { Guid mouseGuid = new Guid("{4d36e96f-e325-11ce-bfc1-08002be10318}"); string instancePath = @"ACPI\PNP0F03&3688D3F&0"; DeviceHelper.SetDeviceEnabled(mouseGuid, instancePath, enable); }
To simplify the task, you can use the DeviceHelper library from the provided code block. It exposes a SetDeviceEnabled method that takes as input the class GUID, instance ID, and enable/disable flag.
The above is the detailed content of How Can I Programmatically Enable or Disable Devices in Win32, and What are the Limitations?. For more information, please follow other related articles on the PHP Chinese website!