Windows Dynamic-Link Libraries 8






Using Run-Time Dynamic Linking Program Example


You can use the same DLL in both load-time and run-time dynamic linking. The following example uses the LoadLibrary() function to get a handle to the Myputs DLL. If LoadLibrary() succeeds, the program uses the returned handle in the GetProcAddress() function to get the address of the DLL's myPuts function. After calling the DLL function, the program calls the FreeLibrary() function to unload the DLL. Because the program uses run-time dynamic linking, it is not necessary to link the module with an import library for the DLL. The following example illustrates an important difference between run-time and load-time dynamic linking. If the DLL is not available, the application using load-time dynamic linking must simply terminate. The run-time dynamic linking example, however, can respond to the error.

Create a new empty Win32 console application project. Give a suitable project name and change the project location if needed.


Using Run-Time Dynamic Linking Program Example: Creating new Win32 C++ console application project in Visual C++ .NET


Then, add the source file and give it a suitable name.


Using Run-Time Dynamic Linking Program Example: Adding new C++ source file for C++ source code to the existing C++ project

Next, add the following source code.


// A simple program that uses LoadLibrary() and

// GetProcAddress() to access myPuts() from Myputs.dll.

#include <windows.h>

#include <stdio.h>


typedef int (*MYPROC)(LPWSTR);


int wmain(int argc, WCHAR **argv)


    HINSTANCE hinstLib;

    MYPROC ProcAdd;

    BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;


    // Get a handle to the DLL module.

    hinstLib = LoadLibrary(L"MyTestDll.dll");


    // If the handle is valid, try to get the function address.

    if (hinstLib != NULL)


        wprintf(L"LoadLibrary() is OK!\n");

        ProcAdd = (MYPROC) GetProcAddress(hinstLib, "myPuts");


        // If the function address is valid, call the function.

        if (ProcAdd != NULL)


            wprintf(L"GetProcAddress() is fine!\n");

            fRunTimeLinkSuccess = TRUE;

            (ProcAdd) (L"Another message from executable lol!");



                  wprintf(L"GetProcAddress() failed miserably, error %d\n", GetLastError());

        // Free the DLL module.

        fFreeResult = FreeLibrary(hinstLib);



            wprintf(L"LoadLibrary() failed miserably, error %d\n", GetLastError());


    // If unable to call the DLL function, use an alternative.

    if (!fRunTimeLinkSuccess)

        wprintf(L"Failed to call the DLL's function! Error %d\n", GetLastError());


      return 0;



Build the project. There should be no error because the LoadLibrary() happens during the run-time. Copy the previously created DLL, MyTestDll.dll into the project’s Debug folder.


Using Run-Time Dynamic Linking Program Example: The generated DLL file is ready to be used


 Run the application project. The following sample output shows the application successfully 'imported' the DLL's function.


Using Run-Time Dynamic Linking Program Example: A sample console program output in action





< Windows Dynamic Link Library 7 | Windows Dynamic Link Library | Win32 Programming | Windows Dynamic Link Library 9 >