How to Get windows Handle using Process Id
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: How to Get windows Handle using Process Id

  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,062

    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 .

  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
    Jul 2010
    Posts
    1

    Re: How to Get windows Handle using Process Id

    according to: http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx
    HWND is defined as such:
    typedef HANDLE HWND;
    typedef PVOID HANDLE;
    typedef void *PVOID;

    Also in Visual Studio 6, HWND is defined in the include files as such:
    typedef /* [wire_marshal] */ void __RPC_FAR *HWND; (C:\Program Files\Microsoft Visual Studio\VC98\Include\WTYPES.H)
    # define __RPC_FAR (C:\Program Files\Microsoft Visual Studio\VC98\Include\RPC.H)
    typedef void *HANDLE; (C:\Program Files\Microsoft Visual Studio\VC98\Include\WINNT.H)
    typedef void *PVOID; (C:\Program Files\Microsoft Visual Studio\VC98\Include\WINNT.H)

    So, as far as I can tell, HWND and HANDLE are exactly equivalent, at least as far as VC6 is concerned. I have yet to confirm this in VS2010.

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,962

    Re: How to Get windows Handle using Process Id

    According to this experiment below, HWND is not equivalent to HANDLE:
    Code:
    // E:\Temp\558\558.cpp
    #include <windows.h>
    
    int main()
    {
        HANDLE h    = NULL;
        HWND   hwnd = NULL;
    
        hwnd = h;
    
        return 0;
    }
    Code:
    E:\Temp\558>cl 558.cpp
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86
    Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.
    
    558.cpp
    558.cpp(8) : error C2440: '=' : cannot convert from 'HANDLE' to 'HWND'
            Conversion from 'void*' to pointer to non-'void' requires an explicit cast
    Best regards,
    Igor

  9. #9
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,062

    Re: How to Get windows Handle using Process Id

    Guys, you know this thread is 5 years old ...

  10. #10
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,962

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center