Click here to Skip to main content
13,251,998 members (68,655 online)
Click here to Skip to main content
Add your own
alternative version


62 bookmarked
Posted 7 Dec 2003

Create Simple Load time DLLs

, 7 Dec 2003
Rate this:
Please Sign up or sign in to vote.
This article explains how to create simple Load time DLL in Microsoft Foundation Class and Win32 API


If you like to create modular software, you choose DLLs. Dynamic linked libraries are easy to create compared to applications. Dynamic linked library is a set of modules that contains a set of functions or other called DLLs.

In windows operating system some of the DLLs are important. They are the following - User32.dll manages user interface related tasks. The Gdi32.dll manages graphic device interface related (GDI) tasks. In DOS world, programs write directly into video memory. But, in windows layer called Graphic Device Interface (GDI) exists. The advapi32.dll manages registry related tasks. The comdlg32.dll manages the common dialog box related tasks (Example File open dialog box, file save and save as dialog box). The comctrl32.dll manages the windows common control related tasks.

The Windows DLLs are the backbone of windows operating system. The windows operating system manages the large set of system DLLs. When the windows operating system starts, the DLLs load to system memory.

Types of DLL

The windows operating system has two types of DLLs. There are the following.

  1. Load Time Dynamic Linking
  2. Run Time Dynamic Linking

The Load Time Dynamic Linking load the DLLs at the load time. When the application starts, all the required DLLs are loaded into memory in the startup time. In this method the system tries to find the DLLs in the following locations.

  1. The Windows System Directory
  2. The Windows directory
  3. The Application local path
  4. The directories listed in the path environment variables

Load Time DLLs requires LIB file and declaration file (.h), when we create application programs. We insert the header file in the application and link with the LIB file in the application project settings. The DLLs do not handle windows messages. The DLLs are simply set of functions or variables declared in the declaration file ( .h) and implemented in the implementation file(.cpp).

The run time dynamic linking tries to load DLL at run time. The LoadLibrary function is used to load a DLL at run time. The syntax for LoadLibrary is the following.

HINSTANCE hinst = LoadLibrary(LPCTSTR dllfilename);

If the LoadLibrary returns success, then we use GetProcAddress get the address of the functions. After calling DLL function we call FreeLibrary to free the DLL library. In this run time dynamic linking, we do not load all the DLL files in the startup time. Whenever we want to load, we load and unload the DLLs.

Create Load time Win32 DLL

We already saw, that using DLLs is easy to create modular applications. In win32 Dynamic linked library project choose DLL and add support for some export symbols in second step. In that class declaration add your method and return types. When you allocate memory in the DLL file, you also deallocate it. If you deallocated from the client (like from application) it may be deallocated but we cannot be sure.

__declspec(dllimport) tells the compiler to add some symbols for import from the DLLs. This way the __declspec(dllexport) tells the compiler to export some symbols. In our sample program declare the three functions. The functions are to add, multiply and subtract the two values. These simple DLLs help you understand the basic DLLs functionality.

