通过进程id c++获取hwnd

时间:2021-10-24 22:04:19

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.

如果我知道流程ID,如何获得申请的HWND ?有人可以寄样品吗?我使用的是MSV c++ 2010。我找到了Process: MainWindowHandle,但是我不知道如何使用它。

3 个解决方案

#1


17  

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);

#2


3  

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

您可以使用本文中提到的枚举窗口和getwindowthreadessid()函数。

#3


0  

A single PID (Process ID) can be associated with more than one window (HWND). For example if the application is using several windows.
The following code locates the handles of all windows per a given PID.

单个PID(进程ID)可以与多个窗口(HWND)相关联。例如,如果应用程序使用多个窗口。下面的代码为每个给定的PID定位所有窗口的句柄。

void GetAllWindowsFromProcessID(DWORD dwProcessID, std::vector <HWND> &vhWnds)
{
    // find all hWnds (vhWnds) associated with a process id (dwProcessID)
    HWND hCurWnd = NULL;
    do
    {
        hCurWnd = FindWindowEx(NULL, hCurWnd, NULL, NULL);
        DWORD dwProcessID = 0;
        GetWindowThreadProcessId(hCurWnd, &dwProcessID);
        if (dwProcessID == dwProcessID)
        {
            vhWnds.push_back(hCurWnd);  // add the found hCurWnd to the vector
            wprintf(L"Found hWnd %d\n", hCurWnd);
        }
    }
    while (hCurWnd != NULL);
}

#1


17  

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);

#2


3  

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

您可以使用本文中提到的枚举窗口和getwindowthreadessid()函数。

#3


0  

A single PID (Process ID) can be associated with more than one window (HWND). For example if the application is using several windows.
The following code locates the handles of all windows per a given PID.

单个PID(进程ID)可以与多个窗口(HWND)相关联。例如,如果应用程序使用多个窗口。下面的代码为每个给定的PID定位所有窗口的句柄。

void GetAllWindowsFromProcessID(DWORD dwProcessID, std::vector <HWND> &vhWnds)
{
    // find all hWnds (vhWnds) associated with a process id (dwProcessID)
    HWND hCurWnd = NULL;
    do
    {
        hCurWnd = FindWindowEx(NULL, hCurWnd, NULL, NULL);
        DWORD dwProcessID = 0;
        GetWindowThreadProcessId(hCurWnd, &dwProcessID);
        if (dwProcessID == dwProcessID)
        {
            vhWnds.push_back(hCurWnd);  // add the found hCurWnd to the vector
            wprintf(L"Found hWnd %d\n", hCurWnd);
        }
    }
    while (hCurWnd != NULL);
}