//获取DNS列表的回调函数 private void DNSListNotify(Int32 _iCount, IntPtr _pDns) { for (int i = 0; i < _iCount; i++) { //创建结构体REG_DNS REG_DNS stDns = new REG_DNS(); //指针下移,读取下一数据 IntPtr pDns = (IntPtr)((UInt32)_pDns + i * Marshal.SizeOf(stDns)); stDns = (REG_DNS)Marshal.PtrToStructure(pDns, typeof(REG_DNS)); string strNvsName = Bytes2Str(stDns.m_stDNSInfo.m_stNvs.m_btNvsName); string strFactoryID = Bytes2Str(stDns.m_stDNSInfo.m_stNvs.m_btFactoryID); string strLANIP = Bytes2Str(stDns.m_stDNSInfo.m_stNvs.m_btNvsIP); string strWANIP = Bytes2Str(stDns.m_stDNSInfo.m_stReserve.m_btReserved1); int iPort = stDns.m_stDNSInfo.m_iPort; int iNvsType = stDns.m_stDNSInfo.m_stNvs.m_iNvsType; int iChannel = stDns.m_stDNSInfo.m_iChannel; string strAccount = ""; string strPwd = ""; //创建匿名委托,以处理跨线程修改DataGridView控件属性 MethodInvoker notify = delegate() { //为DataGridView创建新行 dgvDNS.Rows.Add ( new object[] { strNvsName, strFactoryID, strLANIP, strWANIP, iPort, iNvsType, iChannel, strAccount, strPwd, DateTime.Now } ); }; //将匿名委托交给DataGridView控件处理 dgvDNS.Invoke(notify); } }
//进行DNS查询操作 private void btnDNSQuery_Click(object sender, EventArgs e) { //创建REG_DNS结构体 REG_DNS stRegDNS = new REG_DNS(); //以ID方式进行查询 if (rdoDNSID.Checked) { if (textDNSID.Text.Trim() == "") { MessageBox.Show("DNS ID 不能为空 ! "); return; } //Byte数组进行赋值 Array.Copy(Encoding.ASCII.GetBytes(textDNSID.Text), stRegDNS.m_stDNSInfo.m_stNvs.m_btFactoryID, textDNSID.Text.Length); } else { //以域名方式进行查询 if (textDNSDomainName.Text.Trim() == "") { MessageBox.Show("DNS DomainName 不能为空 ! "); return; } //Byte数组进行赋值 Array.Copy(Encoding.ASCII.GetBytes(textDNSDomainName.Text), stRegDNS.m_stDNSInfo.m_stNvs.m_btNvsName, textDNSDomainName.Text.Length); } Array.Copy(Encoding.ASCII.GetBytes(UserName), stRegDNS.m_stDNSInfo.m_btUserName, UserName.Length); Array.Copy(Encoding.ASCII.GetBytes(Password), stRegDNS.m_stDNSInfo.m_btPwd, Password.Length); //清空DataGridView dgvDNS.Rows.Clear(); int iRet = -1; IntPtr pDns = IntPtr.Zero; try { //申请结构体REG_DNS所需的内存 pDns = Marshal.AllocHGlobal(Marshal.SizeOf(stRegDNS)); //将stRegDNS存入刚申请的内存 Marshal.StructureToPtr(stRegDNS, pDns, true); //查询DNS设备信息 iRet = NVSSDK.NSLook_Query(ServerID, pDns, IntPtr.Zero, NVSSDK.TYPE_DNS); //从内存中读出stRegDNS的值 stRegDNS = (REG_DNS)Marshal.PtrToStructure(pDns, typeof(REG_DNS)); } catch (Exception ex) { //将异常输出到Output窗口 Debug.WriteLine(ex.Message); } finally { //释放内存 Marshal.FreeHGlobal(pDns); } //操作失败 if (iRet != 0) { MessageBox.Show("NSLook_Query DNS Error ! " + iRet); return; } //为DataGridView添加新行 dgvDNS.Rows.Add ( new object[] { Bytes2Str(stRegDNS.m_stDNSInfo.m_stNvs.m_btNvsName), Bytes2Str(stRegDNS.m_stDNSInfo.m_stNvs.m_btFactoryID), Bytes2Str(stRegDNS.m_stDNSInfo.m_stNvs.m_btNvsIP), Bytes2Str(stRegDNS.m_stDNSInfo.m_stReserve.m_btReserved1), stRegDNS.m_stDNSInfo.m_iPort, stRegDNS.m_stDNSInfo.m_stNvs.m_iNvsType, stRegDNS.m_stDNSInfo.m_iChannel, "", "", DateTime.Now } ); }