Home > Backend Development > C++ > Can Win32 APIs Programmatically Enable or Disable All Devices?

Can Win32 APIs Programmatically Enable or Disable All Devices?

Barbara Streisand
Release: 2025-01-06 02:04:40
Original
611 people have browsed it

Can Win32 APIs Programmatically Enable or Disable All Devices?

Controlling Device State via Win32 API

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:

  1. Get the device class GUID: Find it in Device Manager's "Properties" dialog.
  2. Get the device instance path: Retrieve it from Device Manager's "Properties" dialog.
  3. Use the provided code to enable or disable the device:
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);
}
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template