JNI:native returns an object that contains arrays

T

theodosisx

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
 
G

Gordon Beaton

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.

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()).
When it is done, does the returning object contains the values?

Most likely.

/gordon
 
T

Theodosis Ekizoglos

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
 
S

Sonal

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
 
T

theodosisx

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top