I support software that is implemented in C ++, but must have interfaces in several languages, including Java and .NET, but also including delphi and VB6. In addition, it should work on several platforms (so Java should work on unix).
The way this was done is to use a single DLL export of normal C functions using primitive types. For example, if the class Foo is specified:
long MY_EXPORT_FLAG FooCreate() { return (long)new Foo(); } void MY_EXPORT_FLAG FooDestroy(long Handle) { delete (Foo*)Handle; } void MY_EXPORT_FLAG BarMethod(long Handle, const char* pStr1, long* pReturnValue) { *pReturnValue = ((Foo*)Handle)->BarMethod( pStr1 ); }
Then your JNI / .NET / VB6 / Delphi code implements language-specific class wrappers, but calls these functions C dll. Each class wrapper must contain a descriptor and pass it to C.
This works pretty well, because most languages tend to use C as the lowest common denominator, so while you can export your interfaces through the thin C api, you can create interfaces for other languages.
We export the C (rather than C ++) api to a DLL, because function signatures are much more standardized across platforms.
Don't forget that in C # and Java you will have to deal with multibyte string encoding, so you must figure out how to transcode your lines to / from your C ++ code. This usually involves knowing the locales, or you can cheaply and simply support UTF-8.
source share