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