10 messages in net.java.dev.jna.usersRe: [jna-users] Problem with Structur...
FromSent OnAttachments
Rob CopeSep 13, 2007 9:22 pm 
Rob CopeSep 13, 2007 9:36 pm 
Rob CopeSep 13, 2007 9:37 pm 
Rob CopeSep 13, 2007 9:59 pm 
Timothy WallSep 14, 2007 4:50 am 
Timothy WallSep 14, 2007 4:58 am 
Rob CopeSep 14, 2007 8:17 am 
Rob CopeSep 14, 2007 9:17 am 
Timothy WallSep 14, 2007 9:27 am 
Rob CopeSep 14, 2007 7:19 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] Problem with Structure[] field in StructureActions...
From:Rob Cope (rwco@comcast.net)
Date:Sep 13, 2007 9:37:35 pm
List:net.java.dev.jna.users

By the way, I also tried this:

public static class SecKeychainAttributeList extends Structure { public int count; public Structure[] attr; }

and then setting up the call like this:

SecKeychainAttribute attr = new SecKeychainAttribute(); ... SecKeychainAttributeList attrList = new SecKeychainAttributeList(); attrList.attr = attr.toArray(1);

That avoided the NullPointerException, but gave me this instead:

java.lang.IllegalArgumentException: Native type undefined for class com.sun.jna.Structure at com.sun.jna.Structure.getNativeSize(Structure.java:704) at com.sun.jna.Structure.getNativeSize(Structure.java:723) at com.sun.jna.Structure.getNativeAlignment(Structure.java:651) at com.sun.jna.Structure.getNativeAlignment(Structure.java:668) at com.sun.jna.Structure.calculateSize(Structure.java:607) at com.sun.jna.Structure.allocateMemory(Structure.java:139) at com.sun.jna.Structure.write(Structure.java:374) at com.sun.jna.Function.convertArgument(Function.java:338) at com.sun.jna.Function.invoke(Function.java:177) at com.sun.jna.Library$Handler.invoke(Library.java:170) at $Proxy0.SecKeychainItemModifyAttributesAndData(Unknown Source) at keychaintest.Main.modifyPassword(Main.java:183) at keychaintest.Main.main(Main.java:204)

Thanks, Rob

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) at com.sun.jna.Structure.getNativeAlignment(Structure.java: 651) at com.sun.jna.Structure.getNativeAlignment(Structure.java: 668) at com.sun.jna.Structure.calculateSize(Structure.java:607) at com.sun.jna.Structure.allocateMemory(Structure.java:139) at com.sun.jna.Structure.write(Structure.java:374) at com.sun.jna.Function.convertArgument(Function.java:338) at com.sun.jna.Function.invoke(Function.java:177) at com.sun.jna.Library$Handler.invoke(Library.java:170) at $Proxy0.SecKeychainItemModifyAttributesAndData(Unknown Source) at keychaintest.Main.modifyPassword(Main.java:177) at keychaintest.Main.main(Main.java:198)

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 }

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) ...

Bummer. So I tried this:

public static class SecKeychainAttributeList extends Structure { public int count; public Pointer attr; }

But that just crashes the VM (result 138 on Mac OS 10.4.9, Java 1.5.0_07-87). Any advice?