Solved

Using class in DLL's

Posted on 2008-06-23
9
551 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
[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
  • 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
Technology Partners: 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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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.

752 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