在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java  網絡安全/ Jna報Invalid memory access錯誤是怎么回事?

Jna報Invalid memory access錯誤是怎么回事?

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,  // 指向一個足夠大的緩沖區(qū),用于保存描述符
    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   指向一個足夠大的緩沖區(qū),用于保存描述符
     * @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拋出的異常,確認設備信息正確?

2018年5月30日 17:53
編輯回答
孤島

已解決,如下

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   指向一個足夠大的緩沖區(qū),用于保存描述符
 * @param length 指向長度單元,輸入時為準備讀取的長度,返回后為實際讀取的長度
 * @return 0,失?。黄渌?,成功
 */
int CH375GetDeviceDescr(int index, byte[] buff, int[] length);

}

4、調用如下
public static void main(String[] args) {

try {
    byte[] s = new byte[100];
    int[] l = new int[1];
    l[0] = 100;
    int deviceDescr = MotorDllLibrary.motor.CH375GetDeviceDescr(0,s,l);
    System.out.println("deviceDescr:" + deviceDescr);
} catch (Exception e) {
    e.printStackTrace();
}

}

為數據類型轉換問題

2017年3月3日 06:35