Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get hwnd by process id c++

Tags:

c++

get

pid

hwnd

How can I get the HWND of application, if I know the process ID? Anyone could post a sample please? I'm using MSV C++ 2010. I found Process::MainWindowHandle but I don't know how to use it.

like image 645
Luke Avatar asked Jul 29 '12 17:07

Luke


People also ask

How can I get Hwnd from process id?

You can use EnumWindows and GetWindowThreadProcessId() functions as mentioned in this MSDN article.

What is Hwnd C?

A Windows window is identified by a "window handle" ( HWND ) and is created after the CWnd object is created by a call to the Create member function of class CWnd . The window may be destroyed either by a program call or by a user's action.

How do I get a window handle in C++?

The proper way, in my humble opinion, is handle=FindWindowW(NULL, L"Calculator"); (using UNICODE) or handle=FindWindowA(NULL, "Calculator");(using ANSI). Even better, use instead FindWindow macro to avoid know what character set is used in the project, so make your code "agnostic" to character set.


1 Answers

HWND g_HWND=NULL;
BOOL CALLBACK EnumWindowsProcMy(HWND hwnd,LPARAM lParam)
{
    DWORD lpdwProcessId;
    GetWindowThreadProcessId(hwnd,&lpdwProcessId);
    if(lpdwProcessId==lParam)
    {
        g_HWND=hwnd;
        return FALSE;
    }
    return TRUE;
}
EnumWindows(EnumWindowsProcMy,m_ProcessId);
like image 94
Andre Kirpitch Avatar answered Oct 02 '22 08:10

Andre Kirpitch