Home arrow static arrow Java Programming [Archive] - How to get a JNIEnv Pointer from a Windows Callback Process?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - How to get a JNIEnv Pointer from a Windows Callback Process?
This topic has 10 replies on 1 page.

Posts:4
Registered: 8/8/04
How to get a JNIEnv Pointer from a Windows Callback Process?  
Aug 8, 2004 6:09 AM



 
My problem is, i have to send back information to a java method from the callback process:

LRESULT CALLBACK WndProcess(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch(message) {
case (WM_USER + 1):
//here i wanna send back information
break;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}

But how can i get a JNIEnv Pointer to call the CallVoidMethod Process?

 

Posts:179
Registered: 7/30/04
Re: How to get a JNIEnv Pointer from a Windows Callback Process?  
Aug 8, 2004 6:24 AM (reply 1 of 10)



 
Start reading off this URL:

http://java.sun.com/docs/books/tutorial/native1.1/index.html

This contains much material, for example chapters called "Interacting with Java from the Native Side" and "Calling Java Methods"
 

Posts:179
Registered: 7/30/04
Re: How to get a JNIEnv Pointer from a Windows Callback Process?  
Aug 8, 2004 6:29 AM (reply 2 of 10)



 
Just a warning: The docs states that JNIEnv* is only valid on the thread that supplied it to the native (C/C++) function on invocation.

In Windows, it is possible that the windowproc (message dispatch) is called from different threads. You need to ensure that this will not happen.
 

Posts:179
Registered: 7/30/04
Re: How to get a JNIEnv Pointer from a Windows Callback Process?  
Aug 8, 2004 6:39 AM (reply 3 of 10)



 
I need to correct myself:

Your WndProcess handler is called by the thread that created the window, and by that thread alone.
 

Posts:4
Registered: 8/8/04
Re: How to get a JNIEnv Pointer from a Windows Callback Process?  
Aug 8, 2004 6:40 AM (reply 4 of 10)



 
Can i save the JNIEnv i get through a method i call from the java side and use it in the windowproc?
 

Posts:179
Registered: 7/30/04
Re: How to get a JNIEnv Pointer from a Windows Callback Process?  
Aug 8, 2004 7:05 AM (reply 5 of 10)



 
Can i save the JNIEnv i get through a method i call
from the java side and use it in the windowproc?

Think about the thread issue. Java calls your native function, providing you a JNIEnv. You save this pointer, and exit the native function. This means that the thread (on which the JNIEnv is valid) goes back into Java.
If your windowproc gets called now, it has to be called from another thread, and that thread cannot use the stored JNIEnv.

The way it may work, is that Java calls a native function. You store the JNIEnv pointer, creates a window and serves a message pump from the thread that called the native function. This way the messageproc is called in the correct context, and can use the stored JNIEnv.

The message pump needs to exit, and the window must be destroyed before the native function returns to java. This is becase the calling thread is the only thread that is able to serve the window, including a DestroyWindow() call.

 

Posts:4
Registered: 8/8/04
Re: How to get a JNIEnv Pointer from a Windows Callback Process?  
Aug 8, 2004 7:18 AM (reply 6 of 10)



 
ok, this means, i have to create a new thread in the current vm and call the java method from it? the vm is the same, so it should be possible to access the JNIEnv pointer through it, i hope :)
 

Posts:179
Registered: 7/30/04
Re: How to get a JNIEnv Pointer from a Windows Callback Process?  
Aug 8, 2004 7:33 AM (reply 7 of 10)



 
ok, this means, i have to create a new thread in the
current vm and call the java method from it? the vm is
the same, so it should be possible to access the
JNIEnv pointer through it, i hope :)

If you really need JNI, then you need to do it that way.

However, have you considered other means of interprocess communication?
I strongly believe that you can save yourself A LOT of trouble by looking into local TCP/IP connections, or possibly other protocols like named pipes.
 

Posts:4
Registered: 8/8/04
Re: How to get a JNIEnv Pointer from a Windows Callback Process?  
Aug 9, 2004 8:34 AM (reply 8 of 10)



 
yep i have to use jni...
do you have experience with attaching threads in jni? my new problem is the GetMessage loop...after starting it no jni call from the java side comes through...
 

Posts:179
Registered: 7/30/04
Re: How to get a JNIEnv Pointer from a Windows Callback Process?  
Aug 9, 2004 9:40 AM (reply 9 of 10)



 
Just out of curiosity: Why do you need to implement the windowproc and the loop in the first place?

This code is just briefly checked, but you could do something like:

void threadproc(void *arg)
{
JavaVM *jvm = (JavaVM*) arg;
JNIEnv *env;

(*jvm)->AttachCurrentThread(jvm, &env, NULL);

CreateWindow(....);
while (PeekMessage(....)) DispatchMessage(....);

(*jvm)->DetachCurrentThread();
}

JNIEXPORT jvoidJNICALL
Java_Class_Startup(JNIEnv *env, jobject)
{
JavaVM *jvm;
(*env)->GetJavaVM(env, &jvm);
_beginthread(threadproc, 0, jvm);
}

The JNIEnv returned within threadproc is valid for use within the windowproc.

 

Posts:179
Registered: 7/30/04
Re: How to get a JNIEnv Pointer from a Windows Callback Process?  
Aug 9, 2004 9:41 AM (reply 10 of 10)



 
You may want to read http://java.sun.com/docs/books/tutorial/native1.1/invoking/invo.html for more info about invoking JVM and attaching threads.
 
This topic has 10 replies on 1 page.