CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Nov 2003
    Posts
    19

    Question How can I get the window handle from process id?

    By using CreateProcess, I can invoke a new application, and get the process id for the newly created process, but how can I get the handle of the application's main window?
    Thanks!!!

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150
    Well that's really tricky if you're using C++.
    You can use WaitForIdle but that sometimes returns when the splash screen of the launched app is shown, so this does not work in all cases.

    A second way but also a much more complicated way is the following.
    Use SetWindowsHookEx to create a hook for WH_SHELL.
    In your ShellProc handle HSHELL_WINDOWCREATED.
    After you've launched your app, start waiting.
    For each new window created, your ShellProc will be called.
    For each window call GetWindowThreadProcessId(hWnd, &dwProcessID);
    Compare the dwProcessID of the newly created window with the processid you got from CreateProces.
    If those two processids are equal you've found your window.

    NOTE: Even this complicated way does not always work, so make sure you add some kind of timeout in your app, so that you do not keep waiting for your window.

    NOTE 2: Try to do as little as possible in your ShellProc because it's called for each new shell-window that is created.

    NOTE 3: I'm interested if anyone knows a better way.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Nov 2003
    Posts
    19
    Does that mean it's impossible to retrieve the window's handle if I just provide you a process id?
    Thank you!

  4. #4
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150
    Yes, the problem is that a process might create several windows, so you cannot map a processid to just one window, although deep deep down in Windows, there should be something that should make this possible I guess, because in C# you CAN ask for the window after launching an app IIRC.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  5. #5
    Join Date
    Jun 2003
    Posts
    11
    I may be just thinking loud here. Since you know your app. windows name etc., is it possible you call FindWindowEx()?

    Will

  6. #6
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150
    FindWindow is not really a solution. When are you going to call it?
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  7. #7
    Join Date
    Jun 2003
    Posts
    11
    Hi,

    If I haven't misunderstood the question, then calling "FindWindows", in the main app, would return windows handles of all loaded app/windows, including the caller. Of course you may need to worry about syn issue - the new app. windows spawn by CreateProcess must have been loaded fully.

    I tried something similar a while back, but I'm not sure the exact problem we're trying to solve.



    Will

  8. #8
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Use EnumWindows to enumerate all top-level windows and for each window use GetWindowThreadProcessId.

  9. #9
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150
    EnumWindows is not guaranteed to work, because CreateProcess returns immediatly, so when are you going to call EnumWindows?
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  10. #10
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    I did not mention that becuase that question is already answered in a previous post.

  11. #11
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266

    Re: How can I get the window handle from process id?

    This thread is nearly 8 years old, but saying "EnumWindows is not guaranteed to work" is not the same thing as saying that it will never work. If the main window is the only window for the application, then there is no problem.
    Last edited by Sam Hobbs; October 15th, 2011 at 01:46 PM. Reason: Extraneous word
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  12. #12
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: How can I get the window handle from process id?

    There are lots of situations when EnumWindows is "guaranteed not to work" even with a single window. For example, with the process created in foreign session EnumWindows is going to find nothing. The same to window created in current session but another desktop. Etc.
    Best regards,
    Igor

  13. #13
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266

    Re: How can I get the window handle from process id?

    I guess that is why I stopped participating in CodeGuru. Members have a tendency to insist that something is true without regard for truth.

    The truth is that it can work; just because it won't work with some applications does not mean it can never work with other applications.

    I think it is misleading comments such as in this thread that caused CodeGuru to be less successful. CodeProject was started by a previous part-owner of CodeGuru (Chris Maunder); correct? And now CodeProject appears in MSDN results. In my oinion, this thread shows some of the reason why CodeProject became more successful.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  14. #14
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: How can I get the window handle from process id?

    Okay, you stopped participating, you decided CG is on its way down. Why be so emotional about the site you don't care of anymore? Or you still do care? Then just admit that your statement "If the main window is the only window for the application, then there is no problem" was not that accurate? Indeed, EnumWindows may work in some situations (as well as FindWindow may do with the same success, by the way) except special cases I mentioned ...with regard for truth.
    Best regards,
    Igor

  15. #15
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: How can I get the window handle from process id?

    I hope, this article can help the OP:
    Controlling Notepad From C++ Applications
    Of course it contains just one approach, not the only one possible and it's not universally applicable.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured