我需要使用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
尝试了我在互联网上找到的其他方式......结果和错误相同。
结构或界面有问题吗?
答案 0 :(得分:0)
AFAIK,在这种情况下,JNA可以为您传递对结构的引用,因此您的方法将如下所示:
int rdGetStatusHardware(StatusHardware p);
...
StatusHardware ptr = new StatusHardware();
int result = CLibrary.INSTANCE.rdGetStatusHardware(ptr);
修改强>
可能会导致访问冲突,因为您将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];
}