0

我需要使用 JNA 传递一个结构。

我拥有的文档包含以下信息:

int rdGetStatusHardware (struct StatusHardware *stat)

struct StatusHardware{
unsigned char   SerialNumber[12];   
unsigned long   HWVersion;
unsigned long   FWVersion;
unsigned long   MemorySize;
unsigned short  BootVersion;
unsigned short  FPGAVersion;
unsigned long   Devices;
unsigned char   Reserved[8];
};

我将结构映射为:

public class StatusHardware extends Structure {
    public byte SerialNumber[] = new byte[12];
    public byte HWVersion[] = new byte[4];
    public byte FWVersion[] = new byte[4];            
    public byte MemorySize[] = new byte[4];
    public byte BootVersion[] = new byte[2];
    public byte FPGAVersion[] = new byte[2];
    public byte Devices[] = new byte[4];
    public byte Reserved[] = new byte[8];
    public StatusHardware(Pointer p){
        super(p);
    }
}

这是我界面中的方法:

int rdGetStatusHardware(Pointer p);

调用库:

Pointer ptr = new Memory(40);
int result = CLibrary.INSTANCE.rdGetStatusHardware(ptr);

使用这种方法,我收到以下错误:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00000000, pid=3108, tid=5492
#
# JRE version: 6.0_24-b50
# Java VM: Java HotSpot(TM) Client VM (19.1-b02 mixed mode windows-x86 )
# Problematic frame:
# C  0x00000000

尝试了我在互联网上找到的其他方法......结果和错误相同。

是结构问题还是接口问题?

4

1 回答 1

0

AFAIK,在这种情况下,JNA 可以为您传递对结构的引用,因此您的方法将如下所示:

int rdGetStatusHardware(StatusHardware p);
...
StatusHardware ptr = new StatusHardware();
int result = CLibrary.INSTANCE.rdGetStatusHardware(ptr);

编辑

可能会导致访问冲突,因为您将 long 映射到固定大小的字节数组。Long 大小可能因平台而异,因此您应始终使用 NativeLong:

public class StatusHardware extends Structure {
    public byte SerialNumber[] = new byte[12];
    public NativeLong HWVersion;
    public NativeLong FWVersion;            
    public NativeLong MemorySize;
    public short BootVersion;
    public short FPGAVersion;
    public NativeLong Devices;
    public byte Reserved[] = new byte[8];
}
于 2012-09-03T17:51:29.893 回答