Passing an array of objects.

I have been trying to pass an array of objects from java to C. They are simple objects, only fields, no methods.

Example...

class User

{

String name;

int id;

}

public class JNIExample

{

User[] users = new User[10];

public native void nativeCode(User[] usersarray);

static

{

try{

System.loadLibrary("object");

}catch(Exception e)

{

System.out.println("Could not load library.");

}

}

public static void main(String[] args)

{

JNIExample anex = new JNIExample();

anex.nativeCode(anex.users);

}

}

Could someone give me sample code on how to access the array elements and get/set the object values on the C side. In the above code I did not create any of the objects in the array or set their field values but these will be set on the java side.

Any help would be greatly appr.

Thanks,

Darrin

[987 byte] By [dmorris3] at [2007-9-26 13:16:47]
# 1

JNIEXPORT void JNICALL Java_User_nativeCode

(JNIEnv *env, jobject this, jobjectArray arr) {

jsize idx; // array index

// accessing the Users

for(idx = env->GetArrayLength(env, arr); idx-- > 0;) {

jobject user = env->GetObjectArrayElement(env, arr, idx);

// do something with it

}

// setting the Users

for(idx = env->GetArrayLength(env, arr); idx-- > 0;) {

jobject user = ...

env->SetObjectArrayElement(env, arr, idx, user);

}

}

thpreusser at 2007-7-2 13:26:05 > top of Java-index,Java HotSpot Virtual Machine,Specifications...