francesc
francesc

Reputation: 343

getting method address in JNI

Once you've done:

jmethodID mid = (*env)->GetMethodID(env, cls, "run", "()V");

how do you get the run address so that you can pass the function pointer as an argument to a C function? Perhaps some jmethodID field? I haven't been able to find it in the jni docs. Thanks.

Upvotes: 0

Views: 894

Answers (2)

user207421
user207421

Reputation: 310885

You can't. You must use the CallXXXMethod() family of APIs. That's what they're for. If you can't disturb the code that wants a function pointer, you will have to write the callback yourself in C and have it call CallXXXMethod() itself, saving the jobject and methodID somehow.

Upvotes: 0

Reimeus
Reimeus

Reputation: 159754

Your populated method in C/C++ will look like:

#include "MyTest.h"

JNIEXPORT void JNICALL 
 Java_InstanceMethodCall_nativeMethod(JNIEnv *env, jobject obj)
 {
     jclass cls = (*env)->GetObjectClass(env, obj);
     jmethodID mid = (*env)->GetMethodID(env, cls, "run", "()V");
     (*env)->CallVoidMethod(env, obj, mid);
 }

The signature is generated using javah.

Upvotes: 1

Related Questions