原文:http://blog.csdn.net/iseekcode/article/details/4740168
原文的代码可能适用于delphi7,我修改了一下,使它可以在delphi xe2下运行,效果还不错。
代码:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.StdCtrls,Tlhelp32;
type
TForm1 = class(TForm)
button1: TButton;
TreeView1: TTreeView;
Edit1: TEdit;
procedure button1Click(Sender: TObject);
private
{ Private declarations }
public
var
ProcessList: Thandle;
pe: TPROCESSENTRY32;
node: TTreenode;
processnumber: integer;
{ Public declarations }
procedure ProcessEnum(Sender: TObject);
procedure ModuleEnum(processid: Dword);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.button1Click(Sender: TObject);
begin
ProcessEnum(self);
end;
procedure TForm1.ModuleEnum(processid: Dword);
var
ModuleList: Thandle;
pm: TMODULEENTRY32;
begin
ModuleList := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, processid);
pm.dwSize := sizeof(TMODULEENTRY32);
if module32first(ModuleList, pm)
then
begin
TreeView1.Items.addchild(node, pm.szexepath);
while module32next(ModuleList, pm) do
TreeView1.Items.addchild(node, pm.szexepath);
end;
closehandle(ModuleList);
end; // ModuleEnum
procedure TForm1.ProcessEnum(Sender: TObject);
begin // ProcessEnum
processnumber := 0;
TreeView1.Items.Clear;
ProcessList := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
pe.dwSize := sizeof(TPROCESSENTRY32);
if process32first(ProcessList, pe) then
begin
node := TreeView1.Items.Add(nil, pe.szexefile);
ModuleEnum(pe.th32ProcessID);
inc(processnumber);
while process32next(ProcessList, pe) do
begin
node := TreeView1.Items.Add(nil, pe.szexefile);
ModuleEnum(pe.th32ProcessID);
inc(processnumber);
end;
end;
edit1.text := '系统进程 : ' + inttostr(processnumber);
closehandle(ProcessList);
end;
end.