JNA调用C语言动态链接库学习实践总结
2015-08-10 18:36
561 查看
最新因为项目需要,学习了一下JNA框架,在这里记录一下学习和使用心得,给大家分享,希望能帮助新手。
本文主要讲解如何使用JNA框架轻松调用C语言动态链接库,如何使用JNA模拟C语言参数(例如数组、指针等)。
JNA(Java Native Access)框架是一个开源的Java框架,是SUN公司主导开发的,建立在经典的JNI的基础之上的一个框架。
JNA官网地址:https://github.com/twall/jna
官网上有很多例子和学习入门教程,建议认真阅读,很有帮助,比自己网络上到处搜索效果好多了。
Maven项目引入JNA:
[html] view
plaincopy
<!--Java JNA -->
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.1.0</version>
</dependency>
引入JNA:
import com.sun.jna.*;
import com.sun.jna.ptr.*;
C语言函数:
void function1(int a, int b, const unsigned char* data);
char * function2(float * pVal, char * outString);
void function3(Rect * pRect, Rect rect);
JNA模拟:
[java] view
plaincopy
public interface MyLibTest extends Library {
//定义结构体
public static class Rect extends Structure {
//公共字段的顺序,必须与C语言中的结构的顺序一致,否则会报错!
public int left;
public int top;
public int right;
public int bottom;
//结构体传指针
public static class ByReference extends Rect implements Structure.ByReference { }
//结构体传值
public static class ByValue extends Rect implements Structure.ByValue{ }
@Override
protected List getFieldOrder() {
return Arrays.asList(new String[]{"left", "top", "right", "bottom"});
}
//加载库文件
MyLibTest INSTANCE = (MyLibTest) Native.loadLibrary("C:\libTest\CLib.dll",MyLibTest.class);
//函数模拟
void function1(int a, int b, char[] data);
String function2(FloatByReference fRef, char [] outString);
void function3(Rect.ByReference pRect, Rect.ByValue rect);
}
调用方式:
char[] arr1 = new char[数组大小];
function1(1, 2, arr1);
char[] arr2 = new char[数组大小];
FloatByReference fRef = new FloatByReference(0.1F);
String result = function2(fRef, arr2);
Rect.ByReference pRect = new Rect.ByReference(); //指针对象
Rect.ByValue rect = new Rect.ByValue(); //传值对象
function3(pRect, rect);
C语言函数:
int function1(int a, float b, long c)
JNA模拟:
[java] view
plaincopy
int function1(int a, float b, long c)
C语言函数:
void function1(char * data)
void function2(const unsigned char* data)
JNA模拟:
[java] view
plaincopy
void function1(char[] data) 或者 void function1(byte[] data)
void function2(char[] data) 或者 void function2(byte[] data)
JNA的ByReference有很多子类,这些类都在com.sun.jna.ptr包中:
IntByReference,LongByReference,FloatByReference,DoubleByReference,ShortByReference、ByteByReference、PointerByReference等等
从这些名字大家应该可以看出来他们的作用。
下面直接上例子吧:
C语言函数:
long function(int * a, long * b, float * c, double * d, short * e)
JNA模拟:
[java] view
plaincopy
long function(IntByReference aRef, LongByReference bRef, FloatByReference cRef, DoubleByReference dRef, ShortByReference eRef)
如何构建这些对象呢?
[java] view
plaincopy
FloatByReference cRef = new FloatByReference(); //使用默认初始值(具体多少我也不知道)
FloatByReference cRef = new FloatByReference(0); //初始值为0
调用方法和普通参数一样:
function(..., cRef, ...);
获取结果值:
[java] view
plaincopy
float fVal = cRef.getValue();
JNA都为我们做的很简单,不是吗?
C函数:
void function(int * pInt, int ** ppInt, void * pVoid, void ** ppVoid)
JNA模拟:
[java] view
plaincopy
void function(IntByReference pInt, PointerByReference ppInt, Pointer pVoid, PointerByReference ppVoid)
调用举例:
IntByReference pInt = new IntByReference(0);
PointerByReference ppInt = new PointerByReference(Pointer.NULL); //指向指针的指针,初始化为NULL
Pointer pVoid = Pointer.NULL; //创建一个指向NULL的指针
PointerByReference ppVoid = new PointerByReference(Pointer.NULL);
调用:function(pInt, ppInt, pVoid, ppVoid);
(1)PointerByReference是指向指针的指针,遇到指针的指针都可以使用它来模拟,那么如何获取到它指向的指针呢?
Pointer p = ppVoid.getValue(); //获取指针
(2)如何获取指针的指针呢?
[java] view
plaincopy
Pointer p1 = ....;
PointerByReference pp1 = new PointerByReference(p1);
PointerByReference ppp1 = new PointerByReference(pp1.getPointer());
这些操作大家可以自己做实验尝试,对于PointerByReference对象,getValue()是取值,而getPointer()是取这个指针的指针。
看着复杂,其实都很简单!JNA要比JNI好用多了。
参考学习资料:
/article/1672221.html
/article/1672220.html
转载于:http://blog.csdn.net/zht666/article/details/22786123
本文主要讲解如何使用JNA框架轻松调用C语言动态链接库,如何使用JNA模拟C语言参数(例如数组、指针等)。
JNA(Java Native Access)框架是一个开源的Java框架,是SUN公司主导开发的,建立在经典的JNI的基础之上的一个框架。
JNA官网地址:https://github.com/twall/jna
官网上有很多例子和学习入门教程,建议认真阅读,很有帮助,比自己网络上到处搜索效果好多了。
Maven项目引入JNA:
[html] view
plaincopy
<!--Java JNA -->
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.1.0</version>
</dependency>
引入JNA:
import com.sun.jna.*;
import com.sun.jna.ptr.*;
1.构造JNA模拟类
C语言函数:void function1(int a, int b, const unsigned char* data);
char * function2(float * pVal, char * outString);
void function3(Rect * pRect, Rect rect);
JNA模拟:
[java] view
plaincopy
public interface MyLibTest extends Library {
//定义结构体
public static class Rect extends Structure {
//公共字段的顺序,必须与C语言中的结构的顺序一致,否则会报错!
public int left;
public int top;
public int right;
public int bottom;
//结构体传指针
public static class ByReference extends Rect implements Structure.ByReference { }
//结构体传值
public static class ByValue extends Rect implements Structure.ByValue{ }
@Override
protected List getFieldOrder() {
return Arrays.asList(new String[]{"left", "top", "right", "bottom"});
}
//加载库文件
MyLibTest INSTANCE = (MyLibTest) Native.loadLibrary("C:\libTest\CLib.dll",MyLibTest.class);
//函数模拟
void function1(int a, int b, char[] data);
String function2(FloatByReference fRef, char [] outString);
void function3(Rect.ByReference pRect, Rect.ByValue rect);
}
调用方式:
char[] arr1 = new char[数组大小];
function1(1, 2, arr1);
char[] arr2 = new char[数组大小];
FloatByReference fRef = new FloatByReference(0.1F);
String result = function2(fRef, arr2);
Rect.ByReference pRect = new Rect.ByReference(); //指针对象
Rect.ByValue rect = new Rect.ByValue(); //传值对象
function3(pRect, rect);
2.JNA模拟普通传值参数
C语言函数:int function1(int a, float b, long c)
JNA模拟:
[java] view
plaincopy
int function1(int a, float b, long c)
3.JNA模拟C语言数组
C语言函数:void function1(char * data)
void function2(const unsigned char* data)
JNA模拟:
[java] view
plaincopy
void function1(char[] data) 或者 void function1(byte[] data)
void function2(char[] data) 或者 void function2(byte[] data)
4.JNA模拟基本类型指针
JNA的ByReference有很多子类,这些类都在com.sun.jna.ptr包中:IntByReference,LongByReference,FloatByReference,DoubleByReference,ShortByReference、ByteByReference、PointerByReference等等
从这些名字大家应该可以看出来他们的作用。
下面直接上例子吧:
C语言函数:
long function(int * a, long * b, float * c, double * d, short * e)
JNA模拟:
[java] view
plaincopy
long function(IntByReference aRef, LongByReference bRef, FloatByReference cRef, DoubleByReference dRef, ShortByReference eRef)
如何构建这些对象呢?
[java] view
plaincopy
FloatByReference cRef = new FloatByReference(); //使用默认初始值(具体多少我也不知道)
FloatByReference cRef = new FloatByReference(0); //初始值为0
调用方法和普通参数一样:
function(..., cRef, ...);
获取结果值:
[java] view
plaincopy
float fVal = cRef.getValue();
JNA都为我们做的很简单,不是吗?
5. JNA模拟指针、指针的指针、模拟void *,void ** 等指针
C函数:void function(int * pInt, int ** ppInt, void * pVoid, void ** ppVoid)
JNA模拟:
[java] view
plaincopy
void function(IntByReference pInt, PointerByReference ppInt, Pointer pVoid, PointerByReference ppVoid)
调用举例:
IntByReference pInt = new IntByReference(0);
PointerByReference ppInt = new PointerByReference(Pointer.NULL); //指向指针的指针,初始化为NULL
Pointer pVoid = Pointer.NULL; //创建一个指向NULL的指针
PointerByReference ppVoid = new PointerByReference(Pointer.NULL);
调用:function(pInt, ppInt, pVoid, ppVoid);
(1)PointerByReference是指向指针的指针,遇到指针的指针都可以使用它来模拟,那么如何获取到它指向的指针呢?
Pointer p = ppVoid.getValue(); //获取指针
(2)如何获取指针的指针呢?
[java] view
plaincopy
Pointer p1 = ....;
PointerByReference pp1 = new PointerByReference(p1);
PointerByReference ppp1 = new PointerByReference(pp1.getPointer());
这些操作大家可以自己做实验尝试,对于PointerByReference对象,getValue()是取值,而getPointer()是取这个指针的指针。
看着复杂,其实都很简单!JNA要比JNI好用多了。
参考学习资料:
/article/1672221.html
/article/1672220.html
转载于:http://blog.csdn.net/zht666/article/details/22786123
相关文章推荐
- 2015年8月10日 C语言 宏和快排
- C语言-06-复杂数据类型
- C++_命名空间namespace 与 using编译指令 与 using声明使用。
- (๑• . •๑)Eclipse的C++排版方式
- 学习笔记-有关C语言的指针的类型和本质
- 堆排序(C++版)
- c语言计算器
- VC++ 如何在显示对话框的时候,指定焦点控件!
- 趣味数学应用:求小球在一条斜线上的运动轨迹
- C++中new是否进行初始化的问题
- C++_布局(placement)new操作符
- 代码静态检查-cppcheck
- C++的XML编程经验――LIBXML2库使用指南
- hdu 2024 C语言合法标识符
- C语言的执行过程
- 解决 “VC++ 多线程编程,win32,MFC 例子(转)”在高版本上运行有错误的问题
- C++ string与int的转换
- C/C++关键字中的extern 和 static
- C语言中二维数组指针的简要说明
- C++ std::tr1::bind使用