c#调用API显示内部局域网内的主机[2]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

    int entries;
    int buffer = 16384;
    IntPtr ptrBuffer = Marshal.AllocHGlobal(buffer);
    NETRESOURCE nr;
    
    for(;;)
    {
     entries = -1;
     buffer = 16384;
     iRet =WNetEnumResource(ptrHandle,ref entries,ptrBuffer,ref buffer);
     if((iRet != 0) || (entries<1))break;

     Int32 ptr = ptrBuffer.ToInt32();
     for(int i=0;i<entries;i++ )
     {
      nr = (NETRESOURCE)Marshal.PtrToStructure( new IntPtr(ptr), typeof(NETRESOURCE) );
      if(RESOURCE_USAGE.RESOURCEUSAGE_CONTAINER == (nr.dwUsage & RESOURCE_USAGE.RESOURCEUSAGE_CONTAINER))
      {
       
       ptr += Marshal.SizeOf( nr );
       if(nr.dwDisplayType == RESOURCE_DISPLAYTYPE.RESOURCEDISPLAYTYPE_NETWORK)
       {
        pNode1 = tView.Nodes.Add(nr.lpRemoteName);//TreeView
        pNode1.ImageIndex = 0;
        pNode1.SelectedImageIndex = 0;
       }
       else if(nr.dwDisplayType == RESOURCE_DISPLAYTYPE.RESOURCEDISPLAYTYPE_DOMAIN)
       {
        pNode2 = pNode1.Nodes.Add(nr.lpRemoteName);
        pNode2.ImageIndex = 1;
        pNode2.SelectedImageIndex =1;
       }
       else if(nr.dwDisplayType == RESOURCE_DISPLAYTYPE.RESOURCEDISPLAYTYPE_SERVER)
       {
        string sPCName = nr.lpRemoteName;
        pNode3 = pNode2.Nodes.Add(sPCName.Substring(2,sPCName.Length-2));\\此处为去处机器名称前的两个"\\"字符
        pNode3.ImageIndex = 2;
        pNode3.SelectedImageIndex = 2;
       }

       WNETOE(nr);
      }
      
     }
    }
    Marshal.FreeHGlobal( ptrBuffer );
    iRet =WNetCloseEnum( ptrHandle );
   }
   catch(Exception ex)
   {
    MessageBox.Show("查找本地网络错误:"+ex.Message,"错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
   }
  }

  #endregion

  //窗体LOAD事件中,使用函数
  private void frmDBConnConfig_Load(object sender, System.EventArgs e)
  {
   WNETOE(null);
  }

作者主页:itbaby.jss.cn

本文关键:c#调用API显示内部局域网内的主机
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top