用JNA调用方法来回调

时间:2013-09-12 17:43:37

标签: java callback jna

我需要通过回调调用使用JNA的方法,问题是api无法识别该方法,因为JNA回调不包含您通过方法参数传递的函数。

C中的函数是:

Result API LS800AutoDocHandle(...
int          (__stdcall *userfunc1)(S_CODELINE_INFO_LS800 *CodelineInfo),
int          (__stdcall *userfunc2)(S_IMAGE_INFO_LS800 *ImageInfo),
int          (__stdcall *userfunc3)(S_IMAGE_INFO_LS800 *ImageInfo),
...);

此函数调用此方法

    Reply = LS800AutoDocHandle(...
    (CodelineType == READ_CODELINE_MICR ? OnCodelineRead : 0),
    (CodelineType == NO_READ_CODELINE ? OnImageFrontReady : 0),
    NULL,//OnImageBackReady,
    ...);                  // Reserved3    

然后我的问题是,如何通过JNA回调调用OnCodelineRead方法?

1 个答案:

答案 0 :(得分:1)

JNA使用Callback个对象来表示函数指针,并包含description of callback usage

原生代码:

int LS800AutoDocHandle(
                       int (__stdcall *userfunc1)(S_CODELINE_INFO_LS800* info),
                       int (__stdcall *userfunc2)(S_CODELINE_INFO_LS800* info),
                       int (__stdcall *userfunc3)(S_CODELINE_INFO_LS800* info)
                      );

JNA映射(应该使用StdCallLibraryAPI可能是__stdcall):

public interface CodeLineRead extends StdCallCallback {
    int invoke(S_IMAGE_INFO_LS800 info);
}
public interface ImageFrontReady extends StdCallCallback {
    int invoke(S_IMAGE_INFO_LS800 info);
}
public interface ImageBackReady extends StdCallCallback {
    int invoke(S_IMAGE_INFO_LS800 info);
}

int LS800AutoDocHandle(CodeLineRead f1, ImageFrontReady f2, ImageBackReady f3);