diff --git a/src/plugins/platforms/windows/qwindowsscreen.cpp b/src/plugins/platforms/windows/qwindowsscreen.cpp index 1f22fb4f60e..74642be3398 100644 --- a/src/plugins/platforms/windows/qwindowsscreen.cpp +++ b/src/plugins/platforms/windows/qwindowsscreen.cpp @@ -132,6 +132,18 @@ struct DiRegKeyHandleTraits using DiRegKeyHandle = QUniqueHandle; +struct DevInfoHandleTraits +{ + using Type = HDEVINFO; + static Type invalidValue() + { + return reinterpret_cast(INVALID_HANDLE_VALUE); + } + static bool close(Type handle) { return SetupDiDestroyDeviceInfoList(handle) == ERROR_SUCCESS; } +}; + +using DevInfoHandle = QUniqueHandle; + } static void setMonitorDataFromSetupApi(QWindowsScreenData &data, @@ -181,13 +193,16 @@ static void setMonitorDataFromSetupApi(QWindowsScreenData &data, constexpr GUID GUID_DEVINTERFACE_MONITOR = { 0xe6f07b5f, 0xee97, 0x4a90, { 0xb0, 0x76, 0x33, 0xf5, 0x7b, 0xf4, 0xea, 0xa7 } }; - const HDEVINFO devInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_MONITOR, nullptr, nullptr, - DIGCF_DEVICEINTERFACE); + const DevInfoHandle devInfo{ SetupDiGetClassDevs( + &GUID_DEVINTERFACE_MONITOR, nullptr, nullptr, DIGCF_DEVICEINTERFACE) }; + + if (!devInfo.isValid()) + continue; SP_DEVICE_INTERFACE_DATA deviceInterfaceData{}; deviceInterfaceData.cbSize = sizeof(deviceInterfaceData); - if (!SetupDiOpenDeviceInterfaceW(devInfo, deviceName.monitorDevicePath, DIODI_NO_ADD, + if (!SetupDiOpenDeviceInterfaceW(devInfo.get(), deviceName.monitorDevicePath, DIODI_NO_ADD, &deviceInterfaceData)) { qCWarning(lcQpaScreen) << u"Unable to open monitor interface to %1:"_s.arg(data.deviceName) @@ -196,7 +211,7 @@ static void setMonitorDataFromSetupApi(QWindowsScreenData &data, } DWORD requiredSize{ 0 }; - if (SetupDiGetDeviceInterfaceDetailW(devInfo, &deviceInterfaceData, nullptr, 0, + if (SetupDiGetDeviceInterfaceDetailW(devInfo.get(), &deviceInterfaceData, nullptr, 0, &requiredSize, nullptr) || GetLastError() != ERROR_INSUFFICIENT_BUFFER) { continue; @@ -207,7 +222,7 @@ static void setMonitorDataFromSetupApi(QWindowsScreenData &data, devicePath->cbSize = sizeof(std::remove_pointer_t); SP_DEVINFO_DATA deviceInfoData{}; deviceInfoData.cbSize = sizeof(deviceInfoData); - if (!SetupDiGetDeviceInterfaceDetailW(devInfo, &deviceInterfaceData, devicePath, + if (!SetupDiGetDeviceInterfaceDetailW(devInfo.get(), &deviceInterfaceData, devicePath, requiredSize, nullptr, &deviceInfoData)) { qCDebug(lcQpaScreen) << u"Unable to get monitor metadata for %1:"_s.arg(data.deviceName) << QSystemError::windowsString(); @@ -215,7 +230,7 @@ static void setMonitorDataFromSetupApi(QWindowsScreenData &data, } const DiRegKeyHandle edidRegistryKey{ SetupDiOpenDevRegKey( - devInfo, &deviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ) }; + devInfo.get(), &deviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ) }; if (!edidRegistryKey.isValid()) continue;