JNative使用,调用动态库

来源:互联网 发布:交换机网络克隆 h3c 编辑:程序博客网 时间:2024/04/30 05:41

由于项目需要,使用读卡机,C++没学好,一些动态库使用起来很困难,在网上搜了好久发现JNative是一个很好的选择,简单,方便,只需要简单的几步就能很好调用动态库,下面是我写一段测试代码:

  1. package JNative;
  2. import java.io.IOException;
  3. import java.util.StringTokenizer;
  4. import org.xvolks.jnative.JNative;
  5. import org.xvolks.jnative.Type;
  6. import org.xvolks.jnative.exceptions.NativeException;
  7. import org.xvolks.jnative.pointers.Pointer;
  8. import org.xvolks.jnative.pointers.memory.MemoryBlockFactory;
  9. public class T2 {
  10.     /**
  11.      * @param args
  12.      * @throws NativeException 
  13.      * @throws IllegalAccessException 
  14.      * @throws IOException 
  15.      */
  16.     public static void main(String[] args) throws NativeException, IllegalAccessException, IOException {
  17.         T2 t=new T2();
  18.         t.Init();
  19.         t.getName();
  20.         
  21.     
  22.         //======CVR_ReadBaseMsg==================
  23.         JNative n8 = new JNative("termb.dll""CVR_ReadBaseMsg");
  24.         Pointer pointer11 = new Pointer(MemoryBlockFactory.createMemoryBlock(1000));
  25.         Pointer pointer12 = new Pointer(MemoryBlockFactory.createMemoryBlock(256));
  26.         Pointer pointer13 = new Pointer(MemoryBlockFactory.createMemoryBlock(1024*1000));
  27.         Pointer pointer14 = new Pointer(MemoryBlockFactory.createMemoryBlock(1024));
  28.         int i=0;
  29.         n8.setParameter(i++, pointer11);
  30.         n8.setParameter(i++, pointer12);
  31.         n8.setParameter(i++, pointer13);
  32.         n8.setParameter(i++, pointer14);
  33.         n8.setParameter(i++, 4);
  34.         n8.setRetVal(Type.INT);
  35.         n8.invoke();
  36.         String msg=pointer11.getAsString();
  37.         StringTokenizer st=new StringTokenizer(msg);
  38.         /*while(st.hasMoreElements()){
  39.             System.out.println(st.nextElement());
  40.         }*/
  41.         t.Close();
  42.     }
  43.     
  44.     /**
  45.      * 分配内存,并返回指针
  46.      */
  47.     public static Pointer createPointer() throws NativeException {
  48.         Pointer pointer = new Pointer(MemoryBlockFactory.createMemoryBlock(getSizeOf()));
  49.         return pointer;
  50.     }
  51.     /**
  52.      * 内存大小
  53.      */
  54.     public static int getSizeOf(){
  55.         return 2 * 2;
  56.     }
  57.     
  58.     public  String getName() throws NativeException, IllegalAccessException{
  59.         //读姓名
  60.         JNative n4 = new JNative("termb.dll""GetPeopleName");
  61.         Pointer pointer3 = new Pointer(MemoryBlockFactory.createMemoryBlock(10));
  62.         Pointer pointer4 = new Pointer(MemoryBlockFactory.createMemoryBlock(2));
  63.         n4.setParameter(0, pointer3);
  64.         n4.setParameter(1,pointer4);
  65.         n4.setRetVal(Type.INT);
  66.         n4.invoke();
  67.         String name=pointer3.getAsString();
  68.         if(n4.getRetValAsInt()==1){
  69.             //System.out.println(n4.getRetVal());
  70.             System.out.println("读取完毕!");
  71.         }       
  72.         //System.out.println("返回值:"+n4.getRetValAsInt());
  73.         System.out.println("姓名是--长度:"+name.length()+"       姓名--"+name);
  74.         return name;
  75.     }
  76.     
  77.     public  String getSex() throws NativeException, IllegalAccessException{
  78.         //读性别
  79.         JNative n2 = new JNative("termb.dll""GetPeopleSex");
  80.         Pointer pointer1 = new Pointer(MemoryBlockFactory.createMemoryBlock(8));
  81.         Pointer pointer2 = new Pointer(MemoryBlockFactory.createMemoryBlock(2));
  82.         n2.setParameter(0, pointer1);
  83.         n2.setParameter(1,pointer2);
  84.         n2.setRetVal(Type.INT);
  85.         n2.invoke();
  86.         String sex=pointer1.getAsString();
  87.         if(n2.getRetValAsInt()==1){
  88.             System.out.println(n2.getRetVal());
  89.             System.out.println("读取完毕!");
  90.         }       
  91.         System.out.println("返回值:"+n2.getRetValAsInt());
  92.         System.out.println("性别是--长度:"+sex.length()+"        性别--"+sex);
  93.         return sex;
  94.     }
  95.     public  String getIDNo() throws NativeException, IllegalAccessException{
  96.         //读身份证号
  97.         JNative n5 = new JNative("termb.dll""GetPeopleIDCode");
  98.         Pointer pointer5 = new Pointer(MemoryBlockFactory.createMemoryBlock(18));
  99.         Pointer pointer6 = new Pointer(MemoryBlockFactory.createMemoryBlock(2));
  100.         n5.setParameter(0, pointer5);
  101.         n5.setParameter(1,pointer6);
  102.         n5.setRetVal(Type.INT);
  103.         n5.invoke();
  104.         String IDNo=pointer5.getAsString();
  105.         if(n5.getRetValAsInt()==1){
  106.             System.out.println(n5.getRetVal());
  107.             System.out.println("读取完毕!");
  108.         }       
  109.         System.out.println("返回值:"+n5.getRetValAsInt());
  110.         System.out.println("身份证号是--长度:"+IDNo.length()+"     身份证号--"+IDNo);
  111.         return IDNo;
  112.     }
  113.     
  114.     public  String getAddress() throws NativeException, IllegalAccessException{
  115.         //读地址
  116.         JNative n6 = new JNative("termb.dll""GetPeopleAddress");
  117.         Pointer pointer7 = new Pointer(MemoryBlockFactory.createMemoryBlock(100));
  118.         Pointer pointer8 = new Pointer(MemoryBlockFactory.createMemoryBlock(2));
  119.         n6.setParameter(0, pointer7);
  120.         n6.setParameter(1,pointer8);
  121.         n6.setRetVal(Type.INT);
  122.         n6.invoke();
  123.         String address=pointer7.getAsString();
  124.         if(n6.getRetValAsInt()==1){
  125.             System.out.println(n6.getRetVal());
  126.             System.out.println("读取完毕!");
  127.         }       
  128.         System.out.println("返回值:"+n6.getRetValAsInt());
  129.         System.out.println("地址是--长度:"+address.length()+"        地址--"+address);         
  130.         return address;
  131.     }
  132.     
  133.     public  String getDepartment() throws NativeException, IllegalAccessException{
  134.         //读发证机关
  135.         JNative n7 = new JNative("termb.dll""GetDepartment");
  136.         Pointer pointer9 = new Pointer(MemoryBlockFactory.createMemoryBlock(100));
  137.         Pointer pointer10 = new Pointer(MemoryBlockFactory.createMemoryBlock(2));
  138.         n7.setParameter(0, pointer9);
  139.         n7.setParameter(1,pointer10);
  140.         n7.setRetVal(Type.INT);
  141.         n7.invoke();
  142.         String department=pointer9.getAsString();
  143.         if(n7.getRetValAsInt()==1){
  144.             System.out.println(n7.getRetVal());
  145.             System.out.println("读取完毕!");
  146.         }       
  147.         System.out.println("返回值:"+n7.getRetValAsInt());
  148.         System.out.println("地址是--长度:"+department.length()+"     地址--"+department);  
  149.         return department;
  150.     }
  151.     
  152.     public  String getBirth() throws NativeException, IllegalAccessException{
  153.         //读生日
  154.         JNative n9 = new JNative("termb.dll""GetPeopleBirthday");
  155.         Pointer pointer15 = new Pointer(MemoryBlockFactory.createMemoryBlock(100));
  156.         Pointer pointer16 = new Pointer(MemoryBlockFactory.createMemoryBlock(2));
  157.         n9.setParameter(0, pointer15);
  158.         n9.setParameter(1,pointer16);
  159.         n9.setRetVal(Type.INT);
  160.         n9.invoke();
  161.         String birth=pointer15.getAsString();
  162.         if(n9.getRetValAsInt()==1){
  163.             System.out.println(n9.getRetVal());
  164.             System.out.println("读取完毕!");
  165.         }       
  166.         System.out.println("返回值:"+n9.getRetValAsInt());
  167.         System.out.println("生日是--长度:"+birth.length()+"      生日--"+birth);   
  168.         return birth;
  169.     }
  170.     
  171.     public void Init() throws NativeException, IllegalAccessException{
  172.         JNative n = new JNative("termb.dll""CVR_InitComm");
  173.         n.setRetVal(Type.INT);
  174.         n.setParameter(0,1001);             
  175.         n.invoke();
  176.         System.out.println("请放置身份证!");
  177.         int cnt=0;
  178.         for(int i=0;;i++){
  179.             //System.out.println("请正确放置身份证! ");
  180.             cnt++;
  181.             JNative n3= new JNative("termb.dll""CVR_Authenticate");
  182.             n3.setRetVal(Type.INT);
  183.             n3.invoke();
  184.             //System.out.println(n3.getRetVal());
  185.             int r=n3.getRetValAsInt();
  186.             if(r==1){
  187.                 break;
  188.             }
  189.         }
  190.         System.out.println("正确放置,身份证初始化完成,卡放置正确!");     
  191.         JNative n1 = new JNative("termb.dll""CVR_Read_Content");
  192.         n1.setParameter(0,6);
  193.         n1.setRetVal(Type.INT);
  194.         n1.invoke();
  195.         //System.out.println(n1.getRetVal());
  196.         if(n1.getRetValAsInt()==1){
  197.             System.out.println("读卡操作正确!");
  198.         }
  199.     }
  200.     
  201.     public void Close() throws NativeException, IllegalAccessException{
  202.         JNative n1 = new JNative("termb.dll""CVR_CloseComm");
  203.         n1.invoke();
  204.         
  205.         //int t=n1.getRetValAsInt();
  206.     }
  207. }
原创粉丝点击