How to return data through native methods parameters?

H

HappyHippy

Hi,

I have a native method which gets an Integer object as a parameter. I
want to create a new Integer object in the method's C/C++ implementation
and return it through the same parameter.

The code I wrote (it actually fails to return data) looks like this:

JNIEXPORT jboolean JNICALL
Java_Java2NativeEngineInterface_startRecognitionNB(JNIEnv *env, jclass,
jobject nHandle)
{
int hResHndl;

if(sr_startRecognition_nb(&hResHndl) != 0)
return false;

jclass objClass = env->GetObjectClass(nHandle);
jmethodID methodId = env->GetMethodID(objClass, "<init>", "(I)V");

if(methodId == 0)
return false;

//This doesn't work... nHandle does not get assigned to a newly
created object
nHandle = env->NewObject(objClass, methodId, hResHndl);

return true;
}

Is there any way to make nHandle point to a new object?

Thank you!
 
C

Chris Uppal

HappyHippy said:
I have a native method which gets an Integer object as a parameter. I
want to create a new Integer object in the method's C/C++ implementation
and return it through the same parameter.

You can't. It's that simple.

The solution is exactly the same as it would be in Java -- i.e. hack it or
redesign it.

-- chris
 
G

Gordon Beaton

I have a native method which gets an Integer object as a parameter.
I want to create a new Integer object in the method's C/C++
implementation and return it through the same parameter.
//This doesn't work... nHandle does not get assigned to a newly created object
nHandle = env->NewObject(objClass, methodId, hResHndl);

Actually it does assign nHandle, but parameters in C are equivalent to
local variables, so changing what nHandle refers to doesn't have any
effect on what it refers to in the caller.

It's the same as if you'd done this:

void baz(int a) {
a = a+1;
System.out.println(a);
}

void foo() {
int a = 10;
System.out.println(a); /* prints 10 */
baz(a); /* prints 11 */
System.out.println(a); /* prints 10 again */
}

The value of a is indeed modified by the operation, but it has no
effect after leaving the method. It's not the *same* a in both places,
the called method gets a copy.

Is there any way to make nHandle point to a new object?

Not in the caller's scope. The logical choice is to either *return*
the value, i.e.:

return env->NewObject(...);

or pass a container object that the native function can update
instead.

/gordon
 
G

Gordon Beaton

I have a native method which gets an Integer object as a parameter.
I want to create a new Integer object in the method's C/C++
implementation and return it through the same parameter.

After looking at this again, I wonder why you don't simply return the
int (not Integer) you get from sr_startRecognition_nb(), and let the
Java part of your application deal with creating and assigning the new
Integer object?

/gordon
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top