473,320 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

exporting a C++ object in a DLL

I have a class that contains a std::map variable. I need to export the
class via a DLL. the class looks something like this:

class MyClass
{
public:
MyClass();
MyClass(const MyClass&);

private:
MyClass& operator=(const MyClass&);

typedef std::map<SomeKey, SomethingElseTreasureChest ;

TreasureChest m_treasures;
};
Jul 1 '07 #1
15 7349
you can either __declspec(dllexport) every member of the class that
you want to export, or __declspec(dllexport) the class definition.

In the "dll user" code you should declare the class with
__declspec(dllimport). There are standard ways to do that using
macros. See
http://www.codeproject.com/dll/SimpleDll2.asp

or
http://msdn2.microsoft.com/en-US/lib...4d(VS.80).aspx

Good Bye
QbProg

Jul 1 '07 #2


QbProg wrote:
you can either __declspec(dllexport) every member of the class that
you want to export, or __declspec(dllexport) the class definition.

In the "dll user" code you should declare the class with
__declspec(dllimport). There are standard ways to do that using
macros. See
http://www.codeproject.com/dll/SimpleDll2.asp

or
http://msdn2.microsoft.com/en-US/lib...4d(VS.80).aspx

Good Bye
QbProg
See: http://support.microsoft.com/kb/168958

Relevant text: 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.

The article was last reviewed in September 2005 - I wanted to know if it
is now possible to export std::map from a DLL
Jul 2 '07 #3

"Grey Alien" <gr**@andromeda.comwrote in message
news:uY*********************@bt.com...
>I have a class that contains a std::map variable. I need to export the
class via a DLL. the class looks something like this:
No you don't. Create an interface (class with pure virtual pointers),
derive the implementation from it, and share only the interface. You do
that by putting the interface definition in a public header file. No
__declspec(dllexport) statement is needed.

Exporting C++ classes is very bad news. __declspec(dllexport) should be
used only for 'extern "C"' functions.
>
class MyClass
{
public:
MyClass();
MyClass(const MyClass&);

private:
MyClass& operator=(const MyClass&);

typedef std::map<SomeKey, SomethingElseTreasureChest ;

TreasureChest m_treasures;
};


Jul 2 '07 #4


Ben Voigt [C++ MVP] wrote:
"Grey Alien" <gr**@andromeda.comwrote in message
news:uY*********************@bt.com...
>>I have a class that contains a std::map variable. I need to export the
class via a DLL. the class looks something like this:


No you don't.
Yes I do. I know what I want.

Create an interface (class with pure virtual pointers),
derive the implementation from it, and share only the interface. You do
that by putting the interface definition in a public header file. No
__declspec(dllexport) statement is needed.
You are assuming that the DLL will be consumed by a C++ client. That is
not the case. Besides, how can you possibly use code in another
compilation unit if you don't link into it (either statically or
dynamically).?
>
Exporting C++ classes is very bad news. __declspec(dllexport) should be
used only for 'extern "C"' functions.
Not necessarily true. In my case, I am taking care of the C++ "name
mangling" - through various policies and procedures (didn't include info
because it is orthogonal to my original question).
>
>>class MyClass
{
public:
MyClass();
MyClass(const MyClass&);

private:
MyClass& operator=(const MyClass&);

typedef std::map<SomeKey, SomethingElseTreasureChest ;

TreasureChest m_treasures;
};



Jul 2 '07 #5
Grey Alien wrote:
You are assuming that the DLL will be consumed by a C++ client. That is
not the case. Besides, how can you possibly use code in another
compilation unit if you don't link into it (either statically or
dynamically).?
Grey:

You want to export a class containing an std::map and it will not be
consumed by a C++ client?

Ben's advice might have seemed a little fierce, but basically I agree
with it. You will save yourself a lot of future headaches if you design
your class with a pure virtual interface (and methods using simple types).

--
David Wilkinson
Visual C++ MVP
Jul 2 '07 #6


