jnative 使用
下载地址:
JNative_1.4RC2_src.zip : http://jaist.dl.sourceforge.net/sourceforge/jnative/JNative_1.4RC2_src.zip
JNative.jar : http://nchc.dl.sourceforge.net/sourceforge/jnative/JNative.jar
如果以上版本不能完成下载,说明版本有可能更新,请从以下地址中下载:
Resource URL: http://jnative.sourceforge.net/
Source Code: http://sourceforge.net/projects/jnative
Detailed Review: http://jnative.free.fr
JavaDOC: http://jnative.free.fr/docs/
JNative相对于其它同类开源组件的优点:
1.容易使用
2.对数据类型的处理做的出色
3.支持CallBack
下面以一个小Demo来学习一下JNative:
1.理解文件用途
JNative_1.4RC2_src.zip是JNative源代码的压缩包把它解压后从中找到libJNativeCpp.so和JNativeCpp.dll两个文件.JNativeCpp.dll应用在Windows平台下.把它放在c:\windows\system32目录下.libJNativeCpp.so应用在Linux平台下.放在哪个目录,这个我不知道.
把JNative.jar加入到所需的工程中.
把要调用的dll文件也放在c:\windows\system32目录下, 这个目录存在一个文件,
2.测试类
- package sms;
- import org.xvolks.jnative.JNative;
- import org.xvolks.jnative.exceptions.NativeException;
- import org.xvolks.jnative.pointers.Pointer;
- import org.xvolks.jnative.pointers.memory.MemoryBlockFactory;
- public class SystemTime extends org.xvolks.jnative.util.Kernel32.SystemTime {
- public short wYear;
- public short wMonth;
- public short wDayOfWeek;
- public short wDay;
- public short wHour;
- public short wMinute;
- public short wSecond;
- public short wMilliseconds;
- /**
- * 分配内存,并返回指针
- */
- public Pointer createPointer() throws NativeException {
- pointer = new Pointer(MemoryBlockFactory.createMemoryBlock(getSizeOf()));
- return pointer;
- }
- /**
- * 内存大小
- */
- public int getSizeOf(){
- return 8 * 2;
- }
- /**
- * 获取通过内存指针解析出结果
- */
- public SystemTime getValueFromPointer() throws NativeException {
- wYear = getNextShort();
- wMonth = getNextShort();
- wDayOfWeek = getNextShort();
- wDay = getNextShort();
- wHour = getNextShort();
- wMinute = getNextShort();
- wSecond = getNextShort();
- wMilliseconds = getNextShort();
- return this;
- }
- public SystemTime() throws NativeException{
- super();
- createPointer();
- }
- public String toString(){
- return wYear + "/" + wMonth + "/" + wDay + " at + " + wHour + ":" + wMinute + ":" + wSecond + ":" + wMilliseconds;
- }
- public static SystemTime GetSystemTime() throws NativeException, IllegalAccessException {
- // 创建对象
- JNative nGetSystemTime = new JNative("Kernel32.dll", "GetSystemTime");
- //GetSystemTime 是dll中的方法
- SystemTime systemTime = new SystemTime();
- // 设置参数
- nGetSystemTime.setParameter(0, systemTime.getPointer());
- //执行方法
- nGetSystemTime.invoke();
- // 解析结构指针内容
- return systemTime.getValueFromPointer();
- }
- public static void main(String[] args) throws NativeException, IllegalAccessException{
- System.err.println(GetSystemTime());
- }
- }