Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling java method from c++ using JNI

i'm trying to wrap a c++ library for a specific usb device in JAVA. the library supports Callback functions to inform the application about the attachment and detachment of usb device to PC.

the call back function must have a specific format like this:

DWORD callbackFunction(void *params);

so i implemented a function like this in JNI dll and want to call a function in Java wapper whenever this function is called.

the question is what JNIENV i should use for calling GetObjectClass, GetMethodID and CallVoidMethod from?


This is how I initialize my DLL. the "Set(AttachDetach)Callback" methods accept a callback function(first parameter) and a void* parameter(secondparameter) that will be passed to the function when module attach/detach is detected.

JNIEXPORT void JNICALL Java_MyPackage_MyClass_InitializeDLL
(JNIEnv *env, jobject obj, jobject callback)
{
      // Storing callback object in global variable.
    callBackObj = callback;

    env->GetJavaVM(&jvm);

    MyInstance = new MyClass();
    MyInstance ->SetAttachCallback(AttachCallBack, &callBackObj);
    MyInstance ->SetDetachCallback(DetachCallBack, &callBackObj);

      // Testing!
    jclass callBackCls = env->FindClass("MyPackage/MyClassCallBacks");
    jmethodID mid = env->GetMethodID(callBackCls, "attach", "(B)V");
    if (!mid)
        return ; /* method not found */
      //This call here works well
    env->CallVoidMethod(callBackObj, mid, 5);
}

then i set a callback function in DLL for the USB device and it is successfully called when i attach the device.

The code i put in attach callback of the USB device is this:

DWORD CALLBACK AttachCallBack(CallbackParams* params)
{
    JNIEnv *env;
    jvm->AttachCurrentThread((void **)&env, NULL);

    jclass callBackCls = env->FindClass("MyPackage/MyClassCallBacks");
    jmethodID mid = env->GetMethodID(callBackCls, "attach", "(B)V");
    if (!mid)
        return -1; /* method not found */
      // This call fails with an access violation Exception
    env->CallVoidMethod(*((jobject *)(params->param)), mid, params->moduleIndex);
      // This fails the same way too
    env->CallVoidMethod(callBackObj, mid, 5);

    jvm->DetachCurrentThread();

    return 0;
}

Before i use AttachCurrentThread i wasn't able to use JNIENV pointer at all. but now any other use of this pointer is successful instead of the call to CallVoidMethod. Do you see what is wrong here?

Let me add that MyPackage.MyClassCallBacks Is an interface that it's method is implemented in another calss namely "callBackClass"

like image 627
Taheri Avatar asked Oct 17 '25 21:10

Taheri


1 Answers

You need to have a reference to the current JVM:

JavaVM *jvm;

You can add an initilizing method to the C++ backend, which obtains this reference when the program starts:

JNIEXPORT void JNICALL init(JNIEnv *env, jclass){
    env->GetJavaVM(&jvm);
}

And when observing for USB attachemnt / detachment, you can obtain JNIEnv from this JavaVM like this:

JNIEnv *env;
jvm->AttachCurrentThread((void **)&env, NULL);

//your code here

jvm->DetachCurrentThread();

This is implemented so that every USB device change creates a new thread. If you use only one thread for the chceking, you need to attach only once (in the initilizer, perhaps?) and you will then have JNIEnv valid as long as your native thread stays attached to the JVM.

like image 194
Jakub Zaverka Avatar answered Oct 20 '25 11:10

Jakub Zaverka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!