From d9784148cb36f01966e689dbe6927350a0d31e7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8ger=20Hanseg=C3=A5rd?= Date: Sun, 5 Nov 2023 13:36:32 +0100 Subject: [PATCH] Remove ambiguity in handling of registry keys from setupAPI.h MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QWindowsScreen uses setupAPI.h's SetupDiOpenDevRegKey function to read EDID data for monitors. This function is documented as returning INVALID_HANDLE_VALUE in case of failure. The QWindowsScreen code was ambiguous in the sense that it considered both nullptr and INVALID_HANDLE_VALUE as being invalid handle values. This inconsistency is likely not a bug, but makes the code harder to understand. This patch removes this ambiguity, and QWindowsScreen now follows the documented behavior when SetupDiOpenDevRegKey fails. In addition, we replace use of unique_ptr with the new QUniqueHandle template class because HKEY is a handle type, not a regular pointer type. Pick-to: 6.5 Change-Id: Ia863bda504077e59833f6f7a0f855e7915e4edd9 Reviewed-by: Tor Arne Vestbø (cherry picked from commit 55141c59cdc9144cd3aa9f5085df9db523f54736) Reviewed-by: Qt Cherry-pick Bot --- .../platforms/windows/qwindowsscreen.cpp | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/plugins/platforms/windows/qwindowsscreen.cpp b/src/plugins/platforms/windows/qwindowsscreen.cpp index 15c5aac8e35..a50f9fd4b05 100644 --- a/src/plugins/platforms/windows/qwindowsscreen.cpp +++ b/src/plugins/platforms/windows/qwindowsscreen.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include @@ -116,16 +117,22 @@ static float getMonitorSDRWhiteLevel(DISPLAYCONFIG_PATH_TARGET_INFO *targetInfo) using WindowsScreenDataList = QList; -struct RegistryHandleDeleter +namespace { + +struct DiRegKeyHandleTraits { - void operator()(HKEY handle) const noexcept + using Type = HKEY; + static Type invalidValue() { - if (handle != nullptr && handle != INVALID_HANDLE_VALUE) - RegCloseKey(handle); + // The setupapi.h functions return INVALID_HANDLE_VALUE when failing to open a registry key + return reinterpret_cast(INVALID_HANDLE_VALUE); } + static bool close(Type handle) { return RegCloseKey(handle) == ERROR_SUCCESS; } }; -using RegistryHandlePtr = std::unique_ptr, RegistryHandleDeleter>; +using DiRegKeyHandle = QUniqueHandle; + +} static void setMonitorDataFromSetupApi(QWindowsScreenData &data, const std::vector &pathGroup) @@ -207,10 +214,10 @@ static void setMonitorDataFromSetupApi(QWindowsScreenData &data, continue; } - const RegistryHandlePtr edidRegistryKey{ SetupDiOpenDevRegKey( + const DiRegKeyHandle edidRegistryKey{ SetupDiOpenDevRegKey( devInfo, &deviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ) }; - if (!edidRegistryKey || edidRegistryKey.get() == INVALID_HANDLE_VALUE) + if (!edidRegistryKey.isValid()) continue; DWORD edidDataSize{ 0 };