Hi,
maybe somebody can help me out with this:
I have a C++ DLL which is called from a Java VM which works fine. I can
do callbacks from C++ to Java within the thread created by the VM.
I store the reference to the VM via env->GetJavaVM( &globalVM ) from
this thread in a global variable.
>From a different thread I use the following code
int CALLBACK func(LPVOID pCmd, LPVOID pParam)
{
JNIEnv* localEnv;
int rc = globalVM->AttachCurrentThread((void**) &localEnv, NULL);
rc = globalVM->GetEnv( (void**)localEnv, JNI_VERSION_1_2 );
if( rc == JNI_EDETACHED )
printf("GetEnv failed: thread not attached\n");
else
if( rc == JNI_EVERSION )
printf("GetEnv failed: wrong jni version\n");
else
if( rc == JNI_OK )
printf("GetEnv ssucceded\n");
localEnv->ExceptionClear();
...
At the last command, I get an unhandled exception in the Java VM.
However, the AttachCurrentThread and the GetEnv seem to work. At least,
they don't give negative numbers.
What am I doing wrong.
Any help appreciated,
Rainer
Jean-Francois Briere - 02 Aug 2006 04:57 GMT
Why not simply:
int CALLBACK func(LPVOID pCmd, LPVOID pParam)
{
JNIEnv* localEnv;
int rc = globalVM->AttachCurrentThread((void**) &localEnv, NULL);
if (rc < 0)
return -1; // or whatever
localEnv->ExceptionClear();
...
Regards
RThaden@web.de - 02 Aug 2006 08:06 GMT
Hi Jean-Francois,
I love you, man!
> Why not simply:
>
[quoted text clipped - 5 lines]
> if (rc < 0)
> return -1; // or whatever
This works perfectly, although I don't know why the other version
didn't work.
I am a C++ programmer and have to use Java.
I understand that a call to GetEnv is not necessary since
AttachCurrentThread already delivered a JNIEnv pointer. But why does it
crash when both are called?
Maybe it's better not to think too much about it.
Kind regards,
Rainer