Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

How to return data through native methods parameters?

Thread view: 
HappyHippy - 07 Sep 2006 08:07 GMT
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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.