Click here to Skip to main content
Licence 
First Posted 5 Jun 2002
Views 186,243
Downloads 3,993
Bookmarked 48 times

Exporting C++ classes without using MFC extension DLL

By Tanzim Husain | 5 Jun 2002
This article describes the process of exporting regular C++ classes without using MFC extension DLLs
10 votes, 28.6%
1
1 vote, 2.9%
2
2 votes, 5.7%
3
9 votes, 25.7%
4
13 votes, 37.1%
5
3.67/5 - 48 votes
μ 3.13, σa 2.95 [?]

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


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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionI got lilnker error LN2019 PinmemberVivek_Mendse19:52 10 Nov '11  
QuestionReally - that simple? PinmemberVaclav_Sal9:48 4 Feb '11  
GeneralUsing dll Pinmembers_t_r_e_a_m_e_r2:19 18 Nov '07  
QuestionHow can I use a class exported from a DLL by using LoadLibrary function Pinmemberzhoujinjun08585:22 27 Aug '07  
AnswerRe: How can I use a class exported from a DLL by using LoadLibrary function Pinmemberafriza17:44 2 Dec '08  
GeneralGreat! Pinmemberdocrob118:48 25 Jan '07  
Generaldelete cuases access violation Pinmembertom cruz3:35 18 Aug '05  
GeneralRe: delete cuases access violation PinmemberIgen18:14 9 Mar '09  
Generalqn PinsussFrom_Nepal9:00 6 Jan '05  
GeneralLoad at runtime PinmemberVerifier5:30 16 Apr '04  
GeneralRe: Load at runtime PinmemberThatsAlok23:32 20 Apr '06  
QuestionHow to create DllTest.dll and DllTest.lib in VC7.0 PinmemberSctt H. Chang11:11 31 Mar '04  
AnswerRe: How to create DllTest.dll and DllTest.lib in VC7.0 PinsussAnonymous0:26 18 Apr '04  
Questionis it mean that regular DLL can replace extension DLL every time? Pinmemberdalink7:00 6 Mar '04  
GeneralA slight correction and enhancement PinmemberDavid Pritchard8:31 29 May '03  
Questionhow to insert a class from MFC to C# Pinmembermosgeorge4:49 13 Apr '03  
AnswerRe: how to insert a class from MFC to C# PinmemberTanzim Husain21:54 23 Apr '03  
AnswerRe: how to insert a class from MFC to C# PinmemberTakeru Koushirou22:31 27 Jun '04  
GeneralRe: how to insert a class from MFC to C# Pinmembercharnos7:49 13 Aug '04  
Generalany Info about Proxy DLL (Trojan DLL) Pinmemberbfadi11:24 5 Mar '03  
GeneralRe: any Info about Proxy DLL (Trojan DLL) PinmemberTanzim Husain22:14 23 Apr '03  
GeneralRe: any Info about Proxy DLL (Trojan DLL) PinmembergypsySUN19:11 17 Oct '03  
on 5 Mar '03 bfadi wrote:
I know that I should use #pragma comment(linker, "/export:DoSomething=DllImpl.ActuallyDoSomething")
But I have not any explication for it

 

This is a little late perhaps,
but
try here for #pragma comment[^]
see here for linker options[^]
 
Incase the links don't work. I cut the relevant bits from MSDN:
 
#pragma comment( comment-type [, commentstring] )
Places a linker option in the object file. You can use this comment-type to specify a linker option instead of placing the option on the Link tab of the Project Settings dialog box. For example, you can specify the /include option to force the inclusion of a symbol:
 
#pragma comment(linker, "/include:__mySymbol")
 

 
Syntax
/EXPORT:entryname[=internalname]
With this option, you can export a function from your program so that other programs can call the function. You can also export data. Exports are usually defined in a DLL.
 
The entryname is the name of the function or data item as it is to be used by the calling program. You can optionally specify the internalname as the function known in the defining program; by default, internalname is the same as entryname.
 

So you can see that by redefining the internalname to be a call to a function exported by another dll, you can have your wrapper dll export the interface of the other dll(or part of it).
GeneralRe: any Info about Proxy DLL (Trojan DLL) Pinmemberbfadi21:42 18 Oct '03  
GeneralRe: any Info about Proxy DLL (Trojan DLL) PinmembergypsySUN6:27 21 Oct '03  
GeneralRe: any Info about Proxy DLL (Trojan DLL) Pinmemberbfadi20:57 21 Oct '03  

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.

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