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原文链接:https://www.f2er.com/cocos2dx/343849.html