JNI: passing Objects containing non-primitive types back and forth

B

bernd

Hi folks,

looked through a lot a threads overhere already but did not find an
answer detailed enough:

I want to map a c-structure like this created in my native code

struct mystruct {

long pid ;
long data[] ;
long meta[][] ;

} ;

to a java-object within my Java-code which would look like this IMHO:

public class ComplexData {

final int maxdata = 100 ;
final int maxmeta = 5 ;
private long pid ;
private data[] = new long[maxdata] ;
private meta[][] = new long[maxdata][maxmeta] ;

public void ComplexData() {

// Assigning values
pid = 1234 ;
data[0] = ...

}...


As far as I understood it is now possible to pass this Java-object to
the native code using the JNI-Function NewObject and read the members
and assign their values to the c-structure's members (to make them
available from within c) and the other way round, assign the values
from the c-structure to the members within the Java-object (possibly
without the initialization in the "Java-world" (I need both
directions).

My problem now is: How do I access the object's members (especially
the array types) from within c? Functions pointed to in different
threads (e.g. (Get|Set)LongArrayRegion ) need the reference to the
array, but I have only the reference to the object up to now.

Any code snippets based on the code above would be helpful.

Cheers


Bernd
 
B

bernd

Hi,

thx a lot for this elaborate answer.

Up to now I have not brought my code to work (currently I cannot get
back the value for the primitive member I assigned BEFORE passing the
object to the native code), but Your example points into the direction
I imagined. I think it will help.

What I regard as astonishing, You did not use NewObject() as it was
indicated. Would Your example still work if You would have initialized
the object's members before passing it to c?

Cheers


Bernd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top