加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2dx java调用c++ -- 字符串传递

发布时间:2020-12-14 20:40:25 所属栏目:百科 来源:网络整理
导读: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::CCIMEDis

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


2、Java_org_cocos2dx_lib_Cocos2dxHelper.cpp

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读