home Tech Library Tech Forum
Publisher
 
Dynamic Link Libraries       
 
 
Linking a DLL to an executable  

 

There are two common methods used for linking an executable to a DLL. Namely

1.Implicit Linking
This method is also referred to as static load or load-time dynamic linking. Here the executable links to the DLL uses an import library (.lib) provided by the maker of the DLL. So the OS loads the DLL when the executable using the DLL is loaded and the client can refer to the methods in the DLLs as if it were in the executable file.

The linking is done with the following steps.
              1. Obtain the header file containing the declarations of the exported functions and the
                   C++ classes. It is also mandatory that all these classes, functions and data should
                   have _declspec (dllimport) declarative in it.
              2. Also obtain the import library (.lib) files to link with.
              3. Also obtain the DLL (.dll file)
              4. Then the executable must include the header file in each source file containing
                   reference to the exported functions.
              5. Then while linking, link with the import library.

And now the executable would be able to locate the DLL while loading the executables. Actually the import library (.lib) contains code to load the DLL and to implement the calls to the functions in the DLL.

Note: the dllexport and dll import storage class attribute are Microsoft specific extensions to C and C++ languages, used to export and import functions data, and objects to and from a DLL. In a way dllexport actually replaces the _export keyword and expose the function with the name mangling. This defines a DLL’s interface to its client and eliminates the need for a module-defenition (.DEF) file.

2.Explicit Linking
This is also referred to as run-time dynamic linking or dynamic load. Here the executable using the DLL should make explicit calls to load and unload the DLL. And to access the exported functions the executable makes use of function pointers.

The explicit linking is done through a number of steps as follows.
        1. Call LoadLibrary() to load the DLL and obtain a handle.
        2. Call GetProcAddress to get the function pointer to each exported function. This taken in
             the name of the function as a parameter and gives the function pointer in return.
        3. Call FreeLibrary() to release the DLL.


Most Applications use implicit linking because it is the easiest method. But at times we do not have a choice and will have to go for explicit linking. So of those scenarios are given below
        1. The application does not know the name of the DLL to be loaded at run time.
        2. A process using implicit linking is terminated by the OS, if the DLL is not found at the
            process start up.
        3. An implicit linked process also terminates if any of the DLL it is linked to contains a DllMain()
            that fails.
        4. A process that links to many DLLs implicitly, will the slow to load and start, since windows
            loads all the DLLs to memory when the process starts.
        5. Explicit linking eliminates the need to relink the application in situations when any changes in
            the DLLs make the export cardinals to change and hence change the import library.
 
     
  RELATED TOPICS  
DLL overview
Advantages of Using DLLs
Application Vs DLLs
Types of DLL
Initializing a DLL
Export from a DLL
LoadLibrary and AfxLoadLibrary
GetProcaddress
FreeLibrary and AfxFreeLibrary
Resource-only DLL
Automation in DLLs


 
           
Home | Profile | Users | Library | Publisher | Tech Forum | News | Contact Us