1

我们必须连接到扫描仪并从我们的 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(?));

}

我被困在如何传递 Win32 函数期望从 JNA 获得的参数上。

4

1 回答 1

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.
于 2013-06-07T13:49:11.837 回答