2009-07-30 75 views

回答

5

Enumerating threads in a process at MSDN Blogs。

代码从那里片段:

#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; 
} 
+0

完美,谢谢! – bdonlan 2009-07-30 14:23:06

1

ToolHelp库为快照过程和枚举其线程(以及其他属性)提供了一个API。 See MSDN for details