delphi获取系统进程和路径


本文整理自网络,侵删。

 unit Unit1;
interface
uses
Windows,Tlhelp32, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, psapi;
type
TForm1 = class(TForm)
ListView1: TListView;
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation

{$R *.dfm}
function GetProcessPath(ProcessID: DWORD): string;
var
Hand: THandle;
ModName: Array[0..Max_Path-1] of Char;
hMod: HModule;
n: DWORD;
begin
Result:='';
Hand:=OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,
False,
ProcessID);
if Hand>0 then
try
ENumProcessModules(Hand,@hMod,Sizeof(hMod),n);
if GetModuleFileNameEx(Hand,hMod,ModName,Sizeof(ModName))>0 then
// Result:=ExtractFilePath(ModName);//得到路径
Result:=ModName; //得到路径和文见名
except end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
found:boolean; //定义枚举进程所需变量
NewItem: TListItem;
FSnapshotHandle:tHANDLE;
lppe:TProcessEntry32;
begin

with listview1 do
begin
Columns.Add;
Columns.Add;
Columns.Add;
ViewStyle:=vsreport;
GridLines:=true;
columns.items[0].caption:='映像名称';

columns.items[1].caption:='进程ID';
columns.items[2].caption:='映像路径';
Columns.Items[0].Width:=100;

Columns.Items[1].Width:=50; //初始化listview
Columns.Items[2].Width:=300;
end;


ListView1.Items.BeginUpdate;
ListView1.Items.Clear;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); //CreateToolhelp32Snapshot函数得到进程快照

lppe.dwSize := Sizeof(lppe); //初始化
found := Process32First(FSnapshotHandle, lppe); //Process32First 得到一个系统快照里第一个进程的信

while found do
begin
NewItem := ListView1.Items.Add; //在ListView1显示
NewItem.ImageIndex := -1;
NewItem.Caption := ExtractFileName(lppe.szExeFile);//进程名称

NewItem.subItems.Add(IntToStr(lppe.th32ProcessID));//进程ID
NewItem.subItems.Add(GetProcessPath(lppe.th32ProcessID));
found := Process32Next(FSnapshotHandle, lppe);
end;
CloseHandle(FSnapshotHandle);
ListView1.Items.EndUpdate;
self.Label1.Caption:='当前系统共有'+''+inttostr(listview1.Items.count)+''+'个进程 ' ;
end;
end.

相关阅读 >>

Delphi 安卓程序如何读取外部配置文件

Delphi提取任意长度随机数

Delphi android 程序名称在哪里设置?

Delphi xe2读取内存偏移数据代码

Delphi urldownloadtofile 实现文件下载

Delphi 读取image组件转换base64编码

Delphi 安装fastreport4.8.xx

Delphi 病毒

vclzip控件的使用

Delphi 控制iis,检测、增加、删除虚拟目录

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



打赏

取消

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

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

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

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

评论

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