13

I initially used a function to return a char* to java as UTF-8 string, but since I kept getting errors, I wrote the following function to return a char* as a Java byte[], so that I could try to convert the array into a String in java side:

jbyteArray Java_com_vektor_amapper_util_InputDeviceManager_getDevNameBytes(JNIEnv* env, jobject thiz, jint index) {
    if(pDevs[index].device_name == NULL) return NULL;
    int n=0;
    while(pDevs[index].device_name){
        n++;
    } if (n==0) return NULL;
    jbyteArray arr = (*env)->NewByteArray(env, n);
    (*env)->SetByteArrayRegion(env,arr,0,n, (jbyte*)pDevs[index].device_name);
    return arr;
}

But when I call it my application crashes. Am I missing something?

Update: The condition was missing a ++ and this caused an infinite loop. But now with the following code:

jbyteArray Java_com_vektor_amapper_util_InputDeviceManager_getDevNameBytes(JNIEnv* env, jobject thiz, jint index) {
    int n=0;
    if(pDevs[index].device_name == NULL) return NULL;
    while(pDevs[index].device_name++){
        n++;
    } if(n==0) return NULL;
        jbyteArray arr = (*env)->NewByteArray(env, n);
        (*env)->SetByteArrayRegion(env,arr,0,n, (jbyte*)pDevs[index].device_name);
        return arr;
}

I get this weird JNI warning:

06-15 22:40:02.303: W/dalvikvm(7616): JNI WARNING: negative jsize (NewByteArray)

How can it be since I am only increasing the value of n?

Update 2: the following code works:

jbyteArray Java_com_vektor_amapper_util_InputDeviceManager_getDevNameBytes(
        JNIEnv* env, jobject thiz, jint index) {

    if(pDevs[index].device_name == NULL) return NULL;
    int n=0;
    char* p = pDevs[index].device_name;
    while(*p++){
        n++;
    } if(n<=0) return NULL;
    jbyteArray arr = (*env)->NewByteArray(env, n);
    (*env)->SetByteArrayRegion(env,arr,0,n, (jbyte*)pDevs[index].device_name);

    return arr;
}
5
  • 2
    If reached this int n=0; while(pDevs[index].device_name){ n++; }looks like an endless loop. Commented Jun 15, 2013 at 14:34
  • @alk You were right, i forgot the ++ in the while condition, but the code doesn't work yet. I put the updated code. Commented Jun 15, 2013 at 14:41
  • FYI, the hello-jni sample in the Android NDK distribution shows you how to return a UTF string. Commented Jun 15, 2013 at 19:50
  • @ChrisStratton Yes, I was able to create the string, but sometimes it gave me errors, so I preferred converting a byte array to string in Java side. Still no errors with this new method. Commented Jun 16, 2013 at 12:21
  • Couldn't you simply do int n = strlen(pDevs[index].device_name) ? Commented Sep 8, 2014 at 10:03

1 Answer 1

9

Shouldn't be this ?

char* p = pDevs[index].device_name;
while( *p++) {
...
}
Sign up to request clarification or add additional context in comments.

1 Comment

You are right, every time I restart coding in C, I forgot all the basics.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.