Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to return data through native methods parameters?

Reply
Thread Tools

How to return data through native methods parameters?

 
 
HappyHippy
Guest
Posts: n/a
 
      09-07-2006
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!
 
Reply With Quote
 
 
 
 
Chris Uppal
Guest
Posts: n/a
 
      09-07-2006
HappyHippy wrote:

> 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


 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      09-07-2006
On Thu, 07 Sep 2006 03:07:41 -0400, HappyHippy wrote:
> 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

--
[ 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
 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      09-07-2006
On Thu, 07 Sep 2006 03:07:41 -0400, HappyHippy wrote:
> 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

--
[ 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
native methods from java BARBARA Java 1 10-07-2003 04:55 PM
ArrayList and native methods Duke Java 1 06-30-2003 07:51 AM



Advertisments