On Jul 12, 2007, at 11:57 AM, Vito Ingrassia wrote:
Hello,
I'm exploiting JNA callback features and in my C library I have the
following declarations:
/*data list definition*/
typedef struct _tagMyDataList
{
char chExchangeCode;
char szExchangeName[61];
struct _tagMyDataList* pcNext;
} MyDataList;
/*callback function definition*/
typedef void (*OnDataExchanges) (int nNumExchanges, const
MyDataList* pcMyDataList);
In Java the mapping I'm doing is
public class MyDataList extends Structure {
public char chExchangeCode;
public String szExchangeName;
public MyDataList pcNext;
}
Structures declared within structures will be nested, so technically
the above definition results in an infinitely-sized structure. If
you want a pointer-to-struct instead, use Pointer. To convert the
pointer to a structure, do
MyDataList dlist = new MyDataList();
// do some stuff
while (dlist.pcNext != null) {
dlist.useMemory(dlist.pcNext);
dlist.read();
// do some more stuff
}
public interface OnDataExchanges extends Callback
{
void callback(int count, Pointer list);
}
My question is: is it right to declare the callback function with
Pointer type instead of MyDataList type?
The structure type is preferable to a generic Pointer for type
safety. JNA will automagically provide you with the desired
structure type.
I'd like and tried the second way but at runtime on the java side,
invoking the callback function from C library and
passing some dinamically created MyDataList objects, I get Pointer
objects. How can I obtain
Structure objects (MyDataList) from Pointer object?