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!
Chris Uppal - 07 Sep 2006 09:01 GMT
> 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
Gordon Beaton - 07 Sep 2006 09:12 GMT
> 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

Signature
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Gordon Beaton - 07 Sep 2006 14:46 GMT
> 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

Signature
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e