Solved

Loading class by "<name>"

Posted on 1997-04-03
4
221 Views
Last Modified: 2010-04-10
I am about to write an ISAPI extension, and can't work out how to dynamically load an instance of a named class whose name is discovered at run-time. All my loaded classes will be derived from the same base class, and I will only be calling the base class methods, so I only need to have the base header (I think) in my 'loader'. All my loaded instances will be implementing these base business methods differently. But I get stuck in thinking this through, as there seems to be no way of saying Declare_Dyncreate("MyClass"), only Declare_Dyncreate(MyClass), which means my loader has to know all the possibles beforehand and have a ginormous switch, and adding a new derived class will mean regenerating my loader.
Am I being naive here in thinking I can dynamically load instances of a common base class, or is there a well-known design pattern that solves this problem?
0
Comment
Question by:dlacey
  • 2
  • 2
4 Comments
 

Author Comment

by:dlacey
ID: 1162558
The title of the question origianlly said
Loading class by ".lt name .gt"
but of course the HTML interpreted the .lt and ,gt symbols!
So the question isn't as stupid as the header makes it look.
0
 
LVL 4

Accepted Solution

by:
jos010697 earned 200 total points
ID: 1162559
I don't know what C++ implementation you're using, but
you might have a look at dlopen(), dlsym() and dlclose().

I think (I may be wrong here) that the MS equivalents
are LoadLibrary(), GetProcAddress() and FreeLibrary().

Their usage is quite simple actually:

first open a library: void* lib= dlopen("my_lib");

get a pointer to the symbol:

<symbol_type>* sym= dlsym(lib, "symbol_name");

and use 'sym' as a pointer to the object ...

when you're done using the lib, simply call dlclose(lib)
to get rid of it ...

In your particular case, 'sym' has to be symbol refering to
the class stuff you want to load ...

kind regards,

Jos aka jos@and.nl
0
 

Author Comment

by:dlacey
ID: 1162560
Yes, I'm in the MS camp, so all I have is GetProcAddress(), which is Get Procedure Address, so all I can point to is a procedure (I believe!)
Are you saying that
hModule = LoadLibrary("MyClass")
hMeth1 = GetProcAddress(hModule, "Method1")
then I can
hModule->hMeth1(arg1, arg2, ...)
That seems so weird ...
0
 
LVL 4

Expert Comment

by:jos010697
ID: 1162561
Almost; you have to write a little wrapper function for every
class you want to load dynamically. GetProcAddress is
used to get the address of this little wrapper, while the
wrapper itself takes care of loading the actual class stuff.
The name of the wrapper function serves as a name for
the associated class then ...

BTW, a class is not an object in C++, so you can never
actually 'load' it, using GetProcAddress (or even dlsym()
which is capable of retrieving the adress of _any_ object).

kind regards,

Jos

0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

831 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