Help with AttachCurrentThread
843829Oct 2 2006 — edited Oct 3 2006Does anyone know what value is returned when AttchCurrentThread is succesful? I'm trying to call into a java function from the native callback code but have been unsuccesful. The code looks as follows:
////////////////////
static JavaVM *jvm = NULL;
void Callback cb(){
JNIEnv* jenv;
int res = jvm->AttachCurrentThread((void **)&jenv, NULL);
jclass jcls = jenv->FindClass("classname");
jmethodID methID =
jenv->GetStaticMethodIDjcls, "javaclass", "()V");
jenv->CallStaticVoidMethod(jcls, methID);
}
JNIEXPORT jint JNICALL Java_com_fn1(JNIEnv *env, jclass cls){
if (jvm == NULL) env->GetJavaVM(&jvm);
if(..)
//call call bck function
cb();
}
/////////////////////////////////////////////////////
AttachCurrentThread returns zero and the classname, methodname derived from the jenv are all null ( jcls, methID etc). Anything wrong with the above code?
For testing, I put the same code in native method as below, AttachCurrentThread still returns 0 but it works fine and calls the java method. Any ideas on what's going wrong with the callback code? Thanks
JNIEXPORT jint JNICALL Java_com_fn1(JNIEnv *env, jclass cls){
if (jvm == NULL) env->GetJavaVM(&jvm);
JNIEnv* jenv;
int res = jvm->AttachCurrentThread((void **)&jenv, NULL);
jclass jcls = jenv->FindClass("classname");
jmethodID methID =
jenv->GetStaticMethodIDjcls, "javaclass", "()V");
jenv->CallStaticVoidMethod(jcls, methID);
}