Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > JNI & CallBack: Please help

Reply
Thread Tools

JNI & CallBack: Please help

 
 
Homer
Guest
Posts: n/a
 
      10-31-2005
Hi All,

For god sake, can anybody email me ((E-Mail Removed) 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

 
Reply With Quote
 
 
 
 
Thomas Fritsch
Guest
Posts: n/a
 
      11-01-2005
"Homer" <(E-Mail Removed)> wrote:
> For god sake, can anybody email me ((E-Mail Removed) 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.
> }
>
> 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);
> }



--
"TFritsch$t-online:de".replace(':','.').replace('$','@')


 
Reply With Quote
 
 
 
 
Homer
Guest
Posts: n/a
 
      11-01-2005
Thanks for your reply. Would you please just send me a sample callback
function that takes JString as parameter.


Thanks,

Homer


Thomas Fritsch wrote:
> "Homer" <(E-Mail Removed)> wrote:
> > For god sake, can anybody email me ((E-Mail Removed) 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.
> > }
> >
> > 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);
> > }

>
>
> --
> "TFritsch$t-online:de".replace(':','.').replace('$','@')


 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      11-01-2005
Homer wrote:

> 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
 
Reply With Quote
 
Homer
Guest
Posts: n/a
 
      11-01-2005

Andrew Thompson wrote:
> Homer wrote:
>
> > 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..


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.

>
> >..a sample callback
> > function that takes JString as parameter.

>
> 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.

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


 
Reply With Quote
 
Homer
Guest
Posts: n/a
 
      11-01-2005

Andrew Thompson wrote:
> Homer wrote:
>
> > 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..


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.

>
> >..a sample callback
> > function that takes JString as parameter.

>
> 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.

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


 
Reply With Quote
 
Thomas Schodt
Guest
Posts: n/a
 
      11-01-2005
Homer wrote:
> Andrew Thompson wrote:
>
>>Homer wrote:
>>>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..

>
> 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.


>>>..a sample callback
>>>function that takes JString as parameter.

>>
>>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.


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


>>Please refrain from cross-posting so widely. Your
>>problem is not that important


>><http://www.physci.org/codes/javafaq.jsp#netiquette>



> 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.
 
Reply With Quote
 
Thomas Fritsch
Guest
Posts: n/a
 
      11-02-2005
"Homer" <(E-Mail Removed)> wrote:
> Thanks for your reply. Would you please just send me a sample callback
> function that takes JString as parameter.
>

See the JNI tutorial at
<http://java.sun.com/docs/books/tutorial/native1.1/TOC.html#summary>.
Follow the link "Calling Java Methods".

--
"TFritsch$t-online:de".replace(':','.').replace('$','@')


 
Reply With Quote
 
Homer
Guest
Posts: n/a
 
      11-02-2005
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;
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JNI thread vs. native thread vs. JNI call bgabrhelik Java 0 09-29-2009 03:33 PM
Re: JNI: Error loading DLL from JNI DDL vasanth Java 0 01-25-2005 11:03 AM
Re: JNI: Error loading DLL from JNI DDL vasanth Java 0 01-25-2005 11:01 AM
Porting JNI Windows under JNI LINUX + Wine ? Pasturel Jean-Louis Java 5 03-03-2004 07:50 PM
IBM's JNI fails where Sun's JNI works Alex Hunsley Java 4 11-04-2003 10:34 AM



Advertisments