Click here to Skip to main content
11,437,816 members (31,461 online)
Click here to Skip to main content

Exporting C++ Classes from an MFC Extension DLL

, 15 Dec 1999
Rate this:
Please Sign up or sign in to vote.
How to simplify importing and exporting classes from an extension DLL
<!-- Article Starts -->

Exporting C++ classes from extension DLLs and importing those classes into applications can be a little confusing at times.  This article discusses one of many ways to simplify this.  Also discussed is a technique to ensure that your DLL's .LIB file is automatically linked into any application (or other DLL) using your DLL, avoiding the need to alter your project link settings.

When building an extension DLL, you want the compiler/linker to export selected C++ classes, but when building your application you want to import those classes.

Traditionally, this has been done by using the AFX_CLASS_EXPORT and AFX_CLASS_IMPORT defines (defined in afxv_dll.h). Swapping these #defines in and out depending on whether you're building the DLL itself or building an application (or another DLL) which uses your exported classes.

If we look at how  AFX_CLASS_EXPORT and AFX_CLASS_IMPORT are defined in afxv_dll.h we see the following.

#define AFX_CLASS_EXPORT __declspec(dllexport)
#define AFX_CLASS_IMPORT __declspec(dllimport)

So, when exporting our classes from our DLL we want the class declarations from the DLL to look like this:-

class __declspec(dllexport) CMyClass : public CObject
{
	...
}

And, when importing our C++ classes into our application we want the class declarations from the DLL to look like this:-

class __declspec(dllimport) CMyClass : public CObject
{
	...
}

OK, so here's how I do things.

In the stdafx.h file for the export DLL, include two #defines at the bottom of the file like this:-

#define _MYLIB_DLLAPI_
#define _MYLIB_NOAUTOLIB_

Now, in the main header file for your DLL, say mylib.h (the main 'point of entry' header for your DLL that you will include in you application later), add the following at the top:-

// The following will ensure that we are exporting our C++ classes when 
// building the DLL and importing the classes when build an application 
// using this DLL.

#ifdef _MYLIB_DLLAPI_
    #define MYLIB_DLLAPI  __declspec( dllexport )
#else
    #define MYLIB_DLLAPI  __declspec( dllimport )
#endif

// The following will ensure that when building an application (or another
// DLL) using this DLL, the appropriate .LIB file will automatically be used
// when linking.

#ifndef _MYLIB_NOAUTOLIB_
#ifdef _DEBUG
#pragma comment(lib, "mylibd.lib")
#else
#pragma comment(lib, "mylib.lib")
#endif
#endif

Now, just declare all the C++ classes you want exported from the DLL like this:-
(Note: Any C++ classes not declared with MYLIB_DLLAPI will not be exported from the DLL)

class MYLIB_DLLAPI CMyClass : public CObject
{
	...
}

So, how does it work? 

When building your DLL, _MYLIB_DLLAPI_ is defined in the DLL's stdafx.h file, so MYLIB_DLLAPI is then defined as __declspec( dllexport ) and your C++ classes will be exported.

When building your application, _MYLIB_DLLAPI_ isn't defined, so MYLIB_DLLAPI will be defined as __declspec( dllimport ) and your classes will be imported.

The other nifty part is the _MYLIB_NOAUTOLIB_.  If _MYLIB_NOAUTOLIB_ isn't defined, (i.e. when building your application), an entry like #pragma comment(lib, "mylibd.lib") appears which tells the linker to automatically link in your DLL's .LIB file.  Hence, there's no need to add the .LIB file to the Object/library modules section in your application project link settings (something I invariable forgot to do!).

The above is basically a 'set and forget' technique.  All you'll ever need to do to use you extension DLL is just include it's header in your application, and all the ugly class export/import stuff is sorted for you.

