如何从进程ID中查找多个窗口句柄 [英] How to find multiple window handles from a process ID

查看:463
本文介绍了如何从进程ID中查找多个窗口句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何查找和枚举与单个PID(进程ID)关联的所有窗口句柄,例如当程序使用单个进程中的多个窗口时。



我的尝试:



我发布了我认为这个问题的答案(为了Code Project社区的利益) )。请随意发布更好的答案。

How can one find and enumerate all window handles associated with a single PID (Process ID), for example when a program uses multiple windows from a single process.

What I have tried:

I published what I think to be the answer for this question (for the benefit of the Code Project community). Please feel free to publish better answers.

推荐答案

以下代码定位每个给定PID的所有窗口的句柄。

The following code locates the handles of all windows per a given 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);
}


上面的代码与函数内main和local变量传递给函数的参数具有相同的名称。

即DWORD dwProcessID = 0;



将导致它枚举所有进程中的所有窗口。



将局部变量名更改为其他名称,并在GetWindowThreadProcessId()函数和比较中使用它。
The above code has the same name for both parameter passed to the function from main and local variable inside the function.
i.e. DWORD dwProcessID = 0;

which will cause it to enumerate all the windows from all the processes.

change the local variable name to something else and use that in GetWindowThreadProcessId() function and comparison too.


这篇关于如何从进程ID中查找多个窗口句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