windows 隐式与显式链接到 DLL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4794032/
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
Implicit vs. Explicit linking to a DLL
提问by Shinnok
When one should implicitly or explicitly link to a DLL and what are common practices or pitfalls?
何时应该隐式或显式链接到 DLL,常见做法或陷阱是什么?
回答by Hans Passant
It is fairly rare to explicitly link a DLL. Mostly because it is painful and error prone. You need to write a function pointer declaration for the exported function and get the LoadLibrary + GetProcAddress + FreeLibrary code right. You'd do so only if you need a runtime dependency on a plug-in style DLL or want to select from a set of DLLs based on configuration. Or to deal with versioning, an API function that's only available on later versions of Windows for example. Explicit linking is the default for COM and .NET DLLs.
显式链接 DLL 是相当罕见的。主要是因为它很痛苦并且容易出错。您需要为导出的函数编写函数指针声明并正确获取LoadLibrary + GetProcAddress + FreeLibrary 代码。仅当您需要对插件样式 DLL 的运行时依赖项或想要根据配置从一组 DLL 中进行选择时,才需要这样做。或者为了处理版本控制,例如一个仅在更高版本的 Windows 上可用的 API 函数。显式链接是 COM 和 .NET DLL 的默认设置。
More background info in this MSDN Library article.
此MSDN 库文章中的更多背景信息。
回答by shoosh
I'm assuming you refer to linking using a .lib
vs loading a DLL dynamically using LoadLibrary()
.
我假设您指的是使用.lib
.vs 动态加载 DLL 进行链接LoadLibrary()
。
Loading a DLL statically by linking to its .lib
is generally safer. The linking stage checks that all the entry points exist in compile time and there is no chance you'll load a DLL that doesn't have the function you're expecting. It is also easier not to have to use GetProcAddress()
.
通过链接到 DLL 静态加载 DLL.lib
通常更安全。链接阶段检查所有入口点在编译时是否存在,并且您不可能加载不具有您期望的功能的 DLL。不必使用也更容易GetProcAddress()
。
So generally you should use dynamic loading only when it is absolutely required.
所以通常你应该只在绝对需要时才使用动态加载。
回答by Oleg
I agree with other who answered you already (Hans Passant and shoosh). I want add only two things:
我同意其他已经回答你的人(Hans Passant 和 shoosh)。我只想添加两件事:
1) One common scenario when you have to use LoadLibrary
and GetProcAddress
is the following: you want use some new API existing in new versions of Windows only, but the API are not critical in your application. So you test with LoadLibrary
and GetProcAddress
whether the function which you need exist, and use it in the case. What your program do if the functions not exist depend total from your implementation.
1)当你必须使用一个常见的情况LoadLibrary
和GetProcAddress
是:你想利用现有的仅适用于Windows的新版本一些新的API,但是API不是你的应用程序的关键。所以,你用测试LoadLibrary
和GetProcAddress
是否您需要的存在,并在情况下使用该功能。如果函数不存在,您的程序会做什么取决于您的实现。
2) There are one important options which you not included in your question: delayed loading of DLLs. In this case the operating system will load the DLL when one of its functions is called and not at the application start. It allows to use import libraries (.lib
files) in some scenarios where explicitly linking should be used at the first look. Moreover it improve the startup time of the applications and are wide used by Windows itself. So the way is also recommended.
2)您的问题中没有包含一个重要的选项:延迟加载 DLLs。在这种情况下,操作系统将在调用其函数之一时而不是在应用程序启动时加载 DLL。它允许.lib
在某些第一眼就应该使用显式链接的场景中使用导入库(文件)。此外,它提高了应用程序的启动时间,并被 Windows 本身广泛使用。所以也是推荐的方式。