Java调用DLL

需要工具dllexp查看DLL内部方法名称

需要工具depends查看DLL的依赖包

 

一。网上开源的工具JNative可以直接调用,但官方只支持32位的,有开源JNativeCPP的源码,没玩过CPP,不知道能否编译出64的

 

二。JNA调用DLL

不需要中间DLL,直接调用即可

import com.sun.jna.Library;
import com.sun.jna.Native;

public class PdfJNA {

    public interface CLibrary extends Library {
        CLibrary INSTANCE = (CLibrary) Native.loadLibrary(("E:\\XPDFLib642.dll"), CLibrary.class);
        int XPDF2XmlWithTable(String format, String s2, byte[] s3);
    }

    public static void main(String[] args) {
        byte[] s3 = new byte[300];
        int r = CLibrary.INSTANCE.XPDF2XmlWithTable("D:\\2013-01-31\\600122_20130201_1.pdf", "D:\\2013-01-31\\dsf", s3);
        System.err.println(r);
    }

}

 

 

三。JNI调用DLL

需要C++的编译器,只要C++调用DLL方法通过,Java就能调用。

 

1.建一个类,包含两部分

static { 
        System.load("E:\\XPDFLib.dll"); 
        System.load("E:\\jniDll.dll");
    } 

 

 public native static int  XPDF2XmlWithTable(String pdfFile,String outFile,String outCtx);

 

2.javac com/Class.java

 

3.javah com/Class

得到Class.h文件

用C++建立一个DLL项目,导入Class.h头文件,实现其方法,方法中调用需要引用的DLL方法。

编译得到jniDll.dll。(jni.h在JDK目录找)

 

4.然后把jniDll.dll和需要调用的XPDFLib.dll一起放到bin下或者指定目录引用,执行XPDF2XmlWithTable方法即可得到结果。

 

附C++中的实现方法,网上搜的,能用:

    //定义方法
    typedef int (_stdcall *_XPDF2XmlWithTable)(char* pdfFile,char* outFile,char* outCtx);

    HINSTANCE hDLL;
    _XPDF2XmlWithTable ttt;

    hDLL=LoadLibrary("XPDFLib64.dll");//加载动态链接库MyDll.dll文件;
    ttt=(_XPDF2XmlWithTable)GetProcAddress(hDLL,"XPDF2XmlWithTable");

    int A=ttt(rtn,rtn2,c3); // 传入的char*参数

    FreeLibrary(hDLL);//卸载MyDll.dll文件;

   

    return A;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值