8

When one should implicitly or explicitly link to a DLL and what are common practices or pitfalls?

Jonas
  • 121,568
  • 97
  • 310
  • 388
Shinnok
  • 6,279
  • 6
  • 31
  • 44
  • 1
    what do you mean by explicit vs implicit? you mean using LoadLibrary or static linking? – tenfour Jan 25 '11 at 13:35
  • 1
    @tenfour implicit is when you use a .h and need to link against a .lib and explicit when you use LoadLibrary and GetProcAddress respectively. Static linking is another chewing gum. – Shinnok Jan 25 '11 at 14:01
  • 1
    Sometimes you use explicit when you don't have a .lib file to hand. – David Heffernan Jan 25 '11 at 14:05
  • I know that the MSDN says "Implicit linking is sometimes referred to as static load" but saying static linking of a DLL is extremely confusing. – Sam Hobbs Jun 24 '16 at 18:05

3 Answers3

10

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
5

I'm assuming you refer to linking using a .lib vs loading a DLL dynamically using 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().

So generally you should use dynamic loading only when it is absolutely required.

shoosh
  • 76,898
  • 55
  • 205
  • 325
5

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 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.

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.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Delay loading is implemented by the Visual Studio linker, not the OS. It inserts the appropriate `LoadLibrary` and `GetProcAddress` calls for you. The OS isn't aware who put in these calls. That's why it works back to Window 95, even though the feature was introduced in 98. – MSalters Jan 28 '11 at 15:30
  • @MSalters: You are wrong. Program Executable (PE) used delay load has has additional `IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT` section. – Oleg Jan 28 '11 at 15:37