Solved

Importing Classes from DLLs

Posted on 2000-04-26
5
190 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

762 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

9 Experts available now in Live!

Get 1:1 Help Now