JNI & CallBack: Please help

H

Homer

Hi All,

For god sake, can anybody email me ([email protected] remove
the obvious part) or to the newsgroup small sample function how to call
a callback function from C++ and pass a Jstring to Java? I don't need
MultiThread and anything special. Just a simple call. I am not good in
C++ and this Vstudio is making me crazy. This is as far as I came:

void doCallback(){
jclass cls = (*environment)->GetObjectClass(environment, object);
jmethodID mid = *environment->GetMethodID(cls, "callback", "()V");
if (mid == 0) {
return;
}
*environment->Java_DirectoryWatcher_nativeMethod(object, mid,
myString);
}

JNIEXPORT void JNICALL
Java_DirectoryWatcher_nativeMethod(JNIEnv *env, jobject obj, jString
msg)
{
jclass cls = env->GetObjectClass(obj);
jmethodID mid = env->GetMethodID(cls, "callback", "(I)V");
if (mid == 0) {
return;
}
env->CallVoidMethod(obj, mid, depth);
}


Thanks,

Homer
 
T

Thomas Fritsch

Homer said:
For god sake, can anybody email me ([email protected] remove
the obvious part) or to the newsgroup small sample function how to call
a callback function from C++ and pass a Jstring to Java? I don't need
MultiThread and anything special. Just a simple call. I am not good in
C++ and this Vstudio is making me crazy.
There is NO need for any C++ knowledge in JNI programming.
But you MUST have much experience in C. Otherwise you would not get anywhere
in JNI programming.
This is as far as I came:

void doCallback(){
jclass cls = (*environment)->GetObjectClass(environment, object);
Where is your declaration of 'environment'? And when and how do you
initialize it?
jmethodID mid = *environment->GetMethodID(cls, "callback", "()V");
if (mid == 0) {
return;
}
*environment->Java_DirectoryWatcher_nativeMethod(object, mid,
myString);
That doesn't make sense, because the C-structure (*environment) has no
member called Java_DirectoryWatcher_nativeMethod.
Java_DirectoryWatcher_nativeMethod is a global function not a struct member.
 
H

Homer

Thanks for your reply. Would you please just send me a sample callback
function that takes JString as parameter.


Thanks,

Homer
 
A

Andrew Thompson

Homer said:
Thanks for your reply. Would you please just send me

People do not 'send you' emails from these groups.

It is not a 'get back to me' service..
..a sample callback
function that takes JString as parameter.

It is also not a code generation machine.

And while we are on the subject..

Please refrain from cross-posting so widely. Your
problem is not that important (it may be to you,
but that is entirely beside the point - if the problem
is that important and urgent to you, be prepared to
pay actual money to any of the many services that
offer that level of support).

Please quote below earlier text, and trim any
text that is no longer necessary.
<http://www.physci.org/codes/javafaq.jsp#netiquette>

TIA
 
H

Homer

Andrew said:
People do not 'send you' emails from these groups.

It is not a 'get back to me' service..

Andrew, The meaning of "send email or reply" means if you think
matter doesn't concerns others or it's long let's take it
off-line.
It is also not a code generation machine.

"people from this newsgroup" are myself and everybody else and YES
we DO sent sample code (it's just matter of copy and paste) to help
each other's out.
And while we are on the subject..

Please refrain from cross-posting so widely. Your
problem is not that important (it may be to you,
but that is entirely beside the point - if the problem
is that important and urgent to you, be prepared to
pay actual money to any of the many services that
offer that level of support).

Who the hell are you to evaluate people's questions and say it's
important enough for others or not. Mind your own business instead of
promoting your junk website.
 
H

Homer

Andrew said:
People do not 'send you' emails from these groups.

It is not a 'get back to me' service..

Andrew, The meaning of "send email or reply" means if you think
matter doesn't concerns others or it's long let's take it
off-line.
It is also not a code generation machine.

"people from this newsgroup" are myself and everybody else and YES
we DO sent sample code (it's just matter of copy and paste) to help
each other's out.
And while we are on the subject..

Please refrain from cross-posting so widely. Your
problem is not that important (it may be to you,
but that is entirely beside the point - if the problem
is that important and urgent to you, be prepared to
pay actual money to any of the many services that
offer that level of support).

Who the hell are you to evaluate people's questions and say it's
important enough for others or not. Mind your own business instead of
promoting your junk website.
 
T

Thomas Schodt

Homer said:
Andrew, The meaning of "send email or reply" means if you think
matter doesn't concerns others or it's long let's take it
off-line.

Well, usually it means some noob is too lazy to return to usenet to read
any responses.

"Would you please just send me"
is a rather unfortunate wording,
one could easily read impatience into that.

"people from this newsgroup" are myself and everybody else and YES
we DO sent sample code (it's just matter of copy and paste) to help
each other's out.

Can you provide data to back that statement?
I believe I could provide data to refute it.

Who the hell are you to evaluate people's questions and say it's
important enough for others or not. Mind your own business instead of
promoting your junk website.

That you ask indicates you did not put in any effort to find out,
the obvious first step being to read through the Java FAQ maintained by
Mr. Thompson.

The purpose of enlightening novices as to usenet etiquette
is to help them get their Java issues resolved.
Often those exact people you would like to read your question
(because they know the answer) will be put off by posts that demonstrate
a disregard for usenet etiquette.

Getting upset with someone who is trying to help is not productive.
 
H

Homer

Hi Thomas,

Thanks for the link. But I've already seen it and they are talking
about somehow different way for calling callback function. In their
scenario Java is calling C++ function and the same function is calling
Callback function in Java. This was is queit easy because called C++
function already has 'env' and 'obj' parameters. In my scenario, Java
is calling a C++ function. That function is doing some code and Another
C++ function wants to callback a Java function. E.g.:

Java:

class DirectoryWatcher {
public native void watchDirectory();
public native void callBack(String status);

static {
System.loadLibrary("watcher");
}

public void callback(String status) {
System.out.println("Honey, I am home...");
}

public static void main(String[] args) {
DirectoryWatcher dw = new DirectoryWatcher();
dw.watchDirectory();
}
}


C++:

__gc class Watcher
{
public:
void Go(String * s){
.......................
}

void callJavaCallBack() {
HERE IS THE MISSING PART (WHAT SHOULD I DO HERE?)
}

};

JNIEXPORT void JNICALL
Java_DirectoryWatcher_callbback(JNIEnv *env, jobject obj, jint depth)
{
(*env)->CallVoidMethod(env, obj,
instanceMethodCall_callback);

}
JNIEXPORT void JNICALL
Java_DirectoryWatcher_watchDirectory(JNIEnv *env, jobject obj)
{
instanceMethodCall_callback = (*env)->GetMethodID(env, cls,
"callback", "()V");

Watcher * w=new Watcher();
w->Go("c:\\test");
return;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top