从Java调用Scanner Win32 DLL

时间:2013-06-07 10:49:44

标签: java winapi dll jna

我们必须连接到扫描仪并从我们的Java应用程序执行一些功能。客户提供了Win32扫描仪库规范。

Example: BOOL GetScannerInfo(ScannerInfo *scanner)

结构ScannerInfo在头文件中定义。

#define  scannerMAX 10  // Maximum number of connected scanners  
typedef struct{  
        int count;// -> Number of scanners connected  
    BYTE host_no[scannerMAX];   // -> Host adapter number  
    BYTE scsi_id[scannerMAX];   // -> SCSI ID of the scanner  
}ScannerInfo;  

例如,如果两个扫描程序SCSI ID 1和2连接到一个主机适配器,则返回值如下:

count=2  
host_no[0]=0,   host_no[1]=0  
scsi_id[0]=1,   scsi_id[1]=2  

现在,我们必须调用此函数并从Java获取扫描程序相关信息。 第一次开始使用JNA,这是代码。

public interface ScannerInterface extends Library {

    public boolean GetScannerInfo(?);
            /* ? = They have a pointer of a custom object.What should be passed here
             */

}

public static void main(String[] args) {

    ScannerInterface lib = (ScannerInterface) Native.loadLibrary("in64.dll", 
            ScannerInterface.class);        

    System.out.println(lib.GetScannerInfo(?));

}

我遇到了如何从JNA传递Win32函数所期望的参数。

1 个答案:

答案 0 :(得分:0)

您可以定义映射到您的结构的Java类ScannerInfo,如下所示:

public interface ScannerInterface extends Library {
    public static class ScannerInfo extends Structure {
        public static class ByReference 
            extends ScannerInfo 
            implements Structure.ByReference {}

        public int count;
        public byte[] host_no = new byte[10];
        public byte[] scsi_id = new byte[10];
    }

    public int GetScannerInfo(ScannerInfo.ByReference scanner);
}

然后这样称呼:

ScannerInterface lib = (ScannerInterface) Native.loadLibrary("in64.dll", 
        ScannerInterface.class);        

ScannerInterface.ScannerInfo.ByReference scanner = 
    new ScannerInterface.ScannerInfo.ByReference();
int retval = lib.GetScannerInfo(scanner);            
// check retval in case of error
int count = scanner.count;
// etc. etc.