CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Hybrid View

  1. #1
    Join Date
    Aug 2005
    Location
    Chennai,India
    Posts
    136

    Thumbs up How to Get windows Handle using Process Id

    Dear All,

    I am having the process id of all the process. Now I want to get the Windows Handle for that Process. Is it possible ?

    Thanks in Advance.

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: How to Get windows Handle using Process Id

    No.
    A process can have multiple windows open.
    You could however iterate over all the windows and retrieve the process ID from the HWND and match this process ID with the ID you are interested in. Check out GetWindowThreadProcessId .
    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
    May 2005
    Posts
    4,954

    Re: How to Get windows Handle using Process Id

    here the a sample code for doing it:
    Code:
    HWND h = ::GetTopWindow(0 );
    while ( h )
    {
      DWORD pid;
      DWORD dwTheardId = ::GetWindowThreadProcessId( h,&pid);
    
    
    
             if ( pid == /*your process id*/ )
             {
        // here h is the handle to the window
                  break;
             }
             h = ::GetNextWindow( h , GW_HWNDNEXT);
    }
    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  4. #4
    Join Date
    Aug 2005
    Location
    Chennai,India
    Posts
    136

    Thumbs up Re: How to Get windows Handle using Process Id

    Thanks Gurus

    Sample Code is working Fine. I can able to get the Handles of all open windows. Still I have a doubt what is handle (HANDLE) and what is handle to window ( HWND ) is there any difference in that.

    Please clarify my doubt, if possible give a good direction to know more about it.

    Thanks in Advance.

  5. #5
    Join Date
    Jul 2005
    Location
    Germany
    Posts
    1,194

    Re: How to Get windows Handle using Process Id

    A HWND is a special type of a HANDLE: a handle to a window. Likewise HFILE is a handle to a file. Since some functions work with more than one type of handle there is the generic type HANDLE. For example with CloseHandle() you can close a file handle or a thread handle, etc. A handle is only a kind of pointer to the real thing, which is managed internly by windows.
    Please don't forget to rate users who helped you!

  6. #6
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: How to Get windows Handle using Process Id

    Quote Originally Posted by philkr
    A HWND is a special type of a HANDLE: a handle to a window. Likewise HFILE is a handle to a file. Since some functions work with more than one type of handle there is the generic type HANDLE. For example with CloseHandle() you can close a file handle or a thread handle, etc. A handle is only a kind of pointer to the real thing, which is managed internly by windows.
    A HWND is not HANDLE but it is a handle indeed. A HWND cannot be used to replace when a HANDLE is needed. And HFILE shall not be used anymore, it got generally replaced via HANDLE and it's only served through backward compatibility with 16bit windows and the "old" I/O routines.
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  7. #7
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: How to Get windows Handle using Process Id

    Guys, you know this thread is 5 years old ...
    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 ]

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

    Re: How to Get windows Handle using Process Id

    ...yep, and in spite to this the last guy is still unaware of the handles details.
    Best regards,
    Igor

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