Solved

Dynamic load of classes

Posted on 2000-03-06
9
201 Views
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?
0
Comment
Question by:CesarGon
[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
  • 4
  • 3
  • 2
9 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 2588729
See http://www.codeguru.com/win32/dyndllclass.shtml - this artivle (and the followups) discuss this issue...
0
 
LVL 22

Expert Comment

by:nietod
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

continues
0
 
LVL 22

Expert Comment

by:nietod
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)();

***********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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:CesarGon
ID: 2588781
Thanks to nietod as well!
0
 
LVL 86

Expert Comment

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

Expert Comment

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

Expert Comment

by:jkr
ID: 2589833
Well, I fear so, too ;-)
0
 
LVL 3

Author Comment

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

Expert Comment

by:nietod
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.  :-)  )
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

752 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