zoukankan      html  css  js  c++  java
  • jna学习---windows下一个完整调用

    package sample.comomDll;
    
    
    import com.sun.jna.*;
    import com.sun.jna.ptr.ByteByReference;
    import com.sun.jna.win32.StdCallLibrary.StdCallCallback;
    //import com.sun.jna.
    import com.sun.jna.platform.win32.WinNT.HANDLE;
    import com.sun.jna.ptr.IntByReference;
    
    import java.io.File;
    import java.io.UnsupportedEncodingException;
    import java.util.Arrays;
    import java.util.List;
    
    public class nativeDll {
        public interface CLibrary extends Library {
    
            CLibrary INSTANCE = (CLibrary) Native.loadLibrary("CspProductionTool", CLibrary.class);
    
            int     CPTInit(String strIp);
            int     CPTNLInit(String strIp);
            HANDLE  CPTCommConnect(String strIp, Callback CallFun, IntByReference dwUser);
            int     CPTAuth(HANDLE handle,Callback CallFun,String sUserName,String sPassword);
            int     CMDPreSet(HANDLE handle, String strSendData, Callback pCallBack, int dwUser);
    
           int     CMDHardTest(HANDLE handle, Byte  TestCode,  byte[] pVoidStructArgument,  Callback  pCallBack, int dwUser);
    
            public interface JCallback extends StdCallCallback {
                public void MessageHandle(HANDLE handle, int iResult, int iExtend, String strMessage, int dwUser);
            }
    
            public static class FuncComm_search_dev implements JCallback {
                public void MessageHandle(HANDLE handle, int iResult, int iExtend, String strMessage, int dwUser) {
                    // TODO Auto-generated method stub
                    System.out.println("连接回调1!------>:");
                    
                    System.out.println("连接回调!------>:"+iResult+","+dwUser);
     
                   int r= CLibrary.INSTANCE.CPTAuth(handle,nativeDll.comm_auth_dev_one,onuUser,onuPwd);
                    System.out.println("返回 "+r);
                }
            }
            public static class Funccomm_auth_dev_one implements JCallback {
                public void MessageHandle(HANDLE handle, int iResult, int iExtend, String strMessage, int dwUser) {
                    // TODO Auto-generated method stub
                    System.out.println("登录回调!------>:"+iResult+","+dwUser);
                    System.out.println(strMessage);
    
                   // int r= CLibrary.INSTANCE.CMDPreSet(handle,"Enable=0",nativeDll.LedTestCallBack,0);
                    //led
                    ByteByReference cType = new ByteByReference();
                    //1灭 2亮
                    cType.setValue((byte) 1);
                    //int r= CLibrary.INSTANCE.CMDHardTest(handle,(byte)4, cType,nativeDll.LedTestCallBack,0);
    
                    //13
                    byte [] ba =new byte[6];
    
                    //ba[3]=20  则int为 0x00001400
                    // 20 是 水平, 21是垂直
                    /* //垂直测试
                    ba[0] = 1;
                    ba[2]=21;
                    */
                    ba[0] = 0;
                    ba[2] = 1;
    
                    int r= CLibrary.INSTANCE.CMDHardTest(handle,(byte)13, ba,nativeDll.LedTestCallBack,0);
    
    
                }
            }
            public static class FuncLedTestCallBack implements JCallback {
                public void MessageHandle(HANDLE handle, int iResult, int iExtend, String strMessage, int dwUser) {
                    // TODO Auto-generated method stub
                    System.out.println("亮!------>:"+iResult+","+dwUser);
                    System.out.println(strMessage);
                }
            }
    
        }
    
    
    
        public static   nativeDll.CLibrary.JCallback comm_search_dev =null;
        public static   nativeDll.CLibrary.JCallback comm_auth_dev_one =null;
        public static   nativeDll.CLibrary.JCallback LedTestCallBack =null;
    
        public static String bingIp = "192.168.5.100";//"192.168.5.100";
        public static String onuIp = "192.168.5.2";//"192.168.5.1";
        public static String onuUser = "admin";
        public static String onuPwd = "admin";
    
        /*
        public static String bingIp = "192.168.1.100";//"192.168.5.100";
        public static String onuIp = "192.168.1.1";//"192.168.5.1";
        public static String onuUser = "a";//"admin";
        public static String onuPwd = "a";//"admin";
    */
    
        class MyThread1 extends Thread {
            public void run() {
                for (int i = 0; i < 30; i++) {
                    System.out.println("线程1第" + i + "次执行!");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        public static void main(String[] args) {
             CLibrary.INSTANCE.CPTInit(bingIp);
            //CLibrary.INSTANCE.CPTNLInit(bingIp);
    
    
            System.setProperty("jna.encoding","GBK");
            HANDLE   handle;
           // int iRet =0 ;
            IntByReference iRet = new IntByReference();
            iRet.setValue(0);
            nativeDll.comm_search_dev = new  nativeDll.CLibrary.FuncComm_search_dev();
            nativeDll.comm_auth_dev_one = new  nativeDll.CLibrary.Funccomm_auth_dev_one();
            nativeDll.LedTestCallBack = new  nativeDll.CLibrary.FuncLedTestCallBack();
    
            //回头要换成
            handle = CLibrary.INSTANCE.CPTCommConnect(onuIp, comm_search_dev, iRet);
    
            System.out.println("Connect handle = "+ handle.toString());
            System.out.println("iRet = "+ iRet.getValue());
           // Pointer p =handle.getPointer();
    
            nativeDll a = new nativeDll();
            Thread t1 = a.new MyThread1();
            t1.start();
    
    
            /*
            if((handle == 10)
            {
                System.out.println("CPTCommConnect handle = 10!");
                return ;
            }
    
            if ((int)handle <= 1000)
            {
                printf("CPTCommConnect handle <=!");
                return FALSE;
            }
         */
    
    /*
            int time =0;
            while (time<500){
                try {
                    Thread.sleep(1000);
                    System.out.println(time);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                time++;
            }
            */
             //String str = System.getProperty("java.class.path");
             //System.out.println(str);
        }
    }
  • 相关阅读:
    (转)Unity3D 开发优秀技术资源汇总
    (转)Unity3d通过Action注册事件,回调方法
    (转)Unity3D研究院之游戏架构脚本该如何来写(三十九)
    (转)Unity3D研究院之异步加载游戏场景与异步加载游戏资源进度条(三十一)
    Unity3D的主要类图
    C# 事件和Unity3D
    unity3d 场景配置文件生成代码
    497. Random Point in Non-overlapping Rectangles
    478. Generate Random Point in a Circle
    470. Implement Rand10() Using Rand7() (拒绝采样Reject Sampling)
  • 原文地址:https://www.cnblogs.com/cnchengv/p/14413154.html
Copyright © 2011-2022 走看看