windows 延迟加载 DLL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1388388/
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
Delay Loading DLLs
提问by Zaid Amir
I am in desperate need of help, I need to manage an application dependency in Visual Studio. The application links to a DLL only on a specific version of windows, lets say Windows 7. and on other environments, the DLL should not be loaded. How will I be able to achieve that using DLL Delay Loading as this topic is completely new to me and there isn't any good references online for this particular matter.
我迫切需要帮助,我需要在 Visual Studio 中管理应用程序依赖项。应用程序仅在特定版本的 Windows 上链接到 DLL,比如 Windows 7。在其他环境中,不应加载 DLL。我将如何使用 DLL 延迟加载来实现这一点,因为这个主题对我来说是全新的,并且在网上没有关于这个特定问题的任何好的参考资料。
Regards
问候
回答by Goz
MSDN has a pretty good description here.
MSDN 有一个很好的描述here。
Basically what you are doing is settign the DLL in question to be in the delay load section. It will then not load that DLL until you make a call to a function that is in that DLL.
基本上你正在做的是将有问题的 DLL 设置在延迟加载部分。然后它不会加载该 DLL,直到您调用该 DLL 中的函数。
From the above link:
从上面的链接:
The Visual C++ linker now supports the delayed loading of DLLs. This relieves you of the need to use the Windows SDK functions LoadLibrary and GetProcAddress to implement DLL delayed loading.
Before Visual C++ 6.0, the only way to load a DLL at run time was by using LoadLibrary and GetProcAddress; the operating system would load the DLL when the executable or DLL using it was loaded.
Beginning with Visual C++ 6.0, when statically linking with a DLL, the linker provides options to delay load the DLL until the program calls a function in that DLL.
An application can delay load a DLL using the /DELAYLOAD (Delay Load Import)linker option with a helper function (default implementation provided by Visual C++). The helper function will load the DLL at run time by calling LoadLibrary and GetProcAddress for you.
You should consider delay loading a DLL if:
Your program may not call a function in the DLL.
A function in the DLL may not get called until late in your program's execution.
The delayed loading of a DLL can be specified during the build of either a .EXE or .DLL project. A .DLL project that delays the loading of one or more DLLs should not itself call a delay-loaded entry point in Dllmain.
Visual C++ 链接器现在支持 DLL 的延迟加载。这使您无需使用 Windows SDK 函数 LoadLibrary 和 GetProcAddress 来实现 DLL 延迟加载。
在 Visual C++ 6.0 之前,在运行时加载 DLL 的唯一方法是使用 LoadLibrary 和 GetProcAddress;操作系统将在加载使用它的可执行文件或 DLL 时加载 DLL。
从 Visual C++ 6.0 开始,当与 DLL 静态链接时,链接器提供了延迟加载 DLL 的选项,直到程序调用该 DLL 中的函数。
应用程序可以使用带有辅助函数(Visual C++ 提供的默认实现)的/DELAYLOAD(延迟加载导入)链接器选项延迟加载 DLL 。辅助函数将在运行时通过为您调用 LoadLibrary 和 GetProcAddress 加载 DLL。
如果出现以下情况,您应该考虑延迟加载 DLL:
您的程序可能不会调用 DLL 中的函数。
DLL 中的函数可能要到程序执行的后期才会被调用。
可以在构建 .EXE 或 .DLL 项目期间指定 DLL 的延迟加载。延迟加载一个或多个 DLL 的 .DLL 项目本身不应调用 Dllmain 中的延迟加载入口点。
回答by xtofl
Your project can specify that a dll it depends upon should but be loaded when needed, by specifying it in the Linker/Input/Delay Loaded DLLs field. This setting can be different for different build configurations.
通过在链接器/输入/延迟加载的 DLL 字段中指定它,您的项目可以指定它所依赖的 dll 应该但在需要时加载。对于不同的构建配置,此设置可能不同。
回答by 1800 INFORMATION
Instead of using delay loading, have you considered using dynamicloadingwith LoadLibrary
and GetProcAddress
? This is likely to be simpler to use.
而不是使用延迟加载的,你有没有考虑使用动态装载与LoadLibrary
和GetProcAddress
?这可能更易于使用。
typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);
// Call GetNativeSystemInfo if supported or GetSystemInfo otherwise.
PGNSI pGNSI;
SYSTEM_INFO si;
ZeroMemory(&si, sizeof(SYSTEM_INFO));
pGNSI = (PGNSI) GetProcAddress(
GetModuleHandle(TEXT("kernel32.dll")),
"GetNativeSystemInfo");
if(NULL != pGNSI)
pGNSI(&si);
else GetSystemInfo(&si);