Take the 2-minute tour ×

A windows exe file has access to the command string which invoked it, including its path and filename. eg. C:\MyApp\MyApp.exe --help.

But this is not so for a dll invoked via LoadLibrary. Does anyone know of a way for a dll to find out what its path and filename is?

Specifically I'm interested in a Delphi solution, but I suspect that the answer would be pretty much the same for any language.

share|improve this question
add comment

1 Answer

up vote 25 down vote accepted

I think you're looking for GetModuleFileName.


  If you are working on a DLL and are interested in the filename of the
  DLL rather than the filename of the application, then you can use this function:

function GetModuleName: string;
  szFileName: array[0..MAX_PATH] of Char;
  FillChar(szFileName, SizeOf(szFileName), #0);
  GetModuleFileName(hInstance, szFileName, MAX_PATH);
  Result := szFileName;

Untested though, been some time since I worked with Delphi :)

share|improve this answer
SysUtils has GetModuleName - already since D7, I think. –  TOndrej Jun 22 '09 at 5:38
As of Delphi XE, GetModuleName is defined in the System.pas unit –  menjaraz Feb 7 '12 at 11:42
add comment

Your Answer


