Link to home
Start Free TrialLog in
Avatar of dlacey
dlacey

asked on

Loading class by "<name>"

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?
Avatar of dlacey
dlacey

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of jos010697
jos010697

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dlacey

ASKER

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 ...
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