Reputation: 4532
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?
Upvotes: 0
Views: 3191
Reputation: 2085
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.
Upvotes: 1
Reputation: 11954
You can combine 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 to find out which of the windows belongs the the same thread you are being called from.
But then again - why not just pass the window handle to the dll directly?
Upvotes: 1