在JAVA中使用JNA时无效的内存访问

时间:2014-11-27 09:44:47

标签: java memory jna

我正在尝试使用JNA调用mySMS.DLL中的函数来从设备读取SMS。短信详细信息将读入sMessage,sFrom和sTime。但是,我得到以下错误。

不知道导致错误的原因。请帮忙。非常感谢。

C:\Users\Chi\Desktop\SMS_Pool\install\sms.dll\Mysms.dll>set classpath=.;C:\Program Files (x86)\Java\jre7\lib\*
C:\Users\Chi\Desktop\SMS_Pool\install\sms.dll\Mysms.dll>"C:\Program Files (x86)\Java\jre7\bin\java" SMSTest
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:371)
    at com.sun.jna.Function.invoke(Function.java:315)
    at com.sun.jna.Library$Handler.invoke(Library.java:212)
    at com.sun.proxy.$Proxy0.ReadSms(Unknown Source)
    at SMSTest.main(SMSTest.java:35)

DLL的API文件:

_declspec(dllexport) BOOL _stdcall ReadSms(int comport, int baud, int nIndex, char* sMessage, char* sFrom, char* sTime, BOOL bDel);

Java代码:

import com.sun.jna.Library;  
import com.sun.jna.Native;  
import com.sun.jna.Platform;  

public class SMSTest {  
    public interface CLibrary extends Library {  
        CLibrary INSTANCE = (CLibrary) Native.loadLibrary(  Platform.isWindows() ? "Mysms" : "c", CLibrary.class);  
        boolean ReadSms(int comport, int baud, int nIndex, String sMessage, String sFrom, String sTime, boolean bDel);
    }  

public static void main(String[] args) {  
try{
  String a= new String();
  String b= new String();
  String c = new String();
  System.out.println(CLibrary.INSTANCE.ReadSms(6,115200, 1, a,b,c,false));

}catch (Exception e){
}
}   
}

1 个答案:

答案 0 :(得分:2)

请注意原生声明中的_stdcall?这表明您的JNA接口需要实现StdCallLibrary接口以确保它使用正确的调用约定。