I have injected my dll into process.
How can I get Main window handle of host application?
I have injected my dll into process. How can I get Main window handle
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The host application may have multiple ‘main windows’. To detect them, you could
GetCurrentProcessIdto get the PID of the current processEnumWindowsto iterate over all toplevel windows of the desktopGetWindowThreadProcessIdto get the PID of the process which created the windowThat gives you a list of toplevel windows created by the process which you injected your DLL into. However, please note that this approach may yield windows which have been destroyed by the time you process the constructed list of windows. Hence, when doing something with the windows, make sure to use the
IsWindowfunction to ensure that the window at hand is still valid (this is still prone to race conditions since the window may become invalid between your call toIsWindowand actually accessing the window, but the time window is much smaller).Here’s a C++ function implementing this algorithm. It implements a
getToplevelWindowsfunction which yields astd::vector<HWND>containing the handles of all toplevel windows of the current process.UPDATE: These days (about four years after I gave the answer) I would also consider traversing the list of threads of the application and then using
EnumThreadWindowson each thread. I noticed that this is considerably faster in many cases.