Java Native Access doesn't do C ++, does it?

I found a lot of links on the Internet (including some from stackoverflow) for JNAs that are used for C ++ libraries, but nothing I can find in JNA docs indicates that this is possible. There seems to be no way to wrap a C ++ class in particular.

I need my own access to using RTAudio, but all the functions of RTAudio are member functions of the RTAudio class. So, just to confirm that JNA is not the way to go?

+7
java native jni jna
source share
4 answers

This question is that you are asking how to invoke C ++ instance methods using JNA , and it is possible, but you will have to do some work. In particular, you need to write a shell in which extern "C" has any functions that you really should call.

For any arbitrary definition of type* function() you can map the method using JNA as returning com.sun.jna.Pointer , but you cannot call methods on a C ++ object from JNA.

A simple workaround for this would be to write a C interface library that simply calls the method for the objects for you ... so if you have a foo() member function, you can export the C method from your C ++ code:

 extern "C" void bar(type* var){ var->foo(); } 

Obviously, this will add some work to you ... but I suspect that the overhead for going to JNI will be about the same.

JNA only cares about how this method is exported to a DLL - and it should be without C ++ decorations (hence extern "C" ), so you can do whatever you need in any such method without exposing the methods to you are calling.

In my far-fetched example above, this means that foo() , if defined in a DLL, should not actually even display. Since this is a C ++ function, JNA cannot call it directly, but it can be called from a function that JNA can call, so my proposed solution works.

So yes, you can completely encapsulate calls to all member functions (create, manage, destroy) in one function, and JNA doesn't care.

+9
source share

Try Swig . It will create wrappers for you for C ++ classes.

+2
source share

BridJ is the spiritual child of JNA, which adds limited C ++ support (+ full JNAerator support). If you don't use too many templates, this might work ...

(disclaimer: I am the author of BridJ and JNAerator)

+2
source share

You are right JNA to access the native libraries. I think you need a Java-COM bridge. If so, there are several free alternatives:

JCOM http://sourceforge.net/projects/jcom

Jacob http://sourceforge.net/projects/jacob-project

I used Jacob in the aisle with good results, but I think it is a bit outdated.

+1
source share

All Articles