3 messages in net.java.dev.jna.usersNested structure array problem
FromSent OnAttachments
Zaharescu ValentinJan 21, 2008 4:23 am 
Timothy WallJan 21, 2008 8:16 am 
Zaharescu ValentinJan 22, 2008 9:04 am 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Nested structure array problemActions...
From:Zaharescu Valentin (zva@yahoo.com)
Date:Jan 21, 2008 4:23:37 am
List:net.java.dev.jna.users

Hello,

till now I could manage well with mapping from Native C. However I reached a point where the memory is not filled correctly, it's always null for an internal list.

Here the the C code:

typedef struct MyStructList { MyStruct **list; int64_t length; } MyStructList;

where MyStruct is defined as follows:

typedef struct MyStruct { int8_t *name; int64_t index; } MyStruct;

and

DLLAPI void testMyStruct(const MyStructList *structList);

I have mapped MyStruct to JNA as follows: public class MyStruct extends Structure implements Serializable { public static class ByReference extends MyStruct implements
Structure.ByReference{ }

public String name; public long index;

public MyStruct() { }

public MyStruct(String name, long index) { this.name = name; this.index = index; } }

Somehow I tried several possibilities to map MyStructList, here one guess: public class MyStructList extends Structure implements Serializable {

public static class ByReference extends MyStructList implements Structure.ByReference { }

public MyStruct.ByReference[] list; public long length; ... public MyStructList(MyStruct[] myStruct) {

this.length = myStruct.length; list = new MyStruct.ByReference[(int)this.length]; for(int i=0; i<this.length; i++) { MyStruct.ByReference tb = new MyStruct.ByReference();; tb.name = myStruct[i].name; tb.index = myStruct[i].index; list[i] = tb; } } }

I tested with one element. In the Java debugger everything seems to be correct, however on the C side, the first entry in the list is NULL.

I tried also some different mapping, but without success.

I also checked out the JNA sources and took a look to the given examples and to the JUnit tests. The test case testStructureByReferenceArrayField uses a structure similar to that I'm using.
Running this test case I observed that after s.write() and s.read() are executed, the content of the variable s is not the same as before these calls (the field of the inner structure are set to 0, maybe not set at all), like I would expect.

Is this a bug or am I doing something wrong?

Thanks in advance, Valentin