JNI & CallBack: Please help

Discussion in 'Java' started by Homer, Oct 31, 2005.

  1. Homer

    Homer Guest

    Hi All,

    For god sake, can anybody email me ( 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
     
    Homer, Oct 31, 2005
    #1
    1. Advertising

  2. "Homer" <> wrote:
    > For god sake, can anybody email me ( 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('$','@')
     
    Thomas Fritsch, Nov 1, 2005
    #2
    1. Advertising

  3. Homer

    Homer Guest

    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" <> wrote:
    > > For god sake, can anybody email me ( 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('$','@')
     
    Homer, Nov 1, 2005
    #3
  4. 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
     
    Andrew Thompson, Nov 1, 2005
    #4
  5. Homer

    Homer Guest

    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
     
    Homer, Nov 1, 2005
    #5
  6. Homer

    Homer Guest

    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
     
    Homer, Nov 1, 2005
    #6
  7. 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.
     
    Thomas Schodt, Nov 1, 2005
    #7
  8. "Homer" <> 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('$','@')
     
    Thomas Fritsch, Nov 2, 2005
    #8
  9. Homer

    Homer Guest

    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;
    }
     
    Homer, Nov 2, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mic

    JNI callback and jar

    Mic, Feb 12, 2004, in forum: Java
    Replies:
    1
    Views:
    527
    Gordon Beaton
    Feb 13, 2004
  2. Storm
    Replies:
    2
    Views:
    917
    Storm
    Oct 16, 2004
  3. John Smith
    Replies:
    5
    Views:
    6,952
    Chris Uppal
    Jul 27, 2005
  4. Homer

    CallBack JNI help

    Homer, Nov 15, 2005, in forum: Java
    Replies:
    11
    Views:
    11,823
    Gordon Beaton
    Nov 17, 2005
  5. KK
    Replies:
    2
    Views:
    705
    Big Brian
    Oct 14, 2003
Loading...

Share This Page