我使用JNA从Java调用dll。 这在大多数情况下都有效,但有时会引发内存访问异常。
我发现jvm.exe运行时带有msvcr100.dll,而dll正在msvcr90.dll上运行。这会导致问题吗?
Java接口代码:
public interface FooLibInterface extends StdCallLibrary {
FltLibInterface INSTANCE = (FooLibInterface) Native.loadLibrary("C:\\dlls\\FooLibWrapper.dll", FooLibInterface.class);
NativeLong wrapedFooInterface(long id, PointerByReference inDataArray, long inItems, PointerByReference outDataArray, NativeLongByReference outItems, DoubleByReference outParX);
}
Java调用:
synchronized (lock) {
long id = 123;
PointerByReference inputArray = new PointerByReference();
Memory buffer = new Memory(data.length * 8);
buffer.write(0, data, 0, data.length);
inputArray.setValue(buffer);
PointerByReference outputArray = new PointerByReference();
NativeLongByReference outItems = new NativeLongByReference();
DoubleByReference outParX = new DoubleByReference();
NativeLong excRet = ffltLib.wrapedProfileFilterInterface(id, inputArray, data.length, outputArray, outItems, outParX);
items = outItems.getValue().intValue();
double parX = outParX.getValue();
newdata = new double[ret.itemsX];
for (int x = 0; x < items; x += 1) {
newdata [x] = outputArray.getValue().getDouble(x * 8);
}
ffltLib.freemem();
}
c-Header:
#pragma pack(1)
#ifndef __FOOLIBWRAPPER_H__
#define __FOOLIBWRAPPER_H__
#include <iostream>
#include <windows.h>
#include <string.h>
/* To use this exported function of dll, include this header
* in your project.
*/
//Import Strukturen
typedef struct {
double *Data;
long Items;
double ParX;
} TNativeFoo;
typedef void (WINAPI *FOOINTERFACE) (LONGLONG *id, void *input, void *output);
extern "C" __declspec(dllexport) long wrapedFooInterface(LONGLONG id, double** inDataArray, LONGLONG inItems, double** outDataArray, LONGLONG* outItems, double* outParX)
extern "C" __declspec(dllexport) void freemem();
#endif // __FOOLIBWRAPPER_H__
C代码:
extern "C" __declspec(dllexport) long wrapedFooInterface(LONGLONG id, double** inDataArray, LONGLONG inItems, double** outDataArray, LONGLONG* outItems, double* outParX)
{
long i;
HINSTANCE hGetProcIDDLL;
FARPROC lpfnFooInterfaceProcAddress;
FOOINTERFACE fncFooInterface;
long internID(0);
memRegister.getcsmmemid(&internID);
memRegister.getcsmmemid();
hGetProcIDDLL = LoadLibraryA(LibaryPath.c_str());
lpfnFooInterfaceProcAddress = GetProcAddress(HMODULE (hGetProcIDDLL),"FooInterface");
fncFooInterface = FOOINTERFACE(lpfnFooInterfaceProcAddress);
OutputFoo.Data = (double *) memRegister.getmem(internID, inItems, sizeof(double));
InputFoo.Data = (double *) memRegister.getmem(internID, inItems, sizeof(double));
for (i=0L; i<inItems; i++)
InputFoo.Data[i]=inDataArray[0][i];
InputFoo.Items = OutputFoo.Items = inItems;
InputFoo.ParX = OutputFoo.ParX = inParX;
fncFooInterface(&id, &InputFoo, &OutputFoo);
outDataArray[0] = (double *) memRegister.getmem(OutputFoo.Items, sizeof(double));
for(long i=0;i<OutputFoo.Items;i++)
{
outDataArray[0][i]=OutputFoo.Data[i];
}
*outItems = OutputFoo.Items;
*outParX = OutputFoo.ParX;
memRegister.freemem(internID);
FreeLibrary(hGetProcIDDLL);
return 0;
}
例外:
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d316065, pid=10940, tid=11020
#
# JRE version: 7.0_09-b05
# Java VM: Java HotSpot(TM) Client VM (23.5-b02 mixed mode windows-x86 )
# Problematic frame:
# C [FltLibWrapper.dll+0x16065] wrapedProfileFilterInterface+0x105
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# If you would like to submit a bug report, please visit:
# http://bugreport.sun.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
堆栈:
Stack: [0x6a5e0000,0x6a6e0000], sp=0x6a6de620, free space=1017k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C [FooLibWrapper.dll+0x16065] wrapedFooInterface+0x105
C [jna6443424344288876112.dll+0xcc77] Java_com_sun_jna_Native_initialize_1ffi_1type+0x38b7
C [jna6443424344288876112.dll+0xc78a] Java_com_sun_jna_Native_initialize_1ffi_1type+0x33ca
C [jna6443424344288876112.dll+0x4561] Java_com_sun_jna_Pointer__1getString+0xa31
C [jna6443424344288876112.dll+0x4cae] Java_com_sun_jna_Function_invokeVoid+0x2e
j com.sun.jna.Function.invokeVoid(I[Ljava/lang/Object;)V+0
j com.sun.jna.Function.invoke([Ljava/lang/Object;Ljava/lang/Class;Z)Ljava/lang/Object;+45
j com.sun.jna.Function.invoke(Ljava/lang/Class;[Ljava/lang/Object;Ljava/util/Map;)Ljava/lang/Object;+214
j com.sun.jna.Library$Handler.invoke(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;+341
j $Proxy35.wrapedFoorInterface(JLcom/sun/jna/ptr/PointerByReference;JDDLcom/sun/jna/ptr/PointerByReference;Lcom/sun/jna/ptr/NativeLongByReference;Lcom/sun/jna/ptr/DoubleByReference;Lcom/sun/jna/ptr/DoubleByReference;Lcom/sun/jna/ptr/NativeLongByReference;D)V+86
J WrapperStructure.FooLibWrapper.fooInterface(LWrapperStructure/Foo;JD)LWrapperStructure/Foo;
...
j Routes$$anonfun$routes$1$$anonfun$apply$75$$anonfun$apply$76.apply()Lplay/mvc/Result;+38
j Routes$$anonfun$routes$1$$anonfun$apply$75$$anonfun$apply$76.apply()Ljava/lang/Object;+1
j play.core.Router$HandlerInvoker$$anon$5$$anon$1.invocation()Lplay/mvc/Result;+4
j play.core.j.JavaAction$$anon$1.call(Lplay/mvc/Http$Context;)Lplay/mvc/Result;+13
j play.GlobalSettings$1.call(Lplay/mvc/Http$Context;)Lplay/mvc/Result;+5
j controllers.Annotations.CurrentUser.call(Lplay/mvc/Http$Context;)Lplay/mvc/Result;+248
j play.core.j.JavaAction$class.apply(Lplay/core/j/JavaAction;Lplay/api/mvc/Request;)Lplay/api/mvc/Result;+368
j play.core.Router$HandlerInvoker$$anon$5$$anon$1.apply(Lplay/api/mvc/Request;)Lplay/api/mvc/Result;+2
j play.core.ActionInvoker$$anonfun$receive$1$$anonfun$6.apply()Lplay/api/mvc/Result;+8
j play.core.ActionInvoker$$anonfun$receive$1$$anonfun$6.apply()Ljava/lang/Object;+1
j play.utils.Threads$.withContextClassLoader(Ljava/lang/ClassLoader;Lscala/Function0;)Ljava/lang/Object;+16
j play.core.ActionInvoker$$anonfun$receive$1.apply(Ljava/lang/Object;)V+148
j play.core.ActionInvoker$$anonfun$receive$1.apply(Ljava/lang/Object;)Ljava/lang/Object;+2
j akka.actor.Actor$class.apply(Lakka/actor/Actor;Ljava/lang/Object;)V+25
j play.core.ActionInvoker.apply(Ljava/lang/Object;)V+2
j akka.actor.ActorCell.invoke(Lakka/dispatch/Envelope;)V+37
j akka.dispatch.Mailbox.processMailbox(IJ)V+24
j akka.dispatch.Mailbox.run()V+20
j akka.dispatch.ForkJoinExecutorConfigurator$MailboxExecutionTask.exec()Z+6
J akka.jsr166y.ForkJoinTask.doExec()I
v ~StubRoutines::call_stub
V [jvm.dll+0x12a39a]
V [jvm.dll+0x1d978e]
V [jvm.dll+0x12a583]
V [jvm.dll+0x12a5e7]
V [jvm.dll+0xd315f]
V [jvm.dll+0x14a697]
V [jvm.dll+0x14a800]
V [jvm.dll+0x17efe9]
C [msvcr100.dll+0x5c6de] endthreadex+0x3a
C [msvcr100.dll+0x5c788] endthreadex+0xe4
C [KERNEL32.DLL+0x2850d] BaseThreadInitThunk+0xe
C [ntdll.dll+0x5bf39] RtlInitializeExceptionChain+0x85
C [ntdll.dll+0x5bf0c] RtlInitializeExceptionChain+0x58
的DLL:
0x00030000 - 0x0005f000 C:\Program Files (x86)\Java\jdk1.7.0_09_32Bit\bin\java.exe
...
0x6d300000 - 0x6d326000 C:\dlls\FooLibWrapper.dll
0x6cf50000 - 0x6d026000 C:\Windows\WinSxS\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.21022.8_none_96748342450f6aa2\MSVCP90D.dll
0x6ce20000 - 0x6cf43000 C:\Windows\WinSxS\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.21022.8_none_96748342450f6aa2\MSVCR90D.dll
0x675a0000 - 0x675d4000 C:\dlls\FooLib.dll
...
堆
def new generation total 157376K, used 101422K [0x047f0000, 0x0f2b0000, 0x19d40000)
eden space 139904K, 61% used [0x047f0000, 0x09bb6fd8, 0x0d090000)
from space 17472K, 89% used [0x0d090000, 0x0dfd49f8, 0x0e1a0000)
to space 17472K, 0% used [0x0e1a0000, 0x0e1a0000, 0x0f2b0000)
tenured generation total 349568K, used 67082K [0x19d40000, 0x2f2a0000, 0x447f0000)
the space 349568K, 19% used [0x19d40000, 0x1dec28a0, 0x1dec2a00, 0x2f2a0000)
compacting perm gen total 57600K, used 57590K [0x447f0000, 0x48030000, 0x547f0000)
the space 57600K, 99% used [0x447f0000, 0x4802da78, 0x4802dc00, 0x48030000)
No shared spaces configured.
参数数量:
VM Arguments:
jvm_args: -Dfile.encoding=UTF8 -Dsbt.boot.properties=file:///E:/play-2.0.4/framework/sbt/sbt.boot.properties -Dsbt.log.noformat=true -Dsbt.global.plugins="C:\Users\TimoW_000\AppData\Local\Temp\sbt-global-plugin233941470253421253stub" -Dplay.version=2.0.4 -Dsbt.ivy.home=E:\play-2.0.4\repository -Dplay.home=E:\play-2.0.4\framework -Xms512M -Xmx1024M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256M
java_command: xsbt.boot.Boot run
Launcher Type: SUN_STANDARD
Environment Variables:
CLASSPATH=.;C:\Program Files (x86)\Java\jre7_32bit\lib\ext\QTJava.zip
PATH=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 12.1\bin\..\.\bin;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 12.1\bin\..\.\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;E:\play-2.0.4\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files (x86)\Java\jdk1.7.0_09_32Bit\bin;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files\TortoiseGit\bin;C:\Program Files (x86)\Git\cmd;C:\Program Files (x86)\OpenSSH\bin;c:\program files (x86)\jetbrains\intellij idea 12.1\jre\jre\bin;c:\program files (x86)\jetbrains\intellij idea 12.1\jre\jre\bin
USERNAME=TimoW_000
OS=Windows_NT
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 42 Stepping 7, GenuineIntel
系统:
OS: Windows 8 , 64 bit Build 9200
CPU:total 4 (4 cores per cpu, 1 threads per core) family 6 model 42 stepping 7, cmov, cx8, fxsr, mmx, sse, sse2, sse3, ssse3, sse4.1, sse4.2, popcnt, avx, tsc, tscinvbit
Memory: 4k page, physical 8361544k(2628436k free), swap 11376200k(4311964k free)
vm_info: Java HotSpot(TM) Client VM (23.5-b02) for windows-x86 JRE (1.7.0_09-b05), built on Sep 24 2012 22:01:33 by "java_re" with unknown MS VC++:1600
time: Fri Sep 27 12:32:29 2013
elapsed time: 122 seconds