传递字符串数组从Java到C用JNI [英] passing string array from java to C with JNI

查看:1520
本文介绍了传递字符串数组从Java到C用JNI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串数组像 {MYNAME,提供yourname,hisname} ,我想这个数组发送到的 C 使用的 JNI 的。我找不到这方面的任何明确的解决方案。我试图把这个字符串作为 chararray ,但没有成功。

I have a string array like {"myname","yourname","hisname"} and I am trying to send this array to C with using JNI. I could not find any clear solution for this. I have tried to take this string as a chararray but no success.

有没有办法做到这一点?

Is there a way to do this?

推荐答案

您可以编写一个简单的函数,它接受一个 jobjectArray 对象,铸就每一个的jstring,然后调用 GetStringUTFChars 就可以了。

You can write a simple function that takes a jobjectArray object, cast each one to jstring and then call GetStringUTFChars on it.

这样的:

void MyJNIFunction(JNIEnv *env, jobject object, jobjectArray stringArray) {

    int stringCount = env->GetArrayLength(stringArray);

    for (int i=0; i<stringCount; i++) {
        jstring string = (jstring) (env->GetObjectArrayElement(stringArray, i));
        const char *rawString = env->GetStringUTFChars(string, 0);
        // Don't forget to call `ReleaseStringUTFChars` when you're done.
    }
}

这篇关于传递字符串数组从Java到C用JNI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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