From 100e400fed27d568d3cd98ccc9b6ac7b74fc678d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Thu, 14 Jul 2022 16:46:31 +0300 Subject: [PATCH] Dynamically obtain the function pointer to IsHungAppWindow --- .../crashcatcher/windows_crashmonitor.cpp | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/components/crashcatcher/windows_crashmonitor.cpp b/components/crashcatcher/windows_crashmonitor.cpp index e76e8897e1..214557e3a7 100644 --- a/components/crashcatcher/windows_crashmonitor.cpp +++ b/components/crashcatcher/windows_crashmonitor.cpp @@ -17,7 +17,29 @@ namespace Crash { std::unordered_map CrashMonitor::smEventHookOwners{}; + + using IsHungAppWindowFn = BOOL(WINAPI*)(HWND hwnd); + + // Obtains the pointer to user32.IsHungAppWindow, this function may be removed in the future. + // See: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-ishungappwindow + static IsHungAppWindowFn getIsHungAppWindow() noexcept + { + static IsHungAppWindowFn isHungAppWindow = nullptr; + static bool isHungAppWindowInitialized = false; + + if (isHungAppWindowInitialized) + return isHungAppWindow; + auto user32Handle = LoadLibraryA("user32.dll"); + if (user32Handle == nullptr) + return nullptr; + + isHungAppWindow = reinterpret_cast(GetProcAddress(user32Handle, "IsHungAppWindow")); + isHungAppWindowInitialized = true; + + return isHungAppWindow; + } + CrashMonitor::CrashMonitor(HANDLE shmHandle) : mShmHandle(shmHandle) { @@ -123,8 +145,8 @@ namespace Crash else return false; } - if (IsHungAppWindow) - return IsHungAppWindow(mAppWindowHandle); + if (auto isHungAppWindow = getIsHungAppWindow(); isHungAppWindow != nullptr) + return isHungAppWindow(mAppWindowHandle); else { BOOL debuggerPresent;