By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,098 Members | 1,881 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,098 IT Pros & Developers. It's quick & easy.

How to dynamically load an exported class?

P: n/a
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

Jul 30 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Jessica wrote:
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 Lennon
Bs.As.
ARG

Jul 30 '08 #2

P: n/a

"Jessica" <Je*****@discussions.microsoft.comha scritto nel messaggio
news:EE**********************************@microsof t.com...
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

Jul 30 '08 #3

P: n/a
Cholo Lennon wrote:
Jessica wrote:
>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.
BTW, to avoid problems with different heaps, you can also export a complementary
function to destroy the object.

Regards

--
Cholo Lennon
Bs.As.
ARG

Jul 30 '08 #4

P: n/a

"Cholo Lennon" <ch*********@hotmail.comha scritto nel messaggio
news:OB**************@TK2MSFTNGP05.phx.gbl...
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

Jul 30 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.