Classes in dll

Started by
20 comments, last by Dwiel 21 years, 5 months ago
I was wondering how to have a class in a dll and then load the dll or just link to it through the lib file created. When I just use functions, everything works OK because MSVC++ gives me a lib file I can include in main exe, but when I use a class, it doesn''t work this way. I get no lib file. What''s going on? I have my functions like this:
  
#ifndef _DLLTEST_H_
#define _DLLTEST_H_

#include <iostream.h>
#include <stdio.h>
#include <windows.h>

/*
extern "C" __declspec(dllexport) void NumberList();
extern "C" __declspec(dllexport) void LetterList();
*/

class Stuff
{
public:
	void NumberList();
	void LetterList();
};

#endif
  

  
#include "dll test2.h"

#define MAXMODULE 50

char module[MAXMODULE];
/*
extern "C" __declspec(dllexport)

void NumberList() {

//do stuff

}

extern "C" __declspec(dllexport)

void LetterList() {

//do stuff

}
*/

extern "C" __declspec(dllexport)
Stuff stuffer;

void Stuff::NumberList() {

//do stuff


}

void Stuff::LetterList() {

// do stuff


}
  
btw, this is directly from a tutorial on flipcode tazzel3d ~ dwiel
Advertisement
you should be able to find some related posts if you scroll back a few days in general programming.
My Bad... didn''t go back far enough.

Stupid search won''t work

tazzel3d ~` dwiel
I couldn''t find any posts that answered my question all the way back to October 19th.

would soeone be kind enough to post a very short exaple of what is in the cpp and h files?

thats all I need....

please...

maybe soething like what''s in my code above, but even shorter?

thanx a lot for whoever helps !

tazzel3d ~ dwiel
If you are using MSVC++ create a new dll project. In the wizard select the option “Export some symbols”. This will create some example code that exports a variable a function and a class. Hope this helps
Arrg!!
Hey,

I ran into a similar problem a while back. What you need to do is export and import the class like this:

in the DLL:

  class __declspec(dllexport) Stuff{public:	     void NumberList(); 	     void LetterList();};void Stuff::NumberList() {//do stuff}void Stuff::LetterList() {// do stuff}    


In the projects using the DLL:

  class __declspec(dllimport) Stuff{public:	     void NumberList(); 	     void LetterList();};...Stuff stuffer;...  


Do you get the general idea?

If not, I'll help you out some more.

Michael Bartman
Dark Omen Studios
Lead Engine Programmer


[edited by - TheBartman on November 7, 2002 1:47:07 AM]
Michael BartmanDark Omen StudiosLead Engine Programmer
Ok. Thanx for the help!

I can''t test it right now because I have to go to school, but I wiill when I get back.


Thanx a lot.

tazzel3d ~ dwiel
TheBartman''s solution will work fine, with one problem: you will only be able to use the class with the same compiler that created the DLL. If you are the only person likely to use it, that shouldn''t be a problem. However, if you want other compilers to be able to use your class, then you have to do the following:

To export the following class:

class Stuff
{
public:
void NumberList();
void LetterList();
};

Do the following:
(1) Create the following class:
class StuffInterface
{
public:
virtual void NumberList() = 0;
virtual void LetterList() = 0;
};

(2) Then write Stuff, making sure it is derived from StuffInterface.

(3) Create a function:
extern "C" __declspec(dllexport) StuffInterface* CreateStuff()
{
return new Stuff;
}

Now, when you want to use Stuff, instead use StuffInterface*

Reason: The compiler gives any C++ function, including member functions, a horrific decorated name, specific to the compiler you are using(E.g: Stuff::NumberList will become something like ?NumberList@Stuff@@FJFA3FZ ). Any function that you want to use from another compiler must be declared as extern "C". Member functions cannot be declared in this way. However, pure virtual functions in abstract classes are only referenced as in offset into the virtual function table. Hence, the name is immaterial.
quote:#include "dll test2.h"


And for the love of god or whoever you want to love. DO NOT USE SPACES IN INCLUDE FILENAMES.



- Zorak - Neat Fella.
Domine non secundum peccata nostra facias nobis
Hey again,

sbennett is correct, my solution may not work on all compilers, but I assumed you were using MSVC++. If you aren''t using Visual C++, then I might not be able to help you because I only know MSVC++. If you still need help, I will help you out just email me.

Michael Bartman
Dark Omen Studios
Lead Engine Programmer
Michael BartmanDark Omen StudiosLead Engine Programmer

This topic is closed to new replies.

Advertisement