ネットワークコンピュータの取得




Public Function GetNetworkResource( _
    Optional sRet As String = "", _
    Optional nLevel = 0, _
    Optional nScope As Long = -1, _
    Optional nType As Long = 0, _
    Optional nDisplayType As Long = 0, _
    Optional nUsage As Long = 0, _
    Optional sLocalName As String = "", _
    Optional sRemoteName As String = "", _
    Optional sComment As String = "", _
    Optional sProvider As String = "" _
) As String
    Dim nRet As Long
    Dim hEnum As Long
    Dim nBuf() As Long
    Dim nBufSize As Long
    Dim I As Long
    Dim nCount As Long
    Dim nTypeSize As Long
    Dim nBufNext() As Long
    Dim ns As NETRESOURCE
    
    nTypeSize = Len(ns) / 4
    hEnum = 0
    
    If nScope = -1 Then
        nRet = WNetOpenEnum(RESOURCE_CONTEXT, RESOURCETYPE_ANY, 0, 0, hEnum)
    Else
        With ns
            .dwScope = nScope
            .dwType = nType
            .dwDisplayType = nDisplayType
            .dwUsage = nUsage
            .lpLocalName = sLocalName
            .lpRemoteName = sRemoteName
            .lpComment = sComment
            .lpProvider = sProvider
        End With
        
        nRet = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, ns, hEnum)
    End If
    
    If nRet <> NO_ERROR Then
        Exit Function
    End If
    
    nCount = -1
    nBufSize = 1024
    ReDim nBuf(nBufSize)
    nRet = WNetEnumResource(hEnum, nCount, nBuf(0), nBufSize)
    If nRet <> NO_ERROR Then
        Exit Function
    End If
    
    For I = 0 To nCount - 1
        Debug.Print String(80, "*")
        Debug.Print "Scope       : " & GetScopeName(nBuf(I * nTypeSize + 0))
        Debug.Print "Type        : " & GetTypeName(nBuf(I * nTypeSize + 1))
        Debug.Print "DisplayType : " & GetDisplayTypeName(nBuf(I * nTypeSize + 2))
        Debug.Print "Usage       : " & GetUsageName(nBuf(I * nTypeSize + 3))
        Debug.Print "LocalName   : " & GetString(nBuf(I * nTypeSize + 4))
        Debug.Print "RemoteName  : " & GetString(nBuf(I * nTypeSize + 5))
        Debug.Print "Comment     : " & GetString(nBuf(I * nTypeSize + 6))
        Debug.Print "Provider    : " & GetString(nBuf(I * nTypeSize + 7))
        
        If GetString(nBuf(I * nTypeSize + 5)) = "" Then
            sRet = sRet & String(nLevel, vbTab) & GetString(nBuf(I * nTypeSize + 6)) & vbCrLf
        Else
            sRet = sRet & String(nLevel, vbTab) & GetString(nBuf(I * nTypeSize + 5)) & vbCrLf
        End If
        
        Call GetNetworkResource(sRet, _
                                nLevel + 1, _
                                nBuf(I * nTypeSize + 0), _
                                nBuf(I * nTypeSize + 1), _
                                nBuf(I * nTypeSize + 2), _
                                nBuf(I * nTypeSize + 3), _
                                GetString(nBuf(I * nTypeSize + 4)), _
                                GetString(nBuf(I * nTypeSize + 5)), _
                                GetString(nBuf(I * nTypeSize + 6)), _
                                GetString(nBuf(I * nTypeSize + 7)))
    Next
    
    Call WNetCloseEnum(hEnum)
    
    GetNetworkResource = sRet
End Function



サンプル GetNetworkResource.zip