如何检测线程是否具有Windows句柄? [英] How can I detect if a thread has windows handles?

查看:119
本文介绍了如何检测线程是否具有Windows句柄?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于给定的进程,如何以编程方式检测线程上是否具有Windows句柄?

How can I programmatically detect if a thread has windows handles on it for a given process?

spy ++给了我这些信息,但是我需要以编程的方式来做.

spy++ gives me this information but I need to do it programmatically.

我需要在C#中执行此操作,但是.net诊断库没有提供此信息.我想象spy ++正在使用一些我不知道的Windows API调用.

I need to do this in C#, however the .net diagnostics libs don't give me this information. I imagine spy++ is using some windows api call that I don't know about.

我可以访问要调试的系统代码.我想定期嵌入一些由计时器调用的代码,这些代码将检测有多少线程包含Windows句柄并记录此信息.

I have access to the code of the system I'm trying to debug. I want to embed some code called by a timer periodically that will detect how many thread contain windows handles and log this info.

谢谢

推荐答案

我相信您可以使用Win api函数:

I believe you can use win api functions: EnumWindowsProc to iterate through window handles and GetWindowThreadProcessId to get the thread id and process id associated with given window handle

请检查以下示例是否适合您:

Please check if an example below would work for you:

此代码使用System.Diagnostics遍历进程和线程;对于每个线程ID,我正在调用GetWindowHandlesForThread函数(请参见下面的代码)

this code iterates through processes and threads using System.Diagnostics; for each thread ID I'm calling GetWindowHandlesForThread function (see code below)

foreach (Process procesInfo in Process.GetProcesses())
{
    Console.WriteLine("process {0} {1:x}", procesInfo.ProcessName, procesInfo.Id);
    foreach (ProcessThread threadInfo in procesInfo.Threads)
    {
        Console.WriteLine("\tthread {0:x}", threadInfo.Id);
        IntPtr[] windows = GetWindowHandlesForThread(threadInfo.Id);
        if (windows != null && windows.Length > 0)
            foreach (IntPtr hWnd in windows)
                Console.WriteLine("\t\twindow {0:x}", hWnd.ToInt32());
    }
}

GetWindowHandlesForThread实现:

GetWindowHandlesForThread implementation:

private IntPtr[] GetWindowHandlesForThread(int threadHandle)
{
    _results.Clear();
    EnumWindows(WindowEnum, threadHandle);
    return _results.ToArray();
}

private delegate int EnumWindowsProc(IntPtr hwnd, int lParam);

[DllImport("user32.Dll")]
private static extern int EnumWindows(EnumWindowsProc x, int y);
[DllImport("user32.dll")]
public static extern int GetWindowThreadProcessId(IntPtr handle, out int processId);

private List<IntPtr> _results = new List<IntPtr>();

private int WindowEnum(IntPtr hWnd, int lParam)
{          
    int processID = 0;
    int threadID = GetWindowThreadProcessId(hWnd, out processID);
    if (threadID == lParam) _results.Add(hWnd);
    return 1;
}

以上代码的结果应转储到控制台smth中,如下所示:

result of the code above should dump into console smth like this:

...
process chrome b70
    thread b78
        window 2d04c8
        window 10354
...
    thread bf8
    thread c04
...

这篇关于如何检测线程是否具有Windows句柄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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