读书人

JNA调用dll时crash了。求教大牛解决办

发布时间: 2013-06-19 10:26:41 作者: rapoo

JNA调用dll时crash了。求教大牛
这个dll是解调仪厂商给的。
isCorrect和发送命令都正常,可以判断是DataReadPD的问题
文档中的DataReadPD描述如下
DataReadPD(byte ArrayOfData[], int NumberOfBytes) this function reads the
data returned on port 55000 by the sm125 in response to a command issued with
the SendCommandPD function. The function performs both of the reads
necessary to fully interpret the data returned from the sm125. The function returns
a pointer to an array of bytes, ArrayOfData[], containing the data returned, and an
integer, NumberOfBytes, which indicates the size of the data array. The function
returns a value of 0 on successful completion. The function returns an integer
error code if an error occurs. Integer error codes and their descriptions are listed
later in this document
错误日志

#
# An unexpected error has been detected by Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0b0e1c4b, pid=176, tid=4000
#
# Java VM: Java HotSpot(TM) Client VM (11.3-b02 mixed mode windows-x86)
# Problematic frame:
# C [sm125_Comm_Dll.dll+0x1c4b]
#
# If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#





JNA代码如下


import java.nio.ByteBuffer;

import com.sun.jna.Native;
import com.sun.jna.win32.StdCallLibrary;


public interface sm125_Comm_Dll extends StdCallLibrary
{
sm125_Comm_Dll INSTANCE = (sm125_Comm_Dll)Native.loadLibrary("sm125_Comm_Dll",sm125_Comm_Dll.class);
public int sm125_Connect(String IPAddress);
public int sm125_Disconnect();
public int SendCommandPD(String command);
//public int DataReadPD(byte[] ArrayOfData , int NumberOfBytes);
public int DataReadPD(ByteBuffer ArrayOfData , int NumberOfBytes);


}

测试的代码如下:
import java.nio.ByteBuffer;

public class Test
{
public static void main(String[] args)
{
int isConnect = 1;//0代表建立成功,1代表连接失败。
String str = "10.0.0.122";
isConnect = sm125_Comm_Dll.INSTANCE.sm125_Connect(str);

System.out.println("isConnect:" + isConnect);//已测试建立连接

String peaks = "#GET_PEAKS_AND_LEVELS";
String convert = null;

//convert = new String(peaks.getBytes("ASCII"));
if(sm125_Comm_Dll.INSTANCE.SendCommandPD("#GET_PEAKS_AND_LEVELS") != 0)
{

System.out.println("SendCommandPD命令运行失败");
return ;
}
else{
System.out.println("发送命令成功");


}

//byte[] returnedData = new byte[10000];
//byte[] returnedData = ByteBuffer.allocateDirect(10000).array();
ByteBuffer returnedData = ByteBuffer.allocate(10000);
int numberOfBytes = 100;

int read = sm125_Comm_Dll.INSTANCE.DataReadPD(returnedData,numberOfBytes);

System.out.println(read);
System.out.println(returnedData);
}
}

jna crash dll
[解决办法]
内存不足引起的。
[解决办法]
有问题的代码段:

# Problematic frame:
# C [sm125_Comm_Dll.dll+0x1c4b]

native本地代码出现了问题,sm125_Comm_Dll.dll出了错误
猜测应该是public int DataReadPD(ByteBuffer ArrayOfData , int NumberOfBytes);
传递的参数导致底层计算的时候对内存产生了非法的读写
不知道为什么你把参数byte[]改成了ByteBuffer类型,而且分配了10000个字节,传递进去的numberOfBytes却只有100,你先看一下这块

读书人网 >J2SE开发

热点推荐