I am trying to understand jni, so I started hacking up hellojni, and I ran into this problem.
My java code looks like this:
short[] buf = new short[16];
Log.d("hello", "before!");
write(buf, 0, 16);
and my C code looks like this:
jint
Java_com_example_hellojni_HelloJni_write(JNIEnv* env, jshortArray buf, jint off, jint len)
{
char debug[1024];
int ii = 0;
jsize cbuflen = (*env)->GetArrayLength(env, buf);
sprintf(debug, "array length: %d", cbuflen);
LOGD(debug);
...
...
The output is:
array length: 1079082088
Why is the array length so big?
Could you show your entire JNI file? You are not declaring the target object in your JNI function. Usually the arguments are JNIEnv* env, jobject javaObject, etc. This means that what you believe is the jshortArray is actually the pointer to a Java object, which would explain the weird results you are getting.
Related
Does anyone know how to solve the error?
JNIEXPORT jstring JNICALL JAVA_com_pfc_camera_ndkmain_MainActivity_compresion(JNIEnv* env, jobject obj, jobjectArray jargv){
//jargv is a Java array of Java strings
int argc = env->GetArrayLength(jargv);
typedef char *pchar;
pchar *argv = new pchar[argc];
int i;
for(i=0; i<argc; i++)
{
jstring js = env->GetObjectArrayElement(jargv, i); //A Java string
const char *pjc = env->GetStringUTFChars(js); //A pointer to a Java-managed char buffer
size_t jslen = strlen(pjc);
argv[i] = new char[jslen+1]; //Extra char for the terminating null
strcpy(argv[i], pjc); //Copy to *our* buffer. We could omit that, but IMHO this is cleaner. Also, const correctness.
env->ReleaseStringUTFChars(js, pjc);
}
//Call main
Principal *pa=Principal::CreateInstance(argc,argv);
pa->Run();
pa->FreeInstance();
//Now free the array
for(i=0;i<argc;i++)
delete [] argv[i];
delete [] argv;
}
I understand that the error can come from not doing a casting but I do not have it very clear
[ https://i.stack.imgur.com/bOWKZ.png][1]
It seems that the problem has been solved, now I get another error but I do not understand if I'm passing two arguments js and pjc
[ https://i.stack.imgur.com/UHCAR.png][1]
In C++ you have to use explicit conversion to your desired type.
jstring js = (jstring)env->GetObjectArrayElement(jargv, i);
You can learn about jni programming here
Regarding your other question do the following:
const jbyte *pjc = env->GetStringUTFChars(js, NULL);
I have a problem with converting in JNI.
In C++ I'm creating some cipher using AES (Library CryptoPP). I'm converting result to string and returning it. This is how the code getting the string looks like:
JNIEXPORT jbyteArray JNICALL Java_com_example_androidake_MutualAuthenticateChip_prepareEncryptionCPP
(JNIEnv *env, jobject thisObj, jboolean hmm, jboolean jinit) {
string encryption= mac->EncryptCertKey();
jbyteArray returns = env->NewByteArray(encryption.size());
env->SetByteArrayRegion(returns, 0, encryption.length(), (jbyte*) encryption.c_str());
return returns;
};
Above string is converting to jbyteArray which is returned. First I wanted just return string using
env->NewStringUTF(encryption.c_str());
but the application has been crashing. I think it is caused by content of variable 'encryption'. I'm using env->NewStringUTF(encryption.c_str()); in other functions, where returned string is for example just a number or something like that.
Then in Java I'm doing conversion from byte to string:
byte[] cipher = mac_A.prepareEncryptionCPP(true, true);
string cipher_str = new String(cipher);
And I'm putting that string again to the C++ object and compare old cipher with the cipher which is sent from Java:
//Java
boolean result = mac_A.compareEncryption(true, cipher);
//JNI
JNIEXPORT jboolean JNICALL Java_com_example_androidake_MutualAuthenticateChip_compareEncryption
(JNIEnv * env, jobject thisObj, jboolean jinit, jstring cipher){
bool init = jinit;
bool result;
jsize length = env->GetStringUTFLength(cipher);
const char *inCStr_ek = env->GetStringUTFChars(cipher, 0);
string s(inCStr_ek, length);
result = mac->CompareCipher(s);
env->ReleaseStringUTFChars(cipher, inCStr_ek);
return result;
};
Comparing in C++ :
bool MyClass::CompareCipher(std::string cipher_2){
if(cipher == cipher_2){
return true;
}else{
return false;
}
}
And it always returns false. I do not know what I'm doing wrong. I've even sent this cipher from Java to C++ and take it back to Java and the strings are equals, but in C++ side are not.
on your java side code you have
byte[] cipher = mac_A.prepareEncryptionCPP(true, true);
boolean result = mac_A.compareEncryption(true, cipher);
compareEncryption jni function is defined with jstring , not jbytearray.
So from JNI side you are sending a byte array to java, and from java side you send back the same byte array to native side (but uses jstring in call), but then you are using env->GetStringUTFChars(cipher, 0) which converts that byte array into a modified UTF-8 string, so its technically not that same byte array anymore.
if you need strings do the conversions in java side, and just use with same plain byte arrays between jni and java. See this for string encoding issues in Android JNI.
Eclipse gives me this error when I try to build:
jni/cyberlevel9.c:17:31: error: request for member 'NewDirectByteBuffer' in something not a structure or union
jni/cyberlevel9.c:18:28: error: request for member 'NewGlobalRef' in something not a structure or union
This is the problem part of the code:
JNIEXPORT jobject JNICALL Java_com_cyberbg_natcamlevel9_NativeCameraLevel9Start_allocNativeBuffer(JNIEnv* env, jobject this, jlong size)
{
void* buffer = malloc(size);
jobject directBuffer = env->NewDirectByteBuffer(buffer, size);
jobject globalRef = env->NewGlobalRef(directBuffer);
return globalRef;
//return (NewDirectByteBuffer*)(*env)->NewDirectByteBuffer(buffer, size);
}
JNI calls from C program look like
(*env)->fun(env, p1, ...)
Your calling style is OK from C++, where a special wrapper class is defined in jni.h:
env->fun(p1, ...)
You can probably resolve your problems by simply renaming jni/cybrrlevel9.c to jni/cyberlevel9.cpp
Or,
jobject directBuffer = (*env)->NewDirectByteBuffer(env, buffer, size);
jobject globalRef = (*env)->NewGlobalRef(env, directBuffer);
I'm newbie in C++ and JNI, I try to find a correct way to convert byte[] in java to unsigned char* in C++ by using JNI, and vice versa ! (I'm working on android)
After looking for a solution in google and SO, I haven't found a good details way to convert byte[] in java to C++. Please help me, and provide a solution for a vice versa (unsigned char* in C++ to byte[] in java). Thanks very much
byte[] in java to unsigned char* in C++:
JAVA :
private static native void nativeReceiveDataFromServer(byte[] value, int length);
JNI:
... (JNIEnv* env, jobject thiz, jbyteArray array, jint array_length)
{
???
}
PS: I modified my question for being a real question for my problem :(
You can use this to convert unsigned char array into a jbyteArray
jbyteArray as_byte_array(unsigned char* buf, int len) {
jbyteArray array = env->NewByteArray (len);
env->SetByteArrayRegion (array, 0, len, reinterpret_cast<jbyte*>(buf));
return array;
}
to convert the other way around...
unsigned char* as_unsigned_char_array(jbyteArray array) {
int len = env->GetArrayLength (array);
unsigned char* buf = new unsigned char[len];
env->GetByteArrayRegion (array, 0, len, reinterpret_cast<jbyte*>(buf));
return buf;
}
The array buf is a variable on stack. After leaving the function is this variable undefined.
A solution is separating this function in two, one to compute the size and another with a pointer to now the allocated array as parameter to fill in the values.
Background
I'm working with byte arrays in JNI. And I can't get length of jbyteArray. I'm writing code in eclipse in Windows 7.
Java code:
private native int Enroll( byte[] pSeed );
JNI code:
In JNI I have a struct that have two members unsigned long length and unsigned char data[1]
typedef struct blobData_s {
unsigned long length;
unsigned char data[1];
} blobData_t;
Now as my JNI function get as argument jbyteArray jpSeed i want to get the length of jpSeed and set it as length member of struct.
JNIEXPORT jint JNICALL Java_com_Test_Enroll( JNIEnv* env, jobject thiz, jbyteArray jpSeed ){
blobData_t* bd = malloc( sizeof(blobData_t) );
bd->length = **Question 1**
bd->data[1] = jbyteArray;
}
Question 1: How I can get the length of jpSeed in JNI ?
Question 2: Will this code works correct bd.data[1] = jbyteArray; ?
You can use GetArrayLength(JNIEnv* env, jbyteArray array) Read here.
Not sure what you want to do, I assume you want the content of jpSeed in bd.data[1].
Anyways, accessing the contents of a byte array, should be done with GetByteArrayElements(...).
Solution
Answer on Question 1. As jpSeed is jbyteArray it mean that you can get it's length by calling GetByteArrayElements( ... ) functions that declared in JNI ( you can read documentation here ) here the right code will be:
JNIEXPORT jint JNICALL Java_com_Test_Enroll( JNIEnv* env, jobject thiz, jbyteArray jpSeed ){
blobData_t* bd = malloc( sizeof(blobData_t) );
bd->length = (*env)->GetArrayLength( env, jpSeed );
.......
}
Answer on Question 2. This bd->data[1] = jbyteArray; code is not correct as it will not be compiled the right solution for this part is:
JNIEXPORT jint JNICALL Java_com_Test_Enroll( JNIEnv* env, jobject thiz, jbyteArray jpSeed ){
blobData_t* bd = malloc( sizeof(blobData_t) );
.......
jbyte* bytes = (*env)->GetByteArrayElements( env, jpSeed, 0 );
bd->data[1] = bytes[1];
}
And don't forgot to release.