Solved

Using class in DLL's

Posted on 2008-06-23
9
544 Views
Last Modified: 2013-12-14
hi experts.
is is it possible to defining a class in a Dll project?
if possible, how is it?
0
Comment
Question by:tms110
  • 5
  • 3
9 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 21845140
>> is is it possible to defining a class in a Dll project?
Yes, no different from any other C++ code.

http://www.cplusplus.com/doc/tutorial/classes.html

class foo
{
   public:
   protected:
   private:
};

Is this what you mean or have I misunderstood your question?

0
 
LVL 40

Expert Comment

by:evilrix
ID: 21845154
Are you trying to figure out how to export a class?

http://msdn.microsoft.com/en-us/library/81h27t8c.aspx
0
 

Author Comment

by:tms110
ID: 21845772
i think you don't understand my mean
i am writing a Dll.
i want to declare a class in the body of the Dll code. (or any other method to using my class in my Dll)
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21845793
>> i want to declare a class in the body of the Dll code
Right, so why do you think you can't do this? Writing code in a DLL is no different from an executable or a static library. The only difference is that if you want to use it outside of the DLL you must export it.
0
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.

 

Author Comment

by:tms110
ID: 21846004
so how i can export a class and importing it in my application?

let me to describe a sample :
when i want to declare a function in my dll i must use this syntax:
extern "C" __declspec(dllexport) void myfunc(int * a){*a = - *a; }
and in my application i must import it with this syntax :
extern "C" __declspec(dllimport) void myfunc(int * a);

now suppose that i want to declare a class instead of the function "myfunc"
how syntax i must use for declaring a class?
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21846022
Did you not read the link I posted above, which describes this in detail?
http://msdn.microsoft.com/en-us/library/81h27t8c.aspx
#define DllExport   __declspec( dllexport )
 

class DllExport C {

   int i;

   virtual int func( void ) { return 1; }

};

Open in new window

0
 

Author Comment

by:tms110
ID: 21846098
yes i see
but it is only for exporting (the code that i use in my Dll)
what code i must use for importing this class? (in my application)
0
 
LVL 40

Accepted Solution

by:
evilrix earned 50 total points
ID: 21846160
You just import it rather than export it.

This seems to be a nice simple demo.
http://www.codeproject.com/KB/DLL/SimpleDll2.aspx
#define DllImport   __declspec( dllimport )

 

class DllImport C {

   int i;

   virtual int func( void ) { return 1; }

};

Open in new window

0
 
LVL 16

Expert Comment

by:George Tokas
ID: 21847517
A warning:
A class exported by a DLL to use by an executable requires static linking and NOT dynamic linking...
So:
BE SURE in executable code that the DLL IS PRESENT, to avoid surprizes.

George Tokas.
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

Suggested Solutions

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
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…

863 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

24 Experts available now in Live!

Get 1:1 Help Now