Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Java Native Interface - passing parameter array of different datatypes (http://www.velocityreviews.com/forums/t137530-java-native-interface-passing-parameter-array-of-different-datatypes.html)

Clemens Arth 10-10-2004 09:21 AM

Java Native Interface - passing parameter array of different datatypes
 
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! ]

Chris Uppal 10-10-2004 11:09 PM

Re: Java Native Interface - passing parameter array of different datatypes
 
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/tutor...1.1/index.html

and particularly:

http://java.sun.com/docs/books/tutor...ng/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! ]

Michael Saunby 10-10-2004 11:10 PM

Re: Java Native Interface - passing parameter array of different datatypes
 

"Clemens Arth" <Belveder@sbox.tu-graz.ac.at> wrote in message
news:74e710b2.0410090001.5729f326@posting.google.c om...
....

>
> 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! ]

Alf P. Steinbach 10-10-2004 11:12 PM

Re: Java Native Interface - passing parameter array of different datatypes
 
* 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! ]


All times are GMT. The time now is 12:50 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.