Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using class in DLL's

Posted on 2008-06-23
9
Medium Priority
?
555 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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 150 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

782 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