列舉網路芳鄰的網路資源

  • 3678
  • 0
  • API
  • 2008-05-14

列舉網路芳鄰的網路資源

此範例會以遞迴方式列舉網路芳鄰的網路資源,在此範例中會使用到 mpr.dll API 函式庫的 WNetEnumResource、WNetOpenEnum、WNetCloseEnum 函式來進行網路芳鄰的網路資源的列舉動作。

TAPIMpr 類別為 mpr.dll API 函式庫的操作類別

   1:      ''' <summary>
   2:      ''' 網路芳鄰相關函式庫(mpr.dll)。
   3:      ''' </summary>
   4:      Public NotInheritable Class TAPIMpr
   5:          Public Enum EResourceScope As Integer
   6:              ''' <summary>
   7:              ''' 正連線的資源。
   8:              ''' </summary>
   9:              RESOURCE_CONNECTED = &H1
  10:              ''' <summary>
  11:              ''' 網路上所有的資源。
  12:              ''' </summary>
  13:              RESOURCE_GLOBALNET = &H2
  14:              ''' <summary>
  15:              ''' 曾經連線的資源。
  16:              ''' </summary>
  17:              RESOURCE_REMEMBERED = &H3
  18:          End Enum
  19:   
  20:          Public Enum EResourceType As Integer
  21:              ''' <summary>
  22:              ''' 所有網路資源。
  23:              ''' </summary>
  24:              RESOURCETYPE_ANY = &H0
  25:              ''' <summary>
  26:              ''' 網路磁碟機。
  27:              ''' </summary>
  28:              RESOURCETYPE_DISK = &H1
  29:              ''' <summary>
  30:              ''' 網路印表機。
  31:              ''' </summary>
  32:              RESOURCETYPE_PRINT = &H2
  33:              ''' <summary>
  34:              ''' 未知。
  35:              ''' </summary>
  36:              RESOURCETYPE_UNKNOWN = &HFFFFFFFF
  37:          End Enum
  38:   
  39:          Public Enum EResourceDisplayType As Integer
  40:              ''' <summary>
  41:              ''' 一般資源。
  42:              ''' </summary>
  43:              RESOURCEDISPLAYTYPE_GENERIC = 0
  44:              ''' <summary>
  45:              ''' 網域資源。
  46:              ''' </summary>
  47:              RESOURCEDISPLAYTYPE_DOMAIN = 1
  48:              ''' <summary>
  49:              ''' 伺服器資源。
  50:              ''' </summary>
  51:              RESOURCEDISPLAYTYPE_SERVER = 2
  52:              ''' <summary>
  53:              ''' 共享資源。
  54:              ''' </summary>
  55:              RESOURCEDISPLAYTYPE_SHARE = 3
  56:              ''' <summary>
  57:              ''' 檔案資源。
  58:              ''' </summary>
  59:              RESOURCEDISPLAYTYPE_FILE = 4
  60:              RESOURCEDISPLAYTYPE_GROUP = 5
  61:              RESOURCEDISPLAYTYPE_NETWORK = 6
  62:              RESOURCEDISPLAYTYPE_ROOT = 7
  63:              RESOURCEDISPLAYTYPE_SHAREADMIN = 8
  64:              RESOURCEDISPLAYTYPE_DIRECTORY = 9
  65:              RESOURCEDISPLAYTYPE_TREE = 10
  66:              RESOURCEDISPLAYTYPE_NDSCONTAINER = 11
  67:          End Enum
  68:   
  69:          Public Enum EResourceUsage As Integer
  70:              RESOURCEUSAGE_CONNECTABLE = &H1
  71:              RESOURCEUSAGE_CONTAINER = &H2
  72:              RESOURCEUSAGE_NOLOCALDEVICE = &H4
  73:              RESOURCEUSAGE_SIBLING = &H8
  74:              RESOURCEUSAGE_ATTACHED = &H10
  75:              RESOURCEUSAGE_ALL = (RESOURCEUSAGE_CONNECTABLE Or RESOURCEUSAGE_CONTAINER Or RESOURCEUSAGE_ATTACHED)
  76:              RESOURCEUSAGE_RESERVED = &H80000000
  77:          End Enum
  78:   
  79:          ''' <summary>
  80:          ''' 網路資源定義。
  81:          ''' </summary>
  82:          <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
  83:          Public Class TNetResource
  84:              Public dwScope As EResourceScope
  85:              Public dwType As EResourceType
  86:              Public dwDisplayType As EResourceDisplayType
  87:              Public dwUsage As EResourceUsage
  88:              <MarshalAs(UnmanagedType.LPTStr)> _
  89:              Public LocalName As String
  90:              <MarshalAs(UnmanagedType.LPTStr)> _
  91:              Public RemoteName As String
  92:              <MarshalAs(UnmanagedType.LPTStr)> _
  93:              Public Comment As String
  94:              <MarshalAs(UnmanagedType.LPTStr)> _
  95:              Public Provider As String
  96:          End Class
  97:   
  98:          ''' <summary>
  99:          ''' 列舉網路芳鄰的網路資源。
 100:          ''' </summary>
 101:          ''' <param name="Handle">從 WNetOpenEnum 函數返回的一個控制碼。</param>
 102:          ''' <param name="Count">最初設為要枚舉的最大資源數量;或設為-1,表示枚舉盡可能多的資源。一旦返回,就會設為實際枚舉的資源數量。</param>
 103:          ''' <param name="NetResource">通常是一個位元組緩衝區的首位元組。該緩衝區裝載了列舉資訊。</param>
 104:          ''' <param name="Size">緩衝區大小。</param>
 105:          <DllImport("mpr.dll", CharSet:=CharSet.Auto)> _
 106:  Public Shared Function WNetEnumResource(ByVal Handle As IntPtr, ByRef Count As Integer, ByVal NetResource As IntPtr, ByRef Size As Integer) As Integer
 107:          End Function
 108:   
 109:          ''' <summary>
 110:          ''' 啟動對網路資源進行列舉的過程。這個函數會返回由 WNetEnumResource 函數用於列舉資源所用的一個控制碼。
 111:          ''' </summary>
 112:          ''' <param name="Scope">要列舉的資源範圍。</param>
 113:          ''' <param name="Type">要列舉的資源類型。</param>
 114:          ''' <param name="Usage">要列舉的資源使用狀況。</param>
 115:          ''' <param name="NetResource">網路資源定義。</param>
 116:          ''' <param name="Handle">傳出一個控制碼。該控制碼由 WNetEnumResource 函數使用。必須用 WNetCloseEnum 函數將其清除。</param>
 117:          <DllImport("mpr.dll", CharSet:=CharSet.Auto)> _
 118:          Public Shared Function WNetOpenEnum(ByVal Scope As EResourceScope, ByVal Type As EResourceType, ByVal Usage As EResourceUsage, ByVal NetResource As TNetResource, <Out()> ByRef Handle As IntPtr) As Integer
 119:          End Function
 120:   
 121:          ''' <summary>
 122:          ''' 結束列舉操作。
 123:          ''' </summary>
 124:          ''' <param name="Handle">由 WNetOpenEnum 函數返回的一個列舉控制碼。</param>
 125:          <DllImport("mpr.dll", CharSet:=CharSet.Auto)> _
 126:          Public Shared Function WNetCloseEnum(ByVal Handle As IntPtr) As Integer
 127:          End Function
 128:   
 129:      End Class

 

 

下列程式碼中的 NetEnumResource 為列舉網路芳鄰的網路資源的函式,NetEnumResourceCallback 為執行 NetEnumResource 方法的回呼函式。

   1:          ''' <summary>
   2:          ''' 執行 NetEnumResource 方法的回呼函式。
   3:          ''' </summary>
   4:          ''' <param name="NetResource">網路資源定義。</param>
   5:          ''' <param name="Cancel">是否取消列舉動作。</param>
   6:          Public Delegate Sub NetEnumResourceCallback(ByVal NetResource As TAPIMpr.TNetResource, ByRef Cancel As Boolean)
   7:   
   8:          ''' <summary>
   9:          ''' 列舉網路芳鄰的網路資源。
  10:          ''' </summary>
  11:          ''' <param name="NetResource">網路資源定義。</param>
  12:          ''' <param name="CallBack">列舉過程中的回呼函式。</param>
  13:          Public Shared Sub NetEnumResource(ByVal NetResource As TAPIMpr.TNetResource, ByVal CallBack As NetEnumResourceCallback)
  14:              Dim bCancel As Boolean
  15:              Dim iRet As Integer
  16:              Dim oHandle As IntPtr = New IntPtr()
  17:              Dim iEntries As Integer
  18:              Dim iBuffer As Integer = 16384
  19:              Dim oBuffer As IntPtr
  20:              Dim oNetResource As TAPIMpr.TNetResource
  21:              Dim iPtr As Int32
  22:              Dim N1 As Integer
  23:              Dim oStructure As Object
  24:   
  25:              bCancel = False
  26:              Try
  27:                  iRet = TAPIMpr.WNetOpenEnum( _
  28:                      TAPIMpr.EResourceScope.RESOURCE_GLOBALNET, _
  29:                      TAPIMpr.EResourceType.RESOURCETYPE_ANY, _
  30:                      TAPIMpr.EResourceUsage.RESOURCEUSAGE_ALL, NetResource, oHandle)
  31:                  If iRet <> 0 Then Exit Sub
  32:   
  33:                  oBuffer = Marshal.AllocHGlobal(iBuffer)
  34:                  Do
  35:                      iEntries = -1
  36:                      iBuffer = 16384
  37:                      iRet = TAPIMpr.WNetEnumResource(oHandle, iEntries, oBuffer, iBuffer)
  38:                      If iRet <> 0 OrElse iEntries < 1 Then Exit Sub
  39:                      iPtr = oBuffer.ToInt32
  40:                      For N1 = 0 To iEntries - 1
  41:                          oStructure = Marshal.PtrToStructure(CType(iPtr, IntPtr), GetType(TAPIMpr.TNetResource))
  42:                          oNetResource = DirectCast(oStructure, TAPIMpr.TNetResource)
  43:   
  44:                          '列舉過程中的回呼函式
  45:                          CallBack.Invoke(oNetResource, bCancel)
  46:                          If bCancel = True Then Exit Do
  47:   
  48:                          '若為 RESOURCEUSAGE_CONTAINER 則利用遞迴往下層列舉網路資源
  49:                          If (TAPIMpr.EResourceUsage.RESOURCEUSAGE_CONTAINER = _
  50:                             (oNetResource.dwUsage And TAPIMpr.EResourceUsage.RESOURCEUSAGE_CONTAINER)) Then
  51:                              NetEnumResource(oNetResource, CallBack)
  52:                          End If
  53:                          iPtr = iPtr + Marshal.SizeOf(oNetResource)
  54:                      Next
  55:                  Loop
  56:   
  57:                  Marshal.FreeHGlobal(oBuffer)
  58:                  TAPIMpr.WNetCloseEnum(oHandle)
  59:              Catch ex As Exception
  60:              End Try
  61:          End Sub

 

在表單上放置一個 ListBox 控制項來顯示列舉的網路資源,呼叫 NetEnumResource 函式來進行列舉,呼叫 NetEnumResource 函式的引數需傳入事先定義好的 NetEnumResourceCallback 回呼函式;當列舉的過程式,當找到網路資源時就會執行 NetEnumResourceCallback 函式,利用傳入的 NetResource 引數就可以依序列舉網路資源。

   1:      ''' <summary>
   2:      ''' 執行 WNetEnumResource 方法的回呼函式。
   3:      ''' </summary>
   4:      ''' <param name="NetResource">網路資源定義。</param>
   5:      ''' <param name="Cancel">是否取消列舉動作。</param>
   6:      Public Sub NetEnumResourceCallback(ByVal NetResource As TAPIMpr.TNetResource, ByRef Cancel As Boolean)
   7:          Dim sText As String
   8:   
   9:          sText = String.Format(" {0} : LocalName='{1}' RemoteName='{2}'", NetResource.dwDisplayType.ToString, NetResource.LocalName, NetResource.RemoteName)
  10:          ListBox1.Items.Add(sText)
  11:          Application.DoEvents()
  12:      End Sub
  13:   
  14:      Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
  15:          Me.Cursor = Cursors.WaitCursor
  16:          Try
  17:              ListBox1.Items.Clear()
  18:              TBMpr.NetEnumResource(Nothing, New NetEnumResourceCallback(AddressOf NetEnumResourceCallback))
  19:              MsgBox("執行完成!")
  20:          Finally
  21:              Me.Cursor = Cursors.Default
  22:          End Try
  23:      End Sub

 

ASP.NET 魔法學院