简体   繁体   中英

How do I send , pointer to array from JNI C code, to Java code?

How do I send a pointer to array from JNI C code to a Java code ? For example :

JNI C Code :

jclass cls = (*env)->GetObjectClass(env,obj);
jmethodID mid = (*env)->GetMethodID(env,cls,"print"," ?? "); // What should be the signature here ?
jvalue a1,*arr;
a1.i = 2002;
a1.f = 12.90;
a1.c = 's';
a1.j = 344554;
a1.b = TRUE;
arr = &a1;
(*env)->CallVoidMethodA(env,obj,mid,arr);

Java Func :

public void print(?????) { // what should be the argument here ?
  // add code here
}

How should the java function look like to receive a pointer to an array ?

First off, two problems with your code:

  1. arr is not actually an array (it points to a single jvalue), so therefore there's no reason to pass it by pointer instead of just by value.
  2. All those a1.X assignments are useless, except the last one, because jvalue is a union of all the possible primitive and reference types.

Next: It's impossible to pass raw C pointers to JNI (well, you can, but you can only get Java to see the pointer as a number and not as an object). You have to create a Java array or buffer object. Arrays are good if you will be creating an array of references, though the memory will have to be copied; buffers are good if you want the C array to share memory with the Java buffer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM