Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Importing Classes from DLLs

Posted on 2000-04-26
5
Medium Priority
?
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 150 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 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.

721 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