Mapping Types From C to Java In JNA

Viewed 40

I am trying to map a function in a C library to a java project using JNA. The C function is this:

int foobar(const int a, u8* b);

What are the correct types to use?

Is this correct?

int foobar(int a, int b);
1 Answers

C's int maps to int in JNA, but u8 is an 8-bit unsigned integer (a byte), plus the * means it's a pointer.

The natural interpretation is a ByteByReference where JNA would allocate a byte and return a pointer to it that you would pass to the function. You'd use getValue() on the ByteByReference to extract the returned byte.

However, there may be other reasons to pass a pointer to a byte. Here is where you'll need the API to document what that second parameter does.

Note also that the u is "unsigned" so the returned value will have a native meaning of 0 to 255, while Java only uses unsigned bytes and you'll get a value from -128 to 127. You'll have to convert.

Related