(注意uses TLHelp32)
uses tlhelp32;
--------------
var
lppe:tprocessentry32;
sshandle:thandle;
found:boolean;
begin
sshandle:=createtoolhelp32snapshot(th32cs_snapail,0);
found:=process32first(sshandle,lppe);
while found do
begin
//进行你的处理其中lppe.szExefile就是程序名。
found:=proccess32next(sshandle,lppe);
end;
end;
//线程用Thread32First,Thread32Next
//模块用Module32First,Module32Next
//堆用Heap32First,Heap32Next
//可执行文件用process32first
/////////////////////////////////////////////////////
uses ... TLHelp32, ...
type
TForm1 = class(TForm)
...
end;
var
Form1: TForm1;
l : Tlist; ////返回的东东在"L"这个TList中。
type
TProcessInfo = Record
ExeFile : String;
ProcessID : DWORD;
end;
pProcessInfo = ^TProcessInfo;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
var p : pProcessInfo;
i : integer;
ContinueLoop:BOOL;
var
FSnapshotHandle:THandle;
FProcessEntry32:TProcessEntry32;
begin
l := TList.Create;
l.Clear;
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32);
while integer(ContinueLoop)<>0 do
begin
New(p);
p.ExeFile := FProcessEntry32.szExeFile;
p.ProcessID := FProcessEntry32.th32ProcessID;
l.Add(p);
ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
var p : pProcessInfo;
i : integer;
begin
With l do
for i := Count - 1 DownTo 0 do
begin p := items[i]; Dispose(p); Delete(i); end;
end;
...
end.
---------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
Const
TH32CS_SNAPMODULE = $00000008; // 模块列表快照
Type
{-- 枚举中 返回 模块信息结构 --}
TMODULEENTRY32 = record
dwSize: DWORD; // 本结构尺寸
th32ModuleID: DWORD; // This module
th32ProcessID: DWORD; // owning process
GlblcntUsage: DWORD; // Global usage count on the module
ProccntUsage: DWORD; // Module usage count in th32ProcessID's context
modBaseAddr: PBYTE; // Base address of module in th32ProcessID's context
modBaseSize: DWORD; // Size in bytes of module starting at modBaseAddr
hModule: HMODULE; // The hModule of this module in th32ProcessID's context
szModule: array[0..255] of Char;
szExePath: array[0..260 - 1] of Char;// 模块完整路径
end;
TForm1 = class(TForm)
Memo1: TMemo;
Panel1: TPanel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
function CreateToolhelp32Snapshot(dwFlags, th32ProcessID: DWORD) : THandle ; stdcall; external 'kernel32.dll' name 'CreateToolhelp32Snapshot';
function Module32First(hSnapshot: THandle; var lpme: TModuleEntry32): BOOL ; stdcall; external 'kernel32.dll' name 'Module32First';
function Module32Next(hSnapshot: THandle; var lpme: TModuleEntry32): BOOL ; stdcall; external 'kernel32.dll' name 'Module32Next';
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
ModuleStruct : TMODULEENTRY32; // 模块信息结构
ModuleHandle : LongWord; // 快照句柄
FoundModule : Boolean ; // 是否找到模块
FullFileName : string ; // 完整路径
WinProcessId : LongWord; // 进程id
Begin
{---模块列表快照---}
WinProcessId := 4564;
ModuleHandle := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, WinProcessId);
ModuleStruct.dwSize := sizeof(ModuleStruct);
{----第1个模块----}
FoundModule := Module32First(ModuleHandle, ModuleStruct);
while (FoundModule) do
begin
FullFileName:=ModuleStruct.szExePath;
//FULLFILENAME 就是你要的文件名 你可以自己处理。
memo1.Lines.Add(FullFileName);
{----下一个模块----}
FoundModule := Module32Next(ModuleHandle, ModuleStruct);
end;
{----释放句柄----}
CloseHandle(ModuleHandle);
end;
end.