如何以编程方式获取Windows进程的线程数? [英] How can I programmatically get thread counts for a Windows process?

查看:179
本文介绍了如何以编程方式获取Windows进程的线程数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过C/C ++计算当前Windows进程的本机线程.我看到有一个与.NET答案有关的相关问题,但是我无法使用该解决方案.我怀疑通过PdhOpenQuery/PdhCollectQueryData可能有解决方案,但我还没有探索这个方向,我希望有一种更简单的方法.

I want to count native threads of the current Windows process via C/C++. I see there's a related question with a .NET answer, but I can't use that solution. I suspect that there may be a solution via PdhOpenQuery/PdhCollectQueryData but I haven't explored that direction yet, and I'm hoping there's an easier approach.

更新:我应该说我当前的实现使用CreateToolhelp32Snapshot/Thread32First/Thread32Next,这就是我要替换的东西.该实现非常繁琐,在我的过程中每次调用都会导致20,000页错误.也许我只是用错了?

UPDATE: I should have said that my current implementation uses CreateToolhelp32Snapshot/Thread32First/Thread32Next and that's what I'm trying to replace. That implementation is heavy-handed and causes 20,000 page faults on every invocation in my process. Maybe I'm just using it wrong?

Update2:对我来说最有效的解决方案是使用我感兴趣的进程的PID创建一个类似于"\ Process( _)\ Thread Count"的字符串.然后我调用PdhExpandWildCardPath()进行扩展""通配符.然后,我调用PdhOpenQuery(),PdhAddCounter()和PdhCollectQueryData()进行初始化.之后,我调用了PdhCollectQueryData()和PdhGetFormattedCounterValue()来定期获取我的值.

Update2: The solution that worked best for me was to make a string like "\Process(_)\Thread Count" with the PID of the process I was interested in. Then I called PdhExpandWildCardPath() to expand the "" wildcard. Then I invoked PdhOpenQuery(), PdhAddCounter() and PdhCollectQueryData() to initialize. Thereafter, I called PdhCollectQueryData() and PdhGetFormattedCounterValue() to get my values periodically.

推荐答案

编辑第二个:您的文本显示为当前进程".如果真是这样,您可以实现一个小的DLL,其 DllMain 使用InterlockedDecrement(在DLL_THREAD_DETACH上)和InterlockedIncrement(在DLL_THREAD_ATTACH上)维护活动的线程计数器.

EDIT the second: Your text says "current process". If that is really the case, you could implement a small DLL whose DllMain maintains an active thread counter using InterlockedDecrement (on DLL_THREAD_DETACH) and InterlockedIncrement (on DLL_THREAD_ATTACH).

您必须确保您的进程尽早加载此DLL,以便主线程的线程计数从1开始.然后,您的线程数始终是最新的,并且可以通过Interlocked* API快速访问.

You would have to make sure your process loads up this DLL early on so the thread count starts at 1 for our main thread. Then your thread count is always up to date and quickly accessible via the Interlocked* APIs.

为了提高性能,您可以一次访问PerfMon计数器来获取您的进程,并获得给定进程的线程计数.您可以在此处的VB代码中进行建模

For improved performance, you could access the PerfMon counters for your process and get the thread count for a given process in one shot. There is VB code here that you could model off of.

您还可以使用WMI按进程枚举线程,但这很难成为简单的编程模型.

You could also use WMI to enumerate threads by process but this is hardly an easy programming model.

PerfMon将是最快的.

PerfMon will be the fastest.

原始: Raymond Chen在此处上有确切的说明.只需按匹配您自己的进程ID进行过滤即可(通过

ORIGINAL: Raymond Chen has exact instructions for this here. Just need to filter by process ID matching your own (obtained via GetCurrentProcessId) in the condition before the printf.

#include <stdio.h>
#include <windows.h>
#include <tlhelp32.h>

int __cdecl main(int argc, char **argv)
{
 HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
 if (h != INVALID_HANDLE_VALUE) {
  THREADENTRY32 te;
  te.dwSize = sizeof(te);
  if (Thread32First(h, &te)) {
   do {
     if (te.dwSize >= FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) +
                      sizeof(te.th32OwnerProcessID)) {
       printf("Process 0x%04x Thread 0x%04x\n",
             te.th32OwnerProcessID, te.th32ThreadID);
     }
   te.dwSize = sizeof(te);
   } while (Thread32Next(h, &te));
  }
  CloseHandle(h);
 }
 return 0;
}

这篇关于如何以编程方式获取Windows进程的线程数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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