7 messages in net.java.dev.jna.userscallback function pointer inside struct
FromSent OnAttachments
fixf...@online.deMay 20, 2008 2:38 am 
Johannes RollenbeckMay 20, 2008 3:39 am 
Timothy WallMay 20, 2008 5:32 am 
Johannes RollenbeckMay 20, 2008 7:41 am 
Johannes RollenbeckMay 20, 2008 7:58 am 
Timothy WallMay 20, 2008 7:58 am 
Johannes RollenbeckMay 21, 2008 12:14 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:callback function pointer inside structActions...
From:Johannes Rollenbeck (fixf@online.de)
Date:May 20, 2008 3:39:34 am
List:net.java.dev.jna.users

Hello,

(Sorry for wrong formatting of previous post) I'm really happy with JNA. But just now I need help with using callbacks. It is no problem to pass a function reference for a callback directly to the library, but if the function pointer is member of a struct then I can't get it work. My setup is:

----- C -----

typedef struct req_pend { int invoke_id; int (*req_done) (struct req_pend *req); } REQ_PEND;

// two functions that just call the callback provided by the argument, returning the callback's return value int callCallback ( REQ_PEND *req_out ); int callCallback2 ( int (*req_done) (struct req_pend *req) );

----- JAVA -----

public interface MyLib extends Library { // callback interface public static interface ReqDoneFunc extends Callback { short callback (REQ_PEND req); }

public static class REQ_PEND extends Structure { public int invoke_id; public ReqDoneFunc req_done; // callback function pointer }

int callCallback ( REQ_PEND req_out ); // callback function stored in Structure

int callCallback2 ( ReqDoneFunc req_done ); // callback function directly as parameter }

// test implementation of callback function:

public class CBrequestDone implements ReqDoneFunc { public int callback( REQ_PEND req ) { System.out.println( "CALLBACK" ); return 5678; } }

...

// this usage works int i = myLib.callCallback2 ( new CBrequestDone() ); System.out.println("call Callback2 returns " + i);

// this usage doesn't (function pointer seems to be null)

REQ_PEND req_pend = new REQ_PEND(); req_pend.invoke_id = 1234; req_pend.req_done = new CBrequestDone();

int i = myLib.callCallback ( req_pend ); System.out.println("call Callback returns " + i);

I guess I'm very near. Thanks for any help!

Johannes