Click here to Skip to main content
11,795,848 members (79,152 online)
Click here to Skip to main content

DLLs are simple: Part 2

, 21 Sep 2004 198.6K 4.1K 114
Rate this:
Please Sign up or sign in to vote.
This article describes how to export classes from a DLL.

Sample Image - SimpleDll2.gif


In my previous article, I mentioned simply DLL creation and using it. But it exports function only. Now, I want to describe "how to export classes from a DLL?"

How to build MFC DLL Containing New Class?

  1. Run VC++.
  2. Choose : File > New.
  3. Create "MFC AppWizard (DLL)" (named e.g.: MyScndDll).
  4. Click right mouse button on root branch in "ClassView", point and click "New Class...".
  5. Select Generic Class as class type, and name it e.g. CRectArea.
  6. Declare in top of file of "RectArea.h":
    #define DLLEXPORT __declspec(dllexport)

    __declspec(dllexport)'s purpose is to add the "export directive" to the object file so you don't need a .DEF file. To make code more readable, define a macro for __declspec(dllexport): DLLEXPORT.

  7. Now, for exporting per class that you want, type before its declaration: "DLLEXPORT" like this:
    class DLLEXPORT CRectArea
       virtual ~CRectArea();
  8. Now, declare a member function for newly created class. E.g., Calculate:
    class DLLEXPORT CRectArea 
        int Calculate(int ParOne,int ParTwo);
        virtual ~CRectArea();
  9. and define it:
    int CRectArea::Calculate(int ParOne,int ParTwo)
        return ParOne*ParTwo;
  10. Press Build button.
  11. Bring out DLL from oven!!

Note: linker also builds an "import library" with same DLL name but .lib extension.

How to use MFC DLL?

  1. Run VC++.
  2. Choose : File > New.
  3. Create "MFC AppWizard (exe)".
  4. Choose "Dialog based", then click Finish.
  5. Choose: Project > Add To Project > New > C/C++ Header File.
  6. Name file, e.g.: Imports.
  7. Declare in Imports.h:
    #define DLLIMPORT __declspec(dllimport) 

    __declspec(dllimport)'s purpose is to add the "import directive" to the object file. To make code more readable, define a macro for __declspec(dllimport): DLLIMPORT.

  8. Type after it, declaration of exported function from DLL. Note: we must type "DLLIMPORT" whenever intending to import class:
    class DLLIMPORT CRectArea
       int Calculate(int ParOne,int ParTwo);
       virtual ~CRectArea();

    This is the same class that you declared and defined in the DLL. Here, have introduce as "import class". But it is unrecognized for current project and we must resolve it for linker.

  9. Copy .lib file from release or debug folder (depending on current project setting) in previous project to current directory project because it must link with EXE. Inside .lib file (same import library), exist information about exported class from DLL.
  10. Back to VC++ environment and choose: Project > Settings > Link (tab).
  11. Type [.lib file previous project] in "Object/library modules:"

    For example: MyScndDll.lib, then press OK. It will resolve externals. Here: CRectArea class.

  12. Cause we intend to use function in ...Dlg.cpp file (e.g.: ImportClassDlg.cpp), type in top of file:
    #include "Imports.h"
  13. Now, you can use function of exported class, like this:

    void CImportClassDlg::OnCalc() 


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


About the Author

Mahmoud Komeily
Web Developer
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
I born in tehran at 1975
and began programming with commodore 64
I established "Pishro Narmafzar Iran" Corporation in 2001.
i'm expert in VC++, VC#, MS SQL Server, ASP .NET & have developed :
1-Persian Photoshop (fully localized)
2-Persian Freehand (fully localized)
3-Pardis (persian/arabic typing in graphical and video editing programs include : Ulead video studio,Ulead media studio,Pinnacle studio, Premiere, Flash, Freehand, 3D Max, Auto CAD, Photoshop, CorelDraw, Ulead Cool 3D,...)
Pardis is only persian/arabic typing tool for Ulead Video Studio in world.
Mahmoud komeily, mahmood komeili, محمود کمیلی

You may also be interested in...

Comments and Discussions

GeneralLoading same dll multiple times in same process address space Pin
RaviPK19-May-08 23:37
memberRaviPK19-May-08 23:37 
GeneralRe: Loading same dll multiple times in same process address space Pin
Mahmood Komeily20-May-08 20:03
memberMahmood Komeily20-May-08 20:03 
GeneralWant to call a Visual Basic 6.0 dll in Visual C 2005 Pin
Schmidtj22-Apr-08 3:03
memberSchmidtj22-Apr-08 3:03 
GeneralCalling Dialog in DLL Pin
smzhaq29-May-07 8:02
membersmzhaq29-May-07 8:02 
Generaldiffrence between *.dll and *.lib Pin
wavebai27-Sep-06 1:29
memberwavebai27-Sep-06 1:29 
AnswerRe: diffrence between *.dll and *.lib Pin
Mahmood Komeily28-Sep-06 11:12
memberMahmood Komeily28-Sep-06 11:12 
GeneralRe: diffrence between *.dll and *.lib Pin
wavebai1-Oct-06 1:37
memberwavebai1-Oct-06 1:37 
GeneralRe: diffrence between *.dll and *.lib Pin
extolfan17-Nov-06 14:02
memberextolfan17-Nov-06 14:02 
QuestionHow to export classes with virtual functions Pin
kiran.pinjarla23-Aug-06 21:52
memberkiran.pinjarla23-Aug-06 21:52 
QuestionClass type redefinition [modified] Pin
Igen110-Aug-06 2:35
memberIgen110-Aug-06 2:35 
QuestionHow to create a MFC dll that contains a dialog? Pin
udiraz22-Nov-05 6:11
memberudiraz22-Nov-05 6:11 
Questionabout importing DLL Pin
mehaghost11-Sep-05 5:46
membermehaghost11-Sep-05 5:46 
AnswerRe: about importing DLL Pin
Mahmood Komeily11-Sep-05 7:28
memberMahmood Komeily11-Sep-05 7:28 
GeneralClass not recognized Pin
Christian J Zimmer22-Mar-05 3:32
memberChristian J Zimmer22-Mar-05 3:32 
GeneralLoading DLL class explicitly Pin
msoto7924-Nov-04 16:27
membermsoto7924-Nov-04 16:27 
GeneralDLL export of Template classes Pin
Jafar amiri parian30-Sep-04 22:38
sussJafar amiri parian30-Sep-04 22:38 
GeneralRe: DLL export of Template classes Pin
VYu3-Oct-04 21:59
memberVYu3-Oct-04 21:59 
GeneralRe: DLL export of Template classes Pin
Jafar amiri parian3-Oct-04 22:16
sussJafar amiri parian3-Oct-04 22:16 
GeneralRe: DLL export of Template classes Pin
VYu3-Oct-04 23:18
memberVYu3-Oct-04 23:18 
GeneralClass Hierarchy Pin
Manish K. Agarwal27-Sep-04 17:07
memberManish K. Agarwal27-Sep-04 17:07 
GeneralName Mangling Pin
Michael Fitzpatrick27-Sep-04 7:16
sussMichael Fitzpatrick27-Sep-04 7:16 
GeneralRe: Name Mangling Pin
Mr Doug27-Sep-04 13:49
memberMr Doug27-Sep-04 13:49 
GeneralValuable !! Pin
WREY25-Sep-04 12:36
memberWREY25-Sep-04 12:36 
You have provided a valuable service here to both beginners and some others who may have had difficulty understanding how this particular use of a DLL service is done.

It's clear and it's easy. A '5' from me.

Thank you.

Smile | :)


Fortes in fide et opere!
GeneralRe: Valuable !! Pin
Mahmood Komeily27-Sep-04 0:24
memberMahmood Komeily27-Sep-04 0:24 
GeneralRe: Valuable !! Pin
Milind Murlidhar Shingade30-Sep-04 4:57
memberMilind Murlidhar Shingade30-Sep-04 4:57 
GeneralC++ is a case sensitive language!!! Pin
Alexander M.22-Sep-04 10:29
memberAlexander M.22-Sep-04 10:29 
Generalcase sesitivity have observed !!! Pin
Mahmood Komeily22-Sep-04 12:15
memberMahmood Komeily22-Sep-04 12:15 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151002.1 | Last Updated 21 Sep 2004
Article Copyright 2004 by Mahmoud Komeily
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid