c++windowsdllfindwindow

C++ FindWindow to find current window


I have a program that opens a window (a video game, actually) on Windows 8.1. That program then calls an "extension" in the form of a C++ .DLL that I compiled. Within that DLL, I need to get a handle to the window of the program (the video game) that is calling the DLL. I can do this using the FindWindow command combined with the name of the video game window. However, I sometimes need to have 2 copies of it open at once, both with the same window name. This means that using FindWindow(windowName) is not guaranteed to select the window that I actually want. Is there a way to get a handle to the window that is the same application that is calling the C++ code, without having to specify the name?


Solution

  • For each window you get from EnumWindow that matches the windowName you can check HWND's process and pick one that belongs to the process you're running in. That can be done using 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.