Java Native Interface - passing parameter array of different datatypes

C

Clemens Arth

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
 
C

Chris Uppal

Clemens said:

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
 
M

Michael Saunby

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

Alf P. Steinbach

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

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top