2010-09-20 46 views
7

有沒有一種方法可以使用標準Windows C/C++ API來查詢當前爲特定進程運行的線程數?如何使用常規Windows C/C++ API查詢進程的線程數

我已經通過MSDN文檔橫行而是來到附近的唯一事情是

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

哪些查詢當前在給定進程,其中包括線程句柄使用的系統句柄數,但不會僅限於他們。

任何見解將不勝感激。

在此先感謝。

比約恩·

回答

5
+2

感謝您的提示,它向我發送了正確的方向。其實這個示例更匹配我所需要的:http://msdn.microsoft.com/en-us/library/ms686701(v=VS.85).aspx – Bjoern 2010-09-20 09:09:36

+0

很高興幫助,很高興你也發佈一個答案。 – Ivo 2010-09-20 10:02:17

5

只是要完成這裏是一個基於代碼示例,可以在接受答案的評論部分規定的鏈接中找到一些示例代碼:

#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 
相關問題