JNI and output parameters

  • Thread starter Robert William Vesterman
  • Start date
R

Robert William Vesterman

I have native C++ functions that each have various output parameters.
For an oversimplified example, let's say:

void setTo37 ( short *num )
{
*num = 37;
return;
}

To access such things from Java, my original plan was to use the Short
class rather than the short type, so I could do something inside the
C++ side of the JNI code like:

JNIEXPORT void JNICALL Java_Blah_Native_1setTo37Wrapper (
JNIEnv *env, jclass cls, jobject jniNum )
{
short tempNum;
setTo37 ( &tempNum );

jclass clsShort = env->GetObjectClass ( jniNum );
jmethodID setVal = env->GetMethodID(clsShort, "setValue", "(S)");

env->CallVoidMethod ( jniNum, setVal, tempNum );
}

But, the obvious flaw in my plan is that there's no such method as
"setValue" (or anything like that) in the Short class.

So what do people normally do in this situation? I can think of a few
possibilities, but I don't like any of them:

(1) Instead of passing a Short, I could pass a BobShort, that's kind
of like a Short except that it has a method to allow you to change its
value;

(2) Instead of having the native function return its natural return
value, I could have it create and return some new class that's
specifically just a wrapper for its return value and the values of all
of the underlying output parameters;

(3) I could have the main JNI function remember (in the C++ side) the
values of the underlying output parameters, then require the Java side
to call additional JNI functions along the lines of
"getOutputParameterX()" and "getOutputParameterY()".

Any of these would work, but none seem copacetic. So, does anyone
have a better way?

Thanks in advance for any help.

Bob Vesterman.
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top