2012-08-31 53 views
-3

我想在JNI中硬編碼一個16字節的數組,並用一個方法返回它。從JNI正確返回一個硬編碼字節[]到Java

這不是工作

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: 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'

任何快速幫助? :)

+0

誰保持低調這一點? – Shark

回答

1

錯誤base operand of '->' has non-pointer type表示您應該使用.而不是->

因此無論您使用的是(*env).NewByteArray(env, 16);還是env->NewByteArray(env, 16);。這與216行相同。

在下面一行(215)說cannot convert "brace-enclosed initializer list>" to 'jbyte*' in assignment也有另一個錯誤,因爲賦值的括號語法只在聲明數組/指針時有效(我認爲它依賴於編譯器也是如此,但我不太確定)。

你應該嘗試:

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

希望這有助於。

+0

關閉,但沒有雪茄。無論如何我會接受這個,如果它讓我的字節雖然。 – Shark

+0

就是這樣,[16]是至關重要的。再次感謝:)但是當使用env->你不需要在方法中傳遞它。 – Shark

+0

很高興幫助:) – Alex

相關問題