Sign in to follow this  

dll for functions? classes?

Recommended Posts

hey! I was playing around with DLL loading using both the static .lib and dynamic loading using the LoadLibrary. I noticed that i can access classes from the static loading, but i havent figured out a way how to do that for dynamic loading. or is there no such possibility? thx! Edwinz

Share this post

Link to post
Share on other sites
for dynamic loading, you theoretically cannot directly instantiate a class object that is defined in the DLL itself. Thats y the static link works: the compiler gets the class definition from the header file of the DLL/LIB.

there is one (half?) workaround. What you can do is define a stub declaration that mimics the class exposed in the DLL. This stub could be an exact duplicate of the original class header, just make sure the function names and parameter signatures and order of declaration is exact. However, you don't need to define the implementation for any of the functions. Just the declaration will do.

What you do next is create a function in the DLL that will create an instance of the particular class. The function should return a pointer to this DLL-local heap variable.

Include the stub header in ur application, load the DLL dynamically, get the address for the 'creator' function and use the function to get the actual object itself.

STUBCLASS *localPtr = Creator();
//NOTE: Since the local definition of the class does NOT define the
//implementation, you cannot instantiate it locally. That's why u use
//the DLL function to create the actual instance.

The stub class is just a means to:
1. Define a name to use for obtaining an object from the DLL
2. To allow your code to be aware of the functions that you can access via the object
This of course implies that you must be fully aware of the class structure, which may not be ideal if the DLL is meant for 3rd party use.

This is a roundabout way though, and may not be ideal for all situations. There is an article on GameDev somewhere about exposing classes in DLLs but i dun have the link right now. Another alternative is to use COM interface, which uses an advanced version of DLL-based allocation and reference counting.

Share this post

Link to post
Share on other sites
ah thx alot!
i din noe there was an article here.


[Edited by - edwinnie on June 22, 2005 4:25:52 AM]

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this