Solved

Loading class by "<name>"

Posted on 1997-04-03
4
217 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now