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);
    }
}

 

posted @ 2021-02-18 19:09  cnchengv  阅读(301)  评论(0编辑  收藏  举报