Solved

DLL Questions Series, Part 4

Posted on 2000-02-17
10
260 Views
Last Modified: 2010-04-02
This is the fourth part of the series.
The Overview can be found at:
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=10294044  
The previous parts can be found at:
(1) http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=10294050  
(2) http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=10295050  
(3) http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=10296575

This question deals with classes inside the DLLs.

1) How do I export/import a class?
2) Can I export/import only specific portions of a class and what happens then? By "what happens" I mean "what if those functions I export call in turn those functions I don't?"
3) What if this class contains inline functions? How about if I want to export those inline functions?
0
Comment
Question by:Lescha
  • 5
  • 4
10 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 70 total points
Comment Utility
1.  A class cannot be exported (the concept doesn't really make sense), but the class's member functions and static data members can be exported.  These are exported just like any other function or global data, but declaring them with the _declspec(dllexport) specificiation  Like

class Exported
{
public:
   __declspec(dllexport) Exported(); // Exported constructor.
};

In addition you can declared all the member functions and static data members as exported by specifying the specifiection for the entire class like

__declspec(dllexport) class Exported
{
public:
   Exported(); // Exported constructor.
};

continues.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> 2) Can I export/import only specific
>> portions of a class and what happens
>> then? By "what happens" I mean "what
>> if those functions I export call in turn those
>> functions I don't?"
You can export on a function-by-function basis, as I suggested above.  But if a program using the DLL needs an unexported function, the program will not link.  Like if you declare a destructor for a class, but don't export it, the DLL will compiler and link fine, but any program that tries to create objects of this class using the DLL will not be able to link.

>> 3) What if this class contains inline functions?
If the implimentation for the inline function is "visible" to a translation unit, the function does not have to be declared exported.  Otherwise, there will be a link error in any program that tries to use the inline function.   for example

// In header file included into program.
class SomeClass
{
public:
   inline void Function1() {}; // Okay, implimentation specified
   inline void Function2(); // Okay implimentation in header below.
   inline __declspec(dllexport) Function3(); // Okay, exported from DLL.
   inline void Function4(); // Problem if used outside of DLL.
}
void SomeClass::Function2() {}; // Implimentation.
0
 
LVL 1

Author Comment

by:Lescha
Comment Utility
"nietod", welcome back!!! ;)

>> 1.  A class cannot be exported (the concept doesn't really make sense) (...)

As a matter of fact, the concept makes sense just fine. If I define a class inside a DLL, how will the user of the DLL know how this class is defined? (Actually, I guess it is really a continuation of Part 3 discussion).

---

Visual Studio 6.0 does not allow to define and declare inline functions in different files. The only way to declare an inline function that will not cause linker error sooner or later, is to put both the definition and declaration in the same file. If it is in a class, inline functions have to be defined inside the class body. This is quite annoying, but then again, as follows from your answer, there should be no problem exporting such a function from a DLL. ;)
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> As a matter of fact, the concept makes sense just fine
Only data and functions can be exported from a DLL, that makes sense right?  A class is a data type, like "int" or "double".  a data type cannot be exported, there isn't anything to export.  Its sort-of a concept, rather than something physical, so it can't be exported.  For example how would you export

struct  Point
{
   int x;
   int y;
};

There is no data to export.  There is no function's to export.  It is simply a concept, a way of looking at data.

>> Visual Studio 6.0 does not allow to define
>> and declare inline functions in different files.
I don't think that is true.  And the C++ standard certainly does not require that the definition of the inline function be included in each translation unit.  However if the definition is not included, there is a greater chance that the function won't be inlined.

>> If it is in a class, inline functions have to be defined
>> inside the class body
This definitely is not true.  You can define them outside as I I did above.
0
 
LVL 1

Author Comment

by:Lescha
Comment Utility
"nietod", according to C++ standard, you are correct. According to Visual Studio 6.0 Bugs, I am correct. If I declare a function as inline and then put its body elsewhere, the program won't link, or sometimes it will link and sometimes it won't. It's a known problem about VS and should've been corrected by now, but wasn't.

(cntd)
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:Lescha
Comment Utility
But to get back to the "makes sense" discussion: If you don't export the whole class as a class, how do you expect the users of your DLL to define the same class correctly? Or am I missing something?

I thought that to import functions belonging to a class, you define that class in your program and declare appropriate functions as "external". Is it done differently?
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> If I declare a function as inline and then
>> put its body elsewhere, the program won't
>> link, or sometimes it
>> will link and sometimes it won't.
It always worked fine for me, do you have an example where it doesn't?

>> If you don't export the whole class as a
>> class, how do you expect the users of
>> your DLL to define the same class
>> correctly?
they will have the entire class definition, including non-exported functions (they would be optional (unless they are virtual))  The EXE can use any exported function of the class, but not any non-exported function.  Use of a non-exported function will compile, but not link.   (Note, if the function is virtual, it is possible that it may be successfuly used even when it is not exported.  (Actually this will often be the case, but there may be times when this won't work, so I don't recommend it.)

>> I thought that to import functions belonging
>> to a class, you define that class in your
>> program and declareappropriate functions as
>> "external". Is it done differently?
use __declspec(dllimport) is better than "external".  But yes.  However, you may have functions in the class that are not imported (and exported) and as long as you don't use them from the EXE, you will be fine.
0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
<delurk>

A function declaration with an inline specifier declares an inline function.  The inline specifier indicates to the implementation that inline substitution of the function body at the point of call is to be preferred to the usual function call mechanism.  An implementation is not required to perform this inline substitution at the point of call; however, even if this inline substitution is omitted, the other rules for inline functions shall still be respected.

An inline function shall be defined in every translation unit in which it is used and shall have exactly the same definition in every case.  [Note: a call to the inline function may be encountered before its definition appears in the translation unit.]  If a function with external linkage is declared inline in one translation unit, it shall be declared inline in all translation units in which it appears; no diagnostic is required.  An inline function with external linkage shall have the same address in all translation units.  A static local variable in an extern inline function always refers to the same object.  A string literal in an extern inline function is the same object in different translation units.

<relurk>
0
 
LVL 1

Author Comment

by:Lescha
Comment Utility
"nietod" - thanks for the explanation. So, again, it all depends on the "politeness" of the creator of the DLL.

Also: maybe we use slightly different syntax or slightly different versions of Visual Studio. I don't have any examples of inline functions defined outside that don't work, because I've learned not to write any. But, since you say they work, and I know they didn't for me, maybe it's about some or other option or definition I didn't know about. Anyway, it doesn't really matter here and now.

"alexo" - I presume that was a quote? What did you try to tell us?
0
 
LVL 1

Author Comment

by:Lescha
Comment Utility
Thanks to all the Experts who participated in answering:

"nietod", "alexo".

Next part will be posted shortly.

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now