本文介绍了Java使用JNA调用dll错误:无效的内存访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想调用dll来从硬件写入/读取,但是出现以下错误:

I want to call dll to write/read from hardware.However, I get the error below:


dll方法:

dll method:

int NewKey(char *room,char *gate,char *stime,char *guestname,char *guestid, int  overflag, int Breakfast, long *cardno,char * track1,char * track2);

java方法:

int NewKey(String room, String gate,String time,String guestname,String guestid, int overflag, int Breakfast, NativeLongByReference cardno, String track1, String track2);

api文件显示cardno作为out参数,track1,track2可以为空。

The api document shows cardno as a out parameter and track1,track2 could be null.

NativeLongByReference cardNo = new NativeLongByReference ();

int res = CLibrary.INSTANCE.NewKey("010001", "00", "201712021200201712031200", "Guest Name","Account No.", 0, 1, cardNo, null, null);

它不起作用。所以我使用一种简单的方法:

It don t work. So I use a simple method:

dll method :

int EraseCard (long  cardno,char * track1,char * track2);

java方法:

int EraseCard(NativeLong cardno, String  track1, String  track2);

NativeLong a = new NativeLong(0L);

int res = CLibrary.INSTANCE.EraseCard (a, null, null);


再次得到相同的错误:

It gets the same error again:

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 A90PmsInterface.main(A90PmsInterface.java:104)

似乎只有在我尝试使用dll方法读取/写入硬件时才会发生错误。

It seems like the error only occurs when I try to use the dll methods to read/write from/to hardware.

我该如何解决这个问题?

How can I solve the problem?

详细信息:win7 64 bit,jre1.8 32bit,jna4.1

details: win7 64 bite, jre1.8 32bite, jna4.1

推荐答案

我猜 MainDll具有多个依赖项。

I guess the "MainDll" have multi dependencies.

当我将所有需要的依赖关系dll放在项目下时文件夹根路径和
使用相对路径加载我的dll,它终于成功。

When I put all my needed dependcies dll under the project folder root path anduse relative path to load my my dll, it finally successed.

    CLibrary INSTANCE = (CLibrary) Native.synchronizedLibrary((CLibrary) Native.loadLibrary("MainDll", CLibrary.class));

这篇关于Java使用JNA调用dll错误:无效的内存访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 02:46