From 4db2b6528cbfd235f3b4de6abed00511d834daba Mon Sep 17 00:00:00 2001 From: Nemirtingas Date: Wed, 21 Aug 2019 21:50:59 +0200 Subject: [PATCH] Changed window detection code. GetModuleHandle(NULL) get the exe handle. Sometimes the window couldn't be found because the window wasn't created in the .exe but in a dll. Thats why I list all dll HANDLES and try to find the main window handle. --- overlay_experimental/Windows_Hook.cpp | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/overlay_experimental/Windows_Hook.cpp b/overlay_experimental/Windows_Hook.cpp index 55530e30..f755f343 100644 --- a/overlay_experimental/Windows_Hook.cpp +++ b/overlay_experimental/Windows_Hook.cpp @@ -7,20 +7,31 @@ extern LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam #include "../dll/dll.h" +#include + HWND GetGameWindow() { - HWND hWnd = FindWindow(NULL, NULL); - while (hWnd) + HWND hWnd = FindWindow(nullptr, nullptr); + HMODULE hModules[512]; + DWORD needed; + if (EnumProcessModules(GetCurrentProcess(), hModules, 512, &needed) != 0) { - if (!GetParent(hWnd)) + int numMods = needed/sizeof(HMODULE); + while (hWnd) { - if (GetModuleHandle(NULL) == (HMODULE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE)) - break; + HMODULE wndInst = (HMODULE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE); + if (wndInst != nullptr) + { + for (int i = 0; i < numMods; ++i) + { + if (!GetParent(hWnd) && hModules[i] == wndInst) + return hWnd; + } + } + hWnd = GetWindow(hWnd, GW_HWNDNEXT); } - hWnd = GetWindow(hWnd, GW_HWNDNEXT); } - if (!hWnd) - PRINT_DEBUG("Failed to get game window HWND\n"); + PRINT_DEBUG("Failed to get game window HWND\n"); return hWnd; }