8

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.

Community
  • 1
  • 1
Nayana Adassuriya
  • 23,596
  • 30
  • 104
  • 147
  • It's not really about retrieving data that *couldn't* come from the DLL. It's more an optimization -- formatting the data to make life as easy as possible for the linker, to minimize special-casing between linking to static vs. dynamic libraries. – Jerry Coffin Jul 24 '13 at 04:39
  • @Jerry: Which kind of information it pass by Lib for optimization. Does it helps to reduce linking time?. Why those information cannot embed it DLL? So why does in call `DLL` than calling `DLL & LIB`. Thank you! – Nayana Adassuriya Jul 24 '13 at 04:44
  • It's mostly a matter of optimizing the format of the data -- i.e., converting the data for a library into a COFF format object file like the linker expects to deal with otherwise. But -- you can create a .lib from a DLL (or even just an module definition file) so the result doesn't contain any information that's absent from the DLL. – Jerry Coffin Jul 24 '13 at 04:46
  • Note that the GNU linker `ld` can link directly using the DLL instead of an import library so it's clearly technically possible. I have no idea why MS hasn't implemented this in it's toolchain. – Michael Burr Aug 14 '14 at 05:06

2 Answers2

3

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.

snf
  • 2,857
  • 1
  • 18
  • 20
  • +1 for structure of the answer, Thank you for your nicely explained answer. I need some time to go trough the way you propose. I need your help after that. please keep in touch with this. thanks again – Nayana Adassuriya Jul 24 '13 at 06:05
  • 1
    Just a complement to this nice answer. Assuming you use Microsoft tools, you can create an import library for a DLL using LIB tool - from microsoft documentation : *LIB can also be used to create export files and import libraries to reference exported definitions* – Serge Ballesta Aug 13 '14 at 08:32
2

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490