Skip to main content
GameDev.net gamedev.net
🔒 Locked

Dynamic DLL linking?

Started by Wyrframe Aug 4, 2002 at 11:50 PM 3 replies 850+ views
Original Post
Wyrframe
Wyrframe
Here''s a nice, clean, complex problem. I''ve got a selection of DLL files. Each of them contains a single class (with the same name as the DLL file itself), and all of them have the exact same interface. Now... I''ve got a program. How do I ask it to, at run-time, create an instance of one of these DLL classes, if I have only the filename of the DLL?
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
LessBread
LessBread
Use LoadLibrary to load the dll. Parse the filename from the path and use that to retrieve the class.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Wyrframe
Wyrframe
Oh, by the way... all the classes are derived from a basic base class, so don''t worry about fumbling to manage them all...

For example. Let''s say I''ve got a DIRECTX_ENGINE.DLL and an OPENGL_ENGINE.DLL and a SOFTWARE_ENGINE.DLL. They are all derived from the BASE_ENGINE class, which is built-in to the program.


LessBread, thanks for the quick reply!


Some further thought, to retrieve the class in the specific DLL, should all DLLs in question have a function along the lines of...

BASE_ENGINE* CreateMyObject();

... to get the object? Is that a good idea, or is there a better way that comes to your mind?

// Example
BASE_ENGINE* CreateMyObject() {
OPENGL_ENGINE* objRet = new OPENGL_ENGINE();
return objRet;
}
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
Wyrframe
Wyrframe
Once I''ve loaded the library, it will behave as though it were a part of the program, right? As far as scope goes?

Do I have to unload the DLL, or will it automatically unload when my program ends?
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
LessBread
LessBread
CreateMyObject() seems like a decent approach to me. To use GetProcAddress on it you''ll have to declare CreateMyObject as "Extern C" - in my experience, GetProcAddress doesn''t play nicely with decorated names. There''s a tut/article here at game dev regarding how to export classes from Dll''s. You might want to look for it and see how it relates to your project. There''s also a similar tut at flipcode - either in the COTD or TOTD archives.

Yes, it''s a good idea to match each call to LoadLibrary with a corresponding call to FreeLibrary. The OS keeps a reference count of loaded dlls and doesn''t unmap those that have outstanding references.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.