JNI是什么的char [](字符数组)的方法描述符? [英] JNI What is the method descriptor for char [] (char array)?

查看:229
本文介绍了JNI是什么的char [](字符数组)的方法描述符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My JAVA class code snippet .I want to access getReg_chal() method from my C file using JNI:

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

我的C文件做一些JNI操作:

My C file doing some jni operation:

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

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

我想知道我的char []方法描述符。写作()I给了我假的方法描述错误,因为()我被用于诠释。我将填补什么的 * 的。
请帮帮我。先谢谢了。

I want to know the method descriptor for my char[]. Writing "()I" gives me bogus method descriptor error since ()I is used for Int. What would i fill in *. Please Help me. Thanks in advance.

推荐答案

该方法的签名是()[C

您可以阅读有关的细节这里这里

You can read about the details here and here.

要调用的使用方法ID的方法,你只是喜欢写东西。

To call the method using the method id, you'd just write something like

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

这篇关于JNI是什么的char [](字符数组)的方法描述符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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