【发布时间】:2010-09-05 08:20:27
【问题描述】:
从 exe 文件创建的 Windows 进程可以访问调用它的命令字符串,包括其文件的路径和文件名。例如。 C:\MyApp\MyApp.exe --help.
但对于通过LoadLibrary 调用的 dll,情况并非如此。有谁知道通过 dll 加载函数的方法来找出它的路径和文件名是什么?
我对 Delphi 解决方案特别感兴趣,但我怀疑任何语言的答案都差不多。
【问题讨论】:
从 exe 文件创建的 Windows 进程可以访问调用它的命令字符串,包括其文件的路径和文件名。例如。 C:\MyApp\MyApp.exe --help.
但对于通过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 以来已经有一段时间了 :)
【讨论】:
GetModuleName 在 System.pas 单元中定义
uses 子句中需要Windows 来定义MAX_PATH。