如何从win32进程获取线程句柄的列表? [英] How to obtain list of thread handles from a win32 process?

查看:850
本文介绍了如何从win32进程获取线程句柄的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在win32(c ++)中的任何给定时间获取线程句柄的列表?

解决方案

您会发现本文有帮助。它给出了线程枚举的代码,以及使用工具帮助库



为了方便(从文章中提取):



<$> p $ p> #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\\\

te.th32OwnerProcessID,te.th32ThreadID);
}
te.dwSize = sizeof(te);
} while(Thread32Next(h,& te));
}
CloseHandle(h);
}
return 0;
}


Is it possible to get a list of thread handles at any given time for the current process on win32 (in c++)?

解决方案

You will find this article helpful. It gives the code for thread enumeration with the small nuances that come with using the tool help library.

For convenience (lifted from the article):

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

这篇关于如何从win32进程获取线程句柄的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