c++ 遍历当前程序的线程

#include <iostream>
#include <Windows.h>
#include <Psapi.h>
#include <TlHelp32.h>


  HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, GetCurrentProcessId());

  if (hSnap != INVALID_HANDLE_VALUE)
  {
    THREADENTRY32 te;
    te.dwSize = sizeof(THREADENTRY32);
    if ( Thread32First(hSnap, &te) )
    {
      do
      {
        if (te.th32OwnerProcessID == GetCurrentProcessId())
        {
          printf("pid: %d, tid: %d, 优先级: %d\n", te.th32OwnerProcessID, te.th32ThreadID, te.tpBasePri);

          // 关闭线程 
          HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, false, te.th32ThreadID);
          TerminateThread(hThread, 1);
          CloseHandle(hThread);
        }
      } while (Thread32Next(hSnap, &te));
    }
  }

  CloseHandle(hSnap);
posted @ 2020-09-27 13:46  Ajanuw  阅读(601)  评论(0编辑  收藏  举报