Solved

Using class in DLL's

Posted on 2008-06-23
9
540 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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:tms110
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

744 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

15 Experts available now in Live!

Get 1:1 Help Now