【发布时间】:2012-04-01 15:48:37
【问题描述】:
是否可以在任何给定时间为 win32(c++ 中)上的当前进程获取线程句柄列表?
【问题讨论】:
标签: c++ windows multithreading
是否可以在任何给定时间为 win32(c++ 中)上的当前进程获取线程句柄列表?
【问题讨论】:
标签: c++ windows multithreading
您会发现this article 很有帮助。它提供了线程枚举的代码,其中包含使用tool help library 带来的细微差别。
为方便起见(摘自文章):
#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;
}
【讨论】:
NtQuerySystemInformation。
【讨论】: