Solved

Importing Classes from DLLs

Posted on 2000-04-26
5
203 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

920 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

18 Experts available now in Live!

Get 1:1 Help Now