C++ FindWindow 查找当前 window

C++ FindWindow to find current window

我有一个程序可以在 Windows 8.1 上打开 window(实际上是视频游戏)。然后该程序以我编译的 C++ .DLL 的形式调用 "extension"。在该 DLL 中,我需要获取调用该 DLL 的程序(视频游戏)的 window 句柄。我可以使用 FindWindow 命令结合视频游戏的名称 window 来完成此操作。但是,有时我需要同时打开它的 2 个副本,它们都具有相同的 window 名称。这意味着使用 FindWindow(windowName) 不能保证 select 我真正想要的 window 。有没有一种方法可以获取与调用 C++ 代码的应用程序相同的 window 的句柄,而无需指定名称?

对于从 EnumWindow 获得的每个 window 与 windowName 匹配的每个 window,您可以检查 HWND's 进程并选择属于您 运行 in。这可以使用 GetWindowThreadProcessId function - it will give you the PID of the process the window belongs to and you can compare it to your own PID from GetCurrentProcessId.

来完成

您可以组合 FindWindowEx to enumerate all the windows with the given name (set hwndParent to NULL to use desktop as the parent and just pass the previous result as the hwndChildAfter when you search the second time, and so on) with GetWindowThreadProcessId and GetCurrentProcessId 来找出 windows 中的哪一个属于调用您的同一个线程。

但话说回来 - 为什么不直接将 window 句柄传递给 dll?