how can I obtain all names of computers from workgroup 
Author Message
 how can I obtain all names of computers from workgroup

I want to make a chat, and I need to obtain
all computer names, that are running.
I will welcome source code.


Fri, 30 May 2003 04:41:04 GMT  
 how can I obtain all names of computers from workgroup
I made it this way:
Option Explicit

'Api: GetEnum
'Api: GetEnum
'Api: GetEnum

Private Const RESOURCE_GLOBALNET As Long = &H2&
Private Const RESOURCEDISPLAYTYPE_DOMAIN& = &H1
Private Const RESOURCEDISPLAYTYPE_SERVER& = &H2
Private Const RESOURCEDISPLAYTYPE_SHARE& = &H3
Private Const RESOURCETYPE_ANY As Long = &H0&
Private Const RESOURCEUSAGE_ALL As Long = &H0&
Private Const RESOURCEUSAGE_CONTAINER As Long = &H2&
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()

    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 "C:\datalist.ini" For Output Shared As #filNum
        Print #filNum, "Username: : " & strUserName
        Print #filNum, "Computername:   " & strMachinerName,
        For l = 0 To UBound(uNet)
            Select Case uNet(l).dwDisplayType
            Case RESOURCEDISPLAYTYPE_DOMAIN
                Print #filNum, "Arbetsgrupp:",
            Case RESOURCEDISPLAYTYPE_SERVER
                Print #filNum, "Dator",
            Case RESOURCEDISPLAYTYPE_SHARE
                GoTo TheLoop
            End Select
            Print #filNum, uNet(l).sRemoteName
TheLoop:
        Next l
    End If
    Close #filNum
    frmStart.Show
End Sub
Private Sub username()
  On Error Resume Next
    strUserName = String(255, Chr$(0))
    getusername strUserName, 255
    strUserName = Left$(strUserName, InStr(strUserName, Chr$(0)) - 1)
    strMachinerName = String(255, Chr$(0))
    GetComputerName strMachinerName, 255
    strMachinerName = Left$(strMachinerName, InStr(1, strMachinerName,
Chr$(0)) - 1)
End Sub

Quote:

> I want to make a chat, and I need to obtain
> all computer names, that are running.
> I will welcome source code.



Mon, 23 Jun 2003 01:12:01 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. How to detect Computer Name and Workgroup ?

2. How do I get the Workgroup name of the computer

3. Win for Workgroup Computer Names?

4. API calls for retrieving all the Workgroup/Domain/Computer names on the network

5. Workgroup and Computer Name

6. Workgroup and Computer Name

7. API calls for retrieving all the Workgroup/Domain/Computer names on the network

8. How to obtain local network NetBIOS computer names?

9. Obtaining Computer Name

10. How to obtain a computer name?

11. Obtaining the name of the remote computer connecting to Terminal Services

12. Obtaining table names/field names from Access @ runtime

 

 
Powered by phpBB® Forum Software