2 messages in net.java.dev.jna.usersRe: [jna-users] callback funtions and...
FromSent OnAttachments
Vito IngrassiaJul 12, 2007 8:56 am 
Timothy WallJul 12, 2007 11:24 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] callback funtions and PointerActions...
From:Timothy Wall (twal@dev.java.net)
Date:Jul 12, 2007 11:24:39 am
List:net.java.dev.jna.users

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?