Devicecleanup.exe 〈RELIABLE〉
else if (arg == L"/?"
int wmain(int argc, wchar_t* argv[]) for (int i = 1; i < argc; i++) std::wstring arg = argv[i]; if (arg == L"/list") g_action = ACTION_LIST;
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 if (arg == L"/remove-all") g_action = ACTION_REMOVE_ALL; DeviceCleanup.exe
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;
SP_DEVINFO_DATA devInfoData; devInfoData.cbSize = sizeof(SP_DEVINFO_DATA); DWORD idx = 0; int removed = 0;
else if (arg == L"/verbose") g_verbose = true; else if (arg == L"/
SetupDiDestroyDeviceInfoList(hDevInfo); std::wcout << L"Removed " << removed << L" non-present device(s).\n"; return 0;
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;
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; i++) std::wstring arg = argv[i]
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();
else if (arg == L"/force") g_force = true;