Advertisement
jpfassis

GetProcessFileNameFromPID

Sep 20th, 2019
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 0.93 KB | None | 0 0
  1. function GetProcessFileNameFromPID(
  2.   dwProcessID: DWORD): WideString;
  3. var
  4.   Handle: THandle;
  5. begin
  6.   Result := EmptyStr;
  7.   Handle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False,
  8.     dwProcessID);
  9.   try
  10.     SetLength(Result, MAX_PATH);
  11.     if Handle <> 0 then begin
  12.       if GetModuleFileNameEx(Handle, 0, {$IFDEF UNICODE}PWideChar{$ELSE}PAnsiChar{$ENDIF}(Result), MAX_PATH) >= 0 then
  13.         SetLength(Result, StrLen( {$IFDEF UNICODE}PWideChar{$ELSE}PAnsiChar{$ENDIF}(Result) ))
  14.       else
  15.         Result := EmptyStr;
  16.     end else begin// if Handle <> 0 then begin
  17.        if GetModuleFileNameEx(Handle, 0, {$IFDEF UNICODE}PWideChar{$ELSE}PAnsiChar{$ENDIF}(Result), MAX_PATH) >= 0 then
  18.         SetLength(Result, StrLen( {$IFDEF UNICODE}PWideChar{$ELSE}PAnsiChar{$ENDIF}(Result) ))
  19.        else
  20.         Result := EmptyStr;
  21.     end;// if Handle <> 0 then begin
  22.   finally
  23.     CloseHandle(Handle);
  24.   end;// try
  25. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement