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

时间:2020-03-05 18:37:41  来源:igfitidea点击:

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

但是对于通过LoadLibrary调用的dll则不是这样。有谁知道dll找出其路径和文件名的方法吗?

具体来说,我对Delphi解决方案感兴趣,但我怀疑对于任何语言,答案都几乎相同。

解决方案

回答

我认为我们正在寻找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;

自从我与Delphi合作以来,已经有一段时间没有经过测试了:)