Go4Expert (http://www.go4expert.com/)
-   Win32 (http://www.go4expert.com/articles/win32-tutorials/)
-   -   Advantages of explicit linking of Dynamic Link Library (DLL). (http://www.go4expert.com/articles/advantages-explicit-linking-dynamic-t19693/)

d_arin100 7Oct2009 17:26

Advantages of explicit linking of Dynamic Link Library (DLL).


This article will explain how we can explicitly link a DLL and why this approach is better than the implicit linking mechanism of a DLL. Explicit linking is more appropriate for interpreted languages such as Visual Basic, but we can use it from C++. In this linking mechanism we don’t use the “.lib” file (Which is required in the implicit linking) instead we call the Win32 “LoadLibrary() function.


If we implicitly linked a DLL, then the DLL does not remove from the memory unless and until the application is terminated. But in explicit linking we can load a DLL whenever it’s required and unload or free it whenever it’s not required. With explicit linking, applications must make a function call to explicitly load the DLL at run time. To explicitly link to a DLL, an application must call the following Win32 API’s:

1. Call LoadLibrary() function to load the DLL and obtain an HINSTANCE( Handle of the module) . LoadLibrary() takes the DLL's full pathname. If you don't specify the pathname Windows follow the following search sequence to locate the DLL:
  • The directory containing the EXE file
  • The process's current directory
  • The Windows system directory
  • The Windows directory
  • The directories listed in the Path environment variable
2. Call GetProcAddress() to obtain a function pointer to each exported function that the application wants to call. The handle which is returned by the LoadLibrary() function is used to call the GetProcAddress() function. The address which is obtain by the GetProcAddress() function is a pointer of the exported function through which the function is finally called.

3. Call FreeLibrary() whenever the DLL is no more required.

We are a taking a DLL (“SquareRoot.dll”) that has a exported function “SquareRoot()”.


extern "C" __declspec(dllexport) double SquareRoot(double dValue);
Here's the client application will explicitly link the DLL (“SquareRoot.dll”) and then collect the function pointer of the exported function (“SquareRoot()”). After that it will call the function “SquareRoot()” through the function pointer and then unload the DLL from the memory.
Code: Cpp

typedef double (SqrtFunc)(double);
HINSTANCE hInstance;
SqrtFunc* pSqrt;
/// Loading the DLL explicitly
hInstance = ::LoadLibrary("SquareRoot.dll");
/// Collecting the function pointer of the exported function
pSqrt = (SqrtFunc*)::GetProcAddress(hInstance, "SquareRoot");
/// Call the function “SquareRoot()”
double dValue = (*pSqrt)(100.00);
/// Unload the DLL from the memory

The advantage of this type of linking is that we can run time decide which DLL to load and unload. Let we have a DLL with string resources in English and another with string resources in Greek. Now depending on the user choice of the language the appropriate DLL will be loaded.

shabbir 1Nov2009 19:50

Re: Advantages of explicit linking of Dynamic Link Library (DLL).
Nominate this article for Article of the month - Oct 2009

rasd123 8Nov2009 06:28

Re: Advantages of explicit linking of Dynamic Link Library (DLL).
Thanks to share this information.

All times are GMT +5.5. The time now is 03:53.