JNI调用将jstring转换为char * [英] JNI call convert jstring to char*

查看:1473
本文介绍了JNI调用将jstring转换为char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的cpp代码包含一个jni函数,我希望转换为const char *。这是我使用的代码

  externC{
void Java_com_sek_test_JNITest_printSomething(JNIEnv * env,jclass cl, jstring str){

const char * mystring = env-> GetStringUTFChars(env,str,0);
PingoScreen :: notify();
}

我得到一个错误,

 没有匹配的函数调用'_JNIEnv :: GetStringUTFChars(JNIEnv *& _jstring *& int)
pre>

我做错了什么?

解决方案

根据文档,



GetStringUTFChars
const jbyte * GetStringUTFChars(JNIEnv * env,jstring string,
jboolean * isCopy);



返回一个指向修改后的UTF -8编码。这个数组是有效的,直到它被ReleaseStringUTFChars()释放。



如果isCopy不为NULL,那么* isCopy设置为JNI_TRUE,或者如果没有复制,它被设置为JNI_FALSE。



所以最后一个参数应该是一个jboolean;


I my cpp code contains a jni function that i wish to convert to const char*. This is the code i am using

extern "C" {
void Java_com_sek_test_JNITest_printSomething(JNIEnv * env, jclass cl, jstring str) {

    const char* mystring = env->GetStringUTFChars(env, str, 0);
    PingoScreen::notify();
}

I get an error that

no matching function for call to '_JNIEnv::GetStringUTFChars(JNIEnv*&, _jstring*&, int)

What am i doing wrong ?

解决方案

According to the documentation,

GetStringUTFChars const jbyte* GetStringUTFChars(JNIEnv *env, jstring string, jboolean *isCopy);

Returns a pointer to an array of bytes representing the string in modified UTF-8 encoding. This array is valid until it is released by ReleaseStringUTFChars().

If isCopy is not NULL, then *isCopy is set to JNI_TRUE if a copy is made; or it is set to JNI_FALSE if no copy is made.

So the last parameter should be a jboolean;

这篇关于JNI调用将jstring转换为char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