如何使用常规Windows C/C++ API查询进程的线程数

Bjo*_*ern 8 c c++ winapi

有没有办法使用标准的Windows C/C++ API查询当前为特定进程运行的线程数?

我已经在MSDN文档中徘徊,但唯一接近的是

BOOL WINAPI GetProcessHandleCount(
  __in     HANDLE hProcess,
  __inout  PDWORD pdwHandleCount
);
Run Code Online (Sandbox Code Playgroud)

查询给定进程当前正在使用的系统句柄的数量,其中包括线程句柄,但不限于它们.

任何见解将不胜感激.

提前致谢.

比约恩

Bjo*_*ern 9

在这里完成的是一些基于代码示例的示例代码,可以在接受的答案的注释部分中指出的链接下找到:

#if defined(_WIN32)

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

/**
Returns the thread copunt of the current process or -1 in case of failure.
*/
int GetCurrentThreadCount()
{
    // first determine the id of the current process
    DWORD const  id = GetCurrentProcessId();

    // then get a process list snapshot.
    HANDLE const  snapshot = CreateToolhelp32Snapshot( TH32CS_SNAPALL, 0 );

    // initialize the process entry structure.
    PROCESSENTRY32 entry = { 0 };
    entry.dwSize = sizeof( entry );

    // get the first process info.
    BOOL  ret = true;
    ret = Process32First( snapshot, &entry );
    while( ret && entry.th32ProcessID != id ) {
        ret = Process32Next( snapshot, &entry );
    }
    CloseHandle( snapshot );
    return ret 
        ?   entry.cntThreads
        :   -1;
}

#endif // _WIN32
Run Code Online (Sandbox Code Playgroud)


Ivo*_*Ivo 5

请参阅此示例:http://msdn.microsoft.com/en-us/library/ms686852(v = VS.85).aspx

  • 谢谢你的提示,它让我朝着正确的方向前进.实际上这个样本甚至更符合我的需要:http://msdn.microsoft.com/en-us/library/ms686701(v = VS.85).aspx (2认同)