Nayana Adassuriya
Nayana Adassuriya

Reputation: 24756

Why does Implicit DLL Linking need relevant Lib file but Explicit Linking does not?

In a Windows environment,

When I tried to link a DLL to my program Explicitly (using LoadLibrary),

When I tried to link the DLL to my program Implicitly (using header file)

I've already gone trough this question. But I cannnot understand any worthy reason. Please, could someone help to explain this in simple terms. Thank you.

Upvotes: 8

Views: 2598

Answers (2)

David Heffernan
David Heffernan

Reputation: 613481

When linking implicitly, the function declaration specifies the name to be used in the program, and the prototype and calling convention. But more information is needed. Specifically:

  1. The fact that the function is implemented externally in a DLL.
  2. The name of that DLL.
  3. The exported name of the function. That is the name used to export the function from the DLL which may not be the same as that used when you import it.

Some language designers chose to provide this information using language extensions. For example Delphi took this route. Implicit linking is specified entirely in code with no .lib files. On the other hand the convention for C and C++ is to use .lib files to specify the missing information.

Upvotes: 2

snf
snf

Reputation: 3087

Why Implicitly linking need Lib file too.

The .libs have the import information of the dll, you can check the information using the dumpbin command that is included in Windows/Visual Studio SDK.

This is the link information of recv inside ws2_32.lib for example:

Version      : 0
Machine      : 14C (x86)
TimeDateStamp: 4907F6ED Wed Oct 29 01:38:53 2008
SizeOfData   : 00000014
DLL name     : WS2_32.dll
Symbol name  : _recv@16
Type         : code
Name type    : ordinal
Ordinal      : 16

You can check there is the ordinal and the name inside ws2_32.dll (check that now it says to import a DLL).

What information it need to retrieve from 'Lib' file that cannot get from DLL or Header file

In the header file, there is no information from where to extract the imports, so them are marked as imports (__imp__name) when compiled, and when it's linked against the .lib, it resolves the name:

  • If it's inside the .lib it just links against it.
  • But if there is information on external reference (DLL), it will construct the import inside the import table so it's loaded dinamically.

If there is something for question 2, How those information retrieve when explicit loading.

If for explicit loading you mean the LoadLibrary, you are telling it at runtime and not at link time. So the PE loader will search the DLL inside the PATH and will load it dynamically. Then you have other functions to get the exported functions addresses.

If you don't understand something just ask me, try playing with dumpbin and read about PE if you want to understand this better.

Upvotes: 3

Related Questions