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

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

// 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.

解决方案

Your return type needs to be NativeLong.

Your final argument needs to be either IntByReference or int[1].

Unless DeviceDate is compatible with LP_DEVICEINFO, you need to make sure those structure types match.

EDIT

What are the native definitions of DeviceDate and LP_DEVICEINFO?

If LP_DEVICEINFO is just a generic pointer where you can substitute a device-specific structure, then this should be fine, for example:

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:

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;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值