The DLL entry point is the DllMain; the DllMain has the following syntax.

                      DWORD  ul_reason_for_call,
                      LPVOID lpReserved)
    switch( ul_reason_for_call ) {
    return TRUE;

You copy DLL file into system directory and copy LIB file into Visual C++ Lib directory and link to lib file. In application module make sure to implement the header file. In our example file is declared in the following manner.

#define WIN32DLL_API __declspec(dllexport)
#define WIN32DLL_API __declspec(dllimport)

// This class is exported from the Win32DLL.dll
class WIN32DLL_API CWin32DLL {
    // TODO: add your methods here.
    int add(int a,int b);
    int sub(int a,int b);
    int mul(int a,int b);
extern WIN32DLL_API int nWin32DLL;

The class CWin32DLL has been declared as __declspec(dllexport) using WIN32DLL_API. So, all the class member functions and member variables are __declspec(dllexport). All the member functions are declared as public.

In our implementation file, we link and call the functions.

The run time dynamic linking in the client program are the following.
#include "stdio.h"
#include "windows.h"


    HINSTANCE hinstLib;
    MYPROC ProcAdd;
    BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;

    // Get a handle to the DLL module.

    hinstLib = LoadLibrary("win32dll.dll");

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

    if (hinstLib != NULL)
        ProcAdd = (MYPROC) GetProcAddress(hinstLib, "add");

    // Do add operation

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

        if (fRunTimeLinkSuccess = (ProcAdd != NULL))
            (ProcAdd) ("message via DLL function\n");

        // Free the DLL module.

        fFreeResult = FreeLibrary(hinstLib);

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

    if (! fRunTimeLinkSuccess)
        printf("message via alternative method\n");

Create MFC DLL

Microsoft Foundation Class (MFC) library can be used to create simplified DLLs. The MFC supports two types of DLLs. Those are the following.

  1. Regular DLL with MFC statically linked
  2. Regular DLL using shared MFC Dll
  3. MFC Extension DLL ( Using shared MFC DLL)

In the regular DLL with MFC statically linked, the Client may be MFC based application or Non-MFC application. Our sample program application is based on a DLL using shared MFC dll.

MFC Extension DLL has reusable classes derived from existing Microsoft foundation Classes. Extension DLL is built from Dynamic linked library version of the DLL. In MFC, regular DLLs are initialized in three ways. The DLL has a CWinApp Object. MFC OLE initializes using MFC global AfxOleInitModule function. The MFC Database initializes into MFC using global AfxDbInitModule function. MFC Sockets are initialized using AfxNetInitModule function.

Initializing the Extension DLLs is as following.

extern "C" int APIENTRY DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID 
   if (dwReason == DLL_PROCESS_ATTACH)
      // test.dll Initializing!
      // Extension DLL one-time initialization

      AfxInitExtensionModule(PROJNAMEDLL, hInstance);

      // Insert this DLL into the resource chain
      new CDynLinkLibrary(dll);

   else if (dwReason == DLL_PROCESS_DETACH)
      // test.dll Terminating!
   return 1;   // ok

In our example, The Dll has DrawEllipse function. We pass the CRect structure and CDC to DLL. The DLL function draws the DLL with selected Blue Brush.

Benefits of DLLs

The benefits of using DLLs are the following.

  1. The Dynamic linked library shares the memory. So, the system performance is improving compared to using applications.
  2. We can build and test separately each DLL.
  3. We can create DLLs for different languages. Windows use C DLL, C++ DLL, Fortran DLL and PASCAL DLL etc. Even windows support some version of Smalltalk DLLs.
  4. We can load and unload at run time. This helps to improve application performance.
  5. The big software products were divided into several DLLs. The developers easily develop their application.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
India India
Selvam has worked on several technologies like Core Java, VC++, MFC, Windows API and Weblogic server. He takes a lot of interest in reading technical articles and enjoy writing them too. He has been awarded as a Microsoft Community Star in 2004, MVP in 2005-06, SCJP 5.0 in 2009, Microsoft Community Contributor(MCC) 2011.

Github :
Web site:

You may also be interested in...


Comments and Discussions

Questionimport and export Pin
bkelly1320-Feb-16 15:16
memberbkelly1320-Feb-16 15:16 
QuestionPlease Build Step-BY-Step project to use open source DLL in VC++ 2010 MFC Application Pin
Sumit Makhe8-Mar-12 22:25
memberSumit Makhe8-Mar-12 22:25 
AnswerRe: Please Build Step-BY-Step project to use open source DLL in VC++ 2010 MFC Application Pin
R.selvam9-Mar-12 4:24
memberR.selvam9-Mar-12 4:24 
GeneralMy vote of 5 Pin
Aburawan12-Jul-10 22:32
memberAburawan12-Jul-10 22:32 
GeneralC# DllImport, calling the function causes EntryPointNotFoundException Pin
Annedore Rößling18-Jun-10 3:15
memberAnnedore Rößling18-Jun-10 3:15 
QuestionHow to call Dll with Dll? Pin
imclare26-Jan-10 23:22
memberimclare26-Jan-10 23:22 
Generalerror:WIN32DLL_API class CWin32DLL { Pin
slkpovud5-Dec-07 0:14
memberslkpovud5-Dec-07 0:14 
QuestionHow Create Load time DLLs on .net Pin
va250516-Feb-07 8:50
memberva250516-Feb-07 8:50 
AnswerRe: How Create Load time DLLs on .net Pin
Portatofe2-Oct-08 6:56
memberPortatofe2-Oct-08 6:56 
AnswerRe: How Create Load time DLLs on .net Pin
R.selvam8-Sep-09 1:29
memberR.selvam8-Sep-09 1:29 
GeneralStatic .lib Help Pin
ArunAntony17-Mar-06 21:24
memberArunAntony17-Mar-06 21:24 
Questionwrong run time dynamic linking example? Pin
AlenkaZajka23-Jun-05 0:46
memberAlenkaZajka23-Jun-05 0:46 
GeneralCannot find the procedure in the DLL Pin
Nightfox4-Apr-05 19:17
memberNightfox4-Apr-05 19:17 
GeneralRe: Cannot find the procedure in the DLL Pin
R.selvam4-Apr-05 20:09
memberR.selvam4-Apr-05 20:09 
GeneralRe: Cannot find the procedure in the DLL Pin
Frank K1-Nov-05 6:20
memberFrank K1-Nov-05 6:20 
GeneralDLL export for template base classes Pin
Jafar amiri3-Oct-04 1:55
memberJafar amiri3-Oct-04 1:55 
GeneralRe: DLL export for template base classes Pin
R.selvam4-Apr-05 20:05
memberR.selvam4-Apr-05 20:05 
QuestionHow to link implicitly a foreign DLL Pin
CEERI1-Oct-04 1:10
memberCEERI1-Oct-04 1:10 
AnswerRe: How to link implicitly a foreign DLL Pin
Anonymous1-Oct-04 5:02
sussAnonymous1-Oct-04 5:02 
QuestionHow to export functions Pin
Michael Olsen18-Mar-04 0:40
memberMichael Olsen18-Mar-04 0:40 
AnswerRe: How to export functions Pin
bdiamond23-Mar-04 4:18
memberbdiamond23-Mar-04 4:18 
GeneralIn MFC Which DLL use Pin
R.selvam15-Dec-03 19:32
memberR.selvam15-Dec-03 19:32 
Questiondlls,lib,headers??? Pin
Nadia Tariq15-Dec-03 18:55
memberNadia Tariq15-Dec-03 18:55 
AnswerRe: dlls,lib,headers??? Pin
R.selvam16-Dec-03 6:46
memberR.selvam16-Dec-03 6:46 
GeneralPros and Cons Pin
Mike Wild9-Dec-03 23:27
memberMike Wild9-Dec-03 23:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 8 Dec 2003
Article Copyright 2003 by R.selvam
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid