Window-handle from process ID?

SephirothSephiroth Fayetteville, NC, USA
I am working on an updater for one of my programs and I have hit a bit of a snag. I know how to enumerate all processes and check the executable filename, but once I have that information, how would I get the window-handle to that process so I can ask it to close for updating? I already have the process ID, the module-handle for the process, and an open handle to the process if it matters.

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

Comments

  • There might be a smoother way, but it should work to use EnumWindows and from the enum proc call GetWindowThreadProcessId and compare it with your process ID.
  • SephirothSephiroth Fayetteville, NC, USA
    : There might be a smoother way, but it should work to use EnumWindows
    : and from the enum proc call GetWindowThreadProcessId and compare it
    : with your process ID.
    :
    Yes I am using that, but I have a strange problem. I am replying here since my thread on the Windows forum is being ignored, or where nobody has a solution.

    Alright, I have the process ID for my second program, but when I enumerate all the windows, none have that process ID. That is my problem now. Why doesn't my window have the process ID of the executable that created it?

    Now, before you think that I may not know what I am doing, I tested my code by looking for other executables and got some odd results. I first tried looking for the Notepad executable in the process list and getting its window-handle, but due to my program being 32bit and this being x64 Pro, I can't "see" 64bit processes with my program.

    To get around that, I fired up "Guild Wars" and had my program look for gw.exe, get the process ID, enumerate all windows until it found one with the same ID, and then end it "WM_CLOSE". This worked, and every time I tried to start GW while my app was running, it got closed.

    I tried it again with World of Warcraft and ran into the EXACT same problem as with my personal program. I can get the process ID of the wow.exe, but not a single window has that process ID. This makes me think that something odd is going on with window-enumeration or something.

    I tried this a third time with "AMD_PwrMon.exe" which monitors the AMD CPU in this laptop. I could close it as well. SO what is it that makes my program and World of Warcraft create windows with no PID, or a different PID?

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
  • : I am working on an updater for one of my programs and I have hit a
    : bit of a snag. I know how to enumerate all processes and check the
    : executable filename, but once I have that information, how would I
    : get the window-handle to that process so I can ask it to close for
    : updating? I already have the process ID, the module-handle for the
    : process, and an open handle to the process if it matters.


    Some process can create child windows, and you need to enumerate them using EnumChildWindows. Just to make sure you know, process IDs aren't static. When you launch an application its given a unique process ID that may (or may not) be the same as the last time as it runs. If you show some code maybe I can help a little more. Just know that there are ways for applications to actually hide themselves so when trying to find a PID for a running app, there will be none. Also, check out this application called Process Explorer, it allows you to view handles of a process among other things. Double check your code and make sure you don't have off by 1 errors.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories