c++ - Windows : can I dynamically choose what DLL name to look for? (c++) 中的延迟加载 DLL

标签 c++ windows dll load delay

我有一个从 foo.dll 调用函数的库。

在我的 MSVS 设置中,我延迟加载 foo.dll 并在尝试调用它的函数之前手动检查它的存在(这样如果它在机器上不存在,我的库就不会崩溃)。

如果 DLL 存在性检查成功并且我调用了它的函数,则 DLL 将由 windows 延迟加载帮助程序自动加载,并且一切正常。

但是,在 50% 的用户机器上,foo.dll 被重命名为 bar.dll。即使我调用 LoadLibrary("path\bar.dll") 并且它成功了,我的库仍然会崩溃,因为延迟加载帮助程序在我调用其中一个函数时仍然尝试加载 foo.dll。

我使用十六进制编辑器查看我的库的内容,并在一个位置明确命名了“foo.dll”。如果我使用十六进制编辑器将该条目重命名为“bar.dll”,当 bar.dll 是用户机器上的 DLL 的名称时,我的库运行完美(并且在 foo.dll 是名称的情况下崩溃)。因此,似乎问题在于延迟加载帮助程序试图在我的库中加载显式命名的 DLL。

我如何告诉延迟加载帮助程序,有问题的 DLL 的名称与我编译的库中的显式文件名不匹配?

最佳答案

要告诉延迟加载帮助程序使用与链接的不同的 DLL,您可以使用 delay load failure hook .根据 MSDN:

Linker Support for Delay-Loaded DLLs: Error Handling and Notification

If your code needs to recover or provide an alternate library and/or routine on failure, a hook can be provided to the helper function that can supply or remedy the situation. The hook routine needs to return a suitable value, so that processing can continue (an HINSTANCE or FARPROC) or 0 to indicate that an exception should be thrown. It could also throw its own exception or longjmp out of the hook. There are notification hooks and failure hooks.



当延迟加载的 DLL 加载失败时,将使用 dliFailLoadLib 调用 Hook 。包含有关加载操作和错误的详细信息的通知。 Hook 可以通过返回有效的 HMODULE 从错误中恢复。供助手使用,或返回 0 以使加载操作失败。

If the notification is dliFailLoadLib, the hook function can return:

  • 0, if it cannot handle the failure.

  • An HMODULE, if the failure hook fixed the problem and loaded the library itself.



因此,如果错误报告失败的 DLL 是 foo.dll ,您的钩子(Hook)可以加载并返回 HMODULE对于 bar.dll ,然后助手将加载所有 foo.dll的延迟加载函数来自 bar.dll反而。

关于c++ - Windows : can I dynamically choose what DLL name to look for? (c++) 中的延迟加载 DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60879800/

相关文章:

c++ - 在 vector 中存储重复字符串时节省内存?

c++ - using 声明是否应该隐藏继承的虚函数?

c++ - 使用 GDI 或 GDI+ 的高级图形

windows - "' WinJS ' is not defined."和 "Windows' 未定义。”

c# - "Can' t 找到 PInvoke DLL 'dbnetlib.dll'。“智能设备应用程序中出现错误

c++ - 'HMODULE LoadLibraryA(LPCSTR )': cannot convert argument 1 from ' const _Elem *' to ' LPCSTR'

c++ - 返回对结构的引用

c++ - 在进程堆内存中查找模式

java - 检查默认java安装版本是否大于1.x的DOS脚本

c++ - 将我的 DLL 添加到 C++ 中的 Visual Studio 项目中