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 2005

Tip: Looking for answers? Try searching our database.

JNI does not return the original value

Thread view: 
Neena - 28 Sep 2005 06:03 GMT
Hi

I have a java code, which calls my native method setvalue. the setvalue
function receives values from my java code and the values are of type
java.lang.object.the value may be String, Long. Integer, byte[] etc.
the native method receives the value as jobject.my native call makes
another call to set the value and for this i have to convert the
jobject to a void pointer. i just make a direct casting.and the value
gets printed before and after casting are the same.

then from java i make a call to my native method getValue. getValue
makes another call and from there getValue receive the value as a
voidpointer. in Getvalue i have to convert this to a bytearray and then
return to my java call. in java i have to convert this to Object and
return to the application.

And my problem is i have passed a Long value( Long l=new Long(1); ) to
my setValue function. and my getValue returns unexpected value...what i
get back is not the value i've set..

what may be the problem?

my code:
setValue:

JNIEXPORT jboolean JNICALL Java_SetName_setValue
(JNIEnv *env, jobject obj,  jobject nameValue)
{

       printf("Name value in c, %x",nameValue);

      return UpperLayerCall_SetValue((PVOID)nameValue);
    //PVOID: void pointer

}

getValue:

JNIEXPORT jbyteArray JNICALL
Java_GetName_getValue
(JNIEnv *env, jobject obj,  jint valueID, jlong valueLength)

{

    PVOID    nameValue = NULL; //void pinter..it is memory allocated

    jbyteArray  nameValueArray    = NULL;

    UpperLayerCall_GetValue( (int)valueID), nameValue);
    //after this call nameValue will contain value we have set earlier

    nameValueArray = (*env)->NewByteArray(env, valueLength);

    (*env)->SetByteArrayRegion(env, nameValueArray, 0, valueLength,
nameValue);

     
    return nameValueArray ;
   
}
Gordon Beaton - 28 Sep 2005 09:02 GMT
> And my problem is i have passed a Long value( Long l=new Long(1); ) to
> my setValue function. and my getValue returns unexpected value...what i
> get back is not the value i've set..
>
> what may be the problem?

- it isn't clear exactly what UpperLayer_SetValue() and
 UpperLayer_GetValue() do exactly, but I'll assume that one stores
 the original jobject pointer, and the other retrieves it unchanged.

- it isn't clear what valueLength contains, or how you know what
 length to provide.

- it isn't clear how the Java application attempts to convert the byte
 array back to a Long.

However none of these things really matter. The important thing to
realize is that after calling UpperLayer_GetValue(), nameValue does
*not* contain the Long "value". It is a pointer to a JVM-internal
structure that (ultimately) contains the value. To retrieve the value
itself you must use JNI functions made for that purpose, or pass it as
a Long back to the caller. These bytes are not meaningful to your Java
application!

So basically it seems that the native method is filling a byte array
with garbage and you are expecting your Java application to somehow
make sense of it.

Since it isn't clear what real problem you are trying to solve, it
isn't possible to suggest a solution.

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

Roedy Green - 28 Sep 2005 09:49 GMT
>(PVOID)nameValue

You should not do that.  You can only manipulate the pointers handed
to you via the remote-control methods.  For a list of them see
http://mindprod.com/jni.html

JNI is like eating with metre long chopsticks. You are figuratively
speaking trying to eat with your hands.

In addition to that, you are going to have to cast.  I have never cast
one flavour of Java object to another on the C side.  I suggest
reading one of the JNI textbooks for how you go about that. I would
not trust a C-style cast to be sufficient.

Your JNI project seems peculiar. It looks much more suited to Java
that C.  Do your casting on the Java side to simplify the C side.

Consider passing in a structure with many fields, and putting your
return value in one of the slots, and then on the Java side sorting
out just what you got.  That would keep things simple on the C side.

For passing data in, you can have a different, possible overloaded,
method for each type, or again you can pass in a common structure with
just one of its fields filled in and an indicator which one.

You can think of it as a bit like a union that does not overlap.

JNI is so (complicated, persickity, fussy, delicate, fragile) you have
to make everything duck simple to have a change at getting it all to
work.

At any case, you might start by making one of my simplifications, and
once you get that working inch your way back to your original design a
step at a time.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.



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.