JNA粗浅的一些讲解

对着背影说爱祢 提交于 2019-12-28 03:10:23

1.JNA是什么

JNA(Java Native Access )提供一组Java工具类用于在运行期动态访问系统本地库(native library:如Window的dll)而不需要编写任何Native/JNI代码。开发人员只要在一个java接口中描述目标native library的函数与结构,JNA将自动实现Java接口到native function的映射。

2.通过JNA调用DLL

首先我们导入JNA支持的jar包:jna-5.4.0.jar,jna-platform-5.4.0.jar

使用代码调用DLL动态库,举例说明:

// Alternative 1: interface-mapped class, dynamically load the C library
public interface CLibrary extends Library {
    CLibrary INSTANCE = (CLibrary)Native.loadLibrary("c", CLibrary.class);
}

// Alternative 2: direct-mapped class (uses a concrete class rather than an
// interface, with a slight variation in method
// declarations). 
public class Example extends Structure{
    public int dwSize;
    public byte[] byRes = new byte[23];
}

调用Example.

static CLibrary lib = CLibrary.INSTANCE;
public static void main(String[] args){
    lib.Example example = new lib.Example();
}

3.JNA文档

JNA API是JNA的API规范文档,作为资料查阅和知识参考。
以下展示它的目录:
在这里插入图片描述
JNA仓库是JNA的github的仓库,作为问题解决和详细的资料参考途径。

4.JNA类型转换举例

JAVA-C-操作系统数据类型的对照:

Java Type C Type
boolean int
byte char
char wchar_t
short short
int int
long long long,__int64
float float
double double
Buffer,Pointer pointer
<T>[](array of primitive type) pointer,array
String char*
WString wchar_t*
String[] wchar_t**
Structure struct*,struct
Union union
Structure[] struct[]
Callback <T>(*fp)()
NativeMapped varies
NativeLong long
PointerType pointer

接下来介绍几个常用的类型转换例子.

  • Structure与Pointer互转
public class A extends Structure{
    int x;
}

A a = new A();
Pointer ptrA = a.getPointer();
a.write();//结构体内存转移到Pointer
//读取
a.read();//Pointer内存转移到结构体
  • 结构体作为参数进行传递
//A.java
import com.sun.jna.Structure;

public class A extends Structure{
	public int dwsize;
}
//Example2.java
import java.awt.Point;
import java.util.HashMap;
import java.util.Map;

import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.ptr.ByteByReference;

public class Example2 {
	 static void work(Pointer p)
	{
		A stru = new A();
		Pointer pointer = stru.getPointer();
		pointer.write(0, p.getByteArray(0, stru.size()),0, stru.size());
		stru.read();
		stru.dwsize=2;
		stru.write();
		p.write(0, pointer.getByteArray(0, stru.size()), 0, stru.size());
	}
	public static void main(String[] args){
		A dd = new A();
		dd.dwsize=1;
		System.out.println("修改前的dwsize = "+ dd.dwsize);
		Pointer p=dd.getPointer();
		dd.write();
		work(p);
		dd.read();
		System.out.println("修改后的dwsize = "+ dd.dwsize);
	}
}
  • 回调函数
// Original C code
struct _functions {
  int (*open)(const char*,int);
  int (*close)(int);
};

// Equivalent JNA mapping
public class Functions extends Structure {
  public static interface OpenFunc extends Callback {
    int invoke(String name, int options);
  }
  public static interface CloseFunc extends Callback {
    int invoke(int fd);
  }
  public OpenFunc open;
  public CloseFunc close;
}
...
Functions funcs = new Functions();
lib.init(funcs);
int fd = funcs.open.invoke("myfile", 0);
funcs.close.invoke(fd);

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!