(I can't remember where I picked up this technique originally, but full credit to it's originator as it's proved invaluable over the years.)

 

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

Share

About the Author

Steve Driessens
Web Developer
Australia Australia
Sole programmer for a small Brisbane (Australia) based software company.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Braga4215-May-13 13:21
memberBraga4215-May-13 13:21 
QuestionVB.NET dll in MFC? Pin
G_T18-Oct-07 4:46
memberG_T18-Oct-07 4:46 
QuestionDo we need those defines in vc8? Pin
Swinefeaster30-Jan-06 17:03
memberSwinefeaster30-Jan-06 17:03 
AnswerRe: Do we need those defines in vc8? Pin
John M. Drescher30-Jan-06 18:22
memberJohn M. Drescher30-Jan-06 18:22 
QuestionHow to import? Pin
arkopII9-Apr-05 7:19
memberarkopII9-Apr-05 7:19 
AnswerRe: How to import? Pin
John M. Drescher30-Jan-06 18:24
memberJohn M. Drescher30-Jan-06 18:24 
GeneralDeserialization in MFC Pin
ET1984inside21-Oct-04 4:42
sussET1984inside21-Oct-04 4:42 
GeneralCan't add a reference to the specified file. Pin
Kay PEE31-Aug-04 20:09
sussKay PEE31-Aug-04 20:09 
GeneralRe: Can't add a reference to the specified file. Pin
Anonymous28-Feb-05 11:51
sussAnonymous28-Feb-05 11:51 
GeneralRe: Can't add a reference to the specified file. Pin
Anonymous18-Oct-05 23:59
sussAnonymous18-Oct-05 23:59 
I gets the same error. Have your resolve it?
Thanks

______________
Hip Hop Directo
GeneralRe: Can't add a reference to the specified file. Pin
Jeffrey Walton5-Aug-07 6:48
memberJeffrey Walton5-Aug-07 6:48 
Generalincluding functions of a DLL into ObjectARX Pin
smartgms113-Jan-04 17:16
susssmartgms113-Jan-04 17:16 
QuestionHow to port extension dll for VB use? Pin
inglooi10-Nov-03 20:34
memberinglooi10-Nov-03 20:34 
QuestionHow Can I Export a DLL ActiveX VB ? Pin
ziadin8-Aug-03 1:53
memberziadin8-Aug-03 1:53 
Generalhi Pin
Anonymous26-Mar-03 19:03
sussAnonymous26-Mar-03 19:03 
QuestionHow can one use VC++ class through VB? Pin
Anonymous9-Feb-03 20:04
sussAnonymous9-Feb-03 20:04 
GeneralProblems importing DLL extension Classes Pin
Anonymous22-Aug-02 0:22
sussAnonymous22-Aug-02 0:22 
GeneralRe: Problems importing DLL extension Classes Pin
Kay PEE31-Aug-04 20:24
sussKay PEE31-Aug-04 20:24 
General#pragma comment(lib, "mylib.lib") doesn't work Pin
Anonymous17-Jul-02 23:19
sussAnonymous17-Jul-02 23:19 
GeneralRe: #pragma comment(lib, &quot;mylib.lib&quot;) doesn't work Pin
Kay PEE31-Aug-04 20:20
sussKay PEE31-Aug-04 20:20 
GeneralExporting a class that contains STL string Pin
Steffen.Knoll22-Jun-02 0:52
memberSteffen.Knoll22-Jun-02 0:52 
GeneralExporting a class that contains STL string Pin
Steffen.Knoll22-Jun-02 0:51
memberSteffen.Knoll22-Jun-02 0:51 
GeneralRe: Exporting a class that contains STL string Pin
Neville Franks15-Jan-03 2:12
memberNeville Franks15-Jan-03 2:12 
GeneralRe: Exporting a class that contains STL string Pin
Anonymous15-Jul-04 16:31
sussAnonymous15-Jul-04 16:31 
GeneralRe: Exporting a class that contains STL string Pin
John M. Drescher30-Jan-06 18:29
memberJohn M. Drescher30-Jan-06 18:29 
GeneralRe: Exporting a class that contains STL string Pin
tzafrirben2-Nov-04 2:17
membertzafrirben2-Nov-04 2:17 
General#pragma comment(lib, "mylib.lib") doesn't work Pin
Amir Salzberg20-Jun-02 10:57
memberAmir Salzberg20-Jun-02 10:57 
GeneralRe: #pragma comment(lib, "mylib.lib") doesn't work Pin
saravanan_embed9-Jan-07 19:36
membersaravanan_embed9-Jan-07 19:36 
QuestionHow to export a class contents virtual functions in Regular DLL? Pin
Anonymous12-May-02 20:21
memberAnonymous12-May-02 20:21 
QuestionHow can I export non-MFC classes? Pin
lyifan21-Oct-01 11:05
memberlyifan21-Oct-01 11:05 
AnswerRe: How can I export non-MFC classes? Pin
Michael P Butler21-Oct-01 11:38
memberMichael P Butler21-Oct-01 11:38 
AnswerRe: How can I export non-MFC classes? Pin
Anonymous1-May-02 6:16
memberAnonymous1-May-02 6:16 
Generalporting win 16 applications to win 32 Pin
nk_james23-Apr-01 22:54
membernk_james23-Apr-01 22:54 
GeneralCreating dll from VB vs VC++ Pin
anonymous2-Mar-01 8:20
memberanonymous2-Mar-01 8:20 
GeneralRe: Creating dll from VB vs VC++ Pin
Anonymous7-Feb-02 10:00
memberAnonymous7-Feb-02 10:00 
GeneralRe: Creating dll from VB vs VC++ Pin
Cheetah19-May-02 19:07
memberCheetah19-May-02 19:07 
GeneralRe: Creating dll from VB vs VC++ Pin
Cheetah19-May-02 19:10
memberCheetah19-May-02 19:10 
GeneralExporting dialogs Pin
Daniel Hellsson11-Feb-01 23:08
memberDaniel Hellsson11-Feb-01 23:08 
GeneralRe: Exporting dialogs Pin
Marek Hrncir20-May-02 5:18
memberMarek Hrncir20-May-02 5:18 
GeneralCreating dll for VB Pin
vvy24-Dec-00 11:57
membervvy24-Dec-00 11:57 
GeneralRe: Creating dll for VB Pin
Anonymous22-Aug-02 0:28
sussAnonymous22-Aug-02 0:28 
GeneralStandard macro already exist Pin
B Destrez18-Jan-00 0:43
sussB Destrez18-Jan-00 0:43 
GeneralRe: Standard macro already exist Pin
Frederic Ouellet26-Apr-01 8:51
memberFrederic Ouellet26-Apr-01 8:51 
GeneralRe: Standard macro already exist Pin
Paulo2-May-01 19:33
memberPaulo2-May-01 19:33 

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
Web01 | 2.8.150506.1 | Last Updated 16 Dec 1999
Article Copyright 1999 by Steve Driessens
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid