Click here to Skip to main content
Email Password   helpLost your password?

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.)

 

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralVB.NET dll in MFC?
G_T
4:46 18 Oct '07  
Hi everyone,

This is just a quick question... I am relatively new to programing with DLLs, and i have seen/done, MFC dll, and exported it to a .NET application.

I now wanted to go the other way...

I have a .NET DLL and I want to use it in a MFC program.

Is this possible?

How do I do it? Does anyone know where I can get an example of this?

Thanks


GeneralDo we need those defines in vc8?
Swinefeaster
17:03 30 Jan '06  
Is there any way to skip doing this #define mumbo-jumbo? It becomes rather tedious if you want to move classes from dll to dll and there's a lot of classes... Any new such features in Visual Studio 2005?

Thanks!

swine

[b]yte your digital photos with [ae]phid [p]hotokeeper - www.aephid.com.

GeneralRe: Do we need those defines in vc8?
John M. Drescher
18:22 30 Jan '06  
Swinefeaster wrote:
It becomes rather tedious if you want to move classes from dll to dll and there's a lot of classes


When I need to do this I use a full project search and replace addin which does all the work for me...

As for vc8 I am not sure if this has changed, however I highly doubt that it has because this is a windows dll specific behavior that you specify which functions / classes in your project that you want to export . By not explicitly saying that you want to export a function / class is makes that function or class private to anyone using the dll.

John
GeneralHow to import?
arkopII
7:19 9 Apr '05  
Could anyone explain how to import classes which have been exported this way?

Thanks.
GeneralRe: How to import?
John M. Drescher
18:24 30 Jan '06  
Do not define _MYLIB_DLLAPI_ and include the common header.

John
GeneralDeserialization in MFC
ET1984inside
4:42 21 Oct '04  
I dont know how to write a programm to deserialize serialized file in new .txt file.
GeneralCan't add a reference to the specified file.
Kay PEE
20:09 31 Aug '04  
Dear all,
I tried to make a DLL using win32 Dynamic Linked Library and MFC both.
In both of the cases they worked fine in other MFC applications(.EXE) but when I tried to import it to VB using "Reference Dialog Box" it gives ERROR "Can't add a reference to the specified file.". Eagarly waiting for discussion of the Issue.

Thanks (to all who will reply and wont reply)Laugh

Geniuses doesn't do different thing but does the thing differently.
GeneralRe: Can't add a reference to the specified file.
Anonymous
11:51 28 Feb '05  
Anything on this yet?
GeneralRe: Can't add a reference to the specified file.
Anonymous
23:59 18 Oct '05  
I gets the same error. Have your resolve it?
Thanks

______________
Hip Hop Directo
GeneralRe: Can't add a reference to the specified file.
Jeffrey Walton
6:48 5 Aug '07  
Hi,

VB is COM oriented. At minimum, you will need IDL file added, and a ATL COM Object added to the project. This kind of takes you out of the realm of MFC Dlls.

Jeff
Generalincluding functions of a DLL into ObjectARX
smartgms1
17:16 13 Jan '04  
hey to all,

i need to include the functions of a Dll (made in VC++)into another Dll(which gives as output ObjectARX) again made in VC++. basically, help me out for how to include a DLL file into another in VC++ itself...i will be really greatful for ur help.

george.
GeneralHow to port extension dll for VB use?
inglooi
20:34 10 Nov '03  
Hi,
I'm currently using an modified extension dll for wireless communications (modified from network development kit 2.0 on this website) and wish to convert the dll for use in embedded vb 3.0. However, this dll has multiple classes and some functions have the same naming in different classes.

Must I use a normal dll and what's the difference from the extension dll?
How can I port the functions in the respective classes to vb for use?
Also, is it possible for me to port structure classes over to VB?( e.g. struct NDKMessage in this case..)
THanx



IngLooi
Singapore
GeneralHow Can I Export a DLL ActiveX VB ?
ziadin
1:53 8 Aug '03  


Hello

I have created a DLL ActiveX in VB, and generate de DLL ,but when a try to use the GetProcAddress function in VC I can't find my function...

Is there any equivalent sentence in VB to the extern "C" __declspec(dllexport)

Thanks a lot