David Wilkinson wrote:
Grey Alien wrote:
>You are assuming that the DLL will be consumed by a C++ client. That
is not the case. Besides, how can you possibly use code in another
compilation unit if you don't link into it (either statically or
dynamically).?


Grey:

You want to export a class containing an std::map and it will not be
consumed by a C++ client?

Ben's advice might have seemed a little fierce, but basically I agree
with it. You will save yourself a lot of future headaches if you design
your class with a pure virtual interface (and methods using simple types).
Dave:

That may be the case, but the fact remains that the class whose methods
are invoked needs to contain a map member variable. At the moment, I'm
getting this annoying warning:

warning C4251: 'theManager::m_signalMap' : class 'std::map<_Kty,_Ty>'
needs to have dll-interface to be used by clients of class 'theManager'

- which seems to imply that std::map can be exported - which contradicts
the (outdated) article on the MSN site. So do I heed the warning and
export the data type (preferred) or do ignore it (with potentially
disastrous consequences)?
Jul 2 '07 #7
Never mind, I found the solution here:
http://caseys.kcfilms.com/2006/01/us...ross_dll_b.php
Jul 2 '07 #8

"Grey Alien" <gr**@andromeda.comwrote in message
news:1t******************************@bt.com...
>

Ben Voigt [C++ MVP] wrote:
>"Grey Alien" <gr**@andromeda.comwrote in message
news:uY*********************@bt.com...
>>>I have a class that contains a std::map variable. I need to export the
class via a DLL. the class looks something like this:


No you don't.

Yes I do. I know what I want.

Create an interface (class with pure virtual pointers),
>derive the implementation from it, and share only the interface. You do
that by putting the interface definition in a public header file. No
__declspec(dllexport) statement is needed.

You are assuming that the DLL will be consumed by a C++ client. That is
not the case. Besides, how can you possibly use code in another
compilation unit if you don't link into it (either statically or
dynamically).?
If your client isn't C++, the prohibition on __declspec(dllexport) of
classes becomes absolutely instead of just a really good idea.

What do you mean by "don't link into it"? You are creating a DLL, right?
"dynamically linked library" If you create an interface, then the compiler
links the interface v-table to the implementations. The client only needs
the interface definition.

Jul 4 '07 #9

"Grey Alien" <gr**@andromeda.comwrote in message
news:pK******************************@bt.com...
>

David Wilkinson wrote:
>Grey Alien wrote:
>>You are assuming that the DLL will be consumed by a C++ client. That is
not the case. Besides, how can you possibly use code in another
compilation unit if you don't link into it (either statically or
dynamically).?


Grey:

You want to export a class containing an std::map and it will not be
consumed by a C++ client?

Ben's advice might have seemed a little fierce, but basically I agree
with it. You will save yourself a lot of future headaches if you design
your class with a pure virtual interface (and methods using simple
types).

Dave:

That may be the case, but the fact remains that the class whose methods
are invoked needs to contain a map member variable. At the moment, I'm
getting this annoying warning:

warning C4251: 'theManager::m_signalMap' : class 'std::map<_Kty,_Ty>'
needs to have dll-interface to be used by clients of class 'theManager'
- which seems to imply that std::map can be exported - which contradicts
the (outdated) article on the MSN site. So do I heed the warning and
export the data type (preferred) or do ignore it (with potentially
disastrous consequences)?
There is no contradiction, both are correct. std::map should not be
exported, and because of that, it should not be used by clients of
"theManager". Since it is a private implementation detail, why would that
present a problem? Clients aren't using it.

Jul 4 '07 #10


Ben Voigt [C++ MVP] wrote:
>
"Grey Alien" <gr**@andromeda.comwrote in message
news:1t******************************@bt.com...
>>

Ben Voigt [C++ MVP] wrote:
>>"Grey Alien" <gr**@andromeda.comwrote in message
news:uY*********************@bt.com...

I have a class that contains a std::map variable. I need to export
the class via a DLL. the class looks something like this:

