Solved

Loading class by "<name>"

Posted on 1997-04-03
4
219 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 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.

895 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

11 Experts available now in Live!

Get 1:1 Help Now