1

我需要以编程方式卸载所有 Com 端口设备。问题是这些 Com 端口设备不存在,因此完全隐藏。这意味着,即使您想使用设备管理器卸载它们,首先您必须添加devmgr_show_nonpresent_devices = 1到您的环境变量,然后在设备管理器中显示隐藏的设备。然后,您可以右键单击每个设备并卸载。我不想卸载相关的驱动程序。我在高级系统设置下添加该变量,创建并保存一个新的用户变量。

我尝试用devcon做到这一点。可以找到它们,devcon findall但我无法删除它们,因为删除它们的命令失败,说明没有卸载任何设备。此外,没有标志让它寻找不存在的设备。如果我执行标准devcon find,则找不到任何设备(感兴趣的)。

所以,我又被迫弄清楚如何使用我自己的代码来做到这一点,这就是我卡住的地方。这是我到目前为止所拥有的:

// Get all of the devices
PCTSTR enumType = "PORTS";
HDEVINFO devs = NULL;
devs = SetupDiGetClassDevs(NULL,enumType,0,DIGCF_PRESENT | DIGCF_ALLCLASSES);

// Loop through the devices
DWORD devCount = 0;
SP_DEVINFO_DATA devInfo;
int enumeratingDevices = 1;
devInfo.cbSize = sizeof(SP_DEVINFO_DATA);
while(enumeratingDevices){
    enumeratingDevices = SetupDiEnumDeviceInfo (devs,devCount,&devInfo);
    // Uninstall each device
    cout << SetupDiRemoveDevice(devs,&devInfo);
    cout << SetupDiCallClassInstaller(DIF_REMOVE,&devInfo,NULL);
    devCount++;
}
cout << devCount;
SetupDiDestroyDeviceInfoList(devs);
return 0;

现在我得到一个输出001。所以,基本上SetupDiEnumDeviceInfo()还是SetupDiRemoveDevice不能正常运行。我知道枚举是有效的,因为如果我输入enumType = "USB";我会得到十个用于 devCount。

任何帮助或建议都会很棒。

4

2 回答 2

2

所以,经过大量的修补和阅读,我想通了。

// Get all of the devices
   //This enumeration does not work in general, instead passing
   //complete id of the device is probably best. 
   //It is helpful to know the vendor and device ID
PCTSTR enumType = "PORTS"; 
HDEVINFO devs = NULL;
devs = SetupDiGetClassDevs(NULL,enumType,0,DIGCF_ALLCLASSES);

// Loop through the devices
DWORD devCount = 0;
SP_DEVINFO_DATA devInfo;
int enumeratingDevices = 1;
/*This line is essential*/
devInfo.cbSize = sizeof(SP_DEVINFO_DATA);
while(enumeratingDevices){
        enumeratingDevices = SetupDiEnumDeviceInfo (devs,devCount,&devInfo);
        // Uninstall each device
        if(enumeratingDevices){
           SetupDiRemoveDevice(devs,&devInfo);
           devCount++;
        }
}
    //Clean up
SetupDiDestroyDeviceInfoList(devs);

明天当我带着我正在谈论的确切枚举到达实验室时,我会更新这个。但是,使用这种方法,您几乎可以卸载任何设备,即使它不存在并且只是注册表中的“幽灵”。

于 2012-10-09T03:05:34.237 回答
0

我能够使用您的代码成功禁用 USB 设备,但我无法使用 SetupDiunremoveDevice 函数启用 USB 请在此 API 上举例说明重新启用设备 WINSETUPAPI BOOL SetupDiUnremoveDevice(HDEVINFO DeviceInfoSet, PSP_DEVINFO_DATA DeviceInfoData);

于 2021-06-29T07:11:31.747 回答