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;
    
  • 相关阅读:
    算法总结之 两个链表生成相加链表
    算法总结之 复制含有随机指针节点的链表
    算法总结之 将单向链表按某值划分成左边小、中间相等、右边大的形式
    在PHP5.3以上版本运行ecshop和ecmall出现的问题及解决方案
    windows下配置nginx+php环境
    ecmall程序结构图与数据库表分析
    ecmall数据字典
    Ecmall二次开发-增删改查操作
    PHP7:10件事情你需要知道的
    PHP命名空间规则解析及高级功能3
  • 原文地址:https://www.cnblogs.com/coder163/p/9104494.html
Copyright © 2011-2022 走看看