[英]JNI Pass a String from java to c++ and then pass c++ string to string Array
[英]Android JNI: pass std::string to Java and back to C++
我有一个相对较大的std :: string。 我想不复制就将其传递给Java。 然后传递回另一个JNI库。 最好的方法是什么?
jlong some_jni_call() {
string str = createLargeString(); // say this is from 3rd lib only returns string
string* strInHeap = new string(str); // this should just increase the reference count?
jlong handle = (long)strInHeap;
return handle;
}
然后,我可以返回JNI:
void use_string(jlong handle) {
string* str = (string*)handle;
// use the str...
delete str; // doesn't look so nice, since people can forget to delete
}
这是一个好方法吗?
这肯定是一种可行的方法。 您甚至可以使用类似的技巧来回传递函数指针。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.