Click here to Skip to main content
15,891,184 members
Articles / Desktop Programming / MFC
Article

Exporting C++ Classes from an MFC Extension DLL

Rate me:
Please Sign up or sign in to vote.
4.48/5 (20 votes)
15 Dec 1999 318.5K   71   44
How to simplify importing and exporting classes from an extension DLL

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


Written By
Web Developer
Australia Australia
Sole programmer for a small Brisbane (Australia) based software company.

Comments and Discussions

 
AnswerRe: How can I export non-MFC classes? Pin
1-May-02 5:16
suss1-May-02 5:16 
Generalporting win 16 applications to win 32 Pin
23-Apr-01 21:54
suss23-Apr-01 21:54 
GeneralCreating dll from VB vs VC++ Pin
2-Mar-01 7:20
suss2-Mar-01 7:20 
GeneralRe: Creating dll from VB vs VC++ Pin
7-Feb-02 9:00
suss7-Feb-02 9:00 
GeneralRe: Creating dll from VB vs VC++ Pin
19-May-02 18:07
suss19-May-02 18:07 
GeneralRe: Creating dll from VB vs VC++ Pin
19-May-02 18:10
suss19-May-02 18:10 
GeneralExporting dialogs Pin
Daniel Hellsson11-Feb-01 22:08
Daniel Hellsson11-Feb-01 22:08 
GeneralRe: Exporting dialogs Pin
20-May-02 4:18
suss20-May-02 4:18 
I think I will help you. This is the way I use. I have 5 dlls in one of my projects. I developed this class which helps me to manage the states of all dlls which pointers are chained in pModuleState->m_libraryList (see below).
Have a fun. Marek.

/********************************************************
CX2E_RESOURCES_DLL class
===================

Used for managing module handles. Just call
X2E_RESOURCES_DLL_INIT(pModule, pDLL) in your
DllMain(), then call the X2E_DECLARE_RESOURCE_DLL
macro ONCE (!) somewhere in your source file.

Then put X2E_MANAGE_DLL_STATE; as very first statement
in your function/method to ensure your dll will be
set as the first dll pointer in the module chain
which is searched through while looking for the
resource. This macro just define the CX2E_RESOURCES_DLL
object (with calling its constructor). At the end
of function/method destructor of this object is called.

*********************************************************/

class CX2E_RESOURCES_DLL
{
CDynLinkLibrary *m_pOldHead;

public:
static AFX_EXTENSION_MODULE *m_pM;
static CDynLinkLibrary *m_pMyLib;

CX2E_RESOURCES_DLL()
{
// make sure initialized
if (!m_pM || !m_pM->bInitialized)
return;

ASSERT(m_pM->hModule != NULL);
AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
CDynLinkLibrary* pDLL = pModuleState->m_libraryList.GetHead();
m_pOldHead = pDLL;
if(pDLL == m_pMyLib)
return;

while(pDLL != NULL)
{
if(pDLL == m_pMyLib)
{
pModuleState->m_libraryList.Remove(pDLL);
pModuleState->m_libraryList.AddHead(pDLL);
pDLL = NULL;
}
else
pDLL = pModuleState->m_libraryList.GetNext(pDLL);
}
}
~CX2E_RESOURCES_DLL()
{
// make sure initialized
if (!m_pM || !m_pM->bInitialized)
return;

ASSERT(m_pM->hModule != NULL);
AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
CDynLinkLibrary* pDLL = pModuleState->m_libraryList.GetHead();
if(m_pOldHead == pDLL)
return;

while(pDLL != NULL)
{
if(pDLL == m_pOldHead)
{
pModuleState->m_libraryList.Remove(pDLL);
pModuleState->m_libraryList.AddHead(pDLL);
pDLL = NULL;
}
else
pDLL = pModuleState->m_libraryList.GetNext(pDLL);
}
}
};


//You have to call it from your DllMain function in dwReason == DLL_PROCESS_ATTACH part
AFX_INLINE void X2E_RESOURCES_DLL_INIT(AFX_EXTENSION_MODULE * pModule, CDynLinkLibrary * pDLL)
{
ASSERT(pModule != NULL);
ASSERT(pDLL != NULL);
CX2E_RESOURCES_DLL::m_pM = pModule;
CX2E_RESOURCES_DLL::m_pMyLib = pDLL;
}

// You have to add this macro to one of your .dll source code
// if you want to use X2E_MANAGE_DLL_STATE macro
#define X2E_DECLARE_RESOURCE_DLL \
AFX_EXTENSION_MODULE * CX2E_RESOURCES_DLL::m_pM = NULL; \
CDynLinkLibrary * CX2E_RESOURCES_DLL::m_pMyLib = NULL;

// You have to add this macro to the beginning of each function
// where you do some loads from the library resources to ensure
// framework will first look at resources in your library!
#define X2E_MANAGE_DLL_STATE \
CX2E_RESOURCES_DLL _x2eResourcesDLL

// To get current module hangle call this function
AFX_INLINE HINSTANCE GetCurrentModuleHandle()
{
X2E_MANAGE_DLL_STATE;
AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
CDynLinkLibrary* pDLL = pModuleState->m_libraryList.GetHead();
return pDLL->m_hModule;
}
GeneralCreating dll for VB Pin
24-Dec-00 10:57
suss24-Dec-00 10:57 
GeneralRe: Creating dll for VB Pin
Anonymous21-Aug-02 23:28
Anonymous21-Aug-02 23:28 
GeneralStandard macro already exist Pin
B Destrez17-Jan-00 23:43
sussB Destrez17-Jan-00 23:43 
GeneralRe: Standard macro already exist Pin
26-Apr-01 7:51
suss26-Apr-01 7:51 
GeneralRe: Standard macro already exist Pin
Paulo2-May-01 18:33
Paulo2-May-01 18:33 

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.