1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

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

  1. d_arin100

    d_arin100 New Member

    Sep 25, 2009
    Likes Received:
    Trophy Points:


    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.
    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.
  2. shabbir

    shabbir Administrator Staff Member

    Jul 12, 2004
    Likes Received:
    Trophy Points:
  3. rasd123

    rasd123 Banned

    Nov 4, 2009
    Likes Received:
    Trophy Points:
    Thanks to share this information.

Share This Page