'get the full path from a PID using delphi

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:

 function GetFullPathFromPID(PID: DWORD): string;
 var
    hProcess: THandle;
    ModName : Array[0..MAX_PATH + 1] of Char;
 begin
   Result:='';
    hProcess := OpenProcess(PROCESS_ALL_ACCESS,False, PID);
    try
      if hProcess <> 0 then
       if GetModuleFileName(hProcess, ModName, Sizeof(ModName))<>0 then
         Result:=ModName
        else
         ShowMessage(SysErrorMessage(GetLastError));
    finally
     CloseHandle(hProcess);
    end;
 end;

but always return this message:

the specified module could not be found

How can I get the full path from a PID?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source