Delphi 获取系统进程列表和进程所在路径


本文整理自网络,侵删。

 
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls;





type
  TForm1 = class(TForm)
    Button1: TButton;
    ListView: TListView;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }

    // Enum Processes



  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses
  Types, ActiveX, ComObj, Registry, {$ifdef FPC} JwaPsApi {$else} PsAPI {$endif FPC}, Nb30;

// Enum Processes
function ProcessIDs: TIntegerDynArray;
var ProcessSize: DWORD;
begin
  SetLength(Result, 1024);
  repeat
    SetLength(Result, 2 * Length(Result));
    Win32Check(EnumProcesses(@Result[0], Length(Result) * SizeOf(DWORD), ProcessSize));
  until Integer(ProcessSize) < Length(Result) * SizeOf(DWORD);
  SetLength(Result, ProcessSize div SizeOf(DWORD));
end;

function ProcessName(ProcessID: Integer): string;
var
  Process: THandle;
  ProcessName: array [0..MAX_PATH] of Char;
begin
  Result := '';
  Process := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, ProcessID);
  if Process <> 0 then
  try
    if GetModuleBaseName(Process, 0, ProcessName, SizeOf(ProcessName)) <> 0 then
      Result := ProcessName;
  finally
    CloseHandle(Process);
  end;
end;

function ProcessFileName(ProcessID: Integer): string;
var
  Process: THandle;
  FileName: array [0..MAX_PATH] of Char;
begin
  Result := '';
  Process := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, ProcessID);
  if Process <> 0 then
  try
    if GetModuleFileNameEx(Process, 0, FileName, SizeOf(FileName)) <> 0 then
      Result := FileName;
  finally
    CloseHandle(Process);
  end;
end;



procedure TForm1.Button1Click(Sender: TObject);
var
  Processes: TIntegerDynArray;
  I: Integer;
  Item: TListItem;
  ProcessName1: string;
begin
  ListView.Items.BeginUpdate;
  try
    Processes := ProcessIDs;
    for I := 0 to Length(Processes) - 1 do
    begin
      ProcessName1 := ProcessName(Processes[I]);
      if ProcessName1 <> '' then
      begin
        Item := ListView.Items.Add;
        Item.Caption := IntToStr(Processes[I]);
        Item.SubItems.Text := ProcessName1;
        Item.SubItems.Append(ProcessFileName(Processes[I]));
      end;
    end;
  finally
    ListView.Items.EndUpdate;
  end;

end;

end.

相关阅读 >>

Delphi中利用钩子实现qq聊天窗口的修改

如何为Delphi程序添加事件和事件处理器

Delphi 监控系统文件操作

Delphi xe2 新增 system.zip 单元, 可用一句话压缩整个文件夹了

Delphi 合并字符串的函数

Delphi常用获取系统信息的函数单元

Delphi lastdelimiter:在字符串中查找选定的字符最后出现的位置

Delphi tnethttpclient https忽略证书验证

Delphi 带密匙的字符串加密解密函数(支持中文)

Delphi 删除只读文件

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...