M 作 者: z_h_q_2000(★龙飞虎★) 2001-06-01 20:06:01 :0 :0
[回复] [打包] [转贴]
uses tlhelp32;
procedure TForm1.Button1Click(Sender: TObject);
var
pe:PROCESSENTRY32;
me:MODULEENTRY32;
hp,hm:Thandle;
b,b1:boolean;
begin
hp:=CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS,0);
pe.dwSize:=SizeOf(pe);
b:=Process32First(hp,pe);
while b do
begin
hm:=CreateToolHelp32SnapShot(TH32CS_SNAPModule,pe.th32ProcessID);
me.dwSize:=sizeof(ModuleEntry32);
if Module32First(hm,me) then
begin
b1:=Module32First(hm,me);
while b1 do
begin
if me.th32ModuleID=pe.th32ModuleID then
listbox1.Items.Add(me.szExePath);
b1:=Module32Next(hm,me);
end;
end;
b:=Process32Next(hp,pe);
end;
end;
而在NT系统中由于不支持toolhelp api,故
#include "psapi.hpp"
假使process id是 pid 的话:
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,FALSE,pid);
if( NULL != hProcess )
{
HMODULE hMod;
DWORD cbNeeded;
if( EnumProcessModules( hProcess, &hMod, sizeof( hMod ), &cbNeeded ) )
{
char buf[MAX_PATH+1]="";
if(GetModuleBaseName( hProcess, hMod,buf,sizeof buf)>0)
//
// buf放的就是名字
//
}
CloseHandle(hProcess);
}
(注意该
程序运行需要psapi.dll,所以要copy到别的机器上运行时需连同该文件一起拷贝。当然如果装了delphi或者c++ builder就不需要了)