When do these load DLLs : Implicit Linking VS Explicit Linking
Asked Answered
B

1

8

I thought Implicit linking loads a DLL as soon as the application starts because it is also called "load-time dynamic linking". But I found some strange explanations below from the link here(https://msdn.microsoft.com/en-us/library/253b8k2c(VS.80).aspx).

Implicit Linking

Like the rest of a program's code, DLL code is mapped into the address space of the process when the process starts up and it is loaded into memory only when needed. As a result, the PRELOAD and LOADONCALL code attributes used by .def files to control loading in previous versions of Windows no longer have meaning.

Explicit Linking

An application that implicitly links to many DLLs can be slow to start because Windows loads all the DLLs when the application loads. To improve startup performance, an application can implicitly link to those DLLs needed immediately after loading and wait to explicitly link to the other DLLs when they are needed.

And another explanation for implicit linking from here(https://msdn.microsoft.com/en-us/library/151kt790.aspx).

Implicit Linking

The Visual C++ linker now supports the delayed loading of DLLs. This relieves you of the need to use the Windows SDK functions LoadLibrary and GetProcAddress to implement DLL delayed loading.

Before Visual C++ 6.0, the only way to load a DLL at run time was by using LoadLibrary and GetProcAddress; the operating system would load the DLL when the executable or DLL using it was loaded.

Beginning with Visual C++ 6.0, when statically linking with a DLL, the linker provides options to delay load the DLL until the program calls a function in that DLL.

An application can delay load a DLL using the /DELAYLOAD (Delay Load Import) linker option with a helper function (default implementation provided by Visual C++). The helper function will load the DLL at run time by calling LoadLibrary and GetProcAddress for you.

I'm really confused and don't know how to understand these.

1. Does implicit linking load a DLL on startup or only when a function in DLL is called?

2. It means both are similiar in the end because LoadLibrary() is called under the hood?

Bulahbulawayo answered 9/9, 2015 at 18:23 Comment(5)
In implicit linking, the DLL is loaded at app startup, but it might not be fully committed into memory until the DLL is called into for the first time. DLL function calls go directly to the DLL. Explicit linking is managed by the user's own code. Delay loading is a mix of the two. The linker creates a special table in the app's PE header containing information about delay-loaded DLL functions, and the runtime library has stub code that each delay-loaded function is initially pointed at...Glutamine
... When a delay-loaded function is called for the first time, the stub code looks in the delay-load PE table, explicitly loads the specified DLL function pointer, replaces the function's stub pointer with the loaded DLL pointer so subsequent calls will go directly to the DLL, then the stub finishes by calling the loaded function. The app never knows the difference between static-linked function calls and delay-loaded function calls (unless a runtime error occurs trying to load the DLL).Glutamine
@Remy Lebeau Thank you!Bulahbulawayo
@RemyLebeau - Why did you avoid answering? Its a very good explanation, and I think its upvote worthy. My apologies for probing you ...Aerodrome
@Bulahbulawayo - Also see LoadLibrary Reference Counting and the discussion of NTDLL.dll's LdrLoadDll.Aerodrome
I
3

@remy-lebeau provided a good explanation in his comment. I'll try to elaborate here as an answer.

The difference between implicit and explicit dll loading is explained here. In short:

  • In explicit loading, the application loads the dll by calling LoadLibrary explicitly.
  • In implicit loading, the application specifies the dll in compile time, and Windows loader loads it in run time.

Implicit loading has many advantages, but it slows the application loading time because all of the dlls are loaded during this stage.

To solve this issue, Microsoft supports Delayed Loaded Dlls, which is a type of implicit loading.

By using it you can enjoy all of the benefits of implicit loading, but the dll will be loaded only when your application will call one of its functions.

To your questions:

  1. In implicit loading, the dll will be loaded on application startup if you didn't specify it as delayed loaded. If you did, it will be loaded on the first usage.
  2. Yes. In all of the scenarios the dll is loaded and mapped to the application memory.
Immersed answered 30/8, 2019 at 11:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.