我使用JNI在vc++中编写了一些本地方法,供java调用。我的三个方法中,有两个完全正常没有任何问题。然而,当我在运行时调用最后一个方法时,会出现以下错误信息:
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x61e06550, pid=3408, tid=4796
#
# JRE version: 7.0-b147
# Java VM: Java HotSpot(TM) Client VM (21.0-b17 mixed mode, sharing windows-x86 )
# Problematic frame:
# V [jvm.dll+0xa6550]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
以下是我的本地方法代码(使用vc ++):
JNIEXPORT jcharArray JNICALL Java_jniusb_Main_receiveData (JNIEnv *env, jclass, jchar dataIndex)
{
DWORD BytesWritten = 0;
DWORD BytesRead = 0;
unsigned char OutputPacketBuffer[65];
unsigned char InputPacketBuffer[65];
static jcharArray ReturnPacketBuffer;
jchar temp[65];
//send 'receive data' command to the firmware (OutputPacketBuffer[1])
WriteFile(WriteHandle, &OutputPacketBuffer, 65, &BytesWritten, 0);
//retrieve data from firmware
ReadFile(ReadHandle, &InputPacketBuffer, 65, &BytesRead, 0);
for(int i=0;i<64;i++)
{
temp[i] = jchar(InputPacketBuffer[i+1]);
}
(*env).SetCharArrayRegion(ReturnPacketBuffer, 0, 64, temp);
return ReturnPacketBuffer;
}
我的Java代码看起来像这样(当然是缩短版):
public static native char[] receiveData(char dataIndex);
public static void main(String[] args) {
char vid = 0x4d8;
char pid = 0x3f;
//check if read/write handles were retrieved
if(connectHid(vid, pid) == true)
{
System.out.println("connected!!!");
}
else
{
System.out.println("not connected...");
}
char[] test = new char[64];
char[] receivetest = new char[64];
char length = 0x03;
char dataIndex = 0x81;
test[0] = 0x80;
test[1] = 0x80;
sendData(test, length);
receivetest = receiveData(dataIndex);
就像我之前说过的那样,其他方法(即connect和senddata)运行良好,但我从receiveData方法中获得错误。经过一些调试,我发现当我注释掉以下行时,错误会消失:
(*env).SetCharArrayRegion(ReturnPacketBuffer, 0, 64, temp);
在我的本地代码中(当然,在这种情况下数据永远不会被返回...)。我在这里做错了什么?