CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 1999
    Posts
    68

    Getting computer names in the network

    Hi,
    How do I get all the computers connected to the LAN?

    Thanks in advance



  2. #2
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Getting computer names in the network

    A little something from AllAPI.Net. Its output is to the debug window.


    option Explicit

    'Add this code to a module and set the Project's Startup Object to 'Sub Main'
    ' (-> Project Menu -> Project Properties -> General Tab)
    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&
    private Const RESOURCEUSAGE_ALL as Long = &H0&
    private Const RESOURCEUSAGE_CONNECTABLE as Long = &H1&
    private Const RESOURCEUSAGE_CONTAINER as Long = &H2&
    private Const RESOURCEUSAGE_RESERVED as Long = &H80000000
    private Const NO_ERROR = 0
    private Const ERROR_MORE_DATA = 234 'L // dderror
    private Const RESOURCE_ENUM_ALL as Long = &HFFFF
    private 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
    private 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
    private 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
    private 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
    private Declare Function WNetEnumResource Lib "mpr.dll" Alias "WNetEnumResourceA" (byval hEnum as Long, lpcCount as Long, lpBuffer as NETRESOURCE, lpBufferSize as Long) as Long
    private Declare Function WNetCloseEnum Lib "mpr.dll" (byval hEnum as Long) as Long
    private Declare Function VarPtrAny Lib "vb40032.dll" Alias "VarPtr" (lpObject as Any) as Long
    private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (lpTo as Any, lpFrom as Any, byval lLen as Long)
    private Declare Sub CopyMemByPtr Lib "kernel32" Alias "RtlMoveMemory" (byval lpTo as Long, byval lpFrom as Long, byval lLen as Long)
    private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (byval lpString1 as string, byval lpString2 as Any) as Long
    private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (byval lpString as Any) as Long
    private Declare Function getusername Lib "advapi32.dll" Alias "GetUserNameA" (byval lpBuffer as string, nSize as Long) as Long
    private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (byval lpBuffer as string, nSize as Long) as Long
    public strUserName as string
    public strMachinerName as string
    Sub main()
    'KPD-Team 2000
    'URL: http://www.allapi.net/
    'E-Mail: [email protected]
    '-> This sample was created by Donald Grover
    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
    username
    Dim filNum as Integer
    filNum = FreeFile
    for l = 0 to UBound(uNet)
    Select Case uNet(l).dwDisplayType
    Case RESOURCEDISPLAYTYPE_DIRECTORY&
    Debug.print "Directory...",
    Case RESOURCEDISPLAYTYPE_DOMAIN
    Debug.print "Domain...",
    Case RESOURCEDISPLAYTYPE_FILE
    Debug.print "File...",
    Case RESOURCEDISPLAYTYPE_GENERIC
    Debug.print "Generic...",
    Case RESOURCEDISPLAYTYPE_GROUP
    Debug.print "Group...",
    Case RESOURCEDISPLAYTYPE_NETWORK&
    Debug.print "Network...",
    Case RESOURCEDISPLAYTYPE_ROOT&
    Debug.print "Root...",
    Case RESOURCEDISPLAYTYPE_SERVER
    Debug.print "Server...",
    Case RESOURCEDISPLAYTYPE_SHARE
    Debug.print "Share...",
    Case RESOURCEDISPLAYTYPE_SHAREADMIN&
    Debug.print "ShareAdmin...",
    End Select
    Debug.print uNet(l).sRemoteName, uNet(l).sComment
    next l
    End If
    End Sub
    private Sub username()
    on error resume next
    'Create a buffer
    strUserName = string(255, Chr$(0))
    'get the username
    getusername strUserName, 255
    'strip the rest of the buffer
    strUserName = Left$(strUserName, InStr(strUserName, Chr$(0)) - 1)
    'Create a buffer
    strMachinerName = string(255, Chr$(0))
    GetComputerName strMachinerName, 255
    'remove the unnecessary chr$(0)'s
    strMachinerName = Left$(strMachinerName, InStr(1, strMachinerName, Chr$(0)) - 1)
    End Sub




    John G

  3. #3
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Getting computer names in the network

    Public Function GetComputerName() As String
    '
    ' This routine will obtain the Computers name from the system. The
    ' second time it is called it returns the static 'sName' variable
    '
    Static sName As String
    Dim lBuffLen As Long
    Dim sBuffer As String
    Dim lRet As Long

    If Len(sName) > 0 Then
    GetComputerName = sName
    Exit Function
    Else
    lBuffLen = 128
    sBuffer = String$(lBuffLen, vbNullChar)
    lRet = GetComputerName(sBuffer, lBuffLen)
    If lRet < 0 Then
    '
    ' Handle API error Here
    '
    Exit Function
    End If
    sName = Left$(sBuffer, lBuffLen)
    GetComputerName = sName
    End If
    End Function


    'usage
    MsgBox GetComputerName()



    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  4. #4
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Getting computer names in the network

    '====================
    Using the Windows Scripting Host:

    Dim objWSH As Object
    Set objWSH = CreateObject("WScript.Network")
    MsgBox objWSH.ComputerName



    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

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

Featured