Anil Arrabole
Anil Arrabole

Reputation: 4006

How to pass a large structure through JNI from C to JAVA

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

Can somebody please give me the possible solutions?

Upvotes: 2

Views: 875

Answers (2)

Ujjwal
Ujjwal

Reputation: 74

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

Upvotes: 1

rmk
rmk

Reputation: 4455

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.

Upvotes: 3

Related Questions