?
Solved

How to export a Class out of a DLL and Import it in my Progg using LoadLibrary()

Posted on 2003-03-11
3
Medium Priority
?
281 Views
Last Modified: 2010-04-01
All i want to do is to write my own dll, and dynamicly load a class out of this DLL in my programm. I dont want to link the lib staticly, so i want to load the class with LodLibraray and GetProcAddres.
Any suggestions?
0
Comment
Question by:PhunBringer
[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
3 Comments
 

Expert Comment

by:mabotta
ID: 8112505
Hi,
There is an article at CodeGuru (http://www.codeguru.com/misc/dynamic_dll_loading.shtml) that works just fine (I'm using it everywhere)
Good luck
0
 
LVL 12

Accepted Solution

by:
Salte earned 120 total points
ID: 8112739
One thing you should be aware of is that it is a bad idea to export a class interface as such:

class X {
private:
   ....
public:
   X(double x);
   ~X();
   ....
   int foo(const char * s);
   double bar(int x) const;
   static int baz(int y);
   virtual double gazonk(const char * s, int k) const;
};

Let's say you want to export this class and the functions above, then do export the following functions:

extern "C" {
   X * makeX(double x);
   void destroyX(X * p);
   int xfoo(X * p, const char * s);
   double xbar(const X * p, int x);
   int xbaz(int y);
   double xgazonk(const X * p, const char * s, int k);
};

If you use windows you will also use __declspec(dllexport) and declare all functions as __stdcall:

int __declspec(dllexport) __stdcall xfoo(...);

etc..

Then you implement those functions in the DLL:

X * makeX(double x) { return new X(x); }
void destroyX(X * p) { delete p; }
int xfoo(X * p, const char * s) { return p -> foo(s); }
double xbar(const X * p, int x) { return p -> bar(x); }
int xbaz(int y) { return X::baz(y); }

double xgazonk(const X * p, const char * s, int k)
{ return p -> gazonk(s,k); }

And then in the main program side you include that header file which declares the class and the exported functions but it does not define the class. If you define the class you will get references to the member functions if you happened to call them and you don't want that. If you want dynamic loading it means that the code itself must not reference any functions in the class explicitely, if it does you get unreferenced symbols when linking the program.

Therefore you shouldn't define the class, just use a declaration like:

class X;

Also, you don't want to use a header file that defines the functions.

Instead you do LoadLibrary to load the file, then do something like this:

int __stdcall (* pfoo)(X * p, const char * s) = (int __stdcall (*)(X *,const char *))GetProcAddress("xfoo");

And similarly for all the other functions.

Note that this is a lot easier if you typedef the functions:

typedef int __stdcall (* foo_type)(X *,const char *);

foo_type pfoo = (foo_type)GetProcAddress("xfoo");

Now you can call the class like this:

X * x = pmakeX(3.14); // make the object.
int y = pfoo(x,"Hello there");
double z = pbar(x,3);
int u = pbaz(9);
double u = pgazonk(x,"foo",7);
destroyX(x); // kill the object.

Note that the class cannot be defined in the program that uses it since that would run the danger that you might use a method from the class and then the symbol would be referenced and you can't link your executable.

Alf
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9481826
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: Salte

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

771 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