zoukankan      html  css  js  c++  java
  • Delphi遍历进程-Win32API

    本博客的Delphi代码使用的版本均为DelphiXE10.x

    1.1 .枚举进程

    通过进程名称获取指定的进程ID,代码很详细,不再赘述

    unit Uuitls;
    
    interface
    
    uses
      TlHelp32, Winapi.Windows;
    
    function GetPidByProName(proname: string): DWORD;
    
    implementation
    
    function GetPidByProName(proname: string): DWORD;
    var
      hwd: THandle; //句柄
      RocessEntry: TProcessEntry32; //结构类型的变量
      found: Boolean; //返回一个布尔值(用来判断是否找到进程信息)
      processid: dword; //储存找到的进程ID
      Pname: string; //储存找到的进程名称end;
    begin
      Result := 0;
        //获得进程快照句柄
      hwd := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
      //给TProcessEntry32结构的第一个参数赋值(也可以理解为把这个结构的第一个参数初始化)
      RocessEntry.dwSize := sizeof(RocessEntry);
      //使用Process32First函数取得第一个进程的信息
      found := Process32First(hwd, RocessEntry);
      //如果Process32First函数执行成功也就是说找到进程列表里的第一个进程时开始循环
      while found = true do
      begin
      //取得第下一个进程信息
        found := Process32Next(hwd, RocessEntry);
        Pname := RocessEntry.szExeFile;
    
        if Pname = proname then
        begin
          //把找到的进程显示出来
    
          Result := RocessEntry.th32ProcessID;
          Break
        end;
    
      end;
    
    end;
    
    end.
    
    
    

    1.2 枚举进程将符合条件的进程PID存储容器

    此处遇到一个问题,原本想使用数组作为返回值,但是没有成功,只好利用泛型使用TList,如果有朋友能搞定不胜感激

    unit UBaseTools;
    
    interface
    
    uses
      Generics.Collections, psapi, Variants, Winapi.Windows, Winapi.Messages, TLHelp32, Vcl.StdCtrls, System.SysUtils, Vcl.Dialogs;
    
    
    
    {*------------------------------------------------------------------------------
      枚举游戏进程,获取所有的PID
    
      @param ProName
      @return
    -------------------------------------------------------------------------------}
    function ListPids(ProName: string): TList<Cardinal>;
    
    function StrToDword(Value: string): DWORD;
    
     // DwordToStr() : Converts a DWORD to a 4 byte string
    function DwordToStr(Value: dword): string;
    
    implementation
    
    function ListPids(ProName: string): TList<Cardinal>;
    var
      ContinueLoop: BOOL;       //是否继续循环
      FSnapshotHandle: THandle; //进程快照句柄
      FProcessEntry32: TProcessEntry32; //进程入口的结构体信息
      pids: string;
      pid: Integer;
      hProcess: THandle;
      ProcessFullPathName: string;
      buf: array[0..MAX_PATH] of Char;
      buf1: array[0..MAX_PATH] of Char;
    var
      List: TList<Cardinal>;  {定义一个泛型 TList 类, 这指定了要用于 string}
    begin
      List := TList<Cardinal>.Create;
      FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    //CreateToolhelp32Snapshot函数得到进程快照
      FProcessEntry32.dwSize := Sizeof(FProcessEntry32); //初始化
      ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
    //Process32First 得到一个系统快照里第一个进程的信息
    
      while ContinueLoop do
      begin
       //进程ID
        pid := FProcessEntry32.th32ProcessID;
    
        hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, FALSE, pid);
        if hProcess <> 0 then
        begin
          if StrPas(FProcessEntry32.szExeFile) = ProName then
          begin
            List.Add(FProcessEntry32.th32ProcessID);
          end;
    
        end;
    
        ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
      end;
      CloseHandle(FSnapshotHandle);
      Result := List;
    end;
    
  • 相关阅读:
    异常日志以及非异常日志记录方法
    oracle 监测数据库是否存在指定字段
    listview禁止双击一条之后选中复选框按钮的方法
    oracle 的rowid和rownum
    修改文件的名字的写法
    使用C#读取XML节点,修改XML节点
    BZOJ 1004: [HNOI2008]Cards
    P5022 旅行 (NOIP2018)
    P5021 赛道修建 (NOIP2018)
    P5020 货币系统 (NOIP2018)
  • 原文地址:https://www.cnblogs.com/coder163/p/9104494.html
Copyright © 2011-2022 走看看