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 如何屏蔽alt+f4

Delphi 数据库获取所有用户名

Delphi7判断字符是否是数字

Delphi twebbrowser 屏蔽右键菜单

Delphi webbrowser载入自定义html内容并显示

Delphi monthcalendar1 获取选中日期

Delphi 限制form窗体为固定的大小

system单元一些有用的变量或者常数

Delphi 释放资源文件中的exe文件并运行

Delphi 调用cmd命令行并取得返回结果

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



打赏

取消

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

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

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

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

评论

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