【问题标题】:How to query the thread count of a process using the regular Windows C/C++ APIs如何使用常规 Windows C/C++ API 查询进程的线程数
【发布时间】:2010-09-20 08:17:28
【问题描述】:

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

我已经浏览了 MSDN 文档,但唯一接近的是

BOOL WINAPI GetProcessHandleCount(
  __in     HANDLE hProcess,
  __inout  PDWORD pdwHandleCount
);

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

任何见解都将不胜感激。

提前致谢。

比约恩

【问题讨论】:

    标签: c++ c winapi


    【解决方案1】:

    这里要完整的是一些基于代码示例的示例代码,可以在已接受答案的 cmets 部分中所述的链接下找到:

    #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
    

    【讨论】:

      【解决方案2】:

      【讨论】:

      猜你喜欢
      • 2011-08-06
      • 2021-02-06
      • 2020-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多