

![]() | Start a set with this search |
![]() | Include this search in one of my sets |
![]() | Exclude this search from one of my sets |
![]() | Permalink to these results Paste this link in email or IM: |
| Atom feed for tracking future search results Paste this URL into your reader: |
10 messages in net.java.dev.jna.usersRe: [jna-users] Problem with Structur...| From | Sent On | Attachments |
|---|---|---|
| Rob Cope | Sep 13, 2007 9:22 pm | |
| Rob Cope | Sep 13, 2007 9:36 pm | |
| Rob Cope | Sep 13, 2007 9:37 pm | |
| Rob Cope | Sep 13, 2007 9:59 pm | |
| Timothy Wall | Sep 14, 2007 4:50 am | |
| Timothy Wall | Sep 14, 2007 4:58 am | |
| Rob Cope | Sep 14, 2007 8:17 am | |
| Rob Cope | Sep 14, 2007 9:17 am | |
| Timothy Wall | Sep 14, 2007 9:27 am | |
| Rob Cope | Sep 14, 2007 7:19 pm |

![]() | Permalink for this message Paste this link in email or IM: |
![]() | Permalink for this thread Paste this link in email or IM: |
| Atom feed for this thread Paste this URL into your reader: |
| Subject: | Re: [jna-users] Problem with Structure[] field in Structure | Actions... |
|---|---|---|
| From: | Timothy Wall (twal...@dev.java.net) | |
| Date: | Sep 14, 2007 4:58:02 am | |
| List: | net.java.dev.jna.users | |
On Sep 14, 2007, at 12:22 AM, Rob Cope wrote:
I have two Structures defined thusly:
public static class SecKeychainAttribute extends Structure { public int tag; public int length; public String data; }
public static class SecKeychainAttributeList extends Structure { public int count; public SecKeychainAttribute[] attr; }
Then I defined a native Library with the following method:
int SecKeychainItemModifyAttributesAndData( Pointer itemRef, SecKeychainAttributeList attrList, int length, String pw);
When I try to call that method, I get the following NullPointerException (this is using the latest source in the svn trunk):
java.lang.NullPointerException at com.sun.jna.Structure.getNativeSize(Structure.java:712)
The relevant bits start in Structure.getNativeAlignment, line 668:
667 else if (type.isArray()) { 668 alignment = getNativeAlignment(type.getComponentType (), null, firstElement); 669 }
Note that null is passed as the value. Then getNativeAlignment line 651:
651 int size = getNativeSize(type, value);
So that is passing null as the value to getNativeSize. Finally, in Structure.getNativeSize, like 712:
708 protected int getNativeSize(Class type, Object value) { 709 if (Structure.class.isAssignableFrom(type)) { 710 Structure s = (Structure)value; 711 // inline structure 712 return s.size(); //There's the NPE 713 }
This should instead report that the array member of the structure has not yet been initialized. Normally when this is encountered, the size calculation is deferred, which allows structures to initialize their fields inline without requiring a dedicated constructor.
I'm not totally sure what to do about this, however. Checking for value != null in getNativeSize only throws an exception later in getNativeSize(Class cls), since the class in this case is a Structure.
I tried changing SecKeychainAttributeList to this:
public static class SecKeychainAttributeList extends Structure { public int count; public Pointer[] attr; }
and then setting it up like this:
SecKeychainAttribute attr = new SecKeychainAttribute(); ... SecKeychainAttributeList attrList = new SecKeychainAttributeList(); attrList.attr = new Pointer[1]; attrList.attr[0] = attrPtr; attrList.count = attrList.attr.length; attr.write(); int result = keychain.SecKeychainItemModifyAttributesAndData (itemRef, attrList, pw.length(), pw);
But then I just get this;
java.lang.IllegalArgumentException: Inline array of class com.sun.jna.Pointer not supported at com.sun.jna.Structure.writeField(Structure.java:496) ...
Pointer[] is technically a primitive (native) type, and should be allowed. It's an oversight that it is not; I'll make sure it gets enabled. Come to think of it, any array of NativeMapped should be supported.
Keep in mind that any Structure within a Structure is inlined (i.e. the memory for the inner structure is contiguous with the outer). If you want the structure's address, you have to explicitly use a Pointer.
Could you please enter an issue for supporting Pointer[] so this gets properly tracked?
Thanks, T.







