How to YOU export functions in a C++ dll

Started by
8 comments, last by Mulligan 19 years, 8 months ago
I'm looking for a convient standardized way to exporting functions from a dll. I was looking at the Quake II source code and saw that they essentially created a structure in the dll and made all of it's members function pointers to the functions it was exposing. I really really like that idea. Are there better ways? Does anyone use someother method that works well for them?
Advertisement
not a DLL, but a linux shared object, but same thing...

I write a pure virtual class that acts as an interface, then call an

extern "C" myinterface* new_myclass();

function that goes into the SO and just does a "new myclass". From then on, I jsut treat it normally. (even delete()ing it)
i hope you dont allocate the class in the DLL's heap, then
delete it in the Application space... because thats a BIG no no.
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
Also returning an object is not compiler portable, you can't call a function that returns an object from a DLL compiled with VC++ from an exe compiled with MinGW and expect it to work, if the reason you are creating a DLL is to provide a plugin or MODable interface, you'd be forcing your users to use the same compiler as you do.

Stick to extern "C", and returning chars, ints and structs or pointers to those.
How do I do it?

extern "C" {...}


so, how do YOU do it?

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

*in responce to a PM I got*

Take the other people's advice.

Under linux my method has no problems at all, but people pointed out several problems that would pop up on windows. (deleting in app vs dll, which is a nonissue on linux, and the whole no single C++ ABI problem, which is another nonissue on linux (anything incompatible with G++ is worthless))

the "struct of function pointers" is probably the best method.
I always create a COM-like interface and export only a couple of functions to do with Creating/Deleting the new object instance (I got the idea from Striving for Graphics API independence) But that's just me... [ignore]
Using a COM like interface (ie a pure virtual base class) is the best way of exporting objects in C++. All you really need then is a shared header with functions for creating and deleting the objects, and the interface decleration.

Quote:
Under linux my method has no problems at all, but people pointed out several problems that would pop up on windows. (deleting in app vs dll, which is a nonissue on linux, and the whole no single C++ ABI problem, which is another nonissue on linux (anything incompatible with G++ is worthless))


There are several variations of *nix that have problems with this. Heck some even have problems with C because they don't append a leading underscore on to function names. Just because it's GCC compatible doesn't mean it will work with all *nix variations.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Quote:Original post by Washu
There are several variations of *nix that have problems with this.
Maybe, but linux doesn't.
Quote:Original post by Washu
Using a COM like interface (ie a pure virtual base class) is the best way of exporting objects in C++. All you really need then is a shared header with functions for creating and deleting the objects, and the interface decleration.


This is what i ended up doing. Works like a charm.

EDIT: Here is my code, it works alright, but i just want some opinion on it. (this is the sharred header)

namespace CoreAPI{    //-----------------------------------------------------------------------------    // Class: Interface    // Desc: The class interface exposed to client applications    //-----------------------------------------------------------------------------    class __declspec( dllexport ) CInterface    {    public:        virtual char* One() = 0;        virtual char* Two() = 0;        virtual char* Three() = 0;    }; // class __declspec( dllexport ) CInterface    extern "C"    {        __declspec ( dllexport )        CInterface* CreateInterface();        __declspec ( dllexport )        bool DeleteInterface( CInterface* pInterface );    } // extern "C"} // namespace CoreAPI


[Edited by - Mulligan on August 6, 2004 9:23:02 AM]

This topic is closed to new replies.

Advertisement