简体   繁体   English

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

[英]JNA: java.lang.Error: Invalid memory access

I'm using JNA to access some dll function from Java, this dll Native Function is declared as the following: 我正在使用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

and so, I declared it inside library interface as the following: 因此,我在库接口中将其声明如下:

long H264_Login(String sIP, short wPort, String sUserName, String sPassword,
                    Structure DeviceDate, int error);

and then I call it the following way: 然后我用以下方式称呼它:

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

but I'm getting the following Exception: 但我收到以下异常:

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)

It's strange since there is no long variables inside the method parameters, only the returning type is long which I think it has nothing to do with that Exception. 这很奇怪,因为方法参数内没有长变量,只有返回类型很长,我认为这与该异常无关。 Also I tried some of other methods which return long and it works perfectly. 我也尝试了其他一些返回long方法,并且效果很好。

Your return type needs to be NativeLong . 您的返回类型必须为NativeLong

Your final argument needs to be either IntByReference or int[1] . 您的最后一个参数必须是IntByReferenceint[1]

Unless DeviceDate is compatible with LP_DEVICEINFO , you need to make sure those structure types match. 除非DeviceDateLP_DEVICEINFO兼容, LP_DEVICEINFO您需要确保这些结构类型匹配。

EDIT 编辑

What are the native definitions of DeviceDate and LP_DEVICEINFO ? DeviceDateLP_DEVICEINFO的本机定义是什么?

If LP_DEVICEINFO is just a generic pointer where you can substitute a device-specific structure, then this should be fine, for example: 如果LP_DEVICEINFO只是一个通用指针,您可以在其中替换特定于设备的结构,则应该没问题,例如:

typedef void *LP_DEVICEINFO;
typedef struct _DeviceData { /* anything you want in here */ } DeviceData, *pDeviceData;

But if it's got any specific definition, the contents of that structure must be compatible with DeviceDate , for example: 但是,如果有任何特定的定义,则该结构的内容必须与DeviceDate兼容,例如:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM