uses PsAPI; // GetProcessPath 根据进程ID得到进程全路径名称 function GetProcessPath(PID : Integer): String; var hProcess: THandle; hMod : hModule; cbNeeded: DWORD; szProcessName: array[0..1024] of Char; begin hProcess := OpenProcess(PROCESS_QUERY_INFORMATION OR PROCESS_VM_READ, FALSE, PID ); szProcessName := 'unknown'; if (hProcess<>0) then begin if(EnumProcessModules(hProcess,@hMod,sizeof(hMod),cbNeeded)) then begin GetModuleFileNameEx(hProcess,hMod,szProcessName,sizeof(szProcessName)); Result := StrPas(szProcessName); end; end; CloseHandle(hProcess); end;
忘记了,还要先获得当前窗口的进程ID: var wnd:HWND; ThreadId: DWORD; FileName:string; begin GetWindowThreadProcessId(wnd, ThreadId); FileName:=GetProcessPath(ThreadId); end;
PsAPI;
// GetProcessPath 根据进程ID得到进程全路径名称
function GetProcessPath(PID : Integer): String;
var hProcess: THandle;
hMod : hModule;
cbNeeded: DWORD;
szProcessName: array[0..1024] of Char;
begin
hProcess := OpenProcess(PROCESS_QUERY_INFORMATION OR PROCESS_VM_READ,
FALSE, PID ); szProcessName := 'unknown';
if (hProcess<>0) then begin
if(EnumProcessModules(hProcess,@hMod,sizeof(hMod),cbNeeded)) then begin
GetModuleFileNameEx(hProcess,hMod,szProcessName,sizeof(szProcessName));
Result := StrPas(szProcessName);
end;
end;
CloseHandle(hProcess);
end;
var
wnd:HWND;
ThreadId: DWORD;
FileName:string;
begin
GetWindowThreadProcessId(wnd, ThreadId);
FileName:=GetProcessPath(ThreadId);
end;