CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2000
    Posts
    3

    Get Computers within a group

    Hello,
    How can retrieve all the computers within a group or domain?


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

    Re: Get Computers within a group

    Here is a little sample that enumerates all resources in a network. Output is to the Debug window as well as to a text file.

    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
    Open App.Path & "\" & LCase(App.EXEName) & ".txt" for Output Shared as #filNum
    'Open "d:\" & App.EXEName & ".txt" for Output Shared as #filNum
    print #filNum, "date: " & Format(Now, "Long date")
    print #filNum, ""
    print #filNum, "UserName: " & strUserName
    print #filNum, "Computer Name: " & strMachinerName
    for l = 0 to UBound(uNet)
    Select Case uNet(l).dwDisplayType
    Case RESOURCEDISPLAYTYPE_DIRECTORY&
    Debug.print "Directory...",
    print #filNum, "Directory...",
    Case RESOURCEDISPLAYTYPE_DOMAIN
    Debug.print "Domain...",
    print #filNum, "Domain...",
    Case RESOURCEDISPLAYTYPE_FILE
    Debug.print "File...",
    print #filNum, "File...",
    Case RESOURCEDISPLAYTYPE_GENERIC
    Debug.print "Generic...",
    print #filNum, "Generic...",
    Case RESOURCEDISPLAYTYPE_GROUP
    Debug.print "Group...",
    print #filNum, "Group...",
    Case RESOURCEDISPLAYTYPE_NETWORK&
    Debug.print "Network...",
    print #filNum, "Network...",
    Case RESOURCEDISPLAYTYPE_ROOT&
    Debug.print "Root...",
    print #filNum, "Root...",
    Case RESOURCEDISPLAYTYPE_SERVER
    Debug.print "Server...",
    print #filNum, "Server...",
    Case RESOURCEDISPLAYTYPE_SHARE
    Debug.print "Share...",
    print #filNum, "Share...",
    Case RESOURCEDISPLAYTYPE_SHAREADMIN&
    Debug.print "ShareAdmin...",
    print #filNum, "ShareAdmin...",
    End Select
    Debug.print uNet(l).sRemoteName, uNet(l).sComment
    print #filNum, uNet(l).sRemoteName, uNet(l).sComment
    next l
    End If
    Close #filNum
    MsgBox "File " + App.Path & "\" & LCase(App.EXEName) & ".txt created" + vbCrLf + "Open it in a text editor to see the results", vbInformation
    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

    private Sub Form_Load()
    main
    End Sub




    John G

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