基本信息
源码名称:QtDllTestDotNET
源码大小:2.43M
文件格式:.zip
开发语言:C#
更新时间:2015-09-24
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace QtDllTestDotNET
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        void MSG(string msg)
        {
            lboxMSG.Items.Add(msg   " 时间:"   DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
        }        

        [DllImport("HT110BServer.dll")]
        static extern bool InitServer(int port);

        [DllImport("HT110BServer.dll")]
        static extern void CloseServer();

        [DllImport("HT110BServer.dll")]
        static extern void BuFang(string hostID, byte[] data);

        [DllImport("HT110BServer.dll")]
        static extern void CheFang(string hostID, byte[] data); 

        [UnmanagedFunctionPointer(CallingConvention.Winapi)]
        delegate void CallBackHostConnect(string hostID);

        [DllImport("HT110BServer.dll")]
        static extern void HostConnect([MarshalAs(UnmanagedType.FunctionPtr)] CallBackHostConnect CallBack);
        CallBackHostConnect cOnEventHostConnect = null;
        void OnEventHostConnect(string hostID)
        {
            MSG("主机上线:"   hostID);
            cboxHostID.Items.Add(hostID);
            cboxHostID.SelectedIndex = 0;
        }

        [UnmanagedFunctionPointer(CallingConvention.Winapi)]
        delegate void CallBackHostDisConnect(string hostID);

        [DllImport("HT110BServer.dll")]
        static extern void HostDisConnect([MarshalAs(UnmanagedType.FunctionPtr)] CallBackHostDisConnect CallBack);
        CallBackHostDisConnect cOnEventHostDisConnect = null;
        void OnEventHostDisConnect(string hostID)
        {
            MSG("主机下线:"   hostID);
            cboxHostID.Items.Remove(hostID);
        }

        [UnmanagedFunctionPointer(CallingConvention.Winapi)]
        delegate void CallBackHostBuFang(string hostID);

        [DllImport("HT110BServer.dll")]
        static extern void HostBuFang([MarshalAs(UnmanagedType.FunctionPtr)] CallBackHostBuFang CallBack);
        CallBackHostBuFang cOnEventHostBuFang = null;
        void OnEventHostBuFang(string hostID)
        {
            MSG("主机布防:"   hostID);
        }

        [UnmanagedFunctionPointer(CallingConvention.Winapi)]
        delegate void CallBackHostCheFang(string hostID);

        [DllImport("HT110BServer.dll")]
        static extern void HostCheFang([MarshalAs(UnmanagedType.FunctionPtr)] CallBackHostCheFang CallBack);
        CallBackHostCheFang cOnEventHostCheFang = null;
        void OnEventHostCheFang(string hostID)
        {
            MSG("主机撤防:"   hostID);
        }

        [UnmanagedFunctionPointer(CallingConvention.Winapi)]
        delegate void CallBackHostAlarm(string hostID, string defenceID);

        [DllImport("HT110BServer.dll")]
        static extern void HostAlarm([MarshalAs(UnmanagedType.FunctionPtr)] CallBackHostAlarm CallBack);
        CallBackHostAlarm cOnEventHostAlarm = null;
        void OnEventHostAlarm(string hostID,string defenceID)
        {
            MSG("主机报警:"   hostID   " 防区号:"   defenceID);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cOnEventHostConnect = OnEventHostConnect;
            HostConnect(cOnEventHostConnect);

            cOnEventHostDisConnect = OnEventHostDisConnect;
            HostDisConnect(cOnEventHostDisConnect);

            cOnEventHostBuFang = OnEventHostBuFang;
            HostBuFang(cOnEventHostBuFang);

            cOnEventHostCheFang = OnEventHostCheFang;
            HostCheFang(cOnEventHostCheFang);

            cOnEventHostAlarm = OnEventHostAlarm;
            HostAlarm(cOnEventHostAlarm);
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            CloseServer();
        }

        private void btnListen_Click(object sender, EventArgs e)
        {           
            MSG("服务初始化:"   InitServer(Convert.ToInt32(txtPort.Text)));
        }

        byte[] HexStringToByteArray(string s)
        {
            s = s.Replace(" ", "");
            byte[] buffer = new byte[s.Length / 2];
            for (int i = 0; i < s.Length; i  = 2)
                buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
            return buffer;
        }

        private void btnBuFang_Click(object sender, EventArgs e)
        {
            byte[] data = HexStringToByteArray(txtBuFangData.Text);
            BuFang(cboxHostID.Text, data);
        }

        private void btnCheFang_Click(object sender, EventArgs e)
        {
            byte[] data = HexStringToByteArray(txtCheFangData.Text);
            CheFang(cboxHostID.Text, data);
        }

        

    }
}