7

Say I have a C++ DLL with a single exported method such as:

CustomerProcessor* getInstance();

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

I know I can map the getInstance() method to a Class in Java using JNA (extending com.sun.jna.Library), store the returned CustomerProcessor instance in a com.sun.jna.Pointer.

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

William
  • 13,332
  • 13
  • 60
  • 73
  • according to this question you cannot call C++ methods in JNA: http://stackoverflow.com/questions/1556421/use-jni-instead-of-jna-to-call-native-code – dfa Dec 09 '09 at 11:18

2 Answers2

3

For any arbitrary type* function() definition you can map the method using JNA as returning a com.sun.jna.Pointer, but you won't be able to invoke methods on a C++ object from JNA.

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

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

Obviously this will add some work for you...but I suspect the overhead for switching to JNI would be about the same.

Mark Elliot
  • 75,278
  • 22
  • 140
  • 160
  • I've actually already gone down the route of writing a C++ wrapper, but was wondering if there was anyway to eliminate it. Many thanks for the clarification. – William Dec 10 '09 at 17:30
1

JNAerator may facilitate doing what you ask. It has some support for demangling and vtable access (required for calling *this methods).

Marek R
  • 32,568
  • 6
  • 55
  • 140
technomage
  • 9,861
  • 2
  • 26
  • 40