DeviceUtils.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "pch.h"
  2. #include <Windows.h>
  3. #include <setupapi.h>
  4. #include <devguid.h>
  5. #include <cfgmgr32.h>
  6. #pragma comment(lib, "SetupAPI.lib")
  7. void UninstallDriver(LPCWSTR hardwareId, BOOL &rebootRequired)
  8. {
  9. HDEVINFO deviceInfoSet = SetupDiGetClassDevsW(&GUID_DEVCLASS_DISPLAY, NULL, NULL, DIGCF_PRESENT);
  10. if (deviceInfoSet == INVALID_HANDLE_VALUE)
  11. {
  12. WcaLog(LOGMSG_STANDARD, "Failed to get device information set, last error: %d", GetLastError());
  13. return;
  14. }
  15. SP_DEVINFO_LIST_DETAIL_DATA devInfoListDetail;
  16. devInfoListDetail.cbSize = sizeof(SP_DEVINFO_LIST_DETAIL_DATA);
  17. if (!SetupDiGetDeviceInfoListDetailW(deviceInfoSet, &devInfoListDetail))
  18. {
  19. SetupDiDestroyDeviceInfoList(deviceInfoSet);
  20. WcaLog(LOGMSG_STANDARD, "Failed to call SetupDiGetDeviceInfoListDetail, last error: %d", GetLastError());
  21. return;
  22. }
  23. SP_DEVINFO_DATA deviceInfoData;
  24. deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
  25. DWORD dataType;
  26. WCHAR deviceId[MAX_DEVICE_ID_LEN] = { 0, };
  27. DWORD deviceIndex = 0;
  28. while (SetupDiEnumDeviceInfo(deviceInfoSet, deviceIndex, &deviceInfoData))
  29. {
  30. if (!SetupDiGetDeviceRegistryPropertyW(deviceInfoSet, &deviceInfoData, SPDRP_HARDWAREID, &dataType, (PBYTE)deviceId, MAX_DEVICE_ID_LEN, NULL))
  31. {
  32. WcaLog(LOGMSG_STANDARD, "Failed to get hardware id, last error: %d", GetLastError());
  33. deviceIndex++;
  34. continue;
  35. }
  36. if (wcscmp(deviceId, hardwareId) != 0)
  37. {
  38. deviceIndex++;
  39. continue;
  40. }
  41. SP_REMOVEDEVICE_PARAMS remove_device_params;
  42. remove_device_params.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
  43. remove_device_params.ClassInstallHeader.InstallFunction = DIF_REMOVE;
  44. remove_device_params.Scope = DI_REMOVEDEVICE_GLOBAL;
  45. remove_device_params.HwProfile = 0;
  46. if (!SetupDiSetClassInstallParamsW(deviceInfoSet, &deviceInfoData, &remove_device_params.ClassInstallHeader, sizeof(SP_REMOVEDEVICE_PARAMS)))
  47. {
  48. WcaLog(LOGMSG_STANDARD, "Failed to set class install params, last error: %d", GetLastError());
  49. deviceIndex++;
  50. continue;
  51. }
  52. if (!SetupDiCallClassInstaller(DIF_REMOVE, deviceInfoSet, &deviceInfoData))
  53. {
  54. WcaLog(LOGMSG_STANDARD, "ailed to uninstall driver, last error: %d", GetLastError());
  55. deviceIndex++;
  56. continue;
  57. }
  58. SP_DEVINSTALL_PARAMS deviceParams;
  59. if (SetupDiGetDeviceInstallParamsW(deviceInfoSet, &deviceInfoData, &deviceParams))
  60. {
  61. if (deviceParams.Flags & (DI_NEEDRESTART | DI_NEEDREBOOT))
  62. {
  63. rebootRequired = true;
  64. }
  65. }
  66. WcaLog(LOGMSG_STANDARD, "Driver uninstalled successfully");
  67. deviceIndex++;
  68. }
  69. SetupDiDestroyDeviceInfoList(deviceInfoSet);
  70. }