源码:
package com.bsoft.dll;
import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.pointers.Pointer;
import org.xvolks.jnative.pointers.memory.MemoryBlockFactory;
/**
* 执行此函数需要把JNative.jar中的lib-bin\JNativeCpp.dll文件拷贝到C:\Windows\System32下;
* 并且把zjnet.dll文件也拷贝到C:\Windows\System32下
* @author LOVCC
* @date 2011/12/28
*
*/
public class JavaCallsDLL {
//系统调用的dll文件名
private static final String dllName = "zjnet.dll" ;
//dll文件中定义的初始化函数
private static final String initFunName = "InitSocketEnvironment" ;
//dll文件中定义的交易函数
private static final String tradeFunName = "SendDateToTXZFAndReceiveFromTXZF" ;
//dll文件中定义的释放资源函数
private static final String closeFunName = "FinalSocketEnvironment" ;
//为返回消息而设置的内存大小
private static final int memorySize = 65535 ;
//JNative对象用于操作dll文件
// private JNative jn = null ;
// private Pointer point ;
/**
* 初始化资源
* @return 返回初始化的状态码
* >0 代表成功
* @throws Exception
*/
public int open() throws Exception {
JNative jn = null ;
jn = new JNative(dllName, initFunName) ;
jn.setRetVal(Type.INT);
jn.invoke() ;
return jn.getRetValAsInt() ;
}
/**
* 交易函数
* @param send
* @param receive
* @return 返回执行函数的信息
* 失败返回null
* @throws Exception
*/
public String trade(String send, String receive) throws Exception{
JNative jn = null ;
jn = new JNative(dllName, tradeFunName) ;
jn.setRetVal(Type.INT) ;
Pointer point = null ;
point= new Pointer(MemoryBlockFactory.createMemoryBlock(memorySize));
point.setStringAt(0, receive) ;
jn.setParameter(0, Type.STRING, send) ;
jn.setParameter(1, point) ;
jn.invoke() ;
int i = jn.getRetValAsInt() ;
String str = null ;
if(i>=0){
str = point.getAsString() ;
}
else {
System.out.println("交易失败");
}
point.zeroMemory() ;
point.dispose() ;
return str ;
}
/**
* 释放系统资源函数
* @return
* @throws Exception
*/
public String close() throws Exception{
int i ;
String message ;
JNative jn = null ;
jn = new JNative(dllName, closeFunName) ;
jn.setRetVal(Type.INT);
jn.invoke() ;
i = jn.getRetValAsInt() ;
if(i>0)
message = "资源释放成功!" ;
else
message = "资源释放失败!" ;
return message ;
}
/**
* 把以上三个函数封装
* 调用函数直接执行
* 初始化信息,进行交易,释放资源
* 三步
* @param send
* @param receive
* @return 返回交易信息
*/
public String getInfo(String send, String receive){
String info = null ;
try{
int i = open() ;
if(i>0){
info = trade(send, receive) ;
}
else{
System.out.println("建立连接失败") ;
throw new Exception("建立连接失败") ;
}
System.out.println(close()) ;
}catch(Exception e){
e.printStackTrace() ;
}
return info ;
}
public static void main(String args[]){
System.out.println(new JavaCallsDLL().getInfo("Exchcode=H888","ExchCode=H888&yydm=qd0100&qydm=370214")) ;
}
}
程序报错:
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x75932c70, pid=2492, tid=5892
#
# JRE version: 6.0_23-b05
# Java VM: Java HotSpot(TM) Client VM (19.0-b09 mixed mode, sharing windows-x86 )
# Problematic frame:
# C [mswsock.dll+0x2c70]
#
# 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.
#
--------------- T H R E A D ---------------
Current thread (0x01a99400): JavaThread "main" [_thread_in_native, id=5892, stack(0x00340000,0x00390000)]
siginfo: ExceptionCode=0xc0000005, reading address 0x00000000
Registers:
EAX=0x00000000, EBX=0x00000000, ECX=0x77d86500, EDX=0x04380180
ESP=0x00377760, EBP=0x00000000, ESI=0x000001f4, EDI=0x77473918
EIP=0x75932c70, EFLAGS=0x00010246
Register to memory mapping:
EAX=0x00000000
0x00000000 is pointing to unknown location
EBX=0x00000000
0x00000000 is pointing to unknown location
ECX=0x77d86500
0x77d86500 is pointing to unknown location
EDX=0x04380180
0x04380180 is pointing to unknown location
ESP=0x00377760
0x00377760 is pointing into the stack for thread: 0x01a99400
"main" prio=6 tid=0x01a99400 nid=0x1704 runnable [0x0038f000]
java.lang.Thread.State: RUNNABLE
EBP=0x00000000
0x00000000 is pointing to unknown location
ESI=0x000001f4
0x000001f4 is pointing to unknown location
EDI=0x77473918
0x77473918 is pointing to unknown location
Top of Stack: (sp=0x00377760)
0x00377760: 00000002 000e0050 0037779c 03ee504e
0x00377770: 000001f4 00377798 04058e30 00000000
0x00377780: 77473962 000001f4 00377798 77473918
0x00377790: 000001f4 00000002 00000000 00000000
0x003777a0: 10001677 000001f4 00000000 0038fbe8
0x003777b0: 00000000 00000002 00000000 fffffffa
0x003777c0: 4f3c578f 00000000 003779e4 003779f1
0x003777d0: 000001b0 00000000 00000000 00000000
Instructions: (pc=0x75932c70)
0x75932c60: 00 00 00 3b fb 0f 85 83 c6 00 00 33 c0 5f 5e 5b
0x75932c70: c9 c2 08 00 90 90 90 90 90 8b ff 55 8b ec 56 8b
Stack: [0x00340000,0x00390000], sp=0x00377760, free space=221k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C [mswsock.dll+0x2c70]
[error occurred during error reporting (printing native stack), id 0xc0000005]
Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j org.xvolks.jnative.JNative.nInvoke(I)V+0
j org.xvolks.jnative.JNative.invoke()V+67
j com.bsoft.dll.JavaCallsDLL.trade(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;+63
j com.bsoft.dll.JavaCallsDLL.getInfo(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;+16
j com.bsoft.dll.JavaCallsDLL.main([Ljava/lang/String;)V+14
v ~StubRoutines::call_stub
--------------- P R O C E S S ---------------
Java Threads: ( => current thread )
0x01996800 JavaThread "Low Memory Detector" daemon [_thread_blocked, id=6324, stack(0x03dc0000,0x03e10000)]
0x0198ec00 JavaThread "CompilerThread0" daemon [_thread_blocked, id=5736, stack(0x03d70000,0x03dc0000)]
0x0198bc00 JavaThread "Attach Listener" daemon [_thread_blocked, id=8084, stack(0x03d20000,0x03d70000)]
0x01988c00 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=1312, stack(0x03cd0000,0x03d20000)]
0x01947000 JavaThread "Finalizer" daemon [_thread_blocked, id=6184, stack(0x03c80000,0x03cd0000)]
0x01942400 JavaThread "Reference Handler" daemon [_thread_blocked, id=8024, stack(0x03c30000,0x03c80000)]
=>0x01a99400 JavaThread "main" [_thread_in_native, id=5892, stack(0x00340000,0x00390000)]
Other Threads:
0x01940800 VMThread [stack: 0x03be0000,0x03c30000] [id=2512]
0x019a8400 WatcherThread [stack: 0x03e10000,0x03e60000] [id=6328]
VM state:not at safepoint (normal execution)
VM Mutex/Monitor currently owned by a thread: None
Heap
def new generation total 4928K, used 677K [0x23760000, 0x23cb0000, 0x28cb0000)
eden space 4416K, 15% used [0x23760000, 0x238094e8, 0x23bb0000)
from space 512K, 0% used [0x23bb0000, 0x23bb0000, 0x23c30000)
to space 512K, 0% used [0x23c30000, 0x23c30000, 0x23cb0000)
tenured generation total 10944K, used 0K [0x28cb0000, 0x29760000, 0x33760000)
the space 10944K, 0% used [0x28cb0000, 0x28cb0000, 0x28cb0200, 0x29760000)
compacting perm gen total 12288K, used 516K [0x33760000, 0x34360000, 0x37760000)
the space 12288K, 4% used [0x33760000, 0x337e1380, 0x337e1400, 0x34360000)
ro space 10240K, 54% used [0x37760000, 0x37cdc6b0, 0x37cdc800, 0x38160000)
rw space 12288K, 55% used [0x38160000, 0x388001f0, 0x38800200, 0x38d60000)
Dynamic libraries:
0x00400000 - 0x00424000 C:\Program Files\Java\jdk1.6.0_23\bin\javaw.exe
0x77d30000 - 0x77e6c000 C:\Windows\SYSTEM32\ntdll.dll
0x77740000 - 0x77814000 C:\Windows\system32\kernel32.dll
0x76160000 - 0x761aa000 C:\Windows\system32\KERNELBASE.dll
0x77ec0000 - 0x77f60000 C:\Windows\system32\ADVAPI32.dll
0x77870000 - 0x7791c000 C:\Windows\system32\msvcrt.dll
0x761b0000 - 0x761c9000 C:\Windows\SYSTEM32\sechost.dll
0x762a0000 - 0x76341000 C:\Windows\system32\RPCRT4.dll
0x773a0000 - 0x77469000 C:\Windows\system32\USER32.dll
0x76250000 - 0x7629e000 C:\Windows\system32\GDI32.dll
0x77eb0000 - 0x77eba000 C:\Windows\system32\LPK.dll
0x776a0000 - 0x7773d000 C:\Windows\system32\USP10.dll
0x77380000 - 0x7739f000 C:\Windows\system32\IMM32.DLL
0x774b0000 - 0x7757c000 C:\Windows\system32\MSCTF.dll
0x7c340000 - 0x7c396000 C:\Program Files\Java\jdk1.6.0_23\jre\bin\msvcr71.dll
0x6d8a0000 - 0x6db4c000 C:\Program Files\Java\jdk1.6.0_23\jre\bin\client\jvm.dll
0x74900000 - 0x74932000 C:\Windows\system32\WINMM.dll
0x75df0000 - 0x75e3c000 C:\Windows\system32\apphelp.dll
0x6d850000 - 0x6d85c000 C:\Program Files\Java\jdk1.6.0_23\jre\bin\verify.dll
0x6d3d0000 - 0x6d3ef000 C:\Program Files\Java\jdk1.6.0_23\jre\bin\java.dll
0x6d330000 - 0x6d338000 C:\Program Files\Java\jdk1.6.0_23\jre\bin\hpi.dll
0x77ea0000 - 0x77ea5000 C:\Windows\system32\PSAPI.DLL
0x6d890000 - 0x6d89f000 C:\Program Files\Java\jdk1.6.0_23\jre\bin\zip.dll
0x66c40000 - 0x66c62000 C:\Windows\System32\JNativeCpp.dll
0x77bd0000 - 0x77d2c000 C:\Windows\system32\OLE32.dll
0x77920000 - 0x779af000 C:\Windows\system32\OLEAUT32.DLL
0x10000000 - 0x1002c000 C:\Windows\system32\zjnet.dll
0x7c140000 - 0x7c243000 C:\Windows\system32\MFC71.DLL
0x779b0000 - 0x77a07000 C:\Windows\system32\SHLWAPI.dll
0x7c3a0000 - 0x7c41b000 C:\Windows\system32\MSVCP71.dll
0x77470000 - 0x774a5000 C:\Windows\system32\WS2_32.dll
0x77e80000 - 0x77e86000 C:\Windows\system32\NSI.dll
0x5d360000 - 0x5d36a000 C:\Windows\system32\MFC71CHS.DLL
0x03ee0000 - 0x03f07000 C:\Windows\system32\ikutm.dll
0x03f30000 - 0x03f42000 C:\Program Files\Common Files\Thunder Network\NetMon\net_monitor1.0.2.25.dll
0x76380000 - 0x76fca000 C:\Windows\system32\SHELL32.dll
0x75450000 - 0x75459000 C:\Windows\system32\VERSION.dll
0x75ec0000 - 0x75ecb000 C:\Windows\system32\profapi.dll
0x75930000 - 0x7596c000 C:\Windows\system32\mswsock.dll
0x75430000 - 0x75436000 C:\Windows\System32\wshqos.dll
0x75440000 - 0x75445000 C:\Windows\system32\wshtcpip.DLL
0x75920000 - 0x75926000 C:\Windows\system32\wship6.dll
VM Arguments:
jvm_args: -Dfile.encoding=GBK
java_command: com.bsoft.dll.JavaCallsDLL
Launcher Type: SUN_STANDARD
Environment Variables:
JAVA_HOME=C:\Program Files\Java\jdk1.6.0_23
CLASSPATH=.;C:\PROGRA~1\IBM\SQLLIB\java\db2java.zip;C:\PROGRA~1\IBM\SQLLIB\java\db2jcc.jar;C:\PROGRA~1\IBM\SQLLIB\java\sqlj.zip;C:\PROGRA~1\IBM\SQLLIB\java\db2jcc_license_cu.jar;C:\PROGRA~1\IBM\SQLLIB\bin;C:\PROGRA~1\IBM\SQLLIB\tools\db2XTrigger.jar;C:\PROGRA~1\IBM\SQLLIB\java\common.jar
PATH=C:/Program Files/Java/jre6/bin/client;C:/Program Files/Java/jre6/bin;C:/Program Files/Java/jre6/lib/i386;C:\oracle\product\10.2.0\client_2\bin;C:\Program Files\oracle10g\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Common Files\Thunder Network\KanKan\Codecs;C:\Program Files\Java\jdk1.6.0_23\bin;D:\android-sdk-windows\tools;C:\PROGRA~1\IBM\SQLLIB\BIN;C:\PROGRA~1\IBM\SQLLIB\FUNCTION;C:\PROGRA~1\IBM\SQLLIB\SAMPLES\REPL;D:\Maxthon\oracle10g;C:\Program Files\MySQL\MySQL Server 5.1\bin;C:\Program Files\TortoiseSVN\bin
USERNAME=LOVCC
OS=Windows_NT
PROCESSOR_IDENTIFIER=x86 Family 6 Model 23 Stepping 10, GenuineIntel
--------------- S Y S T E M ---------------
OS: Windows 7 Build 7601 Service Pack 1
CPU:total 2 (2 cores per cpu, 1 threads per core) family 6 model 23 stepping 10, cmov, cx8, fxsr, mmx, sse, sse2, sse3, ssse3
Memory: 4k page, physical 2056804k(943808k free), swap 4113608k(2481416k free)
vm_info: Java HotSpot(TM) Client VM (19.0-b09) for windows-x86 JRE (1.6.0_23-b05), built on Nov 12 2010 15:00:43 by "java_re" with MS VC++ 7.1 (VS2003)
time: Thu Feb 16 09:10:39 2012
elapsed time: 0 seconds
唉,折腾一天了,百度,google都没有结果,麻烦了解的给看看!!!
分享到:
相关推荐
《深入理解JNative:Java调用DLL的桥梁》 在Java世界中,由于其平台无关性的特性,直接调用本地动态链接库(DLL)并非易事。然而,JNative库的出现,为Java程序员提供了一个方便的解决方案,使得Java能够无缝地与C/...
3. **JNative.jar**:这是一个Java类库,封装了与本地库交互的逻辑,可能是为了简化调用dll或.so文件的过程。开发者只需要在Java代码中导入这个jar包,并按照库提供的API进行操作,就可以实现跨平台的本地方法调用。...
使用 JNative.jar,Java 开发者可以方便地调用 JNativeCpp.dll 中的本地函数。 "libJNativeCpp.so" 是一个 Linux 平台上的共享对象文件,相当于 Windows 平台上的 DLL 文件。这表明 JNativeCpp 库也支持跨平台,为 ...
《VC++环境与Java调用DLL动态库的深入理解》 在软件开发中,跨语言交互是一个常见的需求,其中,Java作为一种广泛使用的编程语言,有时需要调用C或C++编写的动态链接库(DLL)来扩展其功能或利用特定硬件的底层接口...
简介 笔者当初为了学习JAVA,收集了很多经典源码,源码难易程度分为初级、中级、高级等,详情看源码列表,需要的可以直接下载! 这些源码反映了那时那景笔者对未来的盲目,对代码的热情、执着,对IT的憧憬、向往!...