您的位置:首页 > 编程语言 > Java开发

Java调用DLL

2016-05-12 13:06 417 查看
java调用dll的方法实例:

1、使用JNI调用:参考

 http://wenku.baidu.com/view/927abf92f242336c1fb95e31.html
http://blog.csdn.net/whustyle/article/details/49123933
直接在java中输出类的配置:http://www.oschina.net/question/1402563_133543?fromerr=f7RcQNZn

2、使用JNA调用

例如:

需要导入jna.jar包

(1)接口类:

package com.lp.jna;

import com.sun.jna.Library;

import com.sun.jna.Native;

public interface JnaDllInterFace extends Library{
//void 
//加载Dll
//JnaDllInterFace dllInterFace = (JnaDllInterFace)

    //Native.loadLibrary((Platform.isWindows() ? "UnZipTool.dll" : "c"), JnaDllInterFace.class);

JnaDllInterFace instanceDll  = (JnaDllInterFace)Native.loadLibrary("UnZipTool.dll",JnaDllInterFace.class);  

    //声明Dll中的函数,如果Dll中有个printf的函数,在此声明一个同名的

    //且参数类型一致的方法,java与c的类型映射见第三节
public void getExportPatch(String strSourceZipPath, String strDestZipPath, String strNewDestZipPath);

}

(2)测试

package com.lp.test;

import com.lp.jna.JnaDllInterFace;

import com.sun.jna.Library;

public class TestDLL {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//fun("G:\\zipCeshi1\\Config.zip","G:\\zipCeshi2\\Config.zip","G:\\zipCeshi3");
//方法一:Jna调用DLL
/**
* jna测试
* 1、添加jna的jar包
* 2、添加接口extends Library
* 3、调用方法
*/
JnaDllInterFace.instanceDll.getExportPatch("G:\\zipCeshi1\\Config.zip", "G:\\zipCeshi2\\Config.zip","G:\\zipCeshi3");
System.out.println("hello world");

}

}

3、使用JNative调用

导入JNative.jar包

(1)接口文件

 package com.lp.dll;

import org.xvolks.jnative.exceptions.NativeException;

public interface DllUtilTools {

void ImplementDll(String strSourcePath,String strDestPath,String strSavePath) throws NativeException, IllegalAccessException;


(2)实现接口文件

 package com.lp.dll;

import org.xvolks.jnative.JNative;

import org.xvolks.jnative.Type;

import org.xvolks.jnative.exceptions.NativeException;

public class DllUtilToolsImpl implements DllUtilTools{

static JNative myjnative = null;

public void ImplementDll(String strSourcePath,String strDestPath,String strSavePath) throws NativeException,IllegalAccessException {

try {

if (myjnative == null) {

myjnative = new JNative("UnZipTool.dll", "getExportPatch");

myjnative.setRetVal(Type.INT);

}

//传入参数

//myjnative.setParameter(0,"F:\\ziptest\\ceshi1\\aaa\\aaa.zip 

");

//myjnative.setParameter(1, "F:\\ziptest\\ceshi2\\v\\bbb.zip 

");

//myjnative.setParameter(2, "F:\\ziptest\\ceshi3");

myjnative.setParameter(0, strSourcePath);

myjnative.setParameter(1, strDestPath);

myjnative.setParameter(2, strSavePath);

myjnative.invoke();

myjnative.getRetVal();

System.out.println(myjnative.getRetVal());

//return myjnative.getRetValAsInt();

} finally {

if (myjnative != null) {

myjnative.dispose();

}

}

}

}

(3)测试

DllUtilToolsImpl impl = new DllUtilToolsImpl();

impl.ImplementDll("F:\\ziptest\\ceshi1\\aaa\\aaa.zip 

", "F:\\ziptest\\ceshi2\\v\\bbb.zip 

", "F:\\ziptest\\ceshi3");

System.out.println("hello world!"); 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Java调用DLL