Windows dll 可以检索自己的文件名吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can a Windows dll retrieve its own filename?
提问by Blorgbeard is out
A Windows exe file has access to the command string which invoked it, including its path and filename. eg. C:\MyApp\MyApp.exe --help
.
Windows exe 文件可以访问调用它的命令字符串,包括其路径和文件名。例如。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?
但是对于通过LoadLibrary
. 有谁知道 dll 找出它的路径和文件名的方法?
Specifically I'm interested in a Delphi solution, but I suspect that the answer would be pretty much the same for any language.
具体来说,我对 Delphi 解决方案感兴趣,但我怀疑对于任何语言,答案都几乎相同。
回答by Michael Stum
I think you're looking for GetModuleFileName.
我认为您正在寻找 GetModuleFileName。
http://www.swissdelphicenter.ch/torry/showcode.php?id=143:
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;
Untested though, been some time since I worked with Delphi :)
未经测试,自从我使用 Delphi 以来已经有一段时间了:)