JNI does not return the original value

Discussion in 'Java' started by Neena, Sep 28, 2005.

  1. Neena

    Neena Guest

    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 ;

    }
     
    Neena, Sep 28, 2005
    #1
    1. Advertising

  2. On 27 Sep 2005 22:03:24 -0700, Neena wrote:
    > 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

    --
    [ 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
     
    Gordon Beaton, Sep 28, 2005
    #2
    1. Advertising

  3. Neena

    Roedy Green Guest

    On 27 Sep 2005 22:03:24 -0700, "Neena" <>
    wrote or quoted :

    >(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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Sep 28, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U2FuZHk=?=

    Return user to original view

    =?Utf-8?B?U2FuZHk=?=, Apr 18, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    376
    =?Utf-8?B?QnJpYW4gRmFybmhpbGwgKE1DUCBWQi5ORVQp?=
    Apr 21, 2005
  2. Phoenix
    Replies:
    3
    Views:
    1,392
    Joris Gillis
    Dec 30, 2004
  3. Greenhorn
    Replies:
    15
    Views:
    885
    Keith Thompson
    Mar 6, 2005
  4. MisterEd
    Replies:
    5
    Views:
    214
    Dino Chiesa [Microsoft]
    Oct 28, 2004
  5. mike
    Replies:
    2
    Views:
    97
    Michael Winter
    Mar 2, 2004
Loading...

Share This Page