2

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

Can somebody please give me the possible solutions?

2
  • 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) Commented Feb 8, 2011 at 20:45
  • @Peter Lawrey may be you can help me with this question stackoverflow.com/questions/6215374/from-c-code-to-java-and-jni ? Commented Jun 3, 2011 at 10:12

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.