C++ -> Java -- Multiple Return Values
I am in need to multiple values from a method.
A status code (int) and a return value (double).
From what I have read -- it is customary to use an object array to handle this, since java does not support out parameters.
When I return the Object[] through JNI as a jobjectArray how do I obtain(cast) the values as int and double?
This is how I am getting the values from the array via JNI. However the cast to jint does not work as expected.
jsize numOfObjects = m_env->GetArrayLength(attrReturnArray);
if ( numOfObjects >= 2 )
{
jint status = (jint)m_env->GetObjectArrayElement(attrReturnArray, 0);
jint retValue = (jint)m_env->GetObjectArrayElement(attrReturnArray, 1);
}
Any sugestions?
[859 byte] By [
Shawn@@a] at [2007-10-3 8:43:15]

So maybe my thinking is skewed then... So let us revisit that.
Typical Java -- Return a value on success, throw an exception on failure
C++ -- Can't understand java exceptions, but can handle error codes(int) just fine.
My thoughts --
Why not wrap groups java code together and return success/error codes alonf with a return value. The advantage would be writing less JNI code, and having easy to understand error messages. This is where the multiple return values came into play -- status code and return value.
Is this not a best practice?
Is there a better way to deal with exceptions occurring in java on the JNI (c++) side of things?
Thanks.
Which way are you trying to use JNI?
o Java -> C++, then return?
In this case, maybe you are better off sticking with the return value or exception model.
o C++ -> java, then return?
Here I guess you can do anything you want, but probably returning some kind of object makes sense (although you could, in fact, check for a java exception on return). If you return an object, then just give it two members with "getters": One returns a status code, the other - if meaningful - returns some data.
> Which way are you trying to use JNI?
>
> o Java -> C++, then return?
>
> In this case, maybe you are better off sticking with
> the return value or exception model.
>
> o C++ -> java, then return?
>
> Here I guess you can do anything you want, but
> probably returning some kind of object makes sense
> (although you could, in fact, check for a java
> exception on return). If you return an object, then
> just give it two members with "getters": One returns
> a status code, the other - if meaningful - returns
> some data.
Calling java from c++
Using the invocation interface, you can call java methods, but you can also ask the JVM if an exception has been thrown. So one possible pattern is
o Make the java call.
o Ask the JVM about an exception. (You can also get a reference to the exception that was thrown.)
o No exception - use the data that was returned.
Note that testing for an exception turns off the exception condition.