4 messages in net.java.dev.jna.usersRe: [jna-users] question about String...
FromSent OnAttachments
Simon BASLEMay 31, 2007 11:08 am 
Timothy WallMay 31, 2007 11:29 am 
Simon BASLEJun 8, 2007 7:41 am 
Timothy WallJun 8, 2007 11:39 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] question about Strings and array pointersActions...
From:Timothy Wall (twal@dev.java.net)
Date:Jun 8, 2007 11:39:41 am
List:net.java.dev.jna.users

On Jun 8, 2007, at 10:42 AM, Simon BASLE wrote:

Hi again! I now encounter a char** function (expecting an array of Strings). I tried to use String[ ] but it doesn't let me... Not even when using byte[ ][ ] instead. How can I do that?

File an RFE to automatically convert String[] arguments. You can do it by allocating a Memory object of the appropriate size and calling setPointer at each char* offset; you'd need to construct each element yourself. Not pretty, but one of the goals of JNA's design is to allow you to do just about anything you could do in C.

char** is common enough that it should be supported automatically, though.

Also I noticed that when dealing with string returning functions (that fill a byte array passed as parameter) I have to initialise the receiving byte array to a correct size (where I would just put null with an array of ints for example)... Is that normal behaviour?

Of course, I would think that it is self-evident that you need to provide a non-null buffer that's sufficiently large. If you pass null, then there is no memory for the target function to write to. If you pass too little memory, it's the same as if you called it from C with too small a buffer.

And one last question : is it supported to deal with pointers to variable-size structures ? I mean with a function that return an int but accept a pointer to a variable-size structure which will be created by the said function?

If the function allocates the memory, then you're talking about a pointer to a pointer, where you pass in the address of a pointer (to struct, presumably), so that the called function can write the value of the pointer. PointerByReference handles that case, you can then "cast" to the appropriate structure type by calling Structure.useMemory() after you've created a structure of the appropriate size. Most of the structure sizing is automatic; at worst you explicitly tell the constructor how big you want the structure to be.

If your structure is like this: struct { int size; char buffer[1]; }

where "buffer" may be any length, you should overload Structure.read () like this:

public void read() { // avoid reading buffer until we know how long it is buffer = new byte[0]; super.read(); buffer = getMemory().getByteArray(4, size); }