JNative的初步使用

来源:互联网 发布:排版软件 编辑:程序博客网 时间:2024/06/05 06:46

下载地址:

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.测试类

 

Java代码  收藏代码
  1. package sms;  
  2.   
  3. import org.xvolks.jnative.JNative;  
  4. import org.xvolks.jnative.exceptions.NativeException;  
  5. import org.xvolks.jnative.pointers.Pointer;  
  6. import org.xvolks.jnative.pointers.memory.MemoryBlockFactory;  
  7.   
  8. public class SystemTime extends org.xvolks.jnative.util.Kernel32.SystemTime {  
  9.         public short wYear;  
  10.         public short wMonth;  
  11.         public short wDayOfWeek;  
  12.         public short wDay;  
  13.         public short wHour;  
  14.         public short wMinute;  
  15.         public short wSecond;  
  16.         public short wMilliseconds;  
  17.        /** 
  18.         * 分配内存,并返回指针 
  19.         */  
  20.        public Pointer createPointer() throws NativeException {  
  21.            pointer = new Pointer(MemoryBlockFactory.createMemoryBlock(getSizeOf()));  
  22.            return pointer;  
  23.        }  
  24.   
  25.        /** 
  26.         * 内存大小 
  27.         */  
  28.        public int getSizeOf(){  
  29.            return 8 * 2;  
  30.        }  
  31.   
  32.        /** 
  33.         * 获取通过内存指针解析出结果 
  34.         */  
  35.        public SystemTime getValueFromPointer() throws NativeException {  
  36.            wYear = getNextShort();  
  37.            wMonth = getNextShort();  
  38.            wDayOfWeek = getNextShort();  
  39.            wDay = getNextShort();  
  40.            wHour = getNextShort();  
  41.            wMinute = getNextShort();  
  42.            wSecond = getNextShort();  
  43.            wMilliseconds = getNextShort();  
  44.            return this;  
  45.        }  
  46.   
  47.        public SystemTime() throws NativeException{  
  48.            super();  
  49.            createPointer();  
  50.        }  
  51.   
  52.        public String toString(){  
  53.            return wYear + "/" + wMonth + "/" + wDay + " at + " + wHour + ":" + wMinute + ":" + wSecond + ":" + wMilliseconds;  
  54.        }  
  55.   
  56.        public static SystemTime GetSystemTime() throws NativeException, IllegalAccessException {  
  57.            // 创建对象  
  58.            JNative nGetSystemTime = new JNative("Kernel32.dll""GetSystemTime");  
  59.              
  60.            //GetSystemTime  是dll中的方法  
  61.            SystemTime systemTime = new SystemTime();  
  62.            // 设置参数  
  63.            nGetSystemTime.setParameter(0, systemTime.getPointer());  
  64.            //执行方法  
  65.            nGetSystemTime.invoke();  
  66.            // 解析结构指针内容  
  67.            return systemTime.getValueFromPointer();  
  68.        }  
  69.   
  70.        public static void main(String[] args) throws NativeException, IllegalAccessException{  
  71.            System.err.println(GetSystemTime());  
  72.                
  73.        }  
  74.         
  75. }  

 

0 0