'JNA Exception in thread "main" java.lang.Error: Invalid memory access(Unknown Source)

I'm using JNA 4.0.0 to access some DLL function from Java, this DLL Native Function is declared as the following:

int ApplicationInit(HANDLE hEMV, TLV *tlv_Appl, TLV *tlv_AIP);

Types of the input parameters are described below

/* 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;

and so, I declared it inside the library interface as follows:

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);

and then I call it in the following way:

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));

but I'm getting the following Exception:

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)

Please help and thank you for your attention!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source