jni直接转byte_正确地将硬编码的byte []从JNI返回给Java

在JNI中尝试直接返回一个16字节的硬编码数组时遇到了错误。错误包括使用->而非.操作符,以及在非声明处使用brace初始化列表。解决方案是使用.env.NewByteArray代替(*env)->NewByteArray,并在声明时初始化jbyte数组。
摘要由CSDN通过智能技术生成

I want to hardcode a 16 byte array in JNI and return it with a method.

This isn't working

static jbyteArray JNICALL getKeyBytes(JNIEnv *env, jobject thiz)

{

F_LOG;

Mutex::Autolock _m(sLock);

jbyteArray result;

jbyte* resultType = new jbyte[16];

result = (*env)->NewByteArray(env, 16); //line 214

resultType = {52, 14, 25, 32, 75, 83, 35, 89, 40, 69, 35, 73, 84, 82, 35, 30};

(*env)->SetByteArrayRegion(env, result, 0, 16, resultType);

delete [] resultType;

return result;

}

I get the following errors

NativeCodeCaller.cpp:214:17: error: base operand of '->' has non-pointer type '_JNIEnv'

NativeCodeCaller.cpp:215:78: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x

NativeCodeCaller.cpp:215:78: error: cannot convert "brace-enclosed initializer list>" to 'jbyte*' in assignment

NativeCodeCaller.cpp:216:8: error: base operand of '->' has non-pointer type '_JNIEnv'

any quick help? :)

解决方案

The error base operand of '->' has non-pointer type indicates that you should be using . instead of ->.

So whether you use (*env).NewByteArray(env, 16); or env->NewByteArray(env, 16);. This is the same for line 216.

You also have another error at the following line (215) saying cannot convert "brace-enclosed initializer list>" to 'jbyte*' in assignment, because the brace syntax for assignment is only valid where you declare the array/pointer (and I think it depends on the compiler too, but I'm not that sure).

You should try with:

jbyte resultType[16] = {52, 14, 25, 32, 75, 83, 35, 89, 40, 69, 35, 73, 84, 82, 35, 30};

Hope this helps.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值