隐式与显式链接到 DLL
何时应隐式或显式链接到 DLL?常见做法或陷阱是什么?
When one should implicitly or explicitly link to a DLL and what are common practices or pitfalls?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
显式链接 DLL 的情况相当罕见。主要是因为它很痛苦并且容易出错。您需要为导出的函数编写函数指针声明,并正确获取 LoadLibrary + GetProcAddress + FreeLibrary 代码。仅当您需要插件样式 DLL 的运行时依赖性或想要根据配置从一组 DLL 中进行选择时,才需要这样做。或者处理版本控制,例如仅在更高版本的 Windows 上可用的 API 函数。显式链接是 COM 和 .NET DLL 的默认设置。
更多背景信息请参阅这篇MSDN Library 文章。
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.
More background info in this MSDN Library article.
我假设您指的是使用
.lib
进行链接,而不是使用LoadLibrary()
动态加载 DLL。通过链接到 DLL 的
.lib
静态加载 DLL 通常更安全。链接阶段检查编译时是否存在所有入口点,并且您不可能加载不具有所需函数的 DLL。不必使用GetProcAddress()
也更容易。因此,通常只有在绝对需要时才应该使用动态加载。
I'm assuming you refer to linking using a
.lib
vs loading a DLL dynamically usingLoadLibrary()
.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 useGetProcAddress()
.So generally you should use dynamic loading only when it is absolutely required.
我同意其他人已经回答过你的观点(Hans Passant 和 shoosh)。我只想添加两件事:
1)当您必须使用
LoadLibrary
和GetProcAddress
时,一种常见情况如下:您希望使用新版本 Windows 中现有的一些新 API只是,但 API 在您的应用程序中并不重要。因此,您使用LoadLibrary
和GetProcAddress
测试您需要的函数是否存在,并在情况下使用它。如果函数不存在,您的程序会做什么取决于您的实现。2)有一个重要选项没有包含在您的问题中:延迟加载 DLL。在这种情况下,操作系统将在调用 DLL 的函数之一时加载 DLL,而不是在应用程序启动时加载。它允许在某些第一次应该使用显式链接的场景中使用导入库(
.lib
文件)。此外,它还改善了应用程序的启动时间,并被 Windows 本身广泛使用。所以这个方式也是推荐的。I agree with other who answered you already (Hans Passant and shoosh). I want add only two things:
1) One common scenario when you have to use
LoadLibrary
andGetProcAddress
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 withLoadLibrary
andGetProcAddress
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.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.