微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