簡體   English   中英

使用C ++卸載Windows設備

[英]Windows Device Uninstall using C++

我需要以編程方式卸載所有Com端口設備。 問題在於這些Com Port設備不存在,因此被完全隱藏。 這意味着,即使您想使用設備管理器將其卸載,也必須首先將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"; 我得到10個devCount。

任何幫助或建議都很好。

因此,經過大量的修改和閱讀,我弄清楚了。

// 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);

明天到達實驗室時,我將使用我正在講的確切枚舉進行更新。 但是,使用這種方法,您幾乎可以卸載任何設備,即使該設備不存在,而只是注冊表中的“鬼”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM