5 messages in net.java.dev.jna.usersRe: [jna-users] Getting IllegalArgume...
FromSent OnAttachments
Niels van EijckJan 21, 2008 3:13 am 
Timothy WallJan 21, 2008 8:07 am 
Niels van EijckJan 22, 2008 4:30 am 
Timothy WallJan 22, 2008 5:59 am 
Niels van EijckJan 22, 2008 7:04 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] Getting IllegalArgumentException when using Structure[] as method argumentActions...
From:Timothy Wall (twal@dev.java.net)
Date:Jan 21, 2008 8:07:10 am
List:net.java.dev.jna.users

On Jan 21, 2008, at 6:14 AM, Niels van Eijck wrote:

I'm trying to call a native method that has an array of structs as a method argument. I keep getting an IllegalArgumentException: Structure array elements must use contiguous memory (at element index 1)

I've made some example code to illustrate/reproduce my problem. I'm using the latest JNA code from svn (revision 453).

interface Test extends Library {

class MyNumber extends Structure { public short num; }

int calculateSum(MyNumber[] numbers); }

The proper way to initialize an array of Structure is with Structure.toArray.

When I link to the DLL and I call calculateSum from a Junit test I get the exception. My C++ code looks like this (although I think the problem doesn't lie there):

typedef struct MyNumber { unsigned short num; } MyNumber;

TEST_API int calculateSum(MyNumber* numbers) {

int size = sizeof(numbers) / sizeof(MyNumber);

sizeof(array)/sizeof(array[0]) only works with arrays. The above expression evaluates to sizeof(MyNumber*)/sizeof(MyNumber), which is probably "2" and not what you wanted.

int result = 0;

for (int i = 0; i < size; i++) { result += numbers[i].num; }

return result; }

Did I stumble upon a bug or am I doing something wrong? Any help will be greatly appreciated!

Thanks, Niels.