When one should implicitly or explicitly link to a DLL and what are common practices or pitfalls?
相关问题
- How to know full paths to DLL's from .csproj f
- the application was unable to start correctly 0xc0
- Inheritance impossible in Windows Runtime Componen
- how to call a C++ dll from C# windows application
- how to get running process information in java?
相关文章
- 如何让cmd.exe 执行 UNICODE 文本格式的批处理?
- 怎么把Windows开机按钮通过修改注册表指向我自己的程序
- vs2017wpf项目引用dll的路径不正确的问题
- Warning : HTML 1300 Navigation occured?
- Bundling the Windows Mono runtime with an applicat
- Windows 8.1 How to fix this obsolete code?
- Why windows 64 still makes use of user32.dll etc?
- CosmosDB emulator can't start since port is al
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.
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.
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.