Windows dll可以检索自己的文件名吗?

Blo*_*ard 29 windows delphi dll winapi

Windows exe文件可以访问调用它的命令字符串,包括其路径和文件名.例如.C:\MyApp\MyApp.exe --help.

但是对于通过调用的dll来说并非如此LoadLibrary.有没有人知道一个方法让dll找出它的路径和文件名是什么?

具体来说,我对Delphi解决方案感兴趣,但我怀疑任何语言的答案几乎都是一样的.

Mic*_*tum 37

我想你正在寻找GetModuleFileName.

http://www.swissdelphicenter.ch/torry/showcode.php?id=143:

{
  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;
var
  szFileName: array[0..MAX_PATH] of Char;
begin
  FillChar(szFileName, SizeOf(szFileName), #0);
  GetModuleFileName(hInstance, szFileName, MAX_PATH);
  Result := szFileName;
end;
Run Code Online (Sandbox Code Playgroud)

未经测试,自从我使用Delphi后已经有一段时间了:)

  • SysUtils有GetModuleName - 我认为已经是D7了. (6认同)
  • 从Delphi XE开始,`GetModuleName`在*System.pas*单元中定义 (2认同)