Home > Backend Development > C++ > Can I Programmatically Disable a Windows Device Using Win32 APIs, and How?

Can I Programmatically Disable a Windows Device Using Win32 APIs, and How?

Susan Sarandon
Release: 2025-01-05 21:43:44
Original
839 people have browsed it

Can I Programmatically Disable a Windows Device Using Win32 APIs, and How?

Win32 APIs allow the disabling and enabling of devices programmatically. However, not all devices support this capability. When attempting to disable a touchpad using SetupDi APIs, it may not be possible because the default mouse driver often doesn't support this action.

To verify if a device is disableable, check if the disable option is available in Device Manager. If it's present, the following code can be used:

public static void EnableMouse(bool enable)
{
    // Example mouse GUID: {4d36e96f-e325-11ce-bfc1-08002be10318}
    Guid mouseGuid = new Guid("{4d36e96f-e325-11ce-bfc1-08002be10318}");

    // Get the instance path from Device Manager
    string instancePath = @"ACPI\PNP0F03&3688D3F&0";

    DeviceHelper.SetDeviceEnabled(mouseGuid, instancePath, enable);
}
````

Here's the supporting library:
Copy after login

using System;
using System.Text;
using System.Collections.Generic;
using DisableDevice;
using System.Runtime.InteropServices;
using System.ComponentModel;
using Microsoft.Win32.SafeHandles;
using System.Security;
using System.Runtime.ConstrainedExecution;
using System.Management;

namespace DisableDevice
{

[Flags()]
internal enum SetupDiGetClassDevsFlags
{
    Default = 1,
    Present = 2,
    AllClasses = 4,
    Profile = 8,
    DeviceInterface = (int)0x10
}

internal enum DiFunction
{
    SelectDevice = 1,
    InstallDevice = 2,
    AssignResources = 3,
    Properties = 4,
    Remove = 5,
    FirstTimeSetup = 6,
    FoundDevice = 7,
    SelectClassDrivers = 8,
    ValidateClassDrivers = 9,
    InstallClassDrivers = (int)0xa,
    CalcDiskSpace = (int)0xb,
    DestroyPrivateData = (int)0xc,
    ValidateDriver = (int)0xd,
    Detect = (int)0xf,
    InstallWizard = (int)0x10,
    DestroyWizardData = (int)0x11,
    PropertyChange = (int)0x12,
    EnableClass = (int)0x13,
    DetectVerify = (int)0x14,
    InstallDeviceFiles = (int)0x15,
    UnRemove = (int)0x16,
    SelectBestCompatDrv = (int)0x17,
    AllowInstall = (int)0x18,
    RegisterDevice = (int)0x19,
    NewDeviceWizardPreSelect = (int)0x1a,
    NewDeviceWizardSelect = (int)0x1b,
    NewDeviceWizardPreAnalyze = (int)0x1c,
    NewDeviceWizardPostAnalyze = (int)0x1d,
    NewDeviceWizardFinishInstall = (int)0x1e,
    Unused1 = (int)0x1f,
    InstallInterfaces = (int)0x20,
    DetectCancel = (int)0x21,
    RegisterCoInstallers = (int)0x22,
    AddPropertyPageAdvanced = (int)0x23,
    AddPropertyPageBasic = (int)0x24,
    Reserved1 = (int)0x25,
    Troubleshooter = (int)0x26,
    PowerMessageWake = (int)0x27,
    AddRemotePropertyPageAdvanced = (int)0x28,
    UpdateDriverUI = (int)0x29,
    Reserved2 = (int)0x30
}

internal enum StateChangeAction
{
    Enable = 1,
    Disable = 2,
    PropChange = 3,
    Start = 4,
    Stop = 5
}

[Flags()]
internal enum Scopes
{
    Global = 1,
    ConfigSpecific = 2,
    ConfigGeneral = 4
}

internal enum SetupApiError
{
    NoAssociatedClass = unchecked((int)0xe0000200),
    ClassMismatch = unchecked((int)0xe0000201),
    DuplicateFound = unchecked((int)0xe0000202),
    NoDriverSelected = unchecked((int)0xe0000203),
    KeyDoesNotExist = unchecked((int)0xe0000204),
    InvalidDevinstName = unchecked((int)0xe0000205),
    InvalidClass = unchecked((int)0xe0000206),
    DevinstAlreadyExists = unchecked((int)0xe0000207),
    DevinfoNotRegistered = unchecked((int)0xe0000208),
    InvalidRegProperty = unchecked((int)0xe0000209),
    NoInf = unchecked((int)0xe000020a),
    NoSuchHDevinst = unchecked((int)0xe000020b),
    CantLoadClassIcon = unchecked((int)0xe000020c),
    InvalidClassInstaller = unchecked((int)0xe000020d),
    DiDoDefault = unchecked((int)0xe000020e),
    DiNoFileCopy = unchecked((int)0xe000020f),
    InvalidHwProfile = unchecked((int)0xe0000210),
    NoDeviceSelected = unchecked((int)0xe0000211),
    DevinfolistLocked = unchecked((int)0xe0000212),
    DevinfodataLocked = unchecked((int)0xe0000213),
    DiBadPath = unchecked((int)0xe0000214),
    NoClassInstallParams = unchecked((int)0xe0000215),
    FileQueueLocked = unchecked((int)0xe0000216),
    BadServiceInstallSect = unchecked((int)0xe0000217),
    NoClassDriverList = unchecked((int)0xe0000218),
    NoAssociatedService = unchecked((int)0xe0000219),
    NoDefaultDeviceInterface = unchecked((int)0xe000021a),
    DeviceInterfaceActive = unchecked((int)0xe000021b),
    DeviceInterfaceRemoved = unchecked((int)0xe000021c),
    BadInterfaceInstallSect = unchecked((int)0xe000021d),
    NoSuchInterfaceClass = unchecked((int)0xe000021e),
    InvalidReferenceString = unchecked((int)0xe000021f),
    InvalidMachineName = unchecked((int)0xe0000220),
    RemoteCommFailure = unchecked((int)0xe0000221),
    MachineUnavailable = unchecked((int)0xe0000222),
    NoConfigMgrServices = unchecked((int)0xe0000223),
    InvalidPropPageProvider = unchecked((int)0xe0000224),
    NoSuchDeviceInterface = unchecked((int)0xe0000225),
    DiPostProcessingRequired = unchecked((int)0xe0000226),
    InvalidCOInstaller = unchecked((int)0xe0000227),
    NoCompatDrivers = unchecked((int)0xe0000228),
    NoDeviceIcon = unchecked((int)0xe0000229),
    InvalidInfLogConfig = unchecked((int)0xe000022a),
    DiDontInstall = unchecked((int)0xe000022b),
    InvalidFilterDriver = unchecked((int)0xe000022c),
    NonWindowsNTDriver = unchecked((int)0xe000022d),
    NonWindowsDriver = unchecked((int)0xe000022e),
    NoCatalogForOemInf = unchecked((int)0xe000022f),
    DevInstallQueueNonNative = unchecked((int)0xe0000230),
    NotDisableable = unchecked((int)0xe0000231),
    CantRemoveDevinst = unchecked((int)0xe
Copy after login

The above is the detailed content of Can I Programmatically Disable a Windows Device Using Win32 APIs, and How?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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