K
kowald
Hello everybody,
I try to call java methods from a C++ dll usin JNI and have some
problems I don't understand.
This is under XP using Java 1.5.06
If a function "x" of the dll is called I want to create the JVM and
get a reference to the class I intend to use:
jint res = JNI_CreateJavaVM(&jvm,(void**)&env,&vmArgs);
jclass jclassTmp = env->FindClass("myClass");
jSimuClass = (jclass)(env->NewGlobalRef(jclassTmp));
I want to use the JVM and class later when a different function inside
the dll is called. Therefore I created a global reference for my class
and use global variables for env and jvm in my dll
JNIEnv *env;
JavaVM *jvm;
jclass jSimuClass;
If now the function "y" of the dll is called, I try to get a method ID
doing:
methodID = env->GetMethodID(jSimuClass, "method1", "(I)V");
AND THIS CRASHES THE C++ PROGRAM right at this line of code (it does
not return from this call). If I place this line of code in the dll
function x, right after the creation of jSimuClass it works. If I try
in function y other JNI methods like
jclass jclassTmp = env->FindClass("myClass");
this also works, so env is still valid. The question is why is
jSimuClass no longer valid?
If the C++ part is not a dll, but a normal executable it also works. I
can create the JVM in a function call and then call GetMethodID in a
different function call. I'm now a bit clueless and any hint what could
be wrong or what is special about a dll is welcomed.
Axel
I try to call java methods from a C++ dll usin JNI and have some
problems I don't understand.
This is under XP using Java 1.5.06
If a function "x" of the dll is called I want to create the JVM and
get a reference to the class I intend to use:
jint res = JNI_CreateJavaVM(&jvm,(void**)&env,&vmArgs);
jclass jclassTmp = env->FindClass("myClass");
jSimuClass = (jclass)(env->NewGlobalRef(jclassTmp));
I want to use the JVM and class later when a different function inside
the dll is called. Therefore I created a global reference for my class
and use global variables for env and jvm in my dll
JNIEnv *env;
JavaVM *jvm;
jclass jSimuClass;
If now the function "y" of the dll is called, I try to get a method ID
doing:
methodID = env->GetMethodID(jSimuClass, "method1", "(I)V");
AND THIS CRASHES THE C++ PROGRAM right at this line of code (it does
not return from this call). If I place this line of code in the dll
function x, right after the creation of jSimuClass it works. If I try
in function y other JNI methods like
jclass jclassTmp = env->FindClass("myClass");
this also works, so env is still valid. The question is why is
jSimuClass no longer valid?
If the C++ part is not a dll, but a normal executable it also works. I
can create the JVM in a function call and then call GetMethodID in a
different function call. I'm now a bit clueless and any hint what could
be wrong or what is special about a dll is welcomed.
Axel