How to pass structure as argument to java function or return to java from jni

I have two questions Let's say I have some structure in jni say

struct X
{
    Type_A x;
    Type_B y;
}

Now how am I?

  • Pass this structure as an argument to the java call back function
  • How to return this structure to a Java function.

If possible, please give an example.

+4
source share
4 answers

If you pass a Java data structure, it must be a Java object. You can create it on the JNI side or populate the parameter object passed to Java JNI. (For example, Java can create new byte[4096]and pass it to JNI functions to store the result there.)

, Java . int/long Java. Java ​​, ( C). , JNI . finalize(), (!).

+1

Java Native Access ( , ).

public class MyStructure extends com.sun.jna.Structure {
    public int x;
    public int y;
}

, , .

+2

. :

Struct {

private Type_A x;
private Type_B y;


public Struct(Type_A x, Type_B y) {
    this.x = x;
    this.y = y;
}


public Type_A getX() {
    return x;
}

public void setX(Type_A x) {
    this.x = x;
}

public Type_B getY() {
    return y;
}

public void setY(Type_B y) {
    this.y = y;
}

}

, ​​:

myFunction(new Struct(x,y));

:

public Struct myFunction(Struct struct){

   ....
   return struct;
} 

, !

0

, , , ,

  • java → →

, , , ....

, , ???? , ,

- , ...

0

All Articles