需要工具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;