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.