Accessing a C++ dll from C...

homercles337

Diamond Member
Dec 29, 2004
6,340
3
71
As the title says, i want to access a C++ dll from C. The goal is to have a C interface on my C++ dll. Any suggestions or links to tutorials, code snippets, or whatever would be appreciated.

Edit: Here is a simple example that i have been working with: Linky

In this example i would like to keep the namespace and class declarations. I assume im going to have to wrap these declarations in C, but how?
 

programmer

Senior member
Mar 12, 2003
412
0
0
Before I make comments, why would you want to wrap C++ objects in C code?! Either you should simply export C++ objects, which can only be used by C++, or export C functions, which can be used by anything (VB, VC, FoxPro, whatever). The safe (and ugly) way is to create a complete shim layer of init, term, and accessor/settor functions, but then what is the use of C++ within the dll? Why not just use C within the dll too -- at least at the interface level?
 

homercles337

Diamond Member
Dec 29, 2004
6,340
3
71
Originally posted by: programmer
Before I make comments, why would you want to wrap C++ objects in C code?! Either you should simply export C++ objects, which can only be used by C++, or export C functions, which can be used by anything (VB, VC, FoxPro, whatever). The safe (and ugly) way is to create a complete shim layer of init, term, and accessor/settor functions, but then what is the use of C++ within the dll? Why not just use C within the dll too -- at least at the interface level?

Matlab's loadlibrary() only works with C functions. Also, i havent written anything in C since early in grad school (~9 years ago). Basically, im going to have to do what you say by writing a series of functions in C and compile that to a dll.

tfinch, im aware of extern "C" but its not clear how to make it work with namespaces and classes.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Originally posted by: homercles337
Originally posted by: programmer
Before I make comments, why would you want to wrap C++ objects in C code?! Either you should simply export C++ objects, which can only be used by C++, or export C functions, which can be used by anything (VB, VC, FoxPro, whatever). The safe (and ugly) way is to create a complete shim layer of init, term, and accessor/settor functions, but then what is the use of C++ within the dll? Why not just use C within the dll too -- at least at the interface level?

Matlab's loadlibrary() only works with C functions. Also, i havent written anything in C since early in grad school (~9 years ago). Basically, im going to have to do what you say by writing a series of functions in C and compile that to a dll.

tfinch, im aware of extern "C" but its not clear how to make it work with namespaces and classes.

You can't, really. What you can do is access static functions in a C++ DLL that have been declared with... I think a declspec but to be honest I don't remember. Static C++ methods don't need a this pointer, and so all you have to do is prevent the ordinary name mangling that the compiler uses for type-safety and discriminating overloaded names. Once you introduce classes you're dealing with methods that require a this pointer, and C doesn't understand that calling convention.

You could potentially wrap the C++ DLL in another C++ DLL that carefully manages access and exports everything as statics.