7 messages in net.java.dev.jna.usersRe: [jna-users] callback function poi...
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:Re: [jna-users] callback function pointer inside structActions...
From:Timothy Wall (twal@dev.java.net)
Date:May 20, 2008 5:32:04 am
List:net.java.dev.jna.users

On May 20, 2008, at 6:39 AM, Johannes Rollenbeck wrote:

----- 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);

The return value should be "int" (32 bits) not "short" (16 bits), although this isn't likely to be the cause of failure. Actually, I'm surprised this compiles without warnings, since your implementation uses a different method signature for the "callback" method.

}

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);

Print the callback function pointer value in "callCallback". Is it zero or something else? Is the "invoke_id" field correct?