1、各位大神,在用jna时,总是报错,如下
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:419)
at com.sun.jna.Function.invoke(Function.java:354)
at com.sun.jna.Library$Handler.invoke(Library.java:244)
at tzwy.chip.motor.$Proxy0.CH375WriteData(Unknown Source)
at tzwy.chip.motor.MotorDll.writeData(MotorDll.java:70)
at tzwy.chip.motor.MotorSingleton.initUSB(MotorSingleton.java:159)
at tzwy.chip.motor.MotorSingleton.initMotor(MotorSingleton.java:103)
at tzwy.chip.motor.MotorSingleton.main(MotorSingleton.java:21)
2、C的函数原型为
BOOL WINAPI CH375GetConfigDescr( // 读取配置描述符
ULONG iIndex, // 指定CH375设备序号
PVOID oBuffer, // 指向一个足够大的缓冲区,用于保存描述符
PULONG ioLength ); // 指向长度单元,输入时为准备读取的长度,返回后为实际读取的长度
3、jna封装为
interface MotorDllLibrary extends Library {
String fileName = "CH375DLL.DLL";
String filePath = MotorDllLibrary.class.getResource("").getPath().replaceFirst("/", "").replaceAll("%20", " ") + fileName;
MotorDllLibrary motor = (MotorDllLibrary) Native.loadLibrary(filePath, MotorDllLibrary.class);
/**
* 读取设备描述符
*
* @param index 指定CH375设备序号
* @param buff 指向一个足够大的缓冲区,用于保存描述符
* @param length 指向长度单元,输入时为准备读取的长度,返回后为实际读取的长度
* @return 0,失败;其他,成功
*/
int CH375GetDeviceDescr(int index, int[] buff, int length);
}
4、调用如下
public static void main(String[] args) {
try {
int[] s = new int[100];
int l = 0;
int deviceDescr = MotorDllLibrary.motor.CH375GetDeviceDescr(0,s,l);
System.out.println("deviceDescr:" + deviceDescr);
} catch (Exception e) {
e.printStackTrace();
}
}
5、有哪位大神知道怎么回事?
似乎是DLL抛出的异常,确认设备信息正确?