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

问题描述 投票:0回答:1

从 exe 文件创建的 Windows 进程可以访问调用它的命令字符串,包括其文件的路径和文件名。例如。

C:\MyApp\MyApp.exe --help

但是对于通过

LoadLibrary
调用的 DLL 来说情况并非如此。有谁知道通过 DLL 加载的函数可以找出其路径和文件名的方法吗?

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

windows delphi winapi dll path
1个回答
40
投票

我认为您正在寻找 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 以来已经有一段时间了:)

© www.soinside.com 2019 - 2024. All rights reserved.