How to obtain JNIEnv pointer and jobject reference in arbitrary contexts?
Hi,
I need your hand. How to obtain JNIEnv pointer and jobject reference in arbitrary contexts? (See below source codes for details)
I made some tries according to JNI spec, but still failed to get it work. Could you please provide your solutions? Better show me some source codes.
Thank you very much !!!
#include <jni.h>
#include"MessageThread.h"
#include"MyServer.h"
// this is callback function called by the native C++ library
void processEvent(int progress)
{
/*
in this function, i want to get "env", "obj" ( HOW? ),
then i can do below to invoke Java function "updateUI":
cls = env->GetObjectClass(obj);
mid = env->GetMethodID(cls, "updateUI", "(I)V");
env->CallVoidMethod(obj, mid, progress);
*/
}
JNIEXPORTvoid JNICALL
Java_MessageThread_handleMessageQ(JNIEnv *env, jobject obj)
{
MyServer* server = MTMServer::Instance();
if (server != NULL)
{
// must register a callback function before "QueryProgress"
server->RegisterCallback(processEvent);
// query message within a loop, and callback "processEvent" frequently
server->QueryProgress();
}
return;
}
[1782 byte] By [
princesuna] at [2007-10-3 1:01:22]

bschauwejava, I am new to JNI. I am just wondering whether there is any JNI-specific handlings.
This solution relies on the fact that the "processEvent" is only called during the life time of "Java_MessageThread_handleMessageQ". After handleMessagesQ returns, the JNIEnv/jobject will be deleted. So the saved global reference will also be no longer useful. Is it possible to cause some potential problem? eg. Segmentation Fault, or something like this?
Just want to make sure the solution is popular and safe.Thanks anyway.
> Have handleMessageQ stash the references to the
> environment and jobject in global variables -
> declared outside any subroutines.
>
> JniEnv* globalEnv;
> jobject* globlObject;
>
That isn't how "The Java Native Interface Programmer's Guide and Specification" suggests doing it for the environment. One is supposed to use GetEnv().
And caching the second is definitely wrong. Arguments are passed as local references. The reference lifetime is only guaranteed for the lifetime of the method call.
http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/design.html#wp1242
Objects are passed to native methods as local references.
> I need your hand. How to obtain JNIEnv pointer and
> jobject reference in arbitrary contexts? (See below
> source codes for details)
I would suggest the following....
Use GetEnv to get the environment.
Create a static java method which returns the object reference. In your callback call that method to get the reference that you want to work with.
jschell, thanks for your suggestions.
> And caching the second is definitely wrong. Arguments are passed as local references. The reference lifetime is only guaranteed for the lifetime of the method call.
In my case, it just works, because my "callback" is only called before the "handleMessageQ" returns, which means it's within the lifetime of "jobject", etc.
> Use GetEnv to get the environment.
Could you please provide some sample codes?
>Create a static java method which returns the object reference. In your callback call that method to get the reference that you want to work with.
I didn't really understand. Could you please show me some sample codes?
Thanks a lot!