1

I have a object store in Java. My C program stores data (in form of char array) in java. Now I wish to retrieve data from my store. I cannot find any function call that returns me an char array. How can I do this?

1
  • Your question still needs to be cleaned up a little. How does your C program store something in Java? Have you worked out all of the character encoding issues? Commented Aug 4, 2009 at 13:46

1 Answer 1

1

You need to use one of the various API's provided by JNI, probably GetCharArrayElements().

Following is an example, taken from working code which retrieves a byte array from Java into C (the code is a function which is invoked by Java, but the mechanics are identical).

JNIEXPORT void JNICALL Java_xxx_jniEnqueue(JNIEnv *jep,jobject thsObj,
 jlong handle, jbyteArray jvaKey, jint jvaKeyOfs, jint jvaKeyLen, jbyteArray jvaData, jint jvaDtaOfs, jint jvaDtaLen) {
    jbyte            *jniKey,*jniData;
    jthrowable       escObj;

    jniKey=(*jep)->GetByteArrayElements(jep,jvaKey,0);
    jniData=(*jep)->GetByteArrayElements(jep,jvaData,0);
    ...
    memcpy(odp->enqpfx->Msg,jniKey+jvaKeyOfs,(vuns)jvaKeyLen);              /* key badly named Msg */
    ...
    // enqueue data using key
    if(/* enqueue failed */) {
        (*jep)->ReleaseByteArrayElements(jep,jvaKey ,jniKey ,JNI_ABORT); /* abort to not copy back */
        (*jep)->ReleaseByteArrayElements(jep,jvaData,jniData,JNI_ABORT); /* abort to not copy back */
        throwEscapeObject(jep,escObj);
        return;
        }
    (*jep)->ReleaseByteArrayElements(jep,jvaKey ,jniKey ,JNI_ABORT); /* abort to not copy back */
    (*jep)->ReleaseByteArrayElements(jep,jvaData,jniData,JNI_ABORT); /* abort to not copy back */
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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