如何 enable/Disabe 特定于 C# 的 USB 设备

How to enable/Disabe an USB device specific with C#

我尝试使用 C# enable/disable 特定的 USB 设备。 我很困惑,因为我发现了很多信息,而且它们相互矛盾。 我的第一个问题是,“setupapi.dll”只能在 32 位平台上运行吗? 无论如何,我的程序 return false 调用时:

bool rstl2 = DeviceHelper.SetupDiCallClassInstaller(DeviceHelper.DIF_PROPERTYCHANGE, hDevInfo, ptrToDevInfoData);

我的源代码:

public bool HW_Set_State( bool bEnable)
    {
        try
        {
            Guid myGUID = new Guid("{745a17a0-74d3-11d0-b6fe-00a0c90f57da}");
            
            IntPtr hDevInfo = DeviceHelper.SetupDiGetClassDevs(ref myGUID, 0, IntPtr.Zero, DeviceHelper.DIGCF_ALLCLASSES | DeviceHelper.DIGCF_PRESENT);

            try
            {
                if (hDevInfo.ToInt32() == DeviceHelper.INVALID_HANDLE_VALUE)
                    return false;
            } catch { }
            if (hDevInfo.ToInt64() == DeviceHelper.INVALID_HANDLE_VALUE)
                return false;

            DeviceHelper.SP_DEVINFO_DATA DeviceInfoData;
            
            DeviceInfoData = new DeviceHelper.SP_DEVINFO_DATA();
            DeviceInfoData.cbSize = 28;                
            DeviceInfoData.devInst = 0;
            DeviceInfoData.classGuid = myGUID;
            DeviceInfoData.reserved = 0;
            
            UInt32 i;
            StringBuilder DeviceName = new StringBuilder("");
            DeviceName.Capacity = DeviceHelper.MAX_DEV_LEN;
            
            for (i = 0; DeviceHelper.SetupDiEnumDeviceInfo(hDevInfo, i, DeviceInfoData); i++)
            {                    
                if(DeviceHelper.SetupDiGetDeviceRegistryProperty(hDevInfo, DeviceInfoData, DeviceHelper.SPDRP_DEVICEDESC, 0, DeviceName, DeviceHelper.MAX_DEV_LEN, IntPtr.Zero)){

                    if (DeviceName.ToString().Contains("USB Input Device"))
                    {                         
                        if (DeviceInfoData.classGuid.ToString().Contains("745a17a0-74d3-11d0-b6fe-00a0c90f57da"))
                        {                                
                            ChangeDeviceState(hDevInfo, DeviceInfoData, bEnable);
                        }                            
                    }                                                                        
                }                                                          
            }
            DeviceHelper.SetupDiDestroyDeviceInfoList(hDevInfo);
        }
        catch (Exception ex)
        {
            return false;
        }
        return true;
    }
}

private bool ChangeDeviceState(IntPtr hDevInfo, DeviceHelper.SP_DEVINFO_DATA devInfoData, bool bEnable)
    {
        try
        {
            int szOfPcp;
            IntPtr ptrToPcp;
            int szDevInfoData;
            IntPtr ptrToDevInfoData;

            DeviceHelper.SP_PROPCHANGE_PARAMS pcp = new DeviceHelper.SP_PROPCHANGE_PARAMS();
            if (bEnable)
            {
                pcp.ClassInstallHeader.cbSize = Marshal.SizeOf(typeof(DeviceHelper.SP_CLASSINSTALL_HEADER));
                pcp.ClassInstallHeader.InstallFunction = DeviceHelper.DIF_PROPERTYCHANGE;
                pcp.StateChange = DeviceHelper.DICS_ENABLE;
                pcp.Scope = DeviceHelper.DICS_FLAG_GLOBAL;
                pcp.HwProfile = 0;
                
                szOfPcp = Marshal.SizeOf(pcp);
                ptrToPcp = Marshal.AllocHGlobal(szOfPcp);
                Marshal.StructureToPtr(pcp, ptrToPcp, true);
                szDevInfoData = Marshal.SizeOf(devInfoData);
                ptrToDevInfoData = Marshal.AllocHGlobal(szDevInfoData);

                if (DeviceHelper.SetupDiSetClassInstallParams(hDevInfo, ptrToDevInfoData, ptrToPcp, Marshal.SizeOf(typeof(DeviceHelper.SP_PROPCHANGE_PARAMS))))
                {
                    DeviceHelper.SetupDiCallClassInstaller(DeviceHelper.DIF_PROPERTYCHANGE, hDevInfo, ptrToDevInfoData);
                }
                pcp.ClassInstallHeader.cbSize = Marshal.SizeOf(typeof(DeviceHelper.SP_CLASSINSTALL_HEADER));
                pcp.ClassInstallHeader.InstallFunction = DeviceHelper.DIF_PROPERTYCHANGE;
                pcp.StateChange = DeviceHelper.DICS_ENABLE;
                pcp.Scope = DeviceHelper.DICS_FLAG_CONFIGSPECIFIC;
                pcp.HwProfile = 0;
            }
            else
            {
                pcp.ClassInstallHeader.cbSize = Marshal.SizeOf(typeof(DeviceHelper.SP_CLASSINSTALL_HEADER));
                pcp.ClassInstallHeader.InstallFunction = DeviceHelper.DIF_PROPERTYCHANGE;
                pcp.StateChange = DeviceHelper.DICS_DISABLE;
                pcp.Scope = DeviceHelper.DICS_FLAG_CONFIGSPECIFIC;
                pcp.HwProfile = 0;
            }
            szOfPcp = Marshal.SizeOf(pcp);
            ptrToPcp = Marshal.AllocHGlobal(szOfPcp);
            Marshal.StructureToPtr(pcp, ptrToPcp, true);
            szDevInfoData = Marshal.SizeOf(devInfoData);
            ptrToDevInfoData = Marshal.AllocHGlobal(szDevInfoData);
            Marshal.StructureToPtr(devInfoData, ptrToDevInfoData, true);

            bool rslt1 = DeviceHelper.SetupDiSetClassInstallParams(hDevInfo, ptrToDevInfoData, ptrToPcp, Marshal.SizeOf(typeof(DeviceHelper.SP_PROPCHANGE_PARAMS)));
            bool rstl2 = DeviceHelper.SetupDiCallClassInstaller(DeviceHelper.DIF_PROPERTYCHANGE, hDevInfo, ptrToDevInfoData);
            if ((!rslt1) || (!rstl2))
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        catch (Exception ex)
        {
            return false;
        }
    }

如果我写错了,对不起。我来自巴西 :)

如果您也喜欢使用 WMI,您可以这样做:

*安装管理 NuGet

using System.Management;

*使用您的设备添加字符串 name/guid/deviceid.etc

public string DeviceName = "YourDeviceName";

*为启用

ManagementObjectSearcher myDevices = new ManagementObjectSearcher("root\CIMV2", @"SELECT * FROM Win32_PnPEntity where Name Like " + '"' + DeviceName + '"');

                foreach (ManagementObject item in myDevices.Get())
                {
                    
                    ManagementBaseObject UWFEnable = item.InvokeMethod("Enable", null, null);
                }
                

*为禁用

ManagementObjectSearcher myDevices = new ManagementObjectSearcher("root\CIMV2", @"SELECT * FROM Win32_PnPEntity where Name Like " + '"' + DeviceName + '"');

                foreach (ManagementObject item in smyDevices.Get())
                {
                    ManagementBaseObject inParams = item.InvokeMethod("Disable", null, null);
                }

@编辑: 您也可以使用 Microsoft 网站上的 devcon.exe。