No you don't.


Yes I do. I know what I want.

Create an interface (class with pure virtual pointers),
>>derive the implementation from it, and share only the interface. You
do that by putting the interface definition in a public header file.
No __declspec(dllexport) statement is needed.
Sounds like you're describing the Pimpl pattern. I haven't used it
myself before, though it does sound like a good idea (I can't use it in
my current project though - because legacy code already exports classes).

However, as a matter of interest, if only the interface is specified
(via pure virtuals in a header) - surely, it means that EACH child class
will have to implement the same functionality allover again. The idea
for exporting classes was for code reuse - which you seem to be losing,
using the Pimpl pattern you describe. Am I missing something?
Jul 4 '07 #11
Grey Alien wrote:
Sounds like you're describing the Pimpl pattern. I haven't used it
myself before, though it does sound like a good idea (I can't use it in
my current project though - because legacy code already exports classes).
Grey:

Pimpl and Abstract Base Class (ABC) are not the same thing. A pimpl
class is not a pure interface.

--
David Wilkinson
Visual C++ MVP
Jul 4 '07 #12

"Grey Alien" <gr**@andromeda.comwrote in message
news:rJ******************************@bt.com...
>

Ben Voigt [C++ MVP] wrote:
>>
"Grey Alien" <gr**@andromeda.comwrote in message
news:1t******************************@bt.com...
>>>

Ben Voigt [C++ MVP] wrote:

"Grey Alien" <gr**@andromeda.comwrote in message
news:uY*********************@bt.com...

I have a class that contains a std::map variable. I need to export the
class via a DLL. the class looks something like this:

No you don't.
Yes I do. I know what I want.

Create an interface (class with pure virtual pointers),

derive the implementation from it, and share only the interface. You
do that by putting the interface definition in a public header file.
No __declspec(dllexport) statement is needed.

Sounds like you're describing the Pimpl pattern. I haven't used it myself
before, though it does sound like a good idea (I can't use it in my
current project though - because legacy code already exports classes).

However, as a matter of interest, if only the interface is specified (via
pure virtuals in a header) - surely, it means that EACH child class will
have to implement the same functionality allover again. The idea for
exporting classes was for code reuse - which you seem to be losing, using
the Pimpl pattern you describe. Am I missing something?
Yup. The interface can be implemented in a base class which provides the
implementation, and each derived (or child if you prefer) class inherits.
Only the interface cannot have any implementation, but it can participate in
a full hierarchy.

Jul 4 '07 #13


Ben Voigt [C++ MVP] wrote:
>
>However, as a matter of interest, if only the interface is specified
(via pure virtuals in a header) - surely, it means that EACH child
class will have to implement the same functionality allover again. The
idea for exporting classes was for code reuse - which you seem to be
losing, using the Pimpl pattern you describe. Am I missing something?


Yup. The interface can be implemented in a base class which provides
the implementation, and each derived (or child if you prefer) class
inherits. Only the interface cannot have any implementation, but it can
participate in a full hierarchy.
But if the interface is implemented in a base class, then that base
class needs to be exported, so that its derived classes can correctly
link to the approriate compilation units - so we end up in the same
situation, i.e having to export the (base) class.

Unless I am misunderstanding you, your approach ensures that if we have
a Widget class (say), we will make its interface available as an ABC,
and then every module that needs to use a Widget class implement the
interface. This is not scaleable - as N different classes (in different
library modules) that delegate to the Widget class will have to write N
implementations for the Widget class. The duplicity of effort that
entails (not to mention the increased scope for errors/nightmare code
maintenance etc) is so ridiculous that I have to assume that I have
misunderstood you - please clarify.
Jul 5 '07 #14

"Grey Alien" <gr**@andromeda.comwrote in message
news:1M*********************@bt.com...
>

