Using class in DLL's

hi experts.
is is it possible to defining a class in a Dll project?
if possible, how is it?
tms110Asked:
Who is Participating?
 
evilrixConnect With a Mentor Senior Software Engineer (Avast)Commented:
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
 
evilrixSenior Software Engineer (Avast)Commented:
>> 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
 
evilrixSenior Software Engineer (Avast)Commented:
Are you trying to figure out how to export a class?

http://msdn.microsoft.com/en-us/library/81h27t8c.aspx
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
tms110Author Commented:
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
 
evilrixSenior Software Engineer (Avast)Commented:
>> 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
 
tms110Author Commented:
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
 
evilrixSenior Software Engineer (Avast)Commented:
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
 
tms110Author Commented:
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
 
George TokasCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.