Windows Device Uninstall using C++

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I need to programatically uninstall all Com port devices. The issue is that these Com Port Devices are not present and therefore completely hidden. Meaning, even if you want to uninstall them using device manager, first you have to add `devmgr_show_nonpresent_devices
= 1` to your environment variables and then show hidden devices in device manager. Then, you can right click each device and uninstall. I dont want to uninstall the associated driver though. I am adding that variable under advanced system settings, creating
and saving a new user variable. <br/>
<br/>
I try to do this with devcon. They can be found with `devcon findall` but I cannot remove them because the command to remove them fails stating no device has been uninstalled. Also, there is not flag to have it look for non-present devices. If I do a
standard `devcon find`, no devices are found (of interest).<br/>
<br/>
So, I am back to being forced to figure out how exactly to do this using my own code and here is where I get stuck. Here is what I have so far:
<br/>

<pre class="prettyprint" style=" int _tmain(int argc, _TCHAR* argv[]){
// 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;
}[/code]
<br/>
<br/>
Right now I am getting an output of 001. So, basically, SetupDiEnumDeviceInfo() or SetupDiRemoveDevice do not run correctly. I know that the enumeration is working because if I put in enumType = "USB"; I get ten for the devCount.<br/>
<br/>
Any help or advice would be great.<br/>
<br/>
<br/>


View the full article
 
Back
Top