JNI:native returns an object that contains arrays

Discussion in 'Java' started by theodosisx, Jun 10, 2005.

  1. theodosisx

    theodosisx Guest

    Hi,

    I have a "Response" class in my application, that looks like that:

    class Response
    {
    long[] l_arr = new long[3];
    int[] i_arr = new int[5];
    }

    and I declared a native function that returns a Response object.

    public native Response fillValuesFromC().

    I find the IDs of the arrays (GetFieldID) but I don't know how to
    assign values from C-arrays to them. When it is done, does the
    returning object contains the values?

    Does anyone have an idea or an example?

    thanks

    Theodosis
     
    theodosisx, Jun 10, 2005
    #1
    1. Advertisements

  2. You need more than the IDs, you need references to the arrays
    themselves. Use GetObjectField().

    To actually fill the arrays with values, use SetLongArrayElement() (or
    SetLongArrayRegion()) and SetIntArrayElement() (or
    SetIntArrayRegion()).
    Most likely.

    /gordon
     
    Gordon Beaton, Jun 10, 2005
    #2
    1. Advertisements

  3. Hi Gordon,

    thanks for your answer. It is done for arrays of primitive types.
    Now, I have an array of Strings in the same class (String[] sNames)
    and I would like to fill this array according to a *char[] array from
    C.

    Is it possible? There isn't any SetStringArray... (...) function in JNI.

    thanks in advance

    Theodosis
     
    Theodosis Ekizoglos, Jun 13, 2005
    #3
  4. theodosisx

    Sonal Guest

    Hi,

    You need to define a jobjectArray ret

    ret = (jobjectArray) (env)->NewObjectArray(length of out params,
    env->FindClass("java/lang/String"),
    env->NewStringUTF(""));.

    Then create UTF strings :

    jstring arrayElement = (jstring) env->NewStringUTF(C String);

    Set the above String in the array at the kth position

    env->SetObjectArrayElement(ret,k , arrayElement);

    Hope this helps!

    Sonal
     
    Sonal, Jun 13, 2005
    #4
  5. theodosisx

    theodosisx Guest

    Thank you Sonal,

    now also String[ ] fields are filled from the C code and returned back
    as part of the entire object.

    Thank you very much Gordon, Sonal.

    Theodosis
     
    theodosisx, Jun 13, 2005
    #5
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.