[英]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.