Solved

Importing Classes from DLLs

Posted on 2000-04-26
5
221 Views
Last Modified: 2008-03-17
Hi all!
I want to define a class and its members in a DLL and then export the class so that I may use the class in another application. I have exported the class using __declspec(dllexport) macro in a regular DLL(not extension DLL - the class is not derived from any of the MFC classes).
Now, I link the the DLL in a different application thru its .lib file and import the class using the __declspec(dllimport) macro. The catch is, I am not able to create objects of the imported class unless I dont include the header file from the dll in which the class is defined. How do I create objects of a class exported from a DLL without using the .h file? - u see, i only want to ship the DLL and the LIB file for the exported class.

-thanx
0
Comment
Question by:pratish
  • 2
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 2750971
class.h:
#ifdef _DLL // define this when building the DLL
#define __DYNLINK __declspec( dllexport)
#else
#define __DYNLINK __declspec( dllimport)
#endif

class __DYNLINK CMyClass: public CWhatever
{
public:
  CMyClass();
  virtual ~CMyClass();

// ...
};


Some elaboration:
class __declspec(dllexport) CMyClass: public CWhatever
created a class that is exported from a DLL
whereas
class __declspec(dllimport) CMyClass: public CWhatever
created a class that is imported from a DLL

As you certainly don't want to use 2 different header files,
#ifdef _DLL // define this when building the DLL
#define __DYNLINK __declspec( dllexport)
#else
#define __DYNLINK __declspec( dllimport)
#endif

creates an export modifier '__DYNLINK' that expands to the correct expression depending on what your're building. You could use it e.g. this way:

dll.cpp
#define _DLL
#include "class.h"

so the class will be exported.

When building the application, simple use
app.cpp

#include "class.h"

LONG WINAPI WinMain( ...)
{
//...
}


Please ask if you need more information or some clarification!




0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 2751095
jkr, aren't you missing the point, the question says without shipping the header file.

the only way to do this that I know of without supplying the header file is to use a .def file.  This is extremely unpleasant for C++ classes because you must put mangled names into the .def file.

You should maybe think about why you don't want to ship the .h file.  It is by far the easiest thing to do, using the approach jkr describes.

You can make use of the Proxy class technique to just provide a public interface and hide all implementation details if you need to.
0
 

Author Comment

by:pratish
ID: 2754780
Maybe u r correct Jason,

I'll give the .def approach a try(if thats the only way out) and if things go very ugly, then i think i'll settle down with jkr's approach and ship the header file as well.

Thanx
0
 

Author Comment

by:pratish
ID: 2754781
Maybe u r correct Jason,

I'll give the .def approach a try(if thats the only way out) and if things go very ugly, then i think i'll settle down with jkr's approach and ship the header file as well. Lemme know if there's any other way out.

Thanx
0
 
LVL 86

Expert Comment

by:jkr
ID: 2755099
>>i only want to ship the DLL and the
>>LIB file for the exported class

Using a .def file only makes the import names available - but if you actually want to access methods of these classes, you *have* to ship the header file, as the parameter lists etc. have to be known to the calling program as well... a C++ DLL without a header file is almost unusable...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

679 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