2

Is it possible to pass a large Structure through JNI from C to Java?

Can somebody please give me the possible solutions?

NoAIUser
  • 3,966
  • 6
  • 34
  • 52
  • 2
    Normally, you need to create a Java structure which is a copy of the C structure and pass that back. You cannot use C data types in Java (or visa-versa directly) – Peter Lawrey Feb 08 '11 at 20:45
  • @Peter Lawrey may be you can help me with this question http://stackoverflow.com/questions/6215374/from-c-code-to-java-and-jni ? – Viktor Apoyan Jun 03 '11 at 10:12

2 Answers2

3

Declare a pointer to the struct in your java class like so:

protected long ptrToX;

Next, to set it:

  • Get the field ID using (*env)->GetFieldID(...)
  • Get the pointer using (*env)->GetLongField(...)
  • Set the pointer using (*env)->SetLongField(...)

To get it, just follow the first two steps mentioned above.

Always remember to include a finaliser that will take care of deallocating the pointer when the object is garbage-collected. Alternatively, if you do not want to incur the performance hit incurred by using finalisers, just provide a terminate() method that deallocates the pointer.

rmk
  • 4,395
  • 3
  • 27
  • 32
1

Better you switch to JNA, It's much convenient way to program from C to JAVA.

Ujjwal
  • 74
  • 1