java 传递字符串数组,传递字符串数组从Java到C用JNI

这篇博客探讨了如何通过JNI(Java Native Interface)将Java中的字符串数组传递到C代码中。作者提供了一个简单的函数示例,该函数接受一个jobjectArray对象,将其元素转换为jstring,并调用GetStringUTFChars来获取原始的C风格字符串。这个过程涉及到类型转换和内存管理,确保在完成操作后释放资源。
摘要由CSDN通过智能技术生成

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?

解决方案

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

Like this:

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

int stringCount = env->GetArrayLength(stringArray);

for (int i=0; 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.

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值