我正在使用 JNA 4.0.0 从 Java 访问一些 DLL 函数,这个 DLL Native Function 声明如下:
int ApplicationInit(HANDLE hEMV, TLV *tlv_Appl, TLV *tlv_AIP);
输入参数的类型描述如下
/* Opaque structure */
typedef void *HANDLE;
typedef struct
{
unsigned char *_lenptr; /* pointer to 'len' field (Private member) */
unsigned int _len; /* 'outer' length, specified by user (Private member) */
unsigned short _offset;
unsigned short len; /* number of bytes (Public member) */
unsigned long tag; /* Tag tag (Public member) */
unsigned char *val; /* byte string (Public member) */
unsigned char _tagptr[256]; /* Container for TLV data (Private member) */
} TLV;
因此,我在库接口中声明它如下:
public static class HANDLE extends PointerType {
public HANDLE(Pointer address) {
super(address);
}
public EMV_HANDLE() {
super();
}
}
public class TLV extends Structure {
/**
* pointer to 'len' field (Private member)<br>
* C type : unsigned char*
*/
public Pointer _lenptr;
/** 'outer' length, specified by user (Private member) */
public int _len;
public short _offset;
/** number of bytes (Public member) */
public short len;
/** Tag tag (Public member) */
public NativeLong tag;
/**
* byte string (Public member)<br>
* C type : unsigned char*
*/
public Pointer val;
/**
* Container for TLV data (Private member)<br>
* C type : unsigned char[256]
*/
public byte[] _tagptr = new byte[256];
public TLV() {
super();
}
protected List<? > getFieldOrder() {
return Arrays.asList("_lenptr", "_len", "_offset", "len", "tag", "val", "_tagptr");
}
/**
* @param _lenptr pointer to 'len' field (Private member)<br>
* C type : unsigned char*<br>
* @param _len 'outer' length, specified by user (Private member)<br>
* @param len number of bytes (Public member)<br>
* @param tag Tag tag (Public member)<br>
* @param val byte string (Public member)<br>
* C type : unsigned char*<br>
* @param _tagptr Container for TLV data (Private member)<br>
* C type : unsigned char[256]
*/
public TLV(Pointer _lenptr, int _len, short _offset, short len, NativeLong tag, Pointer val, byte _tagptr[]) {
super();
this._lenptr = _lenptr;
this._len = _len;
this._offset = _offset;
this.len = len;
this.tag = tag;
this.val = val;
if ((_tagptr.length != this._tagptr.length))
throw new IllegalArgumentException("Wrong array size !");
this._tagptr = _tagptr;
}
public static class ByReference extends TLV implements Structure.ByReference {
};
public static class ByValue extends TLV implements Structure.ByValue {
};
}
int EMV_ApplicationInit(AppdefLibrary_EMVCT.EMV_HANDLE hEMV, TLV.ByReference tlv_Appl, TLV.ByReference tlv_AIP);
然后我用以下方式调用它:
EMV_HANDLE hEMV= new EMV_HANDLE();
TLV.ByReference tlv_Appl=new TLV.ByReference();
TLV.ByReference tlv_AIP=new TLV.ByReference();
System.out.println(AppdefLibrary.INSTANCE.ApplicationInit(hEMV, tlv_Appl, tlv_AIP));
但我得到以下异常:
Exception in thread "main" java.lang.Error: Invalid memory access
at com.sun.jna.Native.invokeInt(Native Method)
at com.sun.jna.Function.invoke(Function.java:383)
at com.sun.jna.Function.invoke(Function.java:315)
at com.sun.jna.Library$Handler.invoke(Library.java:212)
at com.sun.proxy.$Proxy1.ApplicationInit(Unknown Source)
at test.Test.main(Test.java:192)
请帮助并感谢您的关注!