Dynamic load of classes

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?
LVL 3
CesarGonAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
jkrConnect With a Mentor Commented:
See http://www.codeguru.com/win32/dyndllclass.shtml - this artivle (and the followups) discuss this issue...
0
 
nietodCommented:
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

continues
0
 
nietodCommented:
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)();

***********Dll.cpp************
#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
{
public:
         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;
};

***************EXE.cpp*****************
WinMain()
{
   HINSTANCE PathLib = Loadlibrary("some path");
   MakePtrTyp MakeProc = (MakePtrTyp) GetProcAddress(PathLib,"MakePaths");

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

   PathsPtr->DoSomethingWith(1,2);
   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.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
CesarGonAuthor Commented:
Thanks to nietod as well!
0
 
jkrCommented:
Sorry Todd, typing an URL you know by heart simply is faster ;-)
0
 
nietodCommented:
Yeah, you get the points and I get the "honerable mention".  I'm going to loose my ranking any minute now...
0
 
jkrCommented:
Well, I fear so, too ;-)
0
 
CesarGonAuthor Commented:
In fact, answers from you both jkr and nietod have been very valuable. May you share your points? 8-)
0
 
nietodCommented:
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.  :-)  )
0
All Courses

From novice to tech pro — start learning today.