1、Java_org_cocos2dx_lib_Cocos2dxRenderer.cpp
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInsertText(JNIEnv* env,jobject thiz,jstring text) { const char* pszText = env->GetStringUTFChars(text,NULL); cocos2d::CCIMEDispatcher::sharedDispatcher()->dispatchInsertText(pszText,strlen(pszText)); env->ReleaseStringUTFChars(text,pszText); }
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxHelper_nativeSetApkPath(JNIEnv* env,jstring apkPath) { g_apkPath = JniHelper::jstring2string(apkPath); }
static string jstring2string_(jstring jstr) { if (jstr == NULL) { return ""; } JNIEnv *env = 0; if (! getEnv(&env)) { return 0; } const char* chars = env->GetStringUTFChars(jstr,NULL); string ret(chars); env->ReleaseStringUTFChars(jstr,chars); return ret; }
总结:
java调用C++传递string类型的参数时,常常需要在C++端转化为C类型的字符串,可以调用
GetStringUTFChars函数进行转化,但是别忘了调用
ReleaseStringUTFChars
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。