Click here to Skip to main content
15,880,651 members
Articles / Desktop Programming / MFC
Article

Creation of a Simple DLL

Rate me:
Please Sign up or sign in to vote.
3.06/5 (32 votes)
28 Jun 20021 min read 283.1K   67   16
Steps to create your first DLL file

Introduction

This article shows a step-by-step technique to create your first DLL with VC++.

Steps to Create Your First DLL

  • Create a Win32 Dynamic Link Library project, adding a *.cpp and a *.h file.
  • In the *.cpp file, create a class instantiated from the CWinApp file.
    C++
    # include <stdafx.h>
    # include "SourceFile.h"
    
    class CDllApp : public CWinApp
    {
        public:
      
            CDllApp::CDllApp()
            {
                Calc(0,0);
            }
    
            DECLARE_MESSAGE_MAP()
    };
    
    BEGIN_MESSAGE_MAP(CDllApp,CWinApp)
    
    END_MESSAGE_MAP()
    
    CDllApp DllObject;
  • In the *.h file (here it is SourceFile.h), define the functions to be used. Also specify the dllexport value for the _declspec function.
    C++
    extern "C" _declspec(dllexport) int Calc(char no1,char no2)
    {
        char result;
        result = no1 + no2;
        return result;
    }
  • Then compile the DLL.
  • Create a normal Win32 Application with a *.cpp file and a *.h file.
  • In the *.h file, (here it is AppHeader.h ), declare the function with the dllimport value of _declspec.
    C++
    extern "C" _declspec(dllimport) Calc(int FirstValue,
        int SecondValue);
    
  • In the *.cpp file, use the function.
    C++
    # include "AFXWIN.H"
    # include "AppHeader.h"
    
    class MainFrame : public CFrameWnd
    {
        public:
    
            MainFrame()
            {
                Create(0,"Trial");
            }
    
            void OnLButtonDown(UINT nFlags,CPoint point)
            {
                int res;
                char str[5];
                res = Calc(998,226);
                sprintf(str,"%d",res);
                MessageBox(str);
             }
    
             DECLARE_MESSAGE_MAP()
    };
  • In the Link tab of the "Project->Settings" dialog, go to the text box labeled "Object / Library Modules" and specify the path of the DLL file. Then copy the compiled DLL file to your current appliation path directory and run the program.

Some Things to Note

The DLL file may not be visible due to the File View options in the Windows folder. So, you can either go to the DOS prompt and copy the file or enable the setting "Show all files" in Windows Explorer to copy the file. To create a DLL that uses MFC, see the following example. Note that extern "C" has not been used and the macro AFX_MANAGE_STATE(AfxGetStaticModuleState()); has been used to implement MFC.

C++
 _declspec(dllexport)CString Display(CString a,CString b)
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    CString Str;
    Str = a + b;
    return Str;
}

That's all, folks. All luck and have a great time.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Founder
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow can i create a same type of dll in vb6? Pin
JothiMurugeswaran13-Nov-07 18:19
JothiMurugeswaran13-Nov-07 18:19 
Questionbuilding dll (C++) to be called by C applications Pin
P.Luke20-Apr-07 13:33
P.Luke20-Apr-07 13:33 
GeneralDll-ASP.NET Pin
niki_nilu23-Feb-07 0:10
niki_nilu23-Feb-07 0:10 
GeneralMessages Pin
The Jerryman27-Oct-06 3:32
The Jerryman27-Oct-06 3:32 
GeneralC DLL without changing the source code Pin
Run in the jungle8-Mar-05 10:47
Run in the jungle8-Mar-05 10:47 
Generalproblem with dll Pin
dnqhung10-Jun-04 1:24
dnqhung10-Jun-04 1:24 
GeneralRe: problem with dll Pin
Christian Kluin14-Jun-04 11:31
Christian Kluin14-Jun-04 11:31 
GeneralError C1083 in VC++ .NET 2002 Pin
Sctt H. Chang8-Mar-04 7:07
Sctt H. Chang8-Mar-04 7:07 
GeneralRe: Error C1083 in VC++ .NET 2002 Pin
Sachin R Sangoi11-Jan-07 2:06
Sachin R Sangoi11-Jan-07 2:06 
Questionhow to export vb function through c++ dll Pin
vbjoo22-Aug-02 20:45
vbjoo22-Aug-02 20:45 
Generaldll compilation error Pin
MINAKANNAN22-Jul-02 20:30
MINAKANNAN22-Jul-02 20:30 
GeneralRe: dll compilation error Pin
Anonymous11-Mar-03 10:05
Anonymous11-Mar-03 10:05 
GeneralRe: dll compilation error Pin
Anonymous25-Aug-04 0:43
Anonymous25-Aug-04 0:43 
GeneralRe: dll compilation error Pin
SiVo6-Apr-03 20:09
SiVo6-Apr-03 20:09 
GeneralRe: dll compilation error Pin
DaFrawg30-Jan-04 2:08
DaFrawg30-Jan-04 2:08 
GeneralRe: dll compilation error Pin
Reza Shademani6-Apr-03 20:09
Reza Shademani6-Apr-03 20:09 

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.