Ziadin
Generalhi
Anonymous
19:03 26 Mar '03  
hi...i like to see the thread
GeneralHow can one use VC++ class through VB?
Anonymous
20:04 9 Feb '03  
How we can use exported classes (from VC) in VB? Plz tell the solution
GeneralProblems importing DLL extension Classes
Anonymous
0:22 22 Aug '02  
We are trying - unsuccessfully - to import classes form a DLL (build by others developpers)

They provided us the corresponding *.lib.

Can anybody tell me how we can ensure that the DLL do export this classes?Eek!

Thanks.

CMK
GeneralRe: Problems importing DLL extension Classes
Kay PEE
20:24 31 Aug '04  
Dear,

Try opening that lib using Microsoft Visual Studio 6.0 Tool OLE View for viewing exported objects and functions.Smile
General#pragma comment(lib, "mylib.lib") doesn't work
Anonymous
23:19 17 Jul '02  
#pragma comment(lib, "mylib.lib") doesn't work on the dll itself, it gives link error:
LINK : fatal error LNK1104: cannot open file "amir.lib"

And the App doesn't recognize the exporting class,too.
Confused
GeneralRe: #pragma comment(lib, "mylib.lib") doesn't work
Kay PEE
20:20 31 Aug '04  
Dear Amir,
try putting this

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_

and in your headerfile

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

you can always replace the word "_MYLIB" and .lib name

reply if it works Red faced enjoy
GeneralExporting a class that contains STL string
Steffen.Knoll
0:52 22 Jun '02  
Hi,

I have a problem when I try to export a class which contains a template of the STL.
The class is defined like this:

#ifdef DLLTEST_EXPORTS
#define DLLTEST_API __declspec(dllexport)
#else
#define DLLTEST_API __declspec(dllimport)
#endif

#include
using namespace std;

class DLLTEST_API CDlltest {
private:
string a;
public:
CDlltest(void);
};


It works, but i always get the compiler warning "C4251".

Can anybody help me?
Please excuse my terrilbe English:Smile

regards
Steffen Knoll
GeneralExporting a class that contains STL string
Steffen.Knoll
0:51 22 Jun '02  
Hi,

I have a porblem when I Try to export a class which contains a template of the STL.
The class is defined like this:

#ifdef DLLTEST_EXPORTS
#define DLLTEST_API __declspec(dllexport)
#else
#define DLLTEST_API __declspec(dllimport)
#endif

#include
using namespace std;

class DLLTEST_API CDlltest {
private:
string a;
public:
CDlltest(void);
};


It works, but i always get the compiler warning "C4251".

Can anybody help me?
Please excuse my terrilbe English:Smile

regards
Steffen Knoll
GeneralRe: Exporting a class that contains STL string
Neville Franks
2:12 15 Jan '03  
Yes I had the same problem and was kindly refered to:
Microsoft Knowledge Base Article - 168958
HOWTO: Exporting STL Components Inside & Outside of a Class
http://support.microsoft.com/default.aspx?scid=KB;en-us;q168958[^]

Neville Franks, Author of ED for Windows. www.getsoft.com
Make money with our new Affilate program
GeneralRe: Exporting a class that contains STL string
Anonymous
16:31 15 Jul '04  
Thanks Nev, that was a very helpful article. Tragically, however, right in the middle of describing the rather convoluted process, it states:

"Some STL classes contain nested classes. These classes can not be exported. ... The only STL container that can currently be exported is vector. The other containers (that is, map, set, queue, list, deque) all contain nested classes and cannot be exported."


- Daz
GeneralRe: Exporting a class that contains STL string
John M. Drescher
18:29 30 Jan '06  
I have used vector, map, string, set and list in this manner with no problems as long as both dll code and program code are using the same exact version of STL. In my case I use stlport 5. I believe the warning is because you can not gaurantee that the client program is using the same version of stl unless it is your own program...

John
GeneralRe: Exporting a class that contains STL string
tzafrirben
2:17 2 Nov '04  
Daz - I've faced same problem as you did, but did found the solution in the article.

"The following STL classes, and various global operators and functions that operate on these classes, are already exported by the C Runtime DLL".
What you need to do is to change the project runtime library. That can be done using the VC.NET by clicking Project -> Properties -> C\C++ -> Code Generation and changing the value of the run Time library to "Multi-Thread DLL [/MD]"


ben


Last Updated 16 Dec 1999 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010