0

C Function which I want to convert below how I declare and call callback.

typedef void (*GenmsgCallback_t)(char *genmsgResponse, size_t genmsgResponseLen);

void genmsg_cb(char *rcvData, size_t rcvDataLen) {

    for (int i = 0; i < rcvDataLen; i++) {
        printf("%c", rcvData[i]);
    }
    printf("\n");

}

How I call the callback with the main function

retVal = init_genmsg_module(genmsg_cb, RootConfigFilePath_Gen_Msg);
//Declaration for main function goes like this 

int init_genmsg_module(GenmsgCallback_t fnptrGenmsgCb, const char *rootCfgFilePath);

I want to change the C function to JAVA code over ndk. I have done one caller fucntion like this which internally call the C function it self but the cb I am not able to create via JAVA so I can call the main function.

JNI code goes like this:

JNIEXPORT jint JNICALL Java_com_example_genralmsg_1module_caller_1genmsg_initgenmsg(JNIEnv *env, jclass caller_genmsg,jstring rootFilePath){
    const char *filepath = (*env)->GetStringUTFChars(env, rootFilePath, 0);
    return init_genmsg_module(genmsg_cb,filepath);
}
Botje
  • 26,269
  • 3
  • 31
  • 41
  • It is not clear what you want to achieve. If it is to have `genmsg_cb` in Java, your best choice would be to provide a proxy callback in C. BTW, your JNI code is [missing ReleaseUTFChars()](https://stackoverflow.com/a/5863081/192373). – Alex Cohn May 11 '21 at 06:57
  • i want to pass in java the callback function pointer GenmsgCallback_t fnptrGenmsgCb inside the JNIEXPORT jint JNICALL Java_com_example_genralmsg_1module_caller_1genmsg_initgenmsg caller function via java – Mohan Krushna May 11 '21 at 09:35
  • The first question, how will your Java side know the C function pointer. But you may cast this pointer freely to and from `jlong`, which means that if `Java.com.example.generalmsg_module.caller_genmsg.initgenmsg` native method accepts a `long` parameter **`cb`**, you can call `init_genmsg_module((GenmsgCallback_t*)cb, filepath)`. – Alex Cohn May 11 '21 at 13:51

0 Answers0