简体   繁体   English

隐式与显式链接到DLL

[英]Implicit vs. Explicit linking to a DLL

当一个人应该隐式或明确地链接到DLL以及什么是常见的做法或陷阱?

It is fairly rare to explicitly link a DLL. 显式链接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. 您需要为导出的函数编写函数指针声明,并获取LoadLibrary + GetProcAddress + FreeLibrary代码。 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. 只有当您需要对插件样式DLL的运行时依赖性或者希望根据配置从一组DLL中进行选择时,才会这样做。 Or to deal with versioning, an API function that's only available on later versions of Windows for example. 或者处理版本控制,这是一个仅在Windows的更高版本中可用的API函数。 Explicit linking is the default for COM and .NET DLLs. 显式链接是COM和.NET DLL的默认链接。

More background info in this MSDN Library article . MSDN Library文章中的更多背景信息。

I'm assuming you refer to linking using a .lib vs loading a DLL dynamically using LoadLibrary() . 我假设您使用.lib引用链接使用LoadLibrary()动态加载DLL。

Loading a DLL statically by linking to its .lib is generally safer. 通过链接到.lib来静态加载DLL通常更安全。 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. 链接阶段检查编译时是否存在所有入口点,并且您没有机会加载没有您期望的函数的DLL。 It is also easier not to have to use GetProcAddress() . 不必使用GetProcAddress()也更容易。

So generally you should use dynamic loading only when it is absolutely required. 因此,通常只有在绝对需要时才应使用动态加载。

I agree with other who answered you already (Hans Passant and shoosh). 我同意已经回答你的其他人(Hans Passant和shoosh)。 I want add only two things: 我想只添加两件事:

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. 1)必须使用LoadLibraryGetProcAddress一个常见情况如下:您只想在新版本的Windows中使用一些新的API,但API在您的应用程序中并不重要。 So you test with LoadLibrary and GetProcAddress whether the function which you need exist, and use it in the case. 因此,您使用LoadLibraryGetProcAddress测试是否存在您需要的函数,并在案例中使用它。 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 . 2)您的问题中没有包含一个重要选项: DLL的延迟加载 In this case the operating system will load the DLL when one of its functions is called and not at the application start. 在这种情况下,操作系统将在调用其中一个函数时加载DLL,而不是在应用程序启动时加载DLL。 It allows to use import libraries ( .lib files) in some scenarios where explicitly linking should be used at the first look. 它允许在某些情况下使用导入库( .lib文件),在这些情况下应该在第一次使用时显式链接。 Moreover it improve the startup time of the applications and are wide used by Windows itself. 此外,它还改善了应用程序的启动时间,并且被Windows本身广泛使用。 So the way is also recommended. 所以也推荐这种方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM