get the full path from a PID using delphi

后端 未结 1 962
粉色の甜心
粉色の甜心 2020-12-10 05:01

I need to get the full path from a PID.

I\'ve checked this question C++ Windows - How to get process path from its PID and I wrote the following code:



        
相关标签:
1条回答
  • 2020-12-10 05:23

    You need to use the GetModuleFileNameEx function. From MSDN:

    GetModuleFileName Function

    Retrieves the fully-qualified path for the file that contains the specified module. The module must have been loaded by the current process.

    To locate the file for a module that was loaded by another process, use the GetModuleFileNameEx function.

    Sample usage (uses PsAPI):

    function GetPathFromPID(const PID: cardinal): string;
    var
      hProcess: THandle;
      path: array[0..MAX_PATH - 1] of char;
    begin
      hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, false, PID);
      if hProcess <> 0 then
        try
          if GetModuleFileNameEx(hProcess, 0, path, MAX_PATH) = 0 then
            RaiseLastOSError;
          result := path;
        finally
          CloseHandle(hProcess)
        end
      else
        RaiseLastOSError;
    end;
    
    0 讨论(0)
提交回复
热议问题