简体   繁体   English

Android JNI:将std :: string传递给Java,然后返回C ++

[英]Android JNI: pass std::string to Java and back to C++

I have a relative large size std::string. 我有一个相对较大的std :: string。 And I want to pass it to Java without copying. 我想不复制就将其传递给Java。 And then pass back to another JNI lib. 然后传递回另一个JNI库。 What is the best approach? 最好的方法是什么?

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;
}

Then I can pass back to JNI: 然后,我可以返回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
}

Is this a good approach? 这是一个好方法吗?

This is surely a feasible approach; 这肯定是一种可行的方法。 you can even use similar trick to pass function pointers back and forth. 您甚至可以使用类似的技巧来回传递函数指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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