Skip to content Skip to sidebar Skip to footer

I Have 2 Very Similar C Functions Being Called From Python And Java. How Can I Combine The 2 Libraries Into 1 That Can Be Called From Both Languages?

Basically I have 2 implementations of a C function 'encrypt' which I call from python using ctypes and java using JNI. I've been told to take the two dynamic libraries that were us

Solution 1:

You can call the python version from the java one. This avoids the duplicate code. You’ll still have to do the to/from JNI marshalling though:

void encrypt(int size, unsigned char *buffer){
    for(int i=0; i < size; i++){
        buffer[i]+=1; 
        printf("%c", buffer[i]);
    }
}

JNIEXPORT void JNICALL Java_jniTest_passBytes
  (JNIEnv *env, jclass cls, jbyteArray array) {
    unsigned char *buffer = (*env)->GetByteArrayElements(env, array, NULL);
    jsize size = (*env)->GetArrayLength(env,array);

    encrypt(size, buffer);

    (*env)->ReleaseByteArrayElements(env, array, buffer, 0);
}

Post a Comment for "I Have 2 Very Similar C Functions Being Called From Python And Java. How Can I Combine The 2 Libraries Into 1 That Can Be Called From Both Languages?"