简体   繁体   中英

Return list<unsigned char*> from C++ to Java using JNI

I have the following JNI method,

JNIEXPORT jobject JNICALL Java_projlib_DeserializeBuffer
(JNIEnv *env, jobject obj, jbyteArray inBufferData)

I have created a list of unsigned char* and filling it using some data extracted from the inBufferData in my C++ code

list<unsigned char*> returnBuffer

I want to return the returnBuffer to my Java code, where it will be a List of byte array, List<byte[]> .

Please tell me how to pass the list of unsigned char* via a jobject through JNI and then get it in Java for further processing.

You will not be able to do that directly. You will have to instantiate an instance of the needed Java list implementation (since List is an interface) in C++, put it in a jobject and then add jbytearray items to it from your list and then return the list.

EXAMPLE

Since I do not have a working JNI environment, this snippet is only illustrational (feel free to edit it when you get it working), but what you need could be achieved by this:

jclass arrayListClass = env->FindClass("java/util/ArrayList"); // Find ArrayList class
jmethodID constructor = env->GetMethodID(arrayListClass, "<init>", "()V"); // Find ArrayList constructor
jobject arrayList = env->NewObject(arrayListClass, constructor); // Create new ArrayList instance
jmethodID add = env->GetMethodID(arrayListClass, "add", "(Ljava/lang/Object;)Z"); // Find the ArrayList::add method
jbyteArray item =env->NewByteArray(10); // Instantiate a new byte[]
env->CallBooleanMethod(arrayList, add, item); // Add the byte[] to the ArrayList

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