I succed, I love the tast of the victory
First, Gordon, I really want to thanks you for your help
here is the exact solution of my pb. maybe it could be usefull for
some body else :
--------------------
/* private field */
static jobject timer_global_obj = NULL;
static jclass timer_global_class = NULL;
static JavaVM *global_jvm = NULL;
void initTimer(JNIEnv *env, jobject obj) {
// get global ref for the object and keep it
timer_global_obj = (*env)->NewGlobalRef(env, obj);;
// get global ref for the JVM and keep it
(*env)->GetJavaVM(env, &global_jvm);
// get object concerned
jclass localRef_class = (*env)->GetObjectClass(env,
timer_global_obj);
if (localRef_class == 0)
printf("NATIVE : error, unable to have local ref\n");
/* get an absolute ref on the object, the local one is lost at the
end of the function call */
timer_global_class = (*env)->NewGlobalRef(env, localRef_class);
if (timer_global_class == 0)
printf("NATIVE : error, unable to have global ref\n");
}
void setTimer(int ms, void(* callback)(void)) {
JNIEnv *jenv = NULL;
jmethodID mid = NULL;
jsize count;
timerStop = callback;
printf("NATIVE : timerFunction, setTimer\n");
// the Java thread and the C one are not the same so re find the
good JNIEnv
if (!(*global_jvm)->AttachCurrentThread(global_jvm, (void
**)&jenv, NULL)) {
// get the method
mid = (*jenv)->GetMethodID(jenv, timer_global_class, "throwSetTimer",
"(I)V");
printf("NATIVE : getMethodID succed\n");
if (mid == 0) {
printf("NATIVE : method not found\n");
return;
}
printf("NATIVE : try to call method\n");
(*jenv)->CallVoidMethod(jenv, timer_global_obj, mid, ms);
printf("NATIVE : call method succed\n");
}
return;
}
|