JNA:java.lang.Error:内存访问无效

JNA:java.lang.Error:内存访问无效,java,c#,dll,jna,Java,C#,Dll,Jna,我使用JNA从Java访问一些dll函数,该dll本机函数声明如下: // it returns (long) H264_Login (char *sIP, unsigned short wPort, char *sUserName, char *sPassword, LP_DEVICEINFO lpDeviceInfo, int *error); // where LP_DEVICEINFO is a struct long H264_Login(String sIP, short wPor

我使用JNA从Java访问一些dll函数,该dll本机函数声明如下:

// it returns (long)
H264_Login (char *sIP, unsigned short wPort, char *sUserName, char *sPassword, LP_DEVICEINFO lpDeviceInfo, int *error); // where LP_DEVICEINFO is a struct
long H264_Login(String sIP, short wPort, String sUserName, String sPassword,
                    Structure DeviceDate, int error);
因此,我在库接口中声明如下:

// it returns (long)
H264_Login (char *sIP, unsigned short wPort, char *sUserName, char *sPassword, LP_DEVICEINFO lpDeviceInfo, int *error); // where LP_DEVICEINFO is a struct
long H264_Login(String sIP, short wPort, String sUserName, String sPassword,
                    Structure DeviceDate, int error);
然后我用下面的方式来称呼它:

simpleDLL INSTANCE = (simpleDLL) Native.loadLibrary(
                ("NetSdk"), simpleDLL.class);
DeviceDate dev = new DeviceDate() // where DeviceDate is a static class inherits com.sun.jna.Structure
int err = (int) INSTANCE.H264_GetLastError();
long result = INSTANCE.H264_DVR_Login("255.255.255.255", (short) 33333, "admin", "admin", dev, err);
但我得到了以下例外:

Exception in thread "main" java.lang.Error: Invalid memory access
    at com.sun.jna.Native.invokeLong(Native Method)
    at com.sun.jna.Function.invoke(Function.java:386)
    at com.sun.jna.Function.invoke(Function.java:315)
    at com.sun.jna.Library$Handler.invoke(Library.java:212)
    at com.sun.proxy.$Proxy0.H264_DVR_Login(Unknown Source)
    at Test.main(Test.java:47)
奇怪的是,在方法参数中没有长变量,只有返回的类型是长的,我认为这与异常无关。我还尝试了其他一些方法
返回时间长且工作正常。

您的返回类型需要为NativeLong

最后一个参数必须是IntByReference或int[1]

除非DeviceDate与LP_DEVICEINFO兼容,否则需要确保这些结构类型匹配

编辑

DeviceDate和LP_DEVICEINFO的本机定义是什么

如果LP_DEVICEINFO只是一个通用指针,您可以在其中替换特定于设备的结构,那么这应该很好,例如:

typedef void *LP_DEVICEINFO;
typedef struct _DeviceData { /* anything you want in here */ } DeviceData, *pDeviceData;
typedef struct _LP_DEVICEINFO {
    int type;
    // Fill in with device-specific information
} DEVICEINFO, *LP_DEVICEINFO;

typedef struct _DeviceDate {
    int type; // Example "common" field
    int timestamp; // device-specific information
} DeviceDate;
但是如果它有任何特定的定义,那么该结构的内容必须与DeviceDate兼容,例如:

typedef void *LP_DEVICEINFO;
typedef struct _DeviceData { /* anything you want in here */ } DeviceData, *pDeviceData;
typedef struct _LP_DEVICEINFO {
    int type;
    // Fill in with device-specific information
} DEVICEINFO, *LP_DEVICEINFO;

typedef struct _DeviceDate {
    int type; // Example "common" field
    int timestamp; // device-specific information
} DeviceDate;

我得到了前两个提示,它们解决了我的问题,但是检查结构类型是否匹配有什么意义呢?如何做到这一点?