Solved

DLL Questions Series, Part 4

Posted on 2000-02-17
10
294 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
ID: 2532282
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
ID: 2532311
>> 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
ID: 2532564
"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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 22

Expert Comment

by:nietod
ID: 2532627
>> 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
ID: 2534472
"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
 
LVL 1

Author Comment

by:Lescha
ID: 2534484
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
ID: 2534621
>> 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
ID: 2535354
<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
ID: 2535923
"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
ID: 2535950
Thanks to all the Experts who participated in answering:

"nietod", "alexo".

Next part will be posted shortly.

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

821 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