How to request the number of process threads using regular Windows C / C ++ APIs

Is there a way to request the number of threads that are currently running for a specific process using the standard Windows C / C ++ APIs?

I have already made my way through MSDN docs, but the only thing approaching is

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

which requests the number of system descriptors currently used by this process, which will include thread handles, but will not be limited to them.

Any ideas would be appreciated.

Thanks in advance.

Bjorn

+7
c ++ c winapi
source share
2 answers
+5
source share

Just to fill in, here is an example code based on a sample code that can be found at the link provided in the comments section of the accepted answer:

 #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 
+5
source share

All Articles