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;
    
  • 相关阅读:
    Objective-C中不同方式实现锁(二)-11-多线程
    共享资源加锁的操作方法-10-多线程
    ios 下锁使用- 09-多线程
    iOS开发-线程安全-09-多线程
    线程同步-iOS多线程编程指南(四)-08-多线程
    《GCD 实现同步锁》-07-多线程
    死锁-06-多线程
    生产者消费者问题-05-多线程
    递归锁+条件锁+互斥锁-04-多线程
    Android开发技术周报 Issue#62
  • 原文地址:https://www.cnblogs.com/coder163/p/9104494.html
Copyright © 2011-2022 走看看