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?