Click here to Skip to main content
6,595,444 members and growing! (18,246 online)
Email Password   helpLost your password?
General Programming » DLLs & Assemblies » General     Intermediate

DLLs are simple: Part 2

By Mahmoud Komeily

This article describes how to export classes from a DLL.
VC6, VC7Win2K, WinXP, Dev
Posted:21 Sep 2004
Views:147,752
Bookmarked:93 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
70 votes for this article.
Popularity: 8.29 Rating: 4.49 out of 5
2 votes, 2.9%
1
2 votes, 2.9%
2
2 votes, 2.9%
3
2 votes, 2.9%
4
62 votes, 88.6%
5

Sample Image - SimpleDll2.gif

Introduction

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
    {
       public:
       CRectArea();
       virtual ~CRectArea();
    };
  8. Now, declare a member function for newly created class. E.g., Calculate:
    class DLLEXPORT CRectArea 
    {
    public:
        int Calculate(int ParOne,int ParTwo);
        CRectArea();
        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
    {
       public:
       int Calculate(int ParOne,int ParTwo);
       CRectArea();
       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() 
    {
       UpdateData();
       m_Result=m_RectArea.Calculate(m_ParOne,m_ParTwo);
       UpdateData(FALSE);
    }

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

About the Author

Mahmoud Komeily


Member
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, محمود کمیلی
Occupation: Web Developer
Location: Iran, Islamic Republic Of Iran, Islamic Republic Of

Other popular DLLs & Assemblies articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 27 (Total in Forum: 27) (Refresh)FirstPrevNext
GeneralLoading same dll multiple times in same process address space PinmemberRaviPK0:37 20 May '08  
GeneralRe: Loading same dll multiple times in same process address space PinmemberMahmood Komeily21:03 20 May '08  
GeneralWant to call a Visual Basic 6.0 dll in Visual C 2005 PinmemberSchmidtj4:03 22 Apr '08  
GeneralCalling Dialog in DLL Pinmembersmzhaq9:02 29 May '07  
Generaldiffrence between *.dll and *.lib Pinmemberwavebai2:29 27 Sep '06  
AnswerRe: diffrence between *.dll and *.lib PinmemberMahmood Komeily12:12 28 Sep '06  
GeneralRe: diffrence between *.dll and *.lib Pinmemberwavebai2:37 1 Oct '06  
GeneralRe: diffrence between *.dll and *.lib Pinmemberextolfan15:02 17 Nov '06  
GeneralHow to export classes with virtual functions Pinmemberkiran.pinjarla22:52 23 Aug '06  
QuestionClass type redefinition [modified] PinmemberIgen13:35 10 Aug '06  
GeneralHow to create a MFC dll that contains a dialog? Pinmemberudiraz7:11 22 Nov '05  
Questionabout importing DLL Pinmembermehaghost6:46 11 Sep '05  
AnswerRe: about importing DLL PinmemberMahmood Komeily8:28 11 Sep '05  
GeneralClass not recognized PinmemberChristian J Zimmer4:32 22 Mar '05  
GeneralLoading DLL class explicitly Pinmembermsoto7917:27 24 Nov '04  
GeneralDLL export of Template classes PinsussJafar amiri parian23:38 30 Sep '04  
GeneralRe: DLL export of Template classes PinmemberVYu22:59 3 Oct '04  
GeneralRe: DLL export of Template classes PinsussJafar amiri parian23:16 3 Oct '04  
GeneralRe: DLL export of Template classes PinmemberVYu0:18 4 Oct '04  
GeneralClass Hierarchy PinmemberManish K. Agarwal18:07 27 Sep '04  
GeneralName Mangling PinsussMichael Fitzpatrick8:16 27 Sep '04  
GeneralRe: Name Mangling PinmemberMr Doug14:49 27 Sep '04  
GeneralValuable !! PinmemberWREY13:36 25 Sep '04  
GeneralRe: Valuable !! PinmemberMahmood Komeily1:24 27 Sep '04  
GeneralRe: Valuable !! PinmemberMilind Murlidhar Shingade5:57 30 Sep '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 21 Sep 2004
Editor: Smitha Vijayan
Copyright 2004 by Mahmoud Komeily
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project