Search code examples
windowsdelphidelphi-2007

How to use GetProcessImageFileName?


I'm trying to use GetProcessImageFileName but I'm getting the following error:

Undeclared identifier: 'GetProcessImageFileName'

I've tried adding PsAPI and TlHelp32 to the uses clause but it has not solved the problem.


Solution

  • I'm not sure if this function is supplied by the RTL anywhere, but you can always just import it :

    function GetProcessImageFileName(hProcess: THandle; 
                                     lpImageFileName: LPTSTR;
                                     nSize: DWORD): DWORD; stdcall;
                                     external 'PSAPI.dll' 
                                     name 'GetProcessImageFileNameA';
    

    The ANSI version (for Delphi 2007) is imported above. Alternatively, if supporting higher versions of delphi (with unicode strings) you would conditionally import GetProcessImageFileNameW.