您的位置:首页 > 其它

通过进程ID获取进程名

2016-03-06 20:38 274 查看
通过进程ID获取进程名
网上流传代码:

uses TLhelp32

function GetProcessNameById(const AID: Integer): String;
var
h:thandle;
f:boolean;
lppe:tprocessentry32;
begin
Result := '';
h := CreateToolhelp32Snapshot(TH32cs_SnapProcess, 0);
lppe.dwSize := sizeof(lppe);
f := Process32First(h, lppe);
while integer(f) <> 0 do
begin
if Integer(lppe.th32ProcessID) = AID then
begin
Result:= StrPas(lppe.szExeFile);
break;
end;
f := Process32Next(h, lppe);
end;

end.


自我改编代码

uses TLhelp32,PsAPI;

function GetProcessName(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
try
ENumProcessModules(Hand, @hMod, Sizeof(hMod), n);
if GetModuleFileNameEx(Hand, hMod, ModName, Sizeof(ModName)) > 0 then
Result := ExtractFileName(ModName);
except
end;

finally
CloseHandle(Hand);
end;
end;

end.




D7编译通过!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: