delphi 通过进程名获得文件全路径的函数


本文整理自网络,侵删。

 uses

TlHelp32,psapi;

function GetPathFileofModule(ModuleName:String):String; //枚举进程文件所在路径
var
hProcSnap: THandle;
pProcess: THandle;
pe32: TProcessEntry32;
buf:array[0..MAX_PATH] of char;
hMod:HMODULE;
cbNeeded:DWORD;
begin
hProcSnap := CreateToolHelp32SnapShot(TH32CS_SNAPALL, 0);
if hProcSnap = INVALID_HANDLE_VALUE then Exit;
pe32.dwSize := SizeOf(ProcessEntry32);
if Process32First(hProcSnap, pe32) = True then
while Process32Next(hProcSnap, pe32) = True do
begin
if uppercase(pe32.szExeFile)=uppercase(ModuleName) then
begin
pProcess:=OpenProcess(PROCESS_QUERY_INFORMATION or
PROCESS_VM_READ,
FALSE,
pe32.th32ProcessID);
if pProcess<>0 then
begin
if EnumProcessModules( pProcess,@hMod,sizeof(hMod),cbNeeded) then
begin
ZeroMemory(@buf,MAX_PATH+1);
GetModuleFileNameEx(pProcess, hMod,buf,MAX_PATH+1);
Result:=strpas(buf);
end;
end;
end;
end;
CloseHandle(hProcSnap);
end;

调用方法:

ShowMessage(GetPathFileofModule('explorer.exe'));

相关阅读 >>

Delphi xe taskbar按钮用法

Delphi强制应用非一次性载入的注册表设置

Delphi图片上写水印文字函数

Delphi xe8 中tidtcpclient的writeln编码变化

Delphi执行cmd命令

Delphi版的base64转换函数(修改版)

Delphi goto 语句的使用

Delphi xe listbox 行高根据内容高度进行调速

Delphi如何把一个文本列表添加到tlistbox并避免重复项

Delphi-adoquery查询、插入、删除、修改

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...