Files
CoopAllTheThings/hook/src/find_window.hpp
BlackMark 635ef51283 Deduplicate the hook DLL and scrub history from its comments
Consolidate four copies of the keyed-mutex shared-texture setup
(present/opengl/d3d9/vk_capture) into one RAII SharedVideoTexture,
two copies of find_main_window into find_window.hpp, audio_hook's
hand-rolled detour guard into the shared DetourGate, the duplicated
vtable_method into vtable_hook.hpp, and the near-identical
Present/Present1 and SwapBuffers/wglSwapBuffers detour pairs into one
shared body each. The vk_layer and vk_capture_perf_test targets now
compile debug_log.cpp since the shared texture code logs.

Comments no longer narrate the past: drop stress-test/game anecdotes,
"used to"/"the old model" phrasing, plan-step labels, and pointers to
docs that do not exist; fix present_hook.hpp/opengl_hook.hpp claims
that predate the D3D12/D3D9/Vulkan backends. Net -266 lines, no
behavior change (full x64 + x86 suites pass, including the mock-game
hook/unhook storm).
2026-07-12 08:53:58 +02:00

47 lines
1.1 KiB
C++

// Locate the game's main window from inside the game process: the largest
// visible, unowned top-level window the pid owns. Shared by the focus spoof
// (subclass target) and the MKB forwarder (PostMessage target).
#pragma once
#include <windows.h>
namespace coop::hook
{
inline HWND find_main_window(DWORD pid)
{
struct Ctx
{
DWORD pid;
HWND best;
long best_area;
} ctx{pid, nullptr, 0};
EnumWindows(
[](HWND hwnd, LPARAM lparam) -> BOOL {
auto* c = reinterpret_cast<Ctx*>(lparam);
DWORD pid = 0;
GetWindowThreadProcessId(hwnd, &pid);
if (pid != c->pid || !IsWindowVisible(hwnd) || GetWindow(hwnd, GW_OWNER) != nullptr)
{
return TRUE; // not ours, hidden, or an owned dialog -- keep looking
}
RECT rect = {};
if (!GetWindowRect(hwnd, &rect))
{
return TRUE;
}
const long area = (rect.right - rect.left) * (rect.bottom - rect.top);
if (area > c->best_area)
{
c->best_area = area;
c->best = hwnd;
}
return TRUE;
},
reinterpret_cast<LPARAM>(&ctx));
return ctx.best;
}
} // namespace coop::hook