|
function
GetProcessID(Exename: string): DWORD;
var
hProcSnap: THandle;
pe32: TProcessEntry32;
begin
result :=
0;
hProcSnap := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS,
0);
if hProcSnap <>
INVALID_HANDLE_VALUE
then
begin
pe32.dwSize :=
SizeOf(ProcessEntry32);
if
Process32First(hProcSnap, pe32) = true
then
begin
while
Process32Next(hProcSnap, pe32) = true
do
begin
if pos(Exename,
pe32.szExeFile) <>
0
then
result := pe32.th32ProcessID;
end;
end;
CloseHandle(hProcSnap);
end;
end;
function GetProcessImageFileName(hProcess: tHANDLE;
lpImageFileName: LPTSTR;
nSize: DWORD): DWORD; stdcall;
external
'psapi.dll' name
'GetProcessImageFileName'
+{$IFDEF UNICODE}'W'{$ELSE}'A'{$ENDIF};
function GetProcessFilePath(pid:cardinal):string;
var
hp: THandle;
Buffer1: array[0..MAX_PATH] of Char;
Len: DWORD;
begin
Result := '';
if pid > 0
then
begin
hp := OpenProcess(PROCESS_QUERY_INFORMATION, False, pid);
if hp > 0
then
try
Len := GetProcessImageFileName(hp, buffer1, Length(buffer1));
if Len > 0
then
Result := Copy(buffer1, 1, Len)
else
Result := SysErrorMessage(GetLastError);
finally
CloseHandle(hp);
end;
end;
end;
function DevicePathToWin32Path(path:String):String;
var
c:char;
s:string;
i:integer;
begin
i:=posex('\', path, 2);
i:=posex('\', path, i+1);
result:=copy(path, i, length(path));
delete(path, i, length(path));
for c:='A'
to 'Z'
do
begin
setlength(s, 1000);
if querydosdevice(pchar(string(c) +':'), pchar(s),
1000) <> 0
then
begin
s := pchar(s);
if sametext(path, s)
then
begin
result:=c +':' +result;
exit;
end;
end;
end;
result:='';
end;
function GetProcessPathEx(ProcessName:
String):
String;
begin
result :=
DevicePathToWin32Path(GetProcessFilePath(GetProcessID(ProcessName)));
end; |