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 unicode转换ansi

Delphi xe6调用android标准功能

Delphi禁止webbrowser弹出窗口或者脚本错误

Delphi 字符串转换ascii码10进制

Delphi 窗体置顶(总在最前面)

Delphi 将word嵌入Delphi

Delphi反调试【初级】检测法

Delphi 按esc快捷键退出程序的简单方法

Delphi 复制拷贝文件目录函数

Delphi access 从数据库里随机选择一行

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



打赏

取消

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

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

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

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

评论

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