📄 75.htm
字号:
<p>通过WnetEnumResource函数获得网络资源 </p>
<p> </p>
<p>Create a new project and add the following code to the form:</p>
<p>Option Explicit</p>
<p></p>
<p>Private Const GMEM_FIXED = &H0</p>
<p>Private Const GMEM_ZEROINIT = &H40</p>
<p>Private Const GPTR = (GMEM_FIXED Or GMEM_ZEROINIT)</p>
<p></p>
<p>Private Declare Function GlobalAlloc Lib "KERNEL32" ( _</p>
<p>ByVal wFlags As Long, ByVal dwBytes As Long) As Long</p>
<p>Private Declare Function GlobalFree Lib "KERNEL32" ( _</p>
<p>ByVal hMem As Long) As Long</p>
<p></p>
<p>Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" _</p>
<p>(hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)</p>
<p></p>
<p>Private Declare Function CopyPointer2String Lib "KERNEL32" _</p>
<p>Alias "lstrcpyA" ( _</p>
<p>ByVal NewString As String, ByVal OldString As Long) As Long</p>
<p></p>
<p>Private Sub Form_click()</p>
<p>Dim hEnum As Long, lpBuff As Long, nr As NETRESOURCE</p>
<p>Dim cbBuff As Long, cCount As Long</p>
<p>Dim p As Long, res As Long, i As Long</p>
<p></p>
<p>'Setup the NETRESOURCE input structure.</p>
<p>nr.dwUsage = RESOURCEUSAGE_CONTAINER</p>
<p>nr.lpRemoteName = 0</p>
<p>cbBuff = 1000</p>
<p>cCount = &HFFFFFFFF</p>
<p></p>
<p>'Open a Net enumeration operation handle: hEnum.</p>
<p>res = WNetOpenEnum(RESOURCE_CONNECTED, RESOURCETYPE_ANY, _</p>
<p>0, nr, hEnum)</p>
<p>If res = 0 Then</p>
<p>'Create a buffer large enough for the results.</p>
<p>'1000 bytes should be sufficient.</p>
<p>lpBuff = GlobalAlloc(GPTR, cbBuff)</p>
<p>'Call the enumeration function.</p>
<p>res = WNetEnumResource(hEnum, cCount, lpBuff, cbBuff)</p>
<p>If res = 0 Then</p>
<p>p = lpBuff</p>
<p>Cls</p>
<p>'WNetEnumResource fills the buffer with an array of</p>
<p>'NETRESOURCE structures. Walk through the list and print</p>
<p>'each local and remote name.</p>
<p>For i = 1 To cCount</p>
<p>CopyMemory nr, ByVal p, LenB(nr)</p>
<p>p = p + LenB(nr)</p>
<p>Print PointerToString(nr.lpLocalName), _</p>
<p>PointerToString(nr.lpRemoteName)</p>
<p>Next i</p>
<p>Else</p>
<p>MsgBox "Error: " & Err.LastDllError, vbOKOnly, _</p>
<p>"WNetEnumResources"</p>
<p>End If</p>
<p>If lpBuff <> 0 Then GlobalFree (lpBuff)</p>
<p>WNetCloseEnum (hEnum) 'Close the enumeration</p>
<p>Else</p>
<p>MsgBox "Error: " & Err.LastDllError, vbOKOnly, "WNetOpenEnum"</p>
<p>End If</p>
<p>End Sub</p>
<p></p>
<p>Private Function PointerToString(p As Long) As String</p>
<p>'The values returned in the NETRESOURCE structures are pointers to</p>
<p>'ANSI strings so they need to be converted to Visual Basic</p>
<p>Strings.</p>
<p>Dim s As String</p>
<p>s = String(255, Chr$(0))</p>
<p>CopyPointer2String s, p</p>
<p>PointerToString = Left(s, InStr(s, Chr$(0)) - 1)</p>
<p>End Function</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -