Internal mechanism of "Static linked DLL" works

Discussion in 'Win32' started by d_arin100, Oct 5, 2009.

  1. d_arin100

    d_arin100 New Member

    Joined:
    Sep 25, 2009
    Messages:
    10
    Likes Received:
    3
    Trophy Points:
    0
    Location:
    Bangalore
    This article is about how static or implicit linking can be done and how the client program gets the information from Dynamic link library (DLL). The two ways we can link a DLL to the client program. One is “Static linking” another is “Dynamic linking”.

    Here our topic is static linking so we will not discuss about the dynamic linking. In static linking the object code of the client program linked with the object code of the library function and form an executable file. Since the linking is done at the time of compilation so this type of linking is called static linking. In this linking mechanism the DLL is not removed from the memory unless and until the client application is terminated.

    Sample



    Before moving to the main topic I would like to discuss about creating a DLL. Let a DLL which is able to square root of a number. The following steps needs to take for creating this DLL:

    1. Create a project of type “MFC AppWizard (dll)” and give project name as “SquareRoot”.

    2. Select the radio button “Regular DLL using shared MFC DLL”.

    3. Create a source file “sqrt.cpp” and add it to the project. The “sqrt.cpp” files contain the following code:

    Code:
    #include "stdafx.h"
    #include "math.h"
     
    /// The __declspce keyword with the dllexport attribute tells the linker
    /// that the function is available for use by the other programs.
    extern "C" __declspec(dllexport) double SquareRoot(double dValue)
    {
     if (dValue >= 0.0)
     {
     return sqrt(dValue);
     }
     AfxMessageBox("Unable to create square root of a negative number.");
     return 0.0;
    }
    
    4. Create another header file “sqrt.h” and write the following code in it:

    Code:
    extern "C" __declspec(dllexport) double SquareRoot(double dValue);
    5. From Build menu build the DLL “SquareRoot”. Two files will be created “SquareRoot.lib” in the projects “debug” or “release” directory.

    Now we need to create a client application which will use this DLL. First of all we need to copy the “sqrt.h” file from the DLL’s project directory into client program project directory. Then need to copy “SquareRoot.lib” and “SquareRoot.dll” from the DLL’s “Debug” or “Release” directory into client’s project directory. After that add “SquareRoot.lib” into “Object/Library modules” edit box of Link page (Select “Project | Settings” menu and the Link tab).
    Code:
    //// client.cpp
    #include "stdafx.h"
    #include "sqrt.h"
     
    int APIENTRY WinMain(HINSTANCE hInstance,
     HINSTANCE hPrevInstance,
     LPSTR lpCmdLine,
     int nCmdShow)
    {
     // TODO: Place code here.
     ///////
     //..............
     ///////
     double dwRet = SquareRoot(100.00);
     if(dwRet >= 0.0)
     {
     //////
     //...........
     /////
     }
     
     return 0;
    }
    
    If we compile and execute the above code base we will get the expected result. Now the question is why the two files are required (“SquareRoot.lib” & “SquareRoot.dll”) for the client application.

    Internal mechanism….



    :thinking: The “SquareRoot.lib” file is little bit different than the normal object library file. The normal object library file contains the object code but when we are creating a DLL the “SquareRoot.lib” will not contain the object code, instead that the DLL file “SquareRoot.dll” file will contain the object code. The “SquareRoot.lib” will contain the information of the exported functions of the DLL and the name of the DLL file in which those functions are there. At the time of compilation the client program takes the information of exported function from the “SquareRoot.lib” file. So after compilation whenever the “client.exe” has been created the “SquareRoot.lib” is not required for execution.

    At the time of execution of the client application (“client.exe”) the “SquareRoot.dll” file will be loaded into the memory. The information of the associated DLL file has already been stored in the client application (“client.exe”) whenever we are linking with “SquareRoot.lib” file. The DLL file contains a table which consists of the name of the exported function and the corresponding address of the function within the DLL. So from client application when an exported function call is occur, then from the table of the DLL the address of the function will be picked up and the program control will be transferred to the exported function of the DLL.
     
    shabbir likes this.
  2. smithray

    smithray New Member

    Joined:
    Oct 8, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hi

    hi to every one
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Hello and welcome to the forum
     
  4. djbrown

    djbrown New Member

    Joined:
    Oct 22, 2009
    Messages:
    16
    Likes Received:
    1
    Trophy Points:
    0
    nice thread "Static linked DLL"
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  6. rasd123

    rasd123 Banned

    Joined:
    Nov 4, 2009
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    Thanks to share this information.
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  8. rasd123

    rasd123 Banned

    Joined:
    Nov 4, 2009
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for this site very helpful.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice