Dynamic load of classes

Posted on 2000-03-06
Last Modified: 2010-04-02
I can write and compile a Win32 DLL in C++ and then dynamically load the library and use its exported functions by using LoadLibrary() and GetProcAddress() API calls. But can I similarly load and use classes and methods?
Question by:CesarGon
  • 4
  • 3
  • 2
LVL 86

Accepted Solution

jkr earned 100 total points
ID: 2588729
See - this artivle (and the followups) discuss this issue...
LVL 22

Expert Comment

ID: 2588740
Opps.  jkr beat me.  here's what I had.

Yes and no.

There are some problems.  How do you create an object, if yoiu don't have the class's definition and constructor?   Once you have the obkject, how can you call its member functions.  Not all member procedure calls can be invoked using a pointer returned from GetProcAddress.   This stuff is circumventable, but it gets really messy.  fortunately there is a good solution

LVL 22

Expert Comment

ID: 2588747
And here is an example I was going to post.  It'll give you a 2nd point of view.

************ Base.h*******
// Note no export directive.  Not of any value for you
// since you are linking at run-time.
class Paths {
 public :
         Paths() ;
        // note all functions are virtual, because linking
        // is to be done through the VTBL  No need for
        // GetProcAddress().
         virtual ~Paths() = 0;;
         virtual int DoSometingWith (int x, int y)  = 0;
         vitual char* SomethingElse (int v, char *w) = 0 ;

// Type defintition for the "factory" function.
typedef Path * (*MakePtrTyp)();

#inlude "Base.h"

// Derived concrete class.  Functions can be added here that
// are not virtual, so long as they are not to be used by the EXE.
// Any functions the EXE uses must be virtual.
class DerivedPaths : public Paths
         DerivedPaths() ;
         virtual ~Paths()  {};
         virtual int DoSometingWith (int x, int y)  { return x+y; };
         vitual char* SomethingElse (int v, char *w)  { return w+v; } ;

// The factory function.  Uses extern "C" so that it is easier for
// the EXE to get using GetProcAddress();
extern "C" Paths *MakePaths()
    return new DerivedPaths;

   HINSTANCE PathLib = Loadlibrary("some path");
   MakePtrTyp MakeProc = (MakePtrTyp) GetProcAddress(PathLib,"MakePaths");

   Paths *PathsPtr = (*makeProc)(); // Make a paths object.

   delete PathsPtr;

A couple more words of explanation.  

The only time you need to use GetProcAddress() with this scheme is to find the factory function in the DLL that creates the objects.  All the other functions are handled through virtual functions calls.  That is obviously much more convenient than lots of GetProcAddress() calls!

The DLL allocates objects using new and the EXE eventually deletes it with delete.  For this to work the two must share a common heap, this means you must use the DLL version of the run-time library.  (a good idea in any case.)  Another alternative is for the DLL to have a function that does the delete, but I would do the other.

The EXE can load different DLLs that define different derived classes.  So if the derived class has different versions or changes with time, the EXE does not need to be recompiled.

This approach is in many ways the C++ version of COM.
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!


Author Comment

ID: 2588781
Thanks to nietod as well!
LVL 86

Expert Comment

ID: 2588920
Sorry Todd, typing an URL you know by heart simply is faster ;-)
LVL 22

Expert Comment

ID: 2589584
Yeah, you get the points and I get the "honerable mention".  I'm going to loose my ranking any minute now...
LVL 86

Expert Comment

ID: 2589833
Well, I fear so, too ;-)

Author Comment

ID: 2590883
In fact, answers from you both jkr and nietod have been very valuable. May you share your points? 8-)
LVL 22

Expert Comment

ID: 2591461
Its not really possible or even really necessary.   (I beat hin to the answer more than he beats me, so I don't want to start a sharing policy.  :-)  )

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Issue accessing member variable of atomic class 16 116
Setting nameservers after res_init fails doing res_query 2 98
Safe conversion? 4 68
DCT of  2D array using fftw in c++ 9 41
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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…
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 member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

777 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