From 369b500ccae4172bf4f6c41037e6acd95ffce2a3 Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Sat, 16 Sep 2023 18:30:26 +0200 Subject: [PATCH 1/3] fix: clipboard exception --- src/util/WindowsHelper.cpp | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/util/WindowsHelper.cpp b/src/util/WindowsHelper.cpp index 73a4d591d7a..c9954285a03 100644 --- a/src/util/WindowsHelper.cpp +++ b/src/util/WindowsHelper.cpp @@ -1,11 +1,13 @@ #include "WindowsHelper.hpp" -#include +#include +#include #include #include #ifdef USEWINSDK +# include # include # include @@ -45,18 +47,11 @@ boost::optional getWindowDpi(HWND hwnd) return boost::none; } -typedef HRESULT(CALLBACK *OleFlushClipboard_)(); - void flushClipboard() { - static HINSTANCE ole32 = LoadLibrary(L"Ole32.dll"); - if (ole32 != nullptr) + if (QApplication::clipboard()->ownsClipboard()) { - if (auto oleFlushClipboard = - OleFlushClipboard_(GetProcAddress(ole32, "OleFlushClipboard"))) - { - oleFlushClipboard(); - } + OleFlushClipboard(); } } From c8b377f101722706a30cd06482f4bd78033fe753 Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Sat, 16 Sep 2023 18:30:43 +0200 Subject: [PATCH 2/3] chore: cleanup windows helpers --- src/util/WindowsHelper.cpp | 60 ++++++++++++++------------------------ 1 file changed, 22 insertions(+), 38 deletions(-) diff --git a/src/util/WindowsHelper.cpp b/src/util/WindowsHelper.cpp index c9954285a03..07e5a9f6620 100644 --- a/src/util/WindowsHelper.cpp +++ b/src/util/WindowsHelper.cpp @@ -1,4 +1,6 @@ -#include "WindowsHelper.hpp" +#include "util/WindowsHelper.hpp" + +#include "common/Literals.hpp" #include #include @@ -8,23 +10,18 @@ #ifdef USEWINSDK # include +# include # include # include namespace chatterino { -typedef enum MONITOR_DPI_TYPE { - MDT_EFFECTIVE_DPI = 0, - MDT_ANGULAR_DPI = 1, - MDT_RAW_DPI = 2, - MDT_DEFAULT = MDT_EFFECTIVE_DPI -} MONITOR_DPI_TYPE; +using namespace literals; -typedef HRESULT(CALLBACK *GetDpiForMonitor_)(HMONITOR, MONITOR_DPI_TYPE, UINT *, - UINT *); -typedef HRESULT(CALLBACK *AssocQueryString_)(ASSOCF, ASSOCSTR, LPCWSTR, LPCWSTR, - LPWSTR, DWORD *); +using GetDpiForMonitor_ = HRESULT CALLBACK (*)(HMONITOR, MONITOR_DPI_TYPE, + UINT *, UINT *); +// TODO: This should be changed to `GetDpiForWindow`. boost::optional getWindowDpi(HWND hwnd) { static HINSTANCE shcore = LoadLibrary(L"Shcore.dll"); @@ -36,8 +33,8 @@ boost::optional getWindowDpi(HWND hwnd) HMONITOR monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST); - UINT xScale, yScale; - + UINT xScale = 96; + UINT yScale = 96; getDpiForMonitor(monitor, MDT_DEFAULT, &xScale, &yScale); return xScale; @@ -55,19 +52,19 @@ void flushClipboard() } } -constexpr const char *runKey = - "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run"; +const QString RUN_KEY = + uR"(HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run)"_s; bool isRegisteredForStartup() { - QSettings settings(runKey, QSettings::NativeFormat); + QSettings settings(RUN_KEY, QSettings::NativeFormat); return !settings.value("Chatterino").toString().isEmpty(); } void setRegisteredForStartup(bool isRegistered) { - QSettings settings(runKey, QSettings::NativeFormat); + QSettings settings(RUN_KEY, QSettings::NativeFormat); if (isRegistered) { @@ -85,19 +82,6 @@ void setRegisteredForStartup(bool isRegistered) QString getAssociatedExecutable(AssociationQueryType queryType, LPCWSTR query) { - static HINSTANCE shlwapi = LoadLibrary(L"shlwapi"); - if (shlwapi == nullptr) - { - return QString(); - } - - static auto assocQueryString = - AssocQueryString_(GetProcAddress(shlwapi, "AssocQueryStringW")); - if (assocQueryString == nullptr) - { - return QString(); - } - // always error out instead of returning a truncated string when the // buffer is too small - avoids race condition when the user changes their // default browser between calls to AssocQueryString @@ -112,28 +96,28 @@ QString getAssociatedExecutable(AssociationQueryType queryType, LPCWSTR query) } else { - return QString(); + return {}; } } DWORD resultSize = 0; - if (FAILED(assocQueryString(flags, ASSOCSTR_EXECUTABLE, query, nullptr, - nullptr, &resultSize))) + if (FAILED(AssocQueryStringW(flags, ASSOCSTR_EXECUTABLE, query, nullptr, + nullptr, &resultSize))) { - return QString(); + return {}; } if (resultSize <= 1) { // resultSize includes the null terminator. if resultSize is 1, the // returned value would be the empty string. - return QString(); + return {}; } QString result; - auto buf = new wchar_t[resultSize]; - if (SUCCEEDED(assocQueryString(flags, ASSOCSTR_EXECUTABLE, query, nullptr, - buf, &resultSize))) + auto *buf = new wchar_t[resultSize]; + if (SUCCEEDED(AssocQueryStringW(flags, ASSOCSTR_EXECUTABLE, query, nullptr, + buf, &resultSize))) { // QString::fromWCharArray expects the length in characters *not // including* the null terminator, but AssocQueryStringW calculates From 0e1f45684a9f03540ac17620dcd0f393d7d7efc0 Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Sat, 16 Sep 2023 19:44:01 +0200 Subject: [PATCH 3/3] fix: callback --- src/util/WindowsHelper.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/util/WindowsHelper.cpp b/src/util/WindowsHelper.cpp index 07e5a9f6620..00b31db37e5 100644 --- a/src/util/WindowsHelper.cpp +++ b/src/util/WindowsHelper.cpp @@ -18,8 +18,8 @@ namespace chatterino { using namespace literals; -using GetDpiForMonitor_ = HRESULT CALLBACK (*)(HMONITOR, MONITOR_DPI_TYPE, - UINT *, UINT *); +using GetDpiForMonitor_ = HRESULT(CALLBACK *)(HMONITOR, MONITOR_DPI_TYPE, + UINT *, UINT *); // TODO: This should be changed to `GetDpiForWindow`. boost::optional getWindowDpi(HWND hwnd)