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



我的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"给了我虚假的方法描述符错误,因为()I用于Int。我该在*中填充什么呢?请帮帮我。

方法签名为" ()[C "。

你可以在这里和这里阅读详细信息。

要使用方法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);

最新更新