2012-10-06 54 views
1

我需要以編程方式卸載所有Com端口設備。問題是這些Com端口設備不存在,因此完全隱藏。也就是說,即使您想使用設備管理器來卸載它們,首先必須將devmgr_show_nonpresent_devices = 1添加到您的環境變量中,然後在設備管理器中顯示隱藏的設備。然後,您可以右鍵單擊每個設備並卸載。我不想卸載關聯的驅動程序。我在高級系統設置下添加該變量,創建並保存一個新的用戶變量。Windows設備使用C++卸載

我試圖用devcon來做到這一點。他們可以在devcon findall找到,但我不能刪除它們,因爲他們的command to remove失敗,說明沒有設備已被卸載。此外,沒有標誌讓它尋找非現在的設備。如果我執行標準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十。

任何幫助或建議將是偉大的。

+0

您是否嘗試過將'DEVMGR_SHOW_NONPRESENT_DEVICES'添加到當前進程的環境中(僅在您的程序中;本地)? – Jay

+0

我已經將它永久添加到系統中,所以它始終存在於我的環境變量中。也許我不明白你的問題。 –

+0

看看devcon源代碼,也許你可以改變一些東西來獲得這個功能。 –

回答

1

因此,經過大量的修補和閱讀後,我發現了它。

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

我明天將更新這個明天,當我到達實驗室時,我正在談論的確切枚舉。但是,使用這種方法,即使它不存在,幾乎可以卸載任何設備,並且只是在註冊表中出現「鬼影」。

+0

你有一個完整的例子嗎?謝謝! – orbitcowboy

+0

我很抱歉,我無法再訪問此項目。 –