我试图在Java中调用VB DLL方法。我成功地调用了简单的Hello World方法。 现在我尝试调用一个方法,该方法将String作为输入并返回String作为输出。 Wrapper DLL编译成功。但是在运行Java代码时,错误
Java运行时环境检测到致命错误:
内部错误(0xe0434352),pid = 3668,tid = 1124
在搜索时,我发现跟随错误主要是由于本机代码中的错误,我无法弄清楚。
Java Runtime Environment检测到致命错误:
内部错误(0xe0434352),pid = 1940,tid = 2640
JRE版本:6.0_32-b05 Java VM:Java HotSpot(TM)客户端VM(20.7-b02混合模式,共享windows-x86) 有问题的框架: C [KERNELBASE.dll + 0x969b]
--------------- T H R E A D ---------------
Current thread (0x002d9800): JavaThread "main" [_thread_in_native,id=2640,
stack(0x01940000,0x01990000)]
siginfo: ExceptionCode=0xe0434352, ExceptionInformation=0x80070002 0x00000000
0x00000000 0x00000000 0x617c0000
Registers:
EAX=0x0198ea44, EBX=0x00000005, ECX=0x00000005, EDX=0x00000000
ESP=0x0198ea44, EBP=0x0198ea94, ESI=0x0198eb04, EDI=0x00000001
EIP=0x75a3969b, EFLAGS=0x00000216
Top of Stack: (sp=0x0198ea44)
0x0198ea44: e0434352 00000001 00000000 75a3969b
0x0198ea54: 00000005 80070002 00000000 00000000
0x0198ea64: 00000000 617c0000 00000001 00000000
0x0198ea74: 776a1e37 00000000 03ea11bc 00004000
0x0198ea84: 0022ae68 0198ea9c 001d9a98 0198ea4c
0x0198ea94: 0198eb38 618e48c7 e0434352 00000001
0x0198eaa4: 00000005 0198eb04 4dc4fa37 040ba034
0x0198eab4: 00227be0 0198ebf0 001d9a98 00000001
Instructions: (pc=0x75a3969b)
0x75a3967b: 89 45 c0 c1 e0 02 50 ff 75 14 8d 45 c4 50 e8 9d
0x75a3968b: d1 ff ff 83 c4 0c 8d 45 b0 50 ff 15 5c 11 a3 75
0x75a3969b: c9 c2 10 00 89 45 c0 eb ed 90 90 90 90 90 8b ff
0x75a396ab: 55 8b ec 83 ec 1c 6a 00 6a 1c 8d 45 e4 50 6a 00
Register to memory mapping:
EAX=0x0198ea44 is pointing into the stack for thread: 0x002d9800
EBX=0x00000005 is an unknown value
ECX=0x00000005 is an unknown value
EDX=0x00000000 is an unknown value
ESP=0x0198ea44 is pointing into the stack for thread: 0x002d9800
EBP=0x0198ea94 is pointing into the stack for thread: 0x002d9800
ESI=0x0198eb04 is pointing into the stack for thread: 0x002d9800
EDI=0x00000001 is an unknown value
Stack: [0x01940000,0x01990000], sp=0x0198ea44, free space=314k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C [KERNELBASE.dll+0x969b] RaiseException+0x54
C [clr.dll+0x1248c7] DllGetClassObjectInternal+0x5f84e
C [clr.dll+0x223abf] GetHistoryFileDirectory+0x1427a
C [clr.dll+0x22c222] GetHistoryFileDirectory+0x1c9dd
C [clrjit.dll+0x10cf]
C [clrjit.dll+0x1f0b0]
C [clrjit.dll+0x1a60]
C [clrjit.dll+0x1c42]
C [clrjit.dll+0x244d]
C [clrjit.dll+0x401c]
C [clrjit.dll+0x4132]
C [clrjit.dll+0x4282]
C [clrjit.dll+0x4595]
C [clr.dll+0x33669] CreateAssemblyNameObject+0x61d0
C [clr.dll+0x33701] CreateAssemblyNameObject+0x6268
C [clr.dll+0x33743] CreateAssemblyNameObject+0x62aa
C [clr.dll+0x3399c] CreateAssemblyNameObject+0x6503
C [clr.dll+0x33496] CreateAssemblyNameObject+0x5ffd
C [clr.dll+0x340db] CreateAssemblyNameObject+0x6c42
C [clr.dll+0x1bcd5] DllRegisterServerInternal+0x98c9
C [clr.dll+0x2ae9]
C 0x03f0ba9e
j CsharpConsumer.displayWithParam(Ljava/lang/String;)Ljava/lang/String;+0
j CsharpConsumer.main([Ljava/lang/String;)V+50
v ~StubRoutines::call_stub
V [jvm.dll+0xfad4b]
V [jvm.dll+0x18c421]
V [jvm.dll+0xfadcd]
V [jvm.dll+0x95836]
V [jvm.dll+0x9d778]
V [jvm.dll+0xaf22b]
C [java.exe+0x2155]
C [java.exe+0x85b4]
C [kernel32.dll+0x51154] BaseThreadInitThunk+0x12
C [ntdll.dll+0x5b299] RtlInitializeExceptionChain+0x63
C [ntdll.dll+0x5b26c] RtlInitializeExceptionChain+0x36
Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j CsharpConsumer.displayWithParam(Ljava/lang/String;)Ljava/lang/String;+0
j CsharpConsumer.main([Ljava/lang/String;)V+50
v ~StubRoutines::call_stub
--------------- P R O C E S S ---------------
Java Threads: ( => current thread )
0x01a24400 JavaThread "Low Memory Detector" daemon [_thread_blocked, id=3172,
stack(0x03e00000,0x03e50000)]
0x01a1e800 JavaThread "C1 CompilerThread0" daemon [_thread_blocked, id=2236,
stack(0x03db0000,0x03e00000)]
0x01a1dc00 JavaThread "Attach Listener" daemon [_thread_blocked, id=5308,
stack(0x03d60000,0x03db0000)]
0x01a1b400 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=3916,
stack(0x03d10000,0x03d60000)]
0x01a0c000 JavaThread "Finalizer" daemon [_thread_blocked, id=3660,
stack(0x03cc0000,0x03d10000)]
0x01a0a000 JavaThread "Reference Handler" daemon [_thread_blocked, id=5116,
stack(0x03c70000,0x03cc0000)]
=>0x002d9800 JavaThread "main" [_thread_in_native, id=2640,
stack(0x01940000,0x01990000)]
Other Threads:
0x019ce000 VMThread [stack: 0x03c20000,0x03c70000] [id=240]
0x01a35c00 WatcherThread [stack: 0x03e50000,0x03ea0000] [id=2652]
VM state:not at safepoint (normal execution)
VM Mutex/Monitor currently owned by a thread: None
Heap
def new generation total 4928K, used 281K [0x23700000, 0x23c50000, 0x28c50000)
eden space 4416K, 6% used [0x23700000, 0x23746498, 0x23b50000)
from space 512K, 0% used [0x23b50000, 0x23b50000, 0x23bd0000)
to space 512K, 0% used [0x23bd0000, 0x23bd0000, 0x23c50000)
tenured generation total 10944K, used 0K [0x28c50000, 0x29700000, 0x33700000)
the space 10944K, 0% used [0x28c50000, 0x28c50000, 0x28c50200, 0x29700000)
compacting perm gen total 12288K, used 31K [0x33700000, 0x34300000, 0x37700000)
the space 12288K, 0% used [0x33700000, 0x33707fb0, 0x33708000, 0x34300000)
ro space 10240K, 54% used [0x37700000, 0x37c7ee18, 0x37c7f000, 0x38100000)
rw space 12288K, 55% used [0x38100000, 0x387a27d8, 0x387a2800, 0x38d00000)
Code Cache [0x01a90000, 0x01b00000, 0x03a90000)
total_blobs=116 nmethods=2 adapters=59 free_code_cache=33110656 largest_free_block=0
VB代码有一个函数,它接受字符串输入并输出字符串
Public Class Level2Display
Public Function displayWithParam(str As String) As String
Return "hello world" + str
End Function
End Class
这是C ++ / CLI包装器代码
JNIEXPORT jstring JNICALL Java_CsharpConsumer_displayWithParam
(JNIEnv *env, jobject jobj, jstring myStr)
{
const jchar* temp = (env)->GetStringChars(myStr, null); //Tried changing to JNI_FALSE, still the error persisted.
if(temp==NULL)
{
return NULL;
}
System::String^ managedString = gcnew System::String((const wchar_t*)temp);
env->ReleaseStringChars(myStr, temp);
Level2Display^ lds = gcnew Level2Display(); //class in the VB dll
System::String^ result = (System::String^)lds->displayWithParam(managedString); //displayWithParam method
return env->NewStringUTF((const char*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(result).ToPointer());
}
请帮忙!
答案 0 :(得分:1)
这条线全都搞砸了。
return env->NewStringUTF((const char*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(result).ToPointer());
你有一个Ansi和UTF-8的错误以及内存泄漏。我建议不要在名称中使用任何Marshal
方法HGlobal
。 (它们通过执行内存分配和字符集转换违反了单一责任原则,如果您实际使用真实HGLOBAL
s)它们会崩溃
相反,请尝试
pin_ptr<const wchar_t> resultChars = PtrToStringChars(result);
return env->NewString(resultChars, result->Length);
答案 1 :(得分:0)
您应该使用GetStringUTFChars而不是GetStringChars。 System :: String是否会生成temp的深层副本?如果没有,请在NewStringUTF之后调用ReleaseStringChars或检查isCopy指针。除此之外,你发布的代码中的一切看起来还不错。