?
Solved

Loading class by "<name>"

Posted on 1997-04-03
4
Medium Priority
?
227 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
[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
  • 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 400 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

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!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

770 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