Thread: How can I get the HWND of a certain process?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    55

    How can I get the HWND of a certain process?

    Suppose I want to get the HWND of the notepad from its process name (notepad.exe). How can I do that?

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Code:
    HWND Handle = FindWindow(NULL, "Untitled - Notepad");
    That finds a notepad with an untitled document, wether this is the best way of doing it, probably not. I don't know of a better way though.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    55
    No, that won't work. I said notepad.exe as an example, but truly I can have the name of any process. And I MUST get its HWND.

  4. #4
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Use EnumWindows to obtain a handle to every window on the desktop, then in your EnumWindowsProc callback function, call GetModuleFileName on each handle and search it for the name of the process you are looking for. The only limitation I can see is that EnumWindows only enumerates top-level windows (so if you are searching for a background process or process with a hidden/no window, this may not work as intended).

  5. #5
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    That's okay, he can call EnumWindows on the resualts that the original EnumWindows returned and do that on every handle, revealing all windows.
    The problem I see is, what if the process has multipul windows?

  6. #6
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Once again, you should really read MSDN before posting...
    Firstly, EnumWindows enumerates through all top-level windows, so "calling it on another window" makes no sense (and is not even possible). You may have been thinking of EnumChildWindows instead; however this makes even less sense as to why you would enumerate through a window's children in order to find its process name?

    Secondly, dealing with multiple windows is not a problem at all, it is exactly the same process as above: you enumerate through all top-level windows and for each HWND that EnumWindows fetches:
    Code:
    if(HWND's process file name == target file name)
         do(x)

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    1. Get the process id for your target process.
    This can be done by enumerating through all the processes on the system with Process32First.

    2. Enumerate all the top level windows.
    This can be done using EnumWindows.

    3. Get the process id of each window.
    This can be done using GetWindowThreadProcessId.

    4. Compare the window process id with the target process id.
    This can be done with the == operator.

    5. Let off a 'whoop, whoop!' when you find a match.

    Conveniently, this is all (except step 5) demonstrated in the killprocess.c code.

  8. #8
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    What if the process doesn't has a window (HWND)?
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  9. #9
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    you cant get the HWND of a process that doesnt have an HWND. I fail to see the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  3. My first "real" windows app
    By JoshR in forum Windows Programming
    Replies: 2
    Last Post: 07-28-2005, 07:40 AM
  4. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM