Devicecleanup.exe -
SetupDiDestroyDeviceInfoList(hDevInfo); std::wcout << L"Removed " << removed << L" device(s).\n"; return 0;
SetupDiDestroyDeviceInfoList(hDevInfo); std::wcout << L"Removed " << removed << L" non-present device(s).\n"; return 0;
if (matches && IsNonPresentDevice(devInst)) std::wcout << L"Removing: " << instanceId << std::endl; if (RemoveDevice(devInst)) removed++;
else if (arg == L"/?"
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;
bool matches = (id == instanceId);
int RemoveAllNonPresent() HDEVINFO hDevInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_ALLCLASSES); if (hDevInfo == INVALID_HANDLE_VALUE) std::wcerr << L"Failed to get device list.\n"; return 1; DeviceCleanup.exe
WCHAR instanceId[256]; if (CM_Get_Device_ID(devInst, instanceId, 256, 0) != CR_SUCCESS) continue;
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
else if (arg == L"/force") g_force = true; L"Removed " <
if (!matches && g_verbose) 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(); while (*hwid) if (id == hwid) matches = true; break; hwid += wcslen(hwid) + 1;
enum Action ACTION_LIST, ACTION_REMOVE_ONE, ACTION_REMOVE_ALL ; Action g_action = ACTION_LIST; std::wstring g_targetId; bool g_force = false; bool g_verbose = false;
else PrintUsage(); return 1;
switch (g_action) case ACTION_LIST: ListDevices(); break; case ACTION_REMOVE_ONE: if (g_targetId.empty()) std::wcerr << L"Error: /remove requires an ID.\n"; return 1; return RemoveById(g_targetId); case ACTION_REMOVE_ALL: return RemoveAllNonPresent();