Stop monitoring closed windows

If it gets repalced, the new one will be watched instead
This commit is contained in:
AnyOldName3 2021-12-20 22:24:47 +00:00
parent 97396da74c
commit d15c2922a9
2 changed files with 21 additions and 1 deletions

View file

@ -18,6 +18,7 @@
namespace Crash namespace Crash
{ {
std::unordered_map<HWINEVENTHOOK, CrashMonitor*> CrashMonitor::smEventHookOwners{};
CrashMonitor::CrashMonitor(HANDLE shmHandle) CrashMonitor::CrashMonitor(HANDLE shmHandle)
: mShmHandle(shmHandle) : mShmHandle(shmHandle)
@ -85,6 +86,10 @@ namespace Crash
bool CrashMonitor::isAppFrozen() bool CrashMonitor::isAppFrozen()
{ {
MSG message;
// Allow the event hook callback to run
PeekMessage(&message, nullptr, 0, 0, PM_NOREMOVE);
if (!mAppWindowHandle) if (!mAppWindowHandle)
{ {
EnumWindows([](HWND handle, LPARAM param) -> BOOL { EnumWindows([](HWND handle, LPARAM param) -> BOOL {
@ -102,7 +107,20 @@ namespace Crash
}, (LPARAM)this); }, (LPARAM)this);
if (mAppWindowHandle) if (mAppWindowHandle)
{ {
// TODO: use https://devblogs.microsoft.com/oldnewthing/20111026-00/?p=9263 to monitor for the window being destroyed DWORD processId;
GetWindowThreadProcessId(mAppWindowHandle, &processId);
HWINEVENTHOOK eventHookHandle = SetWinEventHook(EVENT_OBJECT_DESTROY, EVENT_OBJECT_DESTROY, nullptr,
[](HWINEVENTHOOK hWinEventHook, DWORD event, HWND windowHandle, LONG objectId, LONG childId, DWORD eventThread, DWORD eventTime)
{
CrashMonitor& crashMonitor = *smEventHookOwners[hWinEventHook];
if (event == EVENT_OBJECT_DESTROY && windowHandle == crashMonitor.mAppWindowHandle && objectId == OBJID_WINDOW && childId == INDEXID_CONTAINER)
{
crashMonitor.mAppWindowHandle = nullptr;
smEventHookOwners.erase(hWinEventHook);
UnhookWinEvent(hWinEventHook);
}
}, processId, mAppMainThreadId, WINEVENT_OUTOFCONTEXT);
smEventHookOwners[eventHookHandle] = this;
} }
else else
return false; return false;

View file

@ -33,6 +33,8 @@ private:
HANDLE mShmHandle = nullptr; HANDLE mShmHandle = nullptr;
HANDLE mShmMutex = nullptr; HANDLE mShmMutex = nullptr;
static std::unordered_map<HWINEVENTHOOK, CrashMonitor*> smEventHookOwners;
void signalApp() const; void signalApp() const;
bool waitApp() const; bool waitApp() const;