Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

visual-c++

In a Windows environment,

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

  • First I need to define the function pointers according to each function signature inside the DLL.
  • Then get the function addresses using 'GetProcAddress' and assign them to those pointers.

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

  • First it need the relevant header file to get function signatures.
  • Then it needs the relevant Lib file that was generated with the DLL.

    My questions are

    1. Why does implicitly linking need a Lib file as well?
    2. What information does it need to retrieve from 'Lib' file that it cannot get from the DLL or Header file?
    3. If there is something for question 2, how is information retrieved when explicitly loading?

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.

like image 665
Nayana Adassuriya Avatar asked Jul 24 '13 04:07

Nayana Adassuriya


1 Answers

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.

like image 146
snf Avatar answered Sep 28 '22 18:09

snf