Archived

This topic is now archived and is closed to further replies.

wazoo69

C++ dll's??

Recommended Posts

Okay I knew how to do this ONCE (long time ago in a galaxy far far away...) I just want to create a dll that exports a c++ class...I knew there was some trick to them that''s not immidiately obvious, so I was wondering if anyone out there remembered what it was.. I know how to define the class in the DLL, just not how to dynamically call it and use it in a normal win32 program.. help?? thanks, Wazoo

Share this post


Link to post
Share on other sites
are you using VC?
you can do the following:

in class header, define your class as __declspec(dllexport) when compiling the DLL and __declspec(dllimport) when compiling the app.
then you compile the DLL, and link to the import library it creates (usually dllname.LIB).

i''m pretty sure that''s all you need to do.

crazy166
some people think i''m crazy, some people know i am

Share this post


Link to post
Share on other sites
Here is another suggestion. Located here on Gamedev is a tutorial about taking C++ style classes and creating Interfaces for them. You create the Class normally but add a few functions that you "export" from the dll that just return a handle to the class of your choice. The tutorial explains it very well, and I have a working program demonstrating this issue. If you like, email me and I will send you the code.

Kevin =)

-----------------------------
kevin@mayday-anime.com
http://www.dainteractiveonline.com

Share this post


Link to post
Share on other sites
If you''re statically linking a DLL, then you want something like

class MY_API myclass
{
}

where when you are compiling the dll MY_API is #defined as declspec(dllexport), otherwise have it #defined as declspec(dllimport).

Runtime (late) linking of classes is not quite so easy. The best way I found to do it (I link my renderer at run-time for GL/D3D swapping) is to have a virtual base class that exposes all the methods you need. Then subclass the base class in your dll, while exposing C functions that you can GetProcAddress that return pointers to a new instance of your child class.

You can wrap the ''factory'' C functions in various ways to make it nice - my engine searches all dlls, and keeps a record of what drivers they expose.

HTH,

Henry

Share this post


Link to post
Share on other sites
cool. Thanks for all the responses guys...

Yeah Kevin was kind enough to send me some tiny sample code demonstrating this, as I had wanted to do what Gingko suggested
and load up the required D3D or OGL dll during runtime...

Thanks for the feedback, I think between all the messages in this post, I''ll be able to figure out how to do this as neatly as I can...

thanks everybody again for the help...was wondering if I should have posted this question to a C++ forum, but you guys pulled through!

Why the hell do I have to try and be fancy????

Wazoo

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You could always check out the code i''ve posted on www.flipcode.com

It''s todays code of the day

r

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You could always check out the code i''ve posted on www.flipcode.com

It''s todays code of the day

Share this post


Link to post
Share on other sites