5 messages in net.java.dev.jna.usersRe: [jna-users] Contiguous Structure ...
FromSent OnAttachments
ThinnerDec 12, 2008 1:11 pm 
Timothy WallDec 12, 2008 4:22 pm 
ThinnerDec 12, 2008 5:30 pm 
Timothy WallDec 12, 2008 6:45 pm 
ThinnerDec 13, 2008 2:45 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:Re: [jna-users] Contiguous Structure arrayActions...
From:Timothy Wall (twal@dev.java.net)
Date:Dec 12, 2008 6:45:37 pm
List:net.java.dev.jna.users

Structure.ByReference is only a tagging interface. You need to use fp_print_data.ByReference[].

On Dec 12, 2008, at 8:31 PM, Thinner wrote:

Thank you for the fast reply Timothy.

I've changed my Structure to: public class fp_print_data extends Structure { public static class ByReference extends fp_print_data implements Structure.ByReference { }

public short driver_id; public int devtype; public int type; public int length; public byte[] data;

public fp_print_data(){ data = new byte[4]; } }

and my invokation to public static void main(String[] args){ fp_print_data.ByReference enrolledPrint = myLibrary.INSTANCE.enroll(); // The array must be NULL-terminated, thus I need to allocate one extra element Structure.ByReference[] printGallery = new Structure.ByReference[2]; printGallery[0] = enrolledPrint; myLibrary.INSTANCE.identify(printGallery, printGallery.length-1); }

But at runtime I get an IllegalArgumentException: Unsupported array argument type: interface com.sun.jna.Structure$ByReference cast by the call to myLibrary.INSTANCE.identify(). What am I missing?

On Sat, Dec 13, 2008 at 1:23 AM, Timothy Wall <twal@dev.java.net> wrote:

On Dec 12, 2008, at 4:11 PM, Thinner wrote:

Hello.

I'm trying to connect a fingerprint device to a Java app using libfprint (http://reactivated.net/fprint/). I've come a long way but have now hit a roadblock. I'm new to JNA, so please excuse any obvious errors.

I have a C function, enroll(), that returns the fingerprint data as a struct fp_print_data to my Java app that stores this data in a fp_print_data object, this object is then stored for later identification. Function, struct and object definitions are listed below. This part works fine, I think :)

Now I want to take this stored print and compare this to a freshly scanned print. This is done by sending an array of fp_print_data Structures to a C function called identify(). identify() then returns the index of the print in the array that matched the freshly scanned print. The problem is that I get a segmentation fault. If I pass empty fp_print_data objects to the function I don't get the seg fault so I guess I have a problem with my fp_print_data Structure. It also works fine if I leave out the Java app all together and let a C program do all the work. My guess is that the array of Structures I send to idenfity() is not allocated correctly. I'd appreciate any help I can get :)

C: struct fp_print_data { uint16_t driver_id; uint32_t devtype; enum fp_print_data_type type; size_t length; unsigned char data[0]; };

struct fp_print_data* enroll (void){ // print_data is a globally defined struct fp_print_data* fp_enroll_finger(sel_dev, &print_data); }

size_t* identify(struct fp_print_data **print_gallery, int prints){ size_t *match_offset = -1; int result = -1; print_gallery[prints] = NULL; // The list must be NULL terminated fp_identify_finger(sel_dev, print_gallery, &match_offset); return match_offset; }

Java: public class fp_print_data extends Structure {

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

public int driver_id; public int devtype; public int type; public int length; public IntByReference data; }

driver_id is probably 16 bits, not 32. data is an inline array of bytes, so you'll have to initialize it yourself. you can default to byte[4]. If you don't have to read/ write the data, then you don't have to do anything further.

The function is expecting an array of pointer to struct, not an array of struct, so you'll need to pass an array of Structure.ByReference.

public static void main(String[] args){ fp_print_data enrolledPrint = myLibrary.INSTANCE.enroll(); // The array must be NULL-terminated, thus I need to allocate one extra element Structure[] printGallery = enrolledPrint.toArray(2); myLibrary.INSTNCE.identify(printGallery, printGallery.length-1); }