How to obtain list of thread handles from a win32 process?

前端 未结 2 685
慢半拍i
慢半拍i 2020-12-18 09:29

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

2条回答
  •  半阙折子戏
    2020-12-18 09:59

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

提交回复
热议问题