Jump to content
  • Advertisement
Sign in to follow this  
beebs1

Using DLL files with classes

This topic is 4055 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hiya, I'm just trying to get some information on using C++ classes with Windows DLL files, as I'm planning to use them for the engine for an upcoming project. I have used DLLs before, but only exported functions and not classes, and all of the online tutorials I have found only show how to export functions. Is there a way of storing concrete classes inside a DLL, and using them via interfaces in a client application? My current design already uses factory functions and abstract classes, so my thinking is to export a function such as bool CreateRenderer(IRenderer **ppRenderer); from the DLL, and then call this in the client application to create the object. I'm not in a position test this out right now, so I was wondering if anyone could offer any explanations or suggestions on how it can be done? Many thanks for any help :)

Share this post


Link to post
Share on other sites
Advertisement
I've done this, it worked well for me [smile]

My interface is Object *createObject(); returning NULL on errors, where Object is some pure virtual class. I also needed a destroyObject( Object * ) function too as DLL heap allocation is somewhat separate from the executables, at least under windows.

This makes it very easy to make a nice auto-cleanup interface, by making the function like:

boost::shared_ptr<Object> objectFactory()
{
return boost::shared_ptr<Object>(createObject(),&destroyObject);
}


Biggest headache was the SC++L. You cannot pass std::containers or std::string to a DLL easily, I had to make interface functions which took raw char arrays etc. For instance I don't imagine the above function would be guarenteed to work correctly if it was inside the DLL (it may work at first, but what if you were to build your library with a different version of boost, or a different version of the SC++L?).

If you are interested the project is available for checkout under subversion. I wouldn't recommend anyone actually *use* it (its kind of crap as it evolved only to do exactly what I needed it to do at the time, but whatever right? [grin]), but it does demonstrate the general idea.

Share this post


Link to post
Share on other sites
Thanks for the reply rip-off!

Regarding your destroyObject function, I take it an object created by the DLL will only be able to be deleted by the DLL itself, as the heaps are seperate. That would make sense, as it's run as an inproc server I believe.

Also, if I could grab a copy of your code off subversion that would be very helpful!

Thanks again.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!