Devicecleanup.exe ✮
int RemoveById(const std::wstring& id) HDEVINFO hDevInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_ALLCLASSES); if (hDevInfo == INVALID_HANDLE_VALUE) std::wcerr << L"Failed to get device list.\n"; return 1;
// Get instance ID WCHAR instanceId[256]; if (CM_Get_Device_ID(devInst, instanceId, 256, 0) == CR_SUCCESS) std::wcout << L"Instance: " << instanceId << std::endl;
// Get device description WCHAR desc[256]; if (SetupDiGetDeviceRegistryProperty(hDevInfo, &devInfoData, SPDRP_DEVICEDESC, NULL, (PBYTE)desc, sizeof(desc), NULL)) std::wcout << L" Description: " << desc << std::endl;
SP_DEVINFO_DATA devInfoData; devInfoData.cbSize = sizeof(SP_DEVINFO_DATA); DWORD idx = 0; int count = 0; DeviceCleanup.exe
return (status & DN_NO_SHOW_IN_DM) != 0;
bool IsNonPresentDevice(DEVINST devInst) ULONG status = 0, problem = 0; if (CM_Get_DevNode_Status(&status, &problem, devInst, 0) != CR_SUCCESS) return true; // Cannot query status -> treat as problematic
WCHAR instanceId[256]; if (CM_Get_Device_ID(devInst, instanceId, 256, 0) != CR_SUCCESS) continue; if (hDevInfo == INVALID_HANDLE_VALUE) std::wcerr <
bool matches = (id == instanceId);
int wmain(int argc, wchar_t* argv[]) for (int i = 1; i < argc; i++) arg == L"/help") PrintUsage(); return 0;
while (SetupDiEnumDeviceInfo(hDevInfo, idx++, &devInfoData)) DEVINST devInst = devInfoData.DevInst; L"Failed to get device list.\n"
bool RemoveDevice(DEVINST devInst) CONFIGRET cr = CM_Request_Device_Eject(devInst, NULL, NULL, 0, 0); if (cr == CR_SUCCESS) if (g_verbose) std::wcout << L" -> Eject request succeeded.\n"; cr = CM_Remove_SubTree(devInst, g_force ? CM_REMOVE_NO_RESTART : 0); if (cr == CR_SUCCESS) if (g_verbose) std::wcout << L" -> Device removed.\n"; return true;
void ListDevices() DIGCF_PRESENT); if (hDevInfo == INVALID_HANDLE_VALUE) std::wcerr << L"Failed to get device list. Error: " << GetLastError() << std::endl; return;
void PrintUsage() std::wcout << L"DeviceCleanup.exe - Remove non-present (ghost) devices\n\n"; std::wcout << L"Usage:\n"; std::wcout << L" DeviceCleanup.exe [/list] - List all non-present devices\n"; std::wcout << L" DeviceCleanup.exe /remove <id> - Remove device by instance ID or hardware ID\n"; std::wcout << L" DeviceCleanup.exe /remove-all - Remove all non-present devices\n"; std::wcout << L" DeviceCleanup.exe /force - Bypass pending restart flag (use with /remove or /remove-all)\n"; std::wcout << L" DeviceCleanup.exe /verbose - Show detailed info\n"; std::wcout << L"\nExample:\n"; std::wcout << L" DeviceCleanup.exe /list\n"; std::wcout << L" DeviceCleanup.exe /remove "USB\VID_1234" /force\n"; std::wcout << L" DeviceCleanup.exe /remove-all /verbose\n";
#pragma comment(lib, "setupapi.lib") #pragma comment(lib, "CfgMgr32.lib")
if (g_verbose) // Get hardware IDs DWORD hwIdSize = 0; SetupDiGetDeviceRegistryProperty(hDevInfo, &devInfoData, SPDRP_HARDWAREID, NULL, NULL, 0, &hwIdSize); if (hwIdSize > 0) std::vector<BYTE> buffer(hwIdSize); SetupDiGetDeviceRegistryProperty(hDevInfo, &devInfoData, SPDRP_HARDWAREID, NULL, buffer.data(), hwIdSize, &hwIdSize); WCHAR* hwid = (WCHAR*)buffer.data(); std::wcout << L" Hardware IDs: "; while (*hwid) std::wcout << hwid << L" "; hwid += wcslen(hwid) + 1; std::wcout << std::endl; std::wcout << std::endl;