本文整理自网络,侵删。
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 urldownloadtofile 实现文件下载
更多相关阅读请进入《Delphi》频道 >>