zoukankan      html  css  js  c++  java
  • c++ win32 遍历进程列表

    使用 CreateToolhelp32Snapshot

    #include <iostream>
    #include <Windows.h>
    #include <TlHelp32.h>
    #include <Psapi.h>
    
    int main() {
      HANDLE hSnap = CreateToolhelp32Snapshot(PROCESS_ALL_ACCESS, 0);
      if (hSnap != INVALID_HANDLE_VALUE)
      {
        PROCESSENTRY32 pe;
        pe.dwSize = sizeof(pe);
        if (Process32First(hSnap, &pe))
        {
          do {
            printf("PID: %X	%ls
    ", pe.th32ProcessID, pe.szExeFile);
          } while (Process32Next(hSnap, &pe));
        }
      }
      CloseHandle(hSnap);
      return 0;
    }
    

    使用 EnumProcesses

    #include <iostream>
    #include <Windows.h>
    #include <TlHelp32.h>
    #include <Psapi.h>
    
    int main() {
      DWORD aProcesses[1024], cbNeeded, cProcesses;
      size_t i;
      if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
      {
        return 1;
      }
      cProcesses = cbNeeded / sizeof(DWORD);
      for (i = 0; i < cProcesses; i++)
      {
        if (aProcesses[i] != 0)
        {
          DWORD pid = aProcesses[i];
          TCHAR szProcessName[MAX_PATH] = TEXT("???");
          HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
          if (NULL != hProcess)
          {
            HMODULE hMod;
            DWORD cbNeeded;
            if (EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded))
            {
              GetModuleBaseName(hProcess, hMod, szProcessName, sizeof(szProcessName) / sizeof(TCHAR));
            }
          }
          printf("PID: %X	%ls
    ", pid, szProcessName);
          CloseHandle(hProcess);
        }
      }
      return 0;
    }
    
  • 相关阅读:
    115. Distinct Subsequences
    91. Decode Ways
    72. Edit Distance
    python 输出 a+b
    求次小生成树
    判断最小生成树是否唯一
    二分法求最优值
    黑科技
    线段树+ 区间更新
    2018ICPC青岛赛区J题
  • 原文地址:https://www.cnblogs.com/ajanuw/p/14439105.html
Copyright © 2011-2022 走看看