Click here to Skip to main content
15,922,512 members
Articles / Programming Languages / C++
Article

Exporting C++ classes without using MFC extension DLL

Rate me:
Please Sign up or sign in to vote.
3.72/5 (37 votes)
5 Jun 2002 254.3K   5.2K   51   41
This article describes the process of exporting regular C++ classes without using MFC extension DLLs

Introduction

DLLs are a great way of sharing common pieces of code data between applications. When it comes down to exporting C++ classes from DLLs most of us go for MFC extension DLLs where we can use the AFX_EXT_CLASS macro to export an entire class. Unfortunately, MFC is no lean and mean class architecture, which means that distributing MFC extension DLLs mean that you have to include the big MFC runtime not to mention the fact that your DLL can only be linked to MFC applications exclusively. What's the solution then? Enter standard Win32 DLLs.

Details

I couldn't believe my eyes on how easily one can export C++ classes directly from a plain vanilla Win32 DLL. Just make one and insert your classes into the DLL. Now simply put __declspec(dllexport) in between the class keyword and the class name, i.e.

// in your header...

class __declspec(dllexport) CDllTest
{
public:  
  CDllTest(){}
  ~CDllTest(){}

public:
  void SayHello();
};

// in your cpp...

void CDllTest::SayHello()
{
 printf(_T("Hello C++"));
}

That's it! The sample code and project are pretty self explanatory. Enjoy.

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
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMultiple Classes Pin
GWG11-Jun-02 0:17
GWG11-Jun-02 0:17 
GeneralRe: Multiple Classes Pin
Tanzim Husain23-Apr-03 21:06
Tanzim Husain23-Apr-03 21:06 
GeneralRe: Multiple Classes Pin
bad_source_bios24-Apr-03 10:31
bad_source_bios24-Apr-03 10:31 
GeneralRe: Multiple Classes Pin
Anonymous2-May-03 11:43
Anonymous2-May-03 11:43 
GeneralRe: Multiple Classes Pin
bad_source_bios2-May-03 12:24
bad_source_bios2-May-03 12:24 
GeneralTHAAAAANK YOU!! Pin
Rickard Andersson2010-Jun-02 7:10
Rickard Andersson2010-Jun-02 7:10 
GeneralRe: THAAAAANK YOU!! Pin
Rickard Andersson2010-Jun-02 7:12
Rickard Andersson2010-Jun-02 7:12 
GeneralA note about exporting template classes in VC++... Pin
Vincent Richard6-Jun-02 21:11
Vincent Richard6-Jun-02 21:11 
Hi !

Maybe someone could be interested with this post.

If you attempt to export template classes using Visual C++, you may experience, like me, some compilation/linkage problems. A few months ago, I found a solution that works in all cases.

Suppose that you have this template class :
template <class T>
class MyTemplate
{
public:

    MyTemplate() { }
    ~MyTemplate() { }

    T m_data;
};

You cannot (I believe ?) export template class that are not "implemented".
All you have to do if you want to export/import an implementation of this template class, for example with type 'int', is to write :
#ifdef EXPORTING  // If you are compiling the DLL
   template class __declspec(dllexport) MyTemplate <int>;
   typedef MyTemplate <int> MyTemplateInt;
#else  // If you are using the DLL
   extern template class __declspec(dllimport) MyTemplate <int>;
   typedef MyTemplate <int> MyTemplateInt;
#endif

That's all !
Now you can use MyTemplateInt class in the program that links with your library.

(P.S. : sorry for my English, I'am French Smile | :) !)


Vincent Richard OMG | :OMG:
http://www.vincent-richard.net/
http://www.kisli.com/
GeneralAFX_EXT_CLASS Pin
Marc Richarme6-Jun-02 7:37
Marc Richarme6-Jun-02 7:37 
GeneralRe: AFX_EXT_CLASS Pin
Alexandru Savescu11-Jun-02 20:18
Alexandru Savescu11-Jun-02 20:18 
GeneralRe: AFX_EXT_CLASS Pin
Marc Richarme11-Jun-02 22:05
Marc Richarme11-Jun-02 22:05 

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.