Ben Voigt [C++ MVP] wrote:
>>
>>However, as a matter of interest, if only the interface is specified
(via pure virtuals in a header) - surely, it means that EACH child class
will have to implement the same functionality allover again. The idea
for exporting classes was for code reuse - which you seem to be losing,
using the Pimpl pattern you describe. Am I missing something?


Yup. The interface can be implemented in a base class which provides the
implementation, and each derived (or child if you prefer) class inherits.
Only the interface cannot have any implementation, but it can participate
in a full hierarchy.

But if the interface is implemented in a base class, then that base class
needs to be exported, so that its derived classes can correctly link to
the approriate compilation units - so we end up in the same situation, i.e
having to export the (base) class.

Unless I am misunderstanding you, your approach ensures that if we have a
Widget class (say), we will make its interface available as an ABC, and
then every module that needs to use a Widget class implement the
interface. This is not scaleable - as N different classes (in different
library modules) that delegate to the Widget class will have to write N
implementations for the Widget class. The duplicity of effort that entails
(not to mention the increased scope for errors/nightmare code maintenance
etc) is so ridiculous that I have to assume that I have misunderstood
you - please clarify.
The module that you think you want to dllexport a class from...

Instead you declare an interface (class with only pure virtual functions) in
its public header file. Include this from the clients. Since the members
are pure virtual, the header has the complete definition and no
declspec(dllexport) is needed.

Now in that module providing the class, you create a declspec(dllexport)
extern "C" function (or several) for creating instances, called a factory
method. The instances are of a class that implements the interface. If you
want polymorphism between multiple implementations, multiple classes can
provide an implementation by inheriting the interface. These multiple
classes can each implement the interface independently, or they can inherit
from each other to share an interface.

Jul 5 '07 #15
Grey Alien wrote:
Additionally, is there a name for the mechanism/methodolgy you describe
?. I would be grateful for any links on where to read up more about this
- and maybe convince myself to refactor the existing code to use this
methodology. Tks
Grey:

I call this method "Do It Yourself COM."

--
David Wilkinson
Visual C++ MVP
Jul 5 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: chetan | last post by:
Hi , myself Chetan Is There anybody could help me ? I am working on the project in c++ ,, I am in great confusion that should I export c++ member functions OR methods to create objects of...
0
by: Otis Hunter | last post by:
I have been fighting with this for days and your expert help is needed! Below is the code I am executing which results with "Object doesn't support this property or method". The error is occuring...
2
by: Regnab | last post by:
I've got my code working so that it'll count the number of columns in the table and move across (eg Range A-P and then range Q-W). Problem is when I get to the end of the single letters and get...
8
by: chippy | last post by:
Hi, I've a VB script that creates a Access object in a word doc. Here is the full script. It works for all but the Export. Which always fails with a 3011 error. If I do the same in Access as a...
4
by: Hitesh | last post by:
Hi, I have three datagrid control on my aspx page and one export to excel button, i want to export all the 3 datagrids contents in one excel file. how can i achive that? -- Thanks Hitesh
2
by: pmud | last post by:
Hi, I am exporting data from an EDITABLE DATA GRID EXCEL. But the 1st column in data grid is Edit Column. I want to display all columns in Excel except for the Edit column. The following...
6
by: Steve Richter | last post by:
I am getting error in a vbscript: ActiveX component cant create object: Excel.Application. The vbscript code is: Dim objExcel Set objExcel = CreateObject("Excel.Application") I am pretty...
2
by: bienwell | last post by:
Hi, I have a question about exporting data from datagrid control into Excel file in ASP.NET. On my Web page, I have a linkbutton "Export data". This link will call a Sub Function to perform...
3
by: Bilgehan.Balban | last post by:
Hi, My observation was that a function with `inline' qualifier has file scope in C++ and it's symbol is not exported. Contrary to this, in C an `inline' function symbol is exported, unless it...
2
by: atlbearcat | last post by:
Here's one that's been bugging me for about a week now... I have a form that allows users to filter records, simple enough. But I want to give them the option to export the filtered records to...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.