Sign in to follow this  
Mulligan

How to YOU export functions in a C++ dll

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
*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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

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