From 873eff020d1b328de2a323d81796236936018805 Mon Sep 17 00:00:00 2001 From: SimplestStudio Date: Wed, 5 Jun 2024 12:50:21 +0300 Subject: [PATCH] [win] refactoring setAppUserModelId --- win-linux/extras/projicons/src/main.cpp | 25 ++++++------------- win-linux/src/main.cpp | 2 +- win-linux/src/utils.cpp | 33 +++++++++---------------- win-linux/src/utils.h | 3 +-- 4 files changed, 20 insertions(+), 43 deletions(-) diff --git a/win-linux/extras/projicons/src/main.cpp b/win-linux/extras/projicons/src/main.cpp index 774f94fca..d24ef4038 100644 --- a/win-linux/extras/projicons/src/main.cpp +++ b/win-linux/extras/projicons/src/main.cpp @@ -39,27 +39,16 @@ # include #endif -typedef HRESULT (__stdcall *SetCurrentProcessExplicitAppUserModelIDProc)(PCWSTR AppID); -bool SetAppUserModelId() +void SetAppUserModelId() { - bool result = false; - - // try to load Shell32.dll - HMODULE _lib_shell32 = LoadLibrary(L"shell32.dll"); - if ( _lib_shell32 != NULL ) { - // see if the function is exposed by the current OS - SetCurrentProcessExplicitAppUserModelIDProc setCurrentProcessExplicitAppUserModelId = - reinterpret_cast(GetProcAddress(_lib_shell32, "SetCurrentProcessExplicitAppUserModelID")); - - if ( setCurrentProcessExplicitAppUserModelId != NULL ) { - result = setCurrentProcessExplicitAppUserModelId(QString(APP_USER_MODEL_ID).toStdWString().c_str()) == S_OK; - } - - FreeLibrary(_lib_shell32); + if (HMODULE lib = LoadLibrary(L"shell32")) { + HRESULT (WINAPI *SetAppUserModelID)(PCWSTR AppID); + *(FARPROC*)&SetAppUserModelID = GetProcAddress(lib, "SetCurrentProcessExplicitAppUserModelID"); + if (SetAppUserModelID) + SetAppUserModelID(TEXT(APP_USER_MODEL_ID)); + FreeLibrary(lib); } - - return result; } diff --git a/win-linux/src/main.cpp b/win-linux/src/main.cpp index b3d2dd688..2fafae8e8 100644 --- a/win-linux/src/main.cpp +++ b/win-linux/src/main.cpp @@ -55,7 +55,7 @@ int main( int argc, char *argv[] ) if (argc > 1 && strcmp(argv[1], "--add-to-recent") == 0) return 0; Core_SetProcessDpiAwareness(); - Utils::setAppUserModelId(APP_USER_MODEL_ID); + Utils::setAppUserModelId(); WCHAR * cm_line = GetCommandLine(); InputArgs::init(cm_line); #else diff --git a/win-linux/src/utils.cpp b/win-linux/src/utils.cpp index 46eef0847..fdad5eb55 100644 --- a/win-linux/src/utils.cpp +++ b/win-linux/src/utils.cpp @@ -57,7 +57,6 @@ #include #include "shlobj.h" #include "lmcons.h" -typedef HRESULT (__stdcall *SetCurrentProcessExplicitAppUserModelIDProc)(PCWSTR AppID); #else # include # include @@ -763,6 +762,17 @@ void Utils::setSessionInProgress(bool state) { sessionInProgress = state; } + +void Utils::setAppUserModelId() +{ + if (HMODULE lib = LoadLibrary(L"shell32")) { + HRESULT (WINAPI *SetAppUserModelID)(PCWSTR AppID); + *(FARPROC*)&SetAppUserModelID = GetProcAddress(lib, "SetCurrentProcessExplicitAppUserModelID"); + if (SetAppUserModelID) + SetAppUserModelID(TEXT(APP_USER_MODEL_ID)); + FreeLibrary(lib); + } +} #else void Utils::processMoreEvents(uint timeout) { @@ -806,27 +816,6 @@ void Utils::replaceAll(std::wstring& subject, const std::wstring& search, const } } -bool Utils::setAppUserModelId(const QString& modelid) -{ - bool _result = false; - -#ifdef Q_OS_WIN - HMODULE _lib_shell32 = ::LoadLibrary(L"shell32.dll"); - if ( _lib_shell32 != NULL ) { - SetCurrentProcessExplicitAppUserModelIDProc setCurrentProcessExplicitAppUserModelId = - reinterpret_cast(GetProcAddress(_lib_shell32, "SetCurrentProcessExplicitAppUserModelID")); - - if ( setCurrentProcessExplicitAppUserModelId != NULL ) { - _result = setCurrentProcessExplicitAppUserModelId(modelid.toStdWString().c_str()) == S_OK; - } - - ::FreeLibrary(_lib_shell32); - } -#endif - - return _result; -} - std::wstring Utils::systemUserName() { #ifdef Q_OS_WIN diff --git a/win-linux/src/utils.h b/win-linux/src/utils.h index 7ebed53ce..2437b1f7c 100644 --- a/win-linux/src/utils.h +++ b/win-linux/src/utils.h @@ -106,8 +106,6 @@ public: static void replaceAll(std::wstring& subject, const std::wstring& search, const std::wstring& replace); static bool isFileLocal(const QString&); static QString uniqFileName(const QString& path); - static bool setAppUserModelId(const QString&); - static bool makepath(const QString&); static QString systemLocationCode(); @@ -129,6 +127,7 @@ public: static WinVer getWinVersion(); static bool isSessionInProgress(); static void setSessionInProgress(bool); + static void setAppUserModelId(); #else static void processMoreEvents(uint timeout = 60); static void setInstAppPort(int);