Click here to Skip to main content
Click here to Skip to main content

Exporting C++ classes without using MFC extension DLL

By , 5 Jun 2002
 

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

About the Author

Tanzim Husain
Web Developer
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionI got lilnker error LN2019memberVivek_Mendse10 Nov '11 - 18:52 
error LNK2019: unresolved external symbol "public: void __thiscall Test::funa(void)" (?funa@Test@@QAEXXZ) referenced in function _main
1>C:\VISUAL_STUDIO_2010\test_exe\Debug\test_exe.exe : fatal error LNK1120: 1 unresolved externals
 

code :
Dll header Export_Class.h
 
class __declspec(dllexport) Test
{
 
public:
    Test(){}
    ~Test(){}
    void funa(void);
};
 
dll cpp
 

void Test::funa()
{
    cout<<"hello world \n";
}
 

cpp of exe
 
#include<iostream>
#include"Export_Class.h"
using namespace std;
 
int main()
{
    Test t;
    t.funa();
    return 0;
}

QuestionReally - that simple?memberVaclav_Sal4 Feb '11 - 8:48 
You are using incorrect assumptions (per MSDN) when the DLL is used in conjunction with MFC application.
I say assumptions because you did not define that, therefore, your article is little misleading.
Per MSDN the macro usage depends on definition or no definitions of _AFXDLL and _AFXEXT.
I just followed you advice and replaced the macro AFX_EXT_CLASS with __declspec(dllexport) only to get the compiler   not to recognize MFC CWnd base class(!) I have only _AFXDLL defined, so it makes perfect sense.
GeneralUsing dllmembers_t_r_e_a_m_e_r18 Nov '07 - 1:19 
Actually it is very simple to include this dll into your project. After compiling the dll, Visual Studio creates one lib file beside dll file. Include just header of dll class you want to use into your project, and link it with .lib file. And there is no need for load library Smile | :) .
QuestionHow can I use a class exported from a DLL by using LoadLibrary functionmemberzhoujinjun085827 Aug '07 - 4:22 
I want to use exported C++ classes from a Dll.You know,by using the three functions LoadLibrary ,GetProcAddress,FreeLibrary ,we can use a Dll explicitly .But GetProcAddress function is only used for getting the address of the specified exported DLL function.
Can I use exported-classes by using those functions.
My English is very poor,so I don't know whether you can understand what i mean!
Anywhere ,I will thank you for giving us a so great experience!
 
I hope to have a nice trip!

AnswerRe: How can I use a class exported from a DLL by using LoadLibrary functionmemberafriza2 Dec '08 - 16:44 
If you want to use the class while loading the library *explicitly*, you can write a function that create the class instance and return it.
e.g.
 
extern "C" __declspec(dllexport) MyClassInDLL* CreateMyClassInDLL() {
   return new MyClassInDLL;
}

GeneralGreat!memberdocrob125 Jan '07 - 17:48 
Wink | ;) I suppose I'm too much of a beginner, but I really gained a lot from your article. Thanks for presenting a simple solution to a common problem.
 
docrob1
Generaldelete cuases access violationmembertom cruz18 Aug '05 - 2:35 
I hav e done the same thing in borland builder but
I get an access violation when I use delete.
if I declare the object as so in a function
 
cwidget cw;
 
and just let it fall throught the function. it destroys
itself with no error. why does not delete work to destroy
the object.

 
tom cruz
GeneralRe: delete cuases access violationmemberIgen19 Mar '09 - 7:14 
tom cruz wrote:
and just let it fall throught the function. it destroys
itself with no error.

 
Is it means to let the garbage collector handle the leak?
 
Anyway the standard solution which I red everywhere is to make a Release() function that has delete *this; if I remember right. And all you have to do is to call xxx->Release();. DirectX classes is works the same as far as I know.
GeneralqnsussFrom_Nepal6 Jan '05 - 8:00 
after exporting classes how do I access it's member function from another module by using run time dynamic linking (LoadLibrary, GetProcAddress...)?
GeneralLoad at runtimememberVerifier16 Apr '04 - 4:30 
How can I load a DLL with classes at runtime?

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 6 Jun 2002
Article Copyright 2002 by Tanzim Husain
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid