正确地将硬编码的byte []从JNI返回给Java [英] Properly returning a hardcoded byte[] from JNI to Java

查看:738
本文介绍了正确地将硬编码的byte []从JNI返回给Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JNI中对16字节数组进行硬编码并使用方法返回它。

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

这不起作用

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

我收到以下错误


NativeCodeCaller.cpp:214:17:错误:' - >'的基本操作数有非指针类型'_JNIEnv'

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

NativeCodeCaller.cpp:215:78:警告:扩展初始化列表仅适用于-std = c ++ 0x或-std = gnu ++ 0x

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

NativeCodeCaller.cpp :215:78:错误:无法在分配中将大括号括号初始化列表>转换为jbyte *

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

NativeCodeCaller.cpp:216:8:错误:基数' - >'的操作数有非指针类型'_JNIEnv'

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

任何快速帮助? :)

推荐答案

错误' - >'的基本操作数有非指针类型表示您应该使用而不是 - >

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

所以你是否使用(* env).NewByteArray(env,16); env-> NewByteArray (env,16); 。第216行也是如此。

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

在下一行(215)还有另一个错误,说无法转换括号内的初始化列表> ;在分配中的'jbyte *',因为赋值的括号语法仅在你声明数组/指针的地方有效(我认为它也取决于编译器,但我不确定) 。

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

您应该尝试:

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

希望这会有所帮助。

这篇关于正确地将硬编码的byte []从JNI返回给Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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