Jump to content
Tuts 4 You

get handle from window


Recommended Posts



i injected a new thread into a running process, the thread displays a MessageBox. This is working fine.


However i want to get the handle (hwnd) of the running window to pass them as parameter in the MessageBox api, like: MessageBox(hWnd, str1,str1,1);


but i can't retrieve the right handle of the window. i already tried api's such as Get(ForeGround)Window etc.


Any ideas how to get the handle of the running window?

Link to comment

are you trying to get the handle of the injected dll??? i mean the handle of the message box displayed using injected dll ?? or the handle of the injected process??

Link to comment

you could try FindWindow if you have the window title, otherwise you can try to enumerate all windows and find the process id they belong to. the window you need should have the process id of the process youre trying to find the window of.


Link to comment
Teddy Rogers

Just be aware the messagebox may not be a parent window or root owner window. if your enumerating and filtering out non-parent and owner windows you will never find it. Without knowing a bit more detail about how this messagebox appears I can't give you specific information.


However since your injecting code in to a running process most likely you already know the processID. What I have done in the past is use EnumWindows API to cycle through all the top level windows for their handles and pass the results to GetWindowThreadProcessId API. If the handle returned from EnumWindow and GetWindowThreadProcessId match then I know I have the correct window handle. You can pass your processID for EnumWindows through lParam.

EnumWindows_(@FindMysteryWindow(), YourKnownProcessID) Procedure FindMysteryWindow(hWnd, lParam)
  Protected PID.l
  If GetWindowThreadProcessId_(hWnd, @PID.l)
    If Not PID.l <> lParam
      ; Window and processID match, enter your code here...
ProcedureReturn #False
  ProcedureReturn #True

There are a dozen methods to find window handles, you could also try EnumChildWindows API. Another method is to EnumWindows and use GetWindowText API to search for the messagebox window title. You can find a list of Windows window API functions here...





Link to comment

thanks for all the suggestions tuts4you!


the problem is fixed using the method of post number 5.

Thank you  :punk:

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...