Results 1 to 7 of 7

Thread: List of computers on the network

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    60


    I' sorry but there are not short cuts with this one, the code below is what you have to use, there is no shortening it.

    It basically enumerates all network resources that are connectable and then from there it filters out everything except for machines, then they are placed in a listbox on frmMain

    --Place this code in a module---

    Public Const RESOURCE_CONNECTED As Long = &H1&
    Public Const RESOURCE_GLOBALNET As Long = &H2&
    Public Const RESOURCE_REMEMBERED As Long = &H3&

    Public Const RESOURCEDISPLAYTYPE_SERVER& = &H2

    Public Const RESOURCETYPE_ANY As Long = &H0&
    Public Const RESOURCETYPE_DISK As Long = &H1&
    Public Const RESOURCETYPE_PRINT As Long = &H2&
    Public Const RESOURCETYPE_UNKNOWN As Long = &HFFFF&

    Public Const RESOURCEUSAGE_ALL As Long = &H0&
    Public Const RESOURCEUSAGE_CONNECTABLE As Long = &H1&
    Public Const RESOURCEUSAGE_CONTAINER As Long = &H2&
    Public Const RESOURCEUSAGE_RESERVED As Long = &H80000000

    Public Const NO_ERROR = 0
    Public Const ERROR_MORE_DATA = 234
    Public Const RESOURCE_ENUM_ALL As Long = &HFFFF

    Public Type NETRESOURCE
    dwScope As Long
    dwType As Long
    dwDisplayType As Long
    dwUsage As Long
    pLocalName As Long
    pRemoteName As Long
    pComment As Long
    pProvider As Long
    End Type

    Public Type NETRESOURCE_REAL
    dwScope As Long
    dwType As Long
    dwDisplayType As Long
    dwUsage As Long
    sLocalName As String
    sRemoteName As String
    sComment As String
    sProvider As String
    End Type

    Public Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Long) As Long
    Public Declare Function WNetOpenEnum Lib "mpr.dll" Alias "WNetOpenEnumA" (ByVal dwScope As Long, ByVal dwType As Long, ByVal dwUsage As Long, lpNetResource As Any, lphEnum As Long) As Long
    Public Declare Function WNetEnumResource Lib "mpr.dll" Alias "WNetEnumResourceA" (ByVal hEnum As Long, lpcCount As Long, lpBuffer As NETRESOURCE, lpBufferSize As Long) As Long
    Public Declare Function WNetCloseEnum Lib "mpr.dll" (ByVal hEnum As Long) As Long
    Public Declare Function VarPtrAny Lib "vb40032.dll" Alias "VarPtr" (lpObject As Any) As Long
    Public Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (lpTo As Any, lpFrom As Any, ByVal lLen As Long)
    Public Declare Sub CopyMemByPtr Lib "kernel32" Alias "RtlMoveMemory" (ByVal lpTo As Long, ByVal lpFrom As Long, ByVal lLen As Long)
    Public Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As Any) As Long
    Public Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Any) As Long










    --Place this code where ever you want it--
    Const MAX_RESOURCES = 256
    Const NOT_A_CONTAINER = -1

    Dim bFirstTime As Boolean
    Dim lReturn As Long
    Dim hEnum As Long
    Dim lCount As Long
    Dim lMin As Long
    Dim lLength As Long
    Dim l As Long
    Dim lBufferSize As Long
    Dim lLastIndex As Long
    Dim uNetApi(0 To MAX_RESOURCES) As NETRESOURCE
    Dim uNet() As NETRESOURCE_REAL

    bFirstTime = True

    Do
    If bFirstTime Then
    lReturn = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, RESOURCEUSAGE_ALL, ByVal 0&, hEnum)
    bFirstTime = False
    Else
    If uNet(lLastIndex).dwUsage And RESOURCEUSAGE_CONTAINER Then
    lReturn = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, RESOURCEUSAGE_ALL, uNet(lLastIndex), hEnum)
    Else
    lReturn = NOT_A_CONTAINER
    hEnum = 0
    End If
    lLastIndex = lLastIndex + 1
    End If
    If lReturn = NO_ERROR Then
    lCount = RESOURCE_ENUM_ALL
    Do
    lBufferSize = UBound(uNetApi) * Len(uNetApi(0)) / 2
    lReturn = WNetEnumResource(hEnum, lCount, uNetApi(0), lBufferSize)
    If lCount > 0 Then
    ReDim Preserve uNet(0 To lMin + lCount - 1) As NETRESOURCE_REAL
    For l = 0 To lCount - 1
    'Each Resource will appear here as uNet(i)
    uNet(lMin + l).dwScope = uNetApi(l).dwScope
    uNet(lMin + l).dwType = uNetApi(l).dwType
    uNet(lMin + l).dwDisplayType = uNetApi(l).dwDisplayType
    uNet(lMin + l).dwUsage = uNetApi(l).dwUsage
    If uNetApi(l).pLocalName Then
    lLength = lstrlen(uNetApi(l).pLocalName)
    uNet(lMin + l).sLocalName = Space$(lLength)
    CopyMem ByVal uNet(lMin + l).sLocalName, ByVal uNetApi(l).pLocalName, lLength
    End If
    If uNetApi(l).pRemoteName Then
    lLength = lstrlen(uNetApi(l).pRemoteName)
    uNet(lMin + l).sRemoteName = Space$(lLength)
    CopyMem ByVal uNet(lMin + l).sRemoteName, ByVal uNetApi(l).pRemoteName, lLength
    End If
    If uNetApi(l).pComment Then
    lLength = lstrlen(uNetApi(l).pComment)
    uNet(lMin + l).sComment = Space$(lLength)
    CopyMem ByVal uNet(lMin + l).sComment, ByVal uNetApi(l).pComment, lLength
    End If
    If uNetApi(l).pProvider Then
    lLength = lstrlen(uNetApi(l).pProvider)
    uNet(lMin + l).sProvider = Space$(lLength)
    CopyMem ByVal uNet(lMin + l).sProvider, ByVal uNetApi(l).pProvider, lLength
    End If
    Next l
    End If
    lMin = lMin + lCount
    Loop While lReturn = ERROR_MORE_DATA
    End If
    If hEnum Then
    l = WNetCloseEnum(hEnum)
    End If
    Loop While lLastIndex < lMin

    If UBound(uNet) > 0 Then
    For l = 0 To UBound(uNet)
    Select Case uNet(l).dwDisplayType
    Case Not RESOURCEDISPLAYTYPE_SERVER: Goto skip
    Case RESOURCEDISPLAYTYPE_SERVER

    End Select

    'This is the bit that rights it to a list box
    frmMain.List1.Clear
    frmMain.List1.AddItem uNet(l).sRemoteName
    skip:
    Next l
    End If


    I hope this helps you out,
    Grant French
    -----------------------------------------------
    E-Mail: [email protected]
    ICQ: 33122184

  2. #2
    Addicted Member
    Join Date
    Jul 2000
    Location
    Scotland
    Posts
    184
    I have the same problem. gfrench posted Nice Code, but it does not work. Comments would have been good. Thanks for trying anyway. Guess I'll keep hacking away until I get it.

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    60
    The code does work, i have used it in one of my distributed applications (Remote Shutdown), if you want a demo project mail me and i will send it to you.
    Grant French
    -----------------------------------------------
    E-Mail: [email protected]
    ICQ: 33122184

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    60

    This works, if you have a problem it is not the code

    Instructions
    ------------

    Start A new project, with one form and one module
    On the form place a Tree View control called treeview1

    In FORM_LOAD place the code below:

    refreshlist



    In the module place the code below:

    Private Const RESOURCE_CONNECTED As Long = &H1&
    Private Const RESOURCE_GLOBALNET As Long = &H2&
    Private Const RESOURCE_REMEMBERED As Long = &H3&

    Private Const RESOURCEDISPLAYTYPE_DIRECTORY& = &H9
    Private Const RESOURCEDISPLAYTYPE_DOMAIN& = &H1
    Private Const RESOURCEDISPLAYTYPE_FILE& = &H4
    Private Const RESOURCEDISPLAYTYPE_GENERIC& = &H0
    Private Const RESOURCEDISPLAYTYPE_GROUP& = &H5
    Private Const RESOURCEDISPLAYTYPE_NETWORK& = &H6
    Private Const RESOURCEDISPLAYTYPE_ROOT& = &H7
    Private Const RESOURCEDISPLAYTYPE_SERVER& = &H2
    Private Const RESOURCEDISPLAYTYPE_SHARE& = &H3
    Private Const RESOURCEDISPLAYTYPE_SHAREADMIN& = &H8

    Private Const RESOURCETYPE_ANY As Long = &H0&
    Private Const RESOURCETYPE_DISK As Long = &H1&
    Private Const RESOURCETYPE_PRINT As Long = &H2&
    Private Const RESOURCETYPE_UNKNOWN As Long = &HFFFF&

    Public Const RESOURCEUSAGE_ALL As Long = &H0&
    Public Const RESOURCEUSAGE_CONNECTABLE As Long = &H1&
    Public Const RESOURCEUSAGE_CONTAINER As Long = &H2&
    Public Const RESOURCEUSAGE_RESERVED As Long = &H80000000

    Public Const NO_ERROR = 0
    Public Const ERROR_MORE_DATA = 234
    Public Const RESOURCE_ENUM_ALL As Long = &HFFFF

    Public Type NETRESOURCE
    dwScope As Long
    dwType As Long
    dwDisplayType As Long
    dwUsage As Long
    pLocalName As Long
    pRemoteName As Long
    pComment As Long
    pProvider As Long
    End Type

    Public Type NETRESOURCE_REAL
    dwScope As Long
    dwType As Long
    dwDisplayType As Long
    dwUsage As Long
    sLocalName As String
    sRemoteName As String
    sComment As String
    sProvider As String
    End Type

    Public Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Long) As Long
    Public Declare Function WNetOpenEnum Lib "mpr.dll" Alias "WNetOpenEnumA" (ByVal dwScope As Long, ByVal dwType As Long, ByVal dwUsage As Long, lpNetResource As Any, lphEnum As Long) As Long
    Public Declare Function WNetEnumResource Lib "mpr.dll" Alias "WNetEnumResourceA" (ByVal hEnum As Long, lpcCount As Long, lpBuffer As NETRESOURCE, lpBufferSize As Long) As Long
    Public Declare Function WNetCloseEnum Lib "mpr.dll" (ByVal hEnum As Long) As Long

    Public Declare Function VarPtrAny Lib "vb40032.dll" Alias "VarPtr" (lpObject As Any) As Long

    Public Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (lpTo As Any, lpFrom As Any, ByVal lLen As Long)
    Public Declare Sub CopyMemByPtr Lib "kernel32" Alias "RtlMoveMemory" (ByVal lpTo As Long, ByVal lpFrom As Long, ByVal lLen As Long)

    Public Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As Any) As Long
    Public Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Any) As Long



    Public Sub RefreshList()


    Const MAX_RESOURCES = 256
    Const NOT_A_CONTAINER = -1


    Dim bFirstTime As Boolean
    Dim lReturn As Long
    Dim hEnum As Long
    Dim lCount As Long
    Dim lMin As Long
    Dim lLength As Long
    Dim l As Long
    Dim lBufferSize As Long
    Dim lLastIndex As Long
    Dim uNetApi(0 To MAX_RESOURCES) As NETRESOURCE
    Dim uNet() As NETRESOURCE_REAL


    frmMain.TreeView1.Nodes.Clear 'Clear the treeview1 control on frmMain

    bFirstTime = True 'Make code below aware that that it is the first loop




    Do
    DoEvents
    If bFirstTime Then 'If this is the first time the loop has executed then

    'Start the enumeration of all network resources
    lReturn = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, RESOURCEUSAGE_ALL, ByVal 0&, hEnum) 'Get a handle to a buffer

    'The next time it reaches the if bfirsttime then line it will not be the first time
    bFirstTime = False

    Else

    If uNet(lLastIndex).dwUsage And RESOURCEUSAGE_CONTAINER Then

    lReturn = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, RESOURCEUSAGE_ALL, uNet(lLastIndex), hEnum) 'Initiate the enumeration process

    Else

    lReturn = NOT_A_CONTAINER 'Set the returnvalue to NOT_A_CONTAINER for later reference
    hEnum = 0

    End If

    lLastIndex = lLastIndex + 1 'Increase the network resource number by 1 (array number)

    End If


    If lReturn = NO_ERROR Then 'If there was no error then procress the data retrieved

    lCount = RESOURCE_ENUM_ALL 'Enumerate all resoureces

    Do
    DoEvents

    lBufferSize = UBound(uNetApi) * Len(uNetApi(0)) / 2 'Set the buffer size
    lReturn = WNetEnumResource(hEnum, lCount, uNetApi(0), lBufferSize) 'Get resource information

    If lCount > 0 Then

    'The next bit copys the information from memory to uNetApi array
    ReDim Preserve uNet(0 To lMin + lCount - 1) As NETRESOURCE_REAL

    For l = 0 To lCount - 1

    uNet(lMin + l).dwScope = uNetApi(l).dwScope
    uNet(lMin + l).dwType = uNetApi(l).dwType
    uNet(lMin + l).dwDisplayType = uNetApi(l).dwDisplayType
    uNet(lMin + l).dwUsage = uNetApi(l).dwUsage


    If uNetApi(l).pLocalName Then

    lLength = lstrlen(uNetApi(l).pLocalName)
    uNet(lMin + l).sLocalName = Space$(lLength)
    CopyMem ByVal uNet(lMin + l).sLocalName, ByVal uNetApi(l).pLocalName, lLength

    End If


    If uNetApi(l).pRemoteName Then

    lLength = lstrlen(uNetApi(l).pRemoteName)
    uNet(lMin + l).sRemoteName = Space$(lLength)
    CopyMem ByVal uNet(lMin + l).sRemoteName, ByVal uNetApi(l).pRemoteName, lLength

    End If


    If uNetApi(l).pComment Then

    lLength = lstrlen(uNetApi(l).pComment)
    uNet(lMin + l).sComment = Space$(lLength)
    CopyMem ByVal uNet(lMin + l).sComment, ByVal uNetApi(l).pComment, lLength

    End If


    If uNetApi(l).pProvider Then

    lLength = lstrlen(uNetApi(l).pProvider)
    uNet(lMin + l).sProvider = Space$(lLength)
    CopyMem ByVal uNet(lMin + l).sProvider, ByVal uNetApi(l).pProvider, lLength

    End If

    Next l

    End If

    lMin = lMin + lCount

    Loop While lReturn = ERROR_MORE_DATA

    End If

    If hEnum Then l = WNetCloseEnum(hEnum)

    Loop While lLastIndex < lMin


    If UBound(uNet) > 0 Then

    For l = 0 To UBound(uNet)

    DoEvents

    'Select what type of resource it is (there are more but these are the only ones that we want
    Select Case uNet(l).dwDisplayType


    Case RESOURCEDISPLAYTYPE_DOMAIN
    frmMain.TreeView1.Nodes.Add , 2, uNet(l).sRemoteName, uNet(l).sRemoteName, "Domain", "Domain"
    CurrentDomain = uNet(l).sRemoteName


    Case RESOURCEDISPLAYTYPE_SERVER
    If CurrentDomain <> "" Then
    frmMain.TreeView1.Nodes.Add CurrentDomain, 4, uNet(l).sRemoteName, Mid$(uNet(l).sRemoteName, 3, Len(uNet(l).sRemoteName) - 2), "Computer", "Computer"
    Else
    frmMain.TreeView1.Nodes.Add , 2, uNet(l).sRemoteName, Mid$(uNet(l).sRemoteName, 3, Len(uNet(l).sRemoteName) - 2), "Computer", "Computer"
    End If


    End Select

    Next l

    End If

    End Sub




    This code works, and curses to any one who says it doesnt with cause.

    This will Help you,
    Grant French
    -----------------------------------------------
    E-Mail: [email protected]
    ICQ: 33122184

  5. #5
    Fanatic Member
    Join Date
    Jul 2000
    Location
    Manchester NH
    Posts
    833
    Tested on on windows NT...


    if you want it simplier (but not as clean)

    batch the command

    net view


    that will show you the computers then

    net view [computername]

    will show you its resources
    Kurt Simons
    [I know I'm a hack but my clients don't!]

  6. #6
    New Member
    Join Date
    Aug 1999
    Location
    Veenendaal,Netherlands
    Posts
    1

    Thumbs up

    Works fine for me (W2000) ! (and is exactly what i'm looking for). One extra: is it possible to show hidden shares (like c$)?

  7. #7
    Fanatic Member
    Join Date
    Jul 2000
    Location
    Manchester NH
    Posts
    833
    No, I don't think so.
    Kurt Simons
    [I know I'm a hack but my clients don't!]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width