How to dynamically load an exported class?

  • Thread starter Thread starter Jessica
  • Start date Start date
J

Jessica

Hi All,

I know that I can use the GetProcAddress to get the proc address of a global
exported function from a WIn32 dll.

But if I have an exported class in the dll, is there a way to dynamically
load this dll and get an instance of this class? (No static linking with the
lib file) I am currently thinking about having global exported functions
which internally calls the member functions of the class's single instance.
These global exported dunctions can be accessed through GetProcAddress anyway.

Can anyone please throw some light into this?

TIA,
Jessy
 
But if I have an exported class in the dll, is there a way to dynamically
load this dll and get an instance of this class? (No static linking with
the
lib file) I am currently thinking about having global exported functions
which internally calls the member functions of the class's single
instance.
These global exported dunctions can be accessed through GetProcAddress
anyway.

Can anyone please throw some light into this?

What you propose (i.e. a pure-C wrapping interface to your C++ class) is an
option.

However, you may also consider exporting a factory function, which creates
an instance of your class, so you can use GetProcAddress() to get a pointer
to the factory function, and call the factory function to create an instance
of the class.

i.e. you can export a function like this from the DLL:

CMyClass * BuildMyClass();

and call GetProcAddress() to get its address after loading the DLL with
LoadLibrary(), and then call that function to get a CMyClass pointer.

HTH,
Giovanni
 
Jessica said:
Hi All,

I know that I can use the GetProcAddress to get the proc address of a
global exported function from a WIn32 dll.

But if I have an exported class in the dll, is there a way to
dynamically load this dll and get an instance of this class? (No
static linking with the lib file) I am currently thinking about
having global exported functions which internally calls the member
functions of the class's single instance. These global exported
dunctions can be accessed through GetProcAddress anyway.

Can anyone please throw some light into this?


The usual way is to have a factory method, an exported C function used to create
the desired class (something like COM DllGetClassObject). Your client only needs
the factory method signature and the class definition.

Regards
 
Cholo said:
The usual way is to have a factory method, an exported C function
used to create the desired class (something like COM
DllGetClassObject). Your client only needs the factory method
signature and the class definition.

BTW, to avoid problems with different heaps, you can also export a complementary
function to destroy the object.

Regards
 
BTW, to avoid problems with different heaps, you can also export a
complementary
function to destroy the object.

Or use a reference counting technique like COM AddRef/Release (and when
reference counter becomes 0, the object destroys itself).

Giovanni
 
Back
Top