[RESOLVED] How to get window's HWND from it's process handle?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: [RESOLVED] How to get window's HWND from it's process handle?

  1. #1
    Join Date
    Sep 2004
    Location
    New Delhi, India
    Posts
    640

    [RESOLVED] How to get window's HWND from it's process handle?

    Hi there,

    How can I get the HWND of a window from it's process handle? I'm looking for some way other then GetProcessID because it is supported only for WinXP systems.

    Or, if anyone can tell how can I do the reverse, how can I get the process handle from HWND of a window.

    Basically what I'm trying is to detect if an app is running and then get the handle to it's window.

    Thanks.
    "I rather not play football than wear Nerrazzuri shirt" - Paolo Maldini
    FORZA MILAN!!!

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,288

    Re: How to get window's HWND from it's process handle?

    I guess it could be done the same way as explained in HOWTO: Terminate an Application "Cleanly" in Win32
    BTW, What are you trying to accomplish?

  3. #3
    Join Date
    Sep 2004
    Location
    New Delhi, India
    Posts
    640

    Re: How to get window's HWND from it's process handle?

    Victor, the link you gave tells how to end a process given you have it's process ID. And, getting the process ID is what I'm looking for.

    What I'm trying is to check whether an app, say notepad, is running. If yes then my application would take a screenshot of it. I'm able to determine whether an app is running by using EnumProcesses, now I want to get the process ID of it. With the process ID I would use EnumWindows and call on GetWindowThreadProcessId to search for the window of the process.
    "I rather not play football than wear Nerrazzuri shirt" - Paolo Maldini
    FORZA MILAN!!!

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

    Re: How to get window's HWND from it's process handle?

    I am confused about what you know and what you still need to know. Some of your statements seem to be contradicting, so I will tell you some possibilities:

    1)
    HAVE: Process ID, NEED: Process handle
    Solution: OpenProcess()

    2)
    HAVE: Process handle, NEED: Process ID
    Solution: GetProcessId()

    3)
    HAVE: Window handle, NEED: Process ID
    Solution: GetWindowThreadProcessId()

    4)
    HAVE: Window handle, NEED: Process handle
    Solution: Use 3) and then 1)

    5)
    HAVE: Process ID, NEED: Window handle
    Solution: EnumWindows(), then in the callback function do 3) and check if it matches your process ID.

    6)
    HAVE: Process handle, NEED: Window handle
    Solution: 2) and then 5)
    Please don't forget to rate users who helped you!

  5. #5
    Join Date
    Sep 2004
    Location
    New Delhi, India
    Posts
    640

    Re: How to get window's HWND from it's process handle?

    Yeah...I was a little dizzy. The solution was always in front of me . With EnumProcesses I got the process ID's thus there is no need of GetProcessID (what was I thinking before).

    Victor, Phil, thanks for the help.

    And, it's 3:45 AM in this part of the world and I better catch some sleep.

    Gute Nacht .
    "I rather not play football than wear Nerrazzuri shirt" - Paolo Maldini
    FORZA MILAN!!!

  6. #6
    Join Date
    Jun 2010
    Posts
    1

    Re: [RESOLVED] How to get window's HWND from it's process handle?

    Hello!
    I used the way prescribed here. Firstable I found all processes ID's. Becasue I need to identify specific one, I wanted to do it by it's name. Unfortunately it's <uknown> (see code). So, I enumerated all windows and got thei ID's for each process, and compared with it's ID. It turns out, thath none of them matches. What is the solution?
    Here is code:
    Code:
    #include <stdafx.h>
    #include <windows.h>
    #include <stdio.h>
    #include <psapi.h>
    
    #pragma comment(lib, "psapi.lib")
    
    DWORD processId;
    
    BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lparam)
    {
    	//if(!hwnd) return false;
    	DWORD id=GetWindowThreadProcessId(hwnd,&id);
    	printf("process id: %d %d\n", id, processId);
    	if(id==processId)
    	{
    		printf("THEY MATCH!process id: %d\n", id);
    		char buffer[256];
    		GetWindowText(hwnd,(LPWSTR)buffer,255);
    		printf("%s ", buffer);
    
    	}
    
    	return true;
    
    }
    
    void PrintProcessNameAndID( DWORD processID )
    {
    
    	processId=processID;
        TCHAR szProcessName[256] = TEXT("<unknown>");
    
        // Get a handle to the process.
    
        HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                                       PROCESS_VM_READ,
                                       FALSE, processID );
    
        // Get the process name.
    
        if (NULL != hProcess )
        {
            HMODULE hMod;
            DWORD cbNeeded;
    
            if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
                 &cbNeeded) )
            {
                GetModuleBaseName( hProcess, hMod, szProcessName,
                                   sizeof(szProcessName)/sizeof(TCHAR) );
            }
        }
    
    	EnumWindows(&EnumWindowsProc,NULL);
    
        // Print the process name and identifier.
    
        _tprintf( TEXT("%s  (PID: %u)\n"), szProcessName, processID );
    
        CloseHandle( hProcess );
    	system("PAUSE");
    }
    
    int main()
    {
        // Get the list of process identifiers.
    
        DWORD aProcesses[1024], cbNeeded, cProcesses;
        unsigned int i;
    
        if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
            return 0;
    
        // Calculate how many process identifiers were returned.
    
        cProcesses = cbNeeded / sizeof(DWORD);
    
        // Print the name and process identifier for each process.
    
        for ( i = 0; i < cProcesses; i++ )
            if( aProcesses[i] != 0 )
                PrintProcessNameAndID( aProcesses[i] );
    
            return 0;
    }

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,288

    Re: [RESOLVED] How to get window's HWND from it's process handle?

    I have no time to test/revise your code, but this lie seems to be wrong:
    Quote Originally Posted by Pablosz View Post
    Code:
    ...
    DWORD processId;
    
    BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lparam)
    {
    	//if(!hwnd) return false;
    	DWORD id=GetWindowThreadProcessId(hwnd,&id);
    	....
    }
    Why are you using the same variable for both thread and process IDs?
    Didn't you read the documentation of GetWindowThreadProcessId Function?
    Victor Nijegorodov

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