在 Windows 中枚举线程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1206878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enumerating threads in Windows
提问by bdonlan
How can I enumerate all of the threads in a process given a HANDLE to the process (or a process ID)? I'm interested in doing this so I can further do an EnumThreadWindowson each thread.
如何枚举进程中的所有线程,给进程(或进程 ID)一个 HANDLE?我有兴趣这样做,所以我可以在每个线程上进一步执行EnumThreadWindows。
回答by nik
Enumerating threads in a processat MSDN Blogs.
Code snippet from there:
那里的代码片段:
#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;
}
回答by Steve Gilham
The ToolHelp library gives an API for snapshotting processes and enumerating their threads (amongst other properties). See MSDN for details.
ToolHelp 库提供了用于对进程进行快照并枚举其线程(以及其他属性)的 API。 有关详细信息,请参阅 MSDN。
回答by khalfan
// returns the tid of threads in a given process pid. it is recursive and called from a while loop.
// return 0 when there are no more threads.
DWORD EnumerateThreads(DWORD pid)
{// needs working for a simpler loop
char szText[MAX_PATH];
static BOOL bStarted;
static HANDLE hSnapPro, hSnapThread;
static LPPROCESSENTRY32 ppe32;
static PTHREADENTRY32 pte32;
if (!bStarted)
{
if (!bStarted)
{
bStarted++;
pte32 = new THREADENTRY32;
pte32->dwSize = sizeof(THREADENTRY32);
hSnapThread = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (!hSnapThread)
{
FormatErrorMessage("GetLastError -> hSnapThread = CreateToolhelp32Snapshot\n", GetLastError());
delete pte32;
bStarted = 0;
return 0;
}
if (Thread32First(hSnapThread, pte32))
{
do
{
if (pid == pte32->th32OwnerProcessID)
{
wsprintf(szText, "__yes Thread32First pid: 0x%X - tid: 0x%X\n", pid, pte32->th32ThreadID);
OutputDebugString(szText);
return pte32->th32ThreadID;
}
}
while (Thread32Next(hSnapThread, pte32));
}
else
FormatErrorMessage("GetLastError ->Thread32First\n", GetLastError());
}
}
if (Thread32Next(hSnapThread, pte32))
{
do
{
if (pid == pte32->th32OwnerProcessID)
{
wsprintf(szText, "__yes Thread32First pid: 0x%X - tid: 0x%X\n", pid, pte32->th32ThreadID);
OutputDebugString(szText);
return pte32->th32ThreadID;
}
}
while (Thread32Next(hSnapThread, pte32));
}
else
FormatErrorMessage("GetLastError ->Thread32First\n", GetLastError());
CloseHandle(hSnapThread);
bStarted = 0;
delete pte32;
OutputDebugString("__finished EnumerateThreads\n");
return 0;
}