【问题标题】:Enumerating threads in Windows在 Windows 中枚举线程
【发布时间】:2009-07-30 14:16:12
【问题描述】:

如果给定进程的 HANDLE(或进程 ID),如何枚举进程中的所有线程?我有兴趣这样做,所以我可以在每个线程上进一步做一个EnumThreadWindows

【问题讨论】:

    标签: windows multithreading winapi


    【解决方案1】:

    Enumerating threads in a process 在 MSDN 博客。

    代码 sn-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\n",
                 te.th32OwnerProcessID, te.th32ThreadID);
         }
       te.dwSize = sizeof(te);
       } while (Thread32Next(h, &te));
      }
      CloseHandle(h);
     }
     return 0;
    }
    

    【讨论】:

    • 最好至少列出所需的主要API函数,这样如果链接断开,这个答案就不会变得完全无用(MSDN有更改URL的习惯)
    • MSDN 博客链接断开。现在在devblogs.microsoft.com/oldnewthing/20060223-14/?p=32173;不幸的是,该帖子的格式现在很难看,这是一种耻辱。
    【解决方案2】:

    ToolHelp 库提供了一个 API,用于对进程进行快照并枚举它们的线程(以及其他属性)。 See MSDN for details.

    【讨论】:

      【解决方案3】:
      // 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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-01
        相关资源
        最近更新 更多