java中使用JNative调用dll文件
如果是64位的dll 需要64位的jdk1.8、tomcat
本章调用的是 32位的dll文件。
这个报错是因为 环境和dll文件不符合导致,32位的dll要用 32位的环境
不说别的了,开始整代码。
人啊,干计算机不容易啊。
活着更难,何必自己为难自己呢,大家说 是不。
好了不废话了开始整。
System.out.println(System.getProperty( "java.library.path" ));
// 要调用的 ADD_COMAD.dll 文件
System.loadLibrary("ADD_COMAD");
// 我这主要传的参数是 xml报文形式的
String xmlconter ="xml报文";
Pointer pIn = null;
Pointer pOut = null;
try{
JNative jNative = new JNative("ADD_COMAD.dll", "调用的方法名");
byte[] bytes = (xmlconter + '\0').getBytes();
MemoryBlock memoryBlockIn=MemoryBlockFactory.createMemoryBlock(bytes.length);
pIn = new Pointer(memoryBlockIn);
// 设置传入参数
pIn.setMemory(bytes);
jNative.setParameter(0,pIn);
MemoryBlock memoryBlockOut= MemoryBlockFactory.createMemoryBlock(1000000);
pOut = new Pointer(memoryBlockOut);
jNative.setParameter(1, pOut);
jNative.invoke();
byte[] memory = pOut.getMemory();
String bak = new String(memory, "GBK");
System.err.println(bak);
System.err.println(jNative.getRetVal());
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (pIn != null){
pIn.dispose();
}
if (pOut != null){
pOut.dispose();
}
}catch (NativeException e){
System.err.println("**遇到错误*****"+e);
}
}
希望对大家有帮助。。。。。。。嗨嗨