Java调用dll方法

  • 格式:pdf
  • 大小:53.44 KB
  • 文档页数:1

Java调⽤dll⽅法

使⽤JNA框架⽐⽤原来JNI要⽅便多了,对于DLL不需要额外的包装,直接就能够使⽤:

1.JNA框架

a.定义:JNA(Java Native Access)框架是⼀个开源的Java框架,是SUN公司主导开发的,建⽴在经典的JNI的基础之上的⼀个框架

b.作⽤:JNA提供⼀组Java⼯具类⽤于在运⾏期动态访问系统本地库(native library:如Window的dll)⽽不需要编写任何Native/JNI代码。开发⼈员只要在⼀个java接⼝中描述⽬标native library的函数与结构,JNA将⾃动实现Java接⼝到native function的映射。

c.导⼊maven依赖

net.java.dev.jna jna 5.5.0

2.调⽤科⼤讯飞windows api的⽅法

public class XunfeiSpeech {

public interface MscLibrary extends Library {

// DLL⽂件默认路径为项⽬根⽬录,若DLL⽂件存放在项⽬外,请使⽤绝对路径 MscLibrary INSTANCE = Native.load("D:\\app\\yuyinhecheng\\bin\\msc_x64", MscLibrary.class);

int MSPLogin(String username, String password, String param);

int MSPLogout();

String QTTSSessionBegin(String params, IntByReference errorCode);

int QTTSTextPut(String sessionID, String textString, int textLen, String params);

Pointer QTTSAudioGet(String sessionID, IntByReference audioLen, IntByReference synthStatus, IntByReference errorCode);

int QTTSSessionEnd(String sessionID, String hints); }...

然后在Java代码中就可以使⽤了:

int loginCode = MscLibrary.INSTANCE.MSPLogin(null, null, login_params);