JNA - call methods on a C ++ instance returned from a DLL

Let's say I have a C ++ DLL with one exported method, for example:

CustomerProcessor* getInstance(); 

i.e. it just returns an instance of the class that actually contains the methods that I need to call.

I know that I can map the getInstance () method to a Java class using JNA (extension com.sun.jna.Library), save the returned CustomerProcessor instance to com.sun.jna.Pointer.

Can I somehow map this to the CustomerProcessor class so that I can call methods on it (and if so, how)?

+6
java jna
source share
2 answers

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.

+2
source share

JNAerator can facilitate the execution of what you ask. It has some support for disassembling and accessing the vtable (required to call * these methods).

+1
source share

All Articles