Discussion:
JNA char** usage
t***@public.gmane.org
2011-04-15 18:31:07 UTC
Permalink
JNA has a StringArray function (also a PointerArray) whose purpose is
to be used in situations like this. The array of char* is a fairly
common idiom in C, so JNA provides StringArray(String[]) in order to
pass that sort of thing to C functions. Behind the scenes it converts
the Java strings into an array of pointers and populates it
appropriately. It'll also check for modifications after the function
call and update the original array if the native function changed
anything.
t***@public.gmane.org
2011-04-15 18:38:21 UTC
Permalink
BTW, you should generally avoid having your C program "return" memory
to Java.

If the individual C strings are malloc'ed by the called function,
you'll probably be better off simply using a Pointer[] argument.
You'll have to manually set up the pointers if you need them for input,
and manually copy out the strings after the call (don't forget to free
the memory once you've copied the strings).

If you have a limit on the length of the "returned" strings, you might
be better off just passing in a block of memory and having the called
function fill it in appropriately. That avoids the issue of returning
memory to a client who then has to be responsible for freeing it.
Continue reading on narkive:
Loading...