On Aug 6, 2008, at 2:06 PM, Corey Puffalt wrote:
All...
From searching the archives my understanding was that JNA handled
automatically mapping C char ** to Java String[] (where the array is
assumed to be NULL terminated.) I'm trying to do this with a
Callback that returns a String[] but JNA says:
Exception in thread "main" java.lang.IllegalArgumentException:
Callback return type class [Ljava.lang.String; requires custom type
conversion
Here's the relevant C code definition:
typedef char** (*directories_list_cb)();
extern void register_directory_list_callback(directories_list_cb);
On the Java side I define it as:
interface DirectoryListCallback extends Callback
{ String[] callback(); }
void register_directory_callback(DirectoryListCallback cb);
Is JNA supposed to handle this type of mapping automatically or did
I misunderstand the postings I read?
Return values are a different proposition than arguments (and
callbacks slightly different in handling than functions). Having a
callback return memory is problematic to do automatically, since you
can't tell when the returned memory should be reclaimed.
There is a StringArray object that handles the mapping from Java
String[] to native.
You must either change the signature of your callback to return
Pointer, or set up a type mapper to convert String[] into Pointer. In
either case, you'll need to hold onto a reference to the memory
(StringArray or otherwise) until the native code no longer references
it.