JNI char [](char数组)的方法描述符是什么?

时间:2011-09-14 13:38:11

标签: java android java-native-interface

我的JAVA类代码片段。我想使用JNI从我的C文件访问getReg_chal()方法:

public char[] getReg_chal() {
        return reg_chal;
    }

我的C文件执行一些jni操作:

mid = (*env)->GetMethodID(env, info, "getReg_chal()", "()I");

mid = (*env)->GetMethodID(env, info, "getReg_chal()", ***);

我想知道我的char []的方法描述符。写“()I”给我伪造方法描述符错误,因为()我用于Int。我将填写 * 。 请帮我。提前谢谢。

1 个答案:

答案 0 :(得分:3)

方法签名为“()[C”。

您可以阅读详细信息herehere

要使用方法id调用方法,您只需编写类似

的内容
jobject obj = ... // This is the object you want to call the method on
jcharArray arr = (jcharArray) (*env)->CallObjectMethod(env, obj, mid);
int count = (*env)->GetArrayLength(env, arr);
jchar* chars = (*env)->GetCharArrayElements(env, arr, 0);
// Here, "chars" is a C pointer to an array of "count" characters. It's NOT
// going to be 0-terminated, so be careful! Here's where you would do your
// logging or whatever. One possible way to do this is by turning the `jchar`
// array into a proper 0-terminated character string:
char * message = malloc(count + 1);
memcpy(message, chars, count);
message[count] = 0;
LOGD("NDK:LC: [%s]", message);

// When you're done you must call this!
(*env)->ReleaseCharArrayElements(env, arr, chars, 0);