Java Native Interface - passing parameter array of different datatypes

Discussion in 'Java' started by Clemens Arth, Oct 10, 2004.

  1. Clemens Arth

    Clemens Arth Guest

    Greets!

    Yesterday I googled for hours to find a tutorial how to call Java
    methods from C++ native code. Unfortunately I didn't find anything
    useful, the Java JNI documentation doesn't capture the functionality
    of the JNI very well.

    I want to do the following: I want to call a Java method from C++
    code. The Java method has some parameters of different datatypes, like

    public static boolean dosomething(int x, long y, String z)

    for example. Is there any way to do this? The Call...Methods() take
    the method ID and an array of parameters to pass to the function, but:

    HOW TO CREATE AN ARRAY OF DIFFERENT DATATYPES???

    It's only possible to create a new array calling something like
    NewObjectArray() when passing a datatype given.... I think, the only
    way to pass parameters of different datatypes from C++ to Java methods
    is to create a new class containing all parameters necessary and to
    pass the class as the one and only argument.

    Any ideas?
    Best Regards

    Clemens

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Clemens Arth, Oct 10, 2004
    #1
    1. Advertising

  2. Clemens Arth

    Chris Uppal Guest

    Clemens Arth wrote:

    > Greets!


    Wotcha mate!

    ;-)


    > Yesterday I googled for hours to find a tutorial how to call Java
    > methods from C++ native code. Unfortunately I didn't find anything
    > useful, the Java JNI documentation doesn't capture the functionality
    > of the JNI very well.


    Have you looked at the JNI tutorial on Sun's site ?

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

    and particularly:

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


    > I want to do the following: I want to call a Java method from C++
    > code. The Java method has some parameters of different datatypes, like
    >
    > public static boolean dosomething(int x, long y, String z)
    >
    > for example. Is there any way to do this? The Call...Methods() take
    > the method ID and an array of parameters to pass to the function, but:


    There are two main ways to do this (well, three actually, but the third won't
    interest you). You can either create an array of jvalue-s (which is /not/ the
    same as a Java Object[] array !) and fill it in with your parameters (three of
    them in this case). Something like:

    jvalue params[3];

    params[0].jint = 22;
    params[1].long = 456237L;
    params[2].jobject = aString; // a reference to a Java String

    and then pass that array to the CallBooleanMethodA() function. Note that 'A'
    in the function name, it means you are using the form of the Call*() function
    that takes an array of parameters.

    CallBooleanMethodA(objectID, methodID, params);

    In most cases, though, you won't need to do that. It's easier to use the
    varargs form (takes a variable number of parameters), in which case you can
    just say something like:

    CallBooleanMethod(objectID, methodID, 22, 456237L, aString);

    HTH

    -- chris



    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Chris Uppal, Oct 11, 2004
    #2
    1. Advertising

  3. "Clemens Arth" <-graz.ac.at> wrote in message
    news:...
    ....

    >
    > It's only possible to create a new array calling something like
    > NewObjectArray() when passing a datatype given.... I think, the only
    > way to pass parameters of different datatypes from C++ to Java methods
    > is to create a new class containing all parameters necessary and to
    > pass the class as the one and only argument.
    >
    > Any ideas?


    Take a look at
    http://www.swig.org/Doc1.3/Java.html#adding_downcasts

    I've used SWIG to call C++ from Java, but it looks like it should be of
    use. Good luck.

    Michael Saunby



    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Michael Saunby, Oct 11, 2004
    #3
  4. * Clemens Arth:
    >
    > Yesterday I googled for hours to find a tutorial how to call Java
    > methods from C++ native code. Unfortunately I didn't find anything
    > useful, the Java JNI documentation doesn't capture the functionality
    > of the JNI very well.


    Oh, it's _very_ well documented. At least it was back in 1997/1998 or
    whenever it was I had to delve into it. And just to mention C++ so
    we're not 100% off-topic in clc++m: the JNI physical memory layout is
    based on COM, which again is based on 32-bit C++ vtables.



    > I want to do the following: I want to call a Java method from C++
    > code. The Java method has some parameters of different datatypes, like
    >
    > public static boolean dosomething(int x, long y, String z)
    >
    > for example. Is there any way to do this? The Call...Methods() take
    > the method ID and an array of parameters to pass to the function, but:
    >
    > HOW TO CREATE AN ARRAY OF DIFFERENT DATATYPES???


    See the JNI functions about that; it's off-topic in [comp.lang.c++.moderated],
    but I can assure you it's possible.


    FUT: [comp.lang.java.programmer].

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Alf P. Steinbach, Oct 11, 2004
    #4
    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. Replies:
    13
    Views:
    6,112
  2. Replies:
    3
    Views:
    406
    Lawrence Kirby
    Feb 28, 2005
  3. Henri =?ISO-8859-15?Q?Schom=E4cker?=
    Replies:
    1
    Views:
    303
    Henri =?ISO-8859-15?Q?Schom=E4cker?=
    Dec 17, 2005
  4. Vlastimil Brom
    Replies:
    0
    Views:
    264
    Vlastimil Brom
    Sep 16, 2010
  5. Achim Domma

    Unify datatypes of different webservices

    Achim Domma, Jul 19, 2006, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    126
    John Saunders
    Jul 20, 2006
Loading...

Share This Page