6 messages in net.java.dev.jna.usersRe: [jna-users] Structure with nested...
FromSent OnAttachments
Gabriele ForbitiDec 21, 2007 2:20 am 
Timothy WallDec 21, 2007 6:40 am 
Gabriele ForbitiDec 21, 2007 9:26 am 
Timothy WallDec 21, 2007 2:46 pm 
Gabriele ForbitiDec 22, 2007 10:12 am 
Timothy WallDec 22, 2007 12:30 pm 
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:Re: [jna-users] Structure with nested array of structuresActions...
From:Gabriele Forbiti (4bi@saitweb.it)
Date:Dec 21, 2007 9:26:54 am
List:net.java.dev.jna.users

There's a bug in getNativeSize and getNativeAlignment; the code should check if value is null and if so, assign it the result of newInstance(type). I'll apply a patch shortly.

As a temporary workaround, you can do "new MyStructure().toArray (length)" to get a fully populated array which should avoid the error.

Sorry, no luck. Maybe I'm doing something wrong. Here's the new class according to what I understood from your explanation of the workaround:

public interface TestInterface extends Library { public static class Child extends Structure { public char field1 = '\0'; public int field2 = 0; }

public static class Parent extends Structure { public Child[] children = new Child[128]; //public Child child = new Child();

public Parent() { this.children = (Child[])new Child().toArray(128);

allocateMemory(); } } }

Note that I have to cast the "children" array as the .toArray(128) method gives me an array of Structure objects. Still receiving the same error on executing "allocateMemory();".

java.lang.NullPointerException at com.sun.jna.Structure.getNativeSize(Structure.java:838) at com.sun.jna.Structure.getNativeAlignment(Structure.java:766) at com.sun.jna.Structure.getNativeAlignment(Structure.java:789) at com.sun.jna.Structure.calculateSize(Structure.java:726) at com.sun.jna.Structure.allocateMemory(Structure.java:191) at TestInterface$Parent.<init>(TestInterface.java:24) at Test.main(Test.java:22)

Cheers.

On Dec 21, 2007, at 5:21 AM, Gabriele Forbiti wrote:

Hello,

I have come across the following issue and I hope someone can help. I need to call methods from a third party native dll for which I only have a header file and the binary dll. My JRE is 1.4.2_08.

The issue exists when I declare java classes to map a C structure that contains an array of structures. In detail, the C header file defines the structures something like:

typedef struct _ChildStruct{ byte byteField; char charArray[24+1]; } ChildStruct;

typedef struct _ParentStruct { ChildStruct children[128]; } ParentStruct;

DLL_API int __cdecl MyMethod(ParentStruct *parent);

So I declared the mapping java structures as follows:

public interface TestInterface extends Library { public static class Child extends Structure { public byte byteField = 0; public char[] charArray = new char[24+1]; }

public static class Parent extends Structure { public Child[] children = new Child[128]; //public Child child = new Child();

public Parent() { allocateMemory(); } } }

I receive an error when the "allocateMemory();" method is executed in the Parent class constructor, which means that the native method hasn't been called yet. Here's the stack trace:

java.lang.NullPointerException at com.sun.jna.Structure.getNativeSize(Structure.java:838) at com.sun.jna.Structure.getNativeSize(Structure.java:845) at com.sun.jna.Structure.calculateSize(Structure.java:725) at com.sun.jna.Structure.allocateMemory(Structure.java:191) at TestInterface$Parent.<init>(TestInterface.java:22) at Test.main(Test.java:21) Exception in thread "main"

Looking at jna source code it looks like the

protected int getNativeSize(Class type, Object value)

method receives NULL for the variable "value" which means that the instruction:

Structure s = (Structure)value;

casts "s" to a NULL value and subsequently the "return s.size();" instruction fails with NullPointerException.

Please note the commented field "public Child child = new Child();" in the Parent class. If I declare a single child, instead of an array of children, execution is fine.

If this is a bug with jna, is there a workaround for the issue?

Thank you in advance for any help you can give.