6 messages in net.java.dev.jna.usersRe: [jna-users] Structure mapping que...
FromSent OnAttachments
Steve NewellJul 30, 2008 11:05 am 
Michael Brewer-DavisJul 30, 2008 11:42 am 
Timothy WallJul 30, 2008 11:48 am 
Steve NewellJul 30, 2008 12:34 pm 
Timothy WallJul 30, 2008 2:48 pm 
Steve NewellJul 31, 2008 12:33 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 mapping questionActions...
From:Timothy Wall (twal@dev.java.net)
Date:Jul 30, 2008 11:48:04 am
List:net.java.dev.jna.users

On Jul 30, 2008, at 2:06 PM, Steve Newell wrote:

I have the following C structs:

typedef struct ByteSignature { int offset; int length; PBYTE signature; } ByteSignature;

typedef struct FileSignature { char* mimeType; // NULL terminated string int numSignatures; // number of ByteSignatures in the signatures array ByteSignature* signatures; // array of ByteSignatures. There must be at least 1, to be valid. } FileSignature, * PFileSignature, ** PPFileSignature;

FileSignature is allocated and filled by my Java code, and passed to the dll.

I have mapped these two structures as follows:

public class ByteSignature extends Structure { public int offset; public int length; public byte[] signature;

The signature field needs to be a Pointer. Primitive arrays are inlined, which is not what you want.

public ByteSignature() { super(); } public ByteSignature( Pointer p ) { super(); useMemory( p ); read(); } public ByteSignature( int offset, byte... byteValues ) { super(); this.offset = offset; this.length = byteValues.length; signature = new byte[ byteValues.length ];

for( int i = 0; i < byteValues.length; i++ ) { signature[ i ] = byteValues[ i ]; } } }

You probably want to allocate the memory yourself here for "signature", and use Pointer methods to set its contents.

public class FileSignature extends Structure { public String mimeType; public int numSignatures; public ByteSignature[] signatures;

This needs to be a pointer, since nested structures are inlined. You can also make the field implement Structure.ByReference to force the field to be treated as a pointer rather than by value.

e.g.

class ByteSignature extends Structure { class ByReference extends ByteSignature implements Structure.ByReference { } }

ByteSignature[] bsa = new ByteSignature.ByReference().toArray(size); s.signatures = bsa[0];

public FileSignature() { super(); } public FileSignature( Pointer p ) { super(); useMemory( p ); read(); } }

The function I am attempting to call looks like this:

public int FindMatchingDeletedFiles( WString szDirectory, FileSignature[] fileSignatures, int numFileSignatures, PointerByReference fileHandles );

and this is how I am calling it:

public static void main( String[] args ) { Native.setProtected( true );

Undelete undelete = Undelete.INSTANCE;

PointerByReference fileHandles = new PointerByReference();

FileSignature[] signatures = createFileSignatures();

int numFiles = undelete.FindMatchingDeletedFiles( new WString( "C:\ \" ), signatures, signatures.length, fileHandles );

for( int i = 0; i < numFiles; i++ ) { ... } }

However, when I run this, I get the following error:

Exception in thread "main" java.lang.IllegalStateException: Array fields must be initialized

This error should go away once you eliminate primitive array fields as described above.