Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 2003-03-11
Medium Priority
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?
Question by:PhunBringer

Expert Comment

ID: 8112505
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
LVL 12

Accepted Solution

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 {
   X(double 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(...);


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.


Expert Comment

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.


EE Cleanup Volunteer

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

571 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