Problem:
Enabling or disabling devices programmatically using Win32 APIs.
Discussion:
Not all devices can be disabled or enabled through SetupDi APIs. Default mouse drivers do not support this functionality. This limitation arises from the inability to hot-detach PS/2-connected mice without hardware consequences.
To verify if a device can be disabled via Win32 APIs, check the "Disable" option in Device Manager. If absent, the device cannot be disabled this way.
If the Disable option is present, follow these steps:
public static void EnableMouse(bool enable) { // mouse class GUID Guid mouseGuid = new Guid("{4d36e96f-e325-11ce-bfc1-08002be10318}"); // example instance path: @"\ACPI\PNP0F03&3688D3F&0" string instancePath = @"\ACPI\PNP0F03&3688D3F&0"; DeviceHelper.SetDeviceEnabled(mouseGuid, instancePath, enable); }
Code:
Refer to the provided code in the DeviceHelper class.
Limitations:
This method may fail for devices that cannot be disabled via SetupDi APIs.
The above is the detailed content of Can Win32 APIs Programmatically Enable or Disable All Devices?. For more information, please follow other related articles on the PHP Chinese website!