【问题标题】:Can a Windows dll retrieve its own filename?Windows dll 可以检索自己的文件名吗?
【发布时间】:2010-09-05 08:20:27
【问题描述】:

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

但对于通过LoadLibrary 调用的 dll,情况并非如此。有谁知道通过 dll 加载函数的方法来找出它的路径和文件名是什么?

我对 Delphi 解决方案特别感兴趣,但我怀疑任何语言的答案都差不多。

【问题讨论】:

    标签: windows delphi winapi dll


    【解决方案1】:

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

    【讨论】:

    • 在 Delphi XE 中,GetModuleNameSystem.pas 单元中定义
    • 在 Delphi 6 中,uses 子句中需要Windows 来定义MAX_PATH
    猜你喜欢
    • 2012-02-06
    • 2010-12-24
    • 2021-03-20
    • 2019-03-12
    • 1970-01-01
    • 2016-03-30
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    相关资源
    最近更新 更多