Combining C ++ API in Java or .NET

Has anyone successfully wrapped the C ++ API in Java or .NET? I have an application that provides a C ++ API for writing plugins. I would like to access this API from .NET or Java.

Do I need to use COM, or are there simpler / better alternatives?

+4
source share
7 answers

If you have very direct method signatures (i.e. methods that accept and return primitive types such as int, char [], void * ... etc.), this is pretty easy to do in .NET and still possible, but a bit more complicated in Java with JNI.

However, if your class methods use modern C ++ programming methods, such as formatting common pointers and STL containers, this is a completely different story. In this case, you need to be very careful with memory management.

EDIT: It will be even more interesting if the method has C ++ template arguments, because the C ++ template system is very different from C # or Java generics in that it is just a compile-time mechanism. This basically means that the signature of a method or class is different every time you pass a different data type to a template argument. This made it impossible to complete the method in C # or Java.

+9
source

I think you can ask for something similar to the Java Native Inferface. It allows you to name code written in languages ​​like C ++. I never worked with it. But you can check it out. Hope this helps.

+2
source

On the Java side, there are many options here. this question is actually pretty close to what you are looking for if your API can be placed in DLL, com or activex.

I personally used JIntegra to transfer API calls to the office (Word) and used it directly using Java. It took some hacking to get the desired functionality, but we finished the job. Smoking was actually on the side of the Word; actual integration was relatively easy.

+1
source

If you are going to use .NET, I would recommend creating a C ++ / CLI wrapper . C ++ / CLI allows you to mix managed .NET code with native C ++ code . There are some tricks to this approach, but in most cases it works very well.

The Wikipedia entry also contains good links.

+1
source

Using SWIG to process JNI data makes cpp API termination and using it with Java quite painless.

Here is the swig tutorial

+1
source

I am the lead author of SlimDX . This may not be the largest open source compatibility project, but at the 150K + level throughout the project, it's pretty substantial. It is written using C ++ / CLI, which is a language developed by Microsoft, largely compatible with C ++, which is designed to create wrappers. It works very well, with the caveat that it is only Windows. Mono won't take it. I don’t know which problem is for you, but it is probably a better alternative than COM. Unfortunately, there are many tips and tricks that you should mostly come up with yourself. I meant a blog about many of the ones we use on SlimDX, but for some reason I will never get along with it.

On the Java side, I believe you should use JNI. Good luck with that. I never talked to anyone who worked with JNI if we both hadn't laughed at him, in the bad "oh, that hurts."

0
source

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.

0
source

All Articles