Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Win32 (http://www.go4expert.com/articles/win32-tutorials/)
-   -   Internal mechanism of "Static linked DLL" works (http://www.go4expert.com/articles/internal-mechanism-static-linked-dll-t19668/)

d_arin100 5Oct2009 12:07

Internal mechanism of "Static linked DLL" works
 
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: Cpp

#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.

smithray 8Oct2009 13:48

hi
 
hi to every one

shabbir 8Oct2009 21:04

Re: Internal mechanism of "Static linked DLL" works
 
Hello and welcome to the forum

djbrown 22Oct2009 14:17

Re: Internal mechanism of "Static linked DLL" works
 
nice thread "Static linked DLL"

shabbir 1Nov2009 19:50

Re: Internal mechanism of "Static linked DLL" works
 
Nominate this article for Article of the month - Oct 2009

rasd123 14Nov2009 05:48

Re: Internal mechanism of "Static linked DLL" works
 
Thanks to share this information.

shabbir 17Nov2009 09:27

Re: Internal mechanism of "Static linked DLL" works
 
Start voting for this article for Article of the month - Oct 2009

rasd123 22Nov2009 11:00

Re: Internal mechanism of "Static linked DLL" works
 
Thanks for this site very helpful.


All times are GMT +5.5. The time now is 15:24.