Solved

Getting Computer Names

Posted on 1998-10-10
12
253 Views
Last Modified: 2010-04-30
Can someone tell me how you can get the computer names of all the computers currently on the network? I am running Win95, and have a home network. I figure that the Win API's let you do this. I am not sure how though. Any ideas?
0
Comment
Question by:bubbles
  • 5
  • 2
  • 2
  • +2
12 Comments
 
LVL 9

Expert Comment

by:Dalin
Comment Utility
bubbles,
Try the code. Let me know if you have problems.
Regards
Dalin

Declare Function GetComputerNameA Lib "kernel32" (ByVal lpBuffer As String, nSize As Long) As Long


Function ComputerName() As String
   
Dim UserName As String * 255

Call GetComputerNameA(UserName, 255)
ComputerName = Left$(UserName, InStr(UserName, Chr$(0)) - 1)

End Function
0
 

Author Comment

by:bubbles
Comment Utility
Yeah, that code works fine, but it only gets the computer-name of the computer that is being used. I want to be able to list all of the computer names on the network. I figure that this must be possible, as Windows does it in the Network Neighbourhood. Sorry to reject your answer.
0
 
LVL 2

Expert Comment

by:mkmccreary
Comment Utility
There are several API functions used to do this.  These calls do use Unicode.  The best ways to find out information about this is to search MSDN for the function

WNetOpenEnum

At

http://msdn.microsoft.com

if you didn't already know that.  A summary of many of the functions is listed on:

http://premium.microsoft.com/msdn/library/sdkdoc/network/networks_0u43.htm

Remember, I said that these functions are Unicode, which means strings are handled differently than in VB.  A character consist of two bytes, not one.  I never could get the functions to work quite right, it will take a little playing with them.  I hear there is another API somewhere for doing these things, McIx mentioned it to me.  I just have no idea where it is.

Good Luck,
Martin
0
 
LVL 9

Expert Comment

by:Dalin
Comment Utility
Sorry I misunderstood you.
0
 
LVL 4

Expert Comment

by:mcix
Comment Utility
You might want to look into ADSI on the Platform SDK.  There are a lot of good examples, some in VB and C, maybe Java too...


0
 

Author Comment

by:bubbles
Comment Utility
Ok, so the WNetOpenEnum has something to do with it. I have looked at the articles on this function, and the ones related to it, but they are beyond me. Most of the sample code I have found is written in C, and I have not found any suitable code written in Visual Basic. Anyone know where there are some examples??? Remember:
* I am using VB
* I am running Win95
* I only want computer names, not printers etc.
Thanks
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Accepted Solution

by:
kswinney earned 50 total points
Comment Utility
Bubbles, I have the VB code to do what you want.  It's on my machine at the office.  I'll e-mail it to you tomorrow.
0
 
LVL 2

Expert Comment

by:mkmccreary
Comment Utility
kswinney,
     If you could, please post the code.  I would be very interested in seeing it.  Like I said, I've had problems doing this myself.

Thank You,
Martin
0
 
LVL 2

Expert Comment

by:kswinney
Comment Utility
Bubbles and mkmccreary, here's the code for the enumeration of network resources:

'****************************************************************
'Here are the declares in the netenum.bas file:

Option Explicit

'*** Begin Enumeration Stuff ***
    Type NETRESOURCE
        dwScope         As Long
        dwType          As Long
        dwDisplayType   As Long
        dwUsage         As Long
        lpLocalName     As String
        lpRemoteName    As String
        lpComment       As String
        lpProvider      As String
    End Type
   
    Type NETRESOURCERETURN
        dwScope         As Long
        dwType          As Long
        dwDisplayType   As Long
        dwUsage         As Long
        lpStrings       As String * 1024
    End Type
   
    'Scope parameter constants
    Global Const RESOURCE_CONNECTED = &H1
    Global Const RESOURCE_GLOBALNET = &H2
    Global Const RESOURCE_REMEMBERED = &H3
    Global Const RESOURCE_RECENT = &H4
    Global Const RESOURCE_CONTEXT = &H5
   
    'Type parameter constants
    Global Const RESOURCETYPE_ANY = &H0
    Global Const RESOURCETYPE_DISK = &H1
    Global Const RESOURCETYPE_PRINT = &H2
   
    'Display type parameter constants
    Global Const RESOURCEDISPLAYTYPE_GENERIC = &H0
    Global Const RESOURCEDISPLAYTYPE_DOMAIN = &H1
    Global Const RESOURCEDISPLAYTYPE_SERVER = &H2
    Global Const RESOURCEDISPLAYTYPE_SHARE = &H3
    Global Const RESOURCEDISPLAYTYPE_FILE = &H4
    Global Const RESOURCEDISPLAYTYPE_GROUP = &H5
   
    'Usage parameter constants
    Global Const RESOURCEUSAGE_ALL = &H0
    Global Const RESOURCEUSAGE_CONNECTABLE = &H1
    Global Const RESOURCEUSAGE_CONTAINER = &H2
   
    Declare Function WNetEnumResource Lib "mpr.dll" Alias "WNetEnumResourceA" (ByVal hEnum As Long, lpcCount As Long, lpNetResRetn As NETRESOURCERETURN, lpBufferSize As Long) As Long
    Declare Function WNetCloseEnum Lib "mpr.dll" (ByVal hEnum As Long) As Long
    Declare Function WNetOpenEnum Lib "mpr.dll" Alias "WNetOpenEnumA" (ByVal dwScope As Long, ByVal dwType As Long, ByVal dwUsage As Long, lpNetResource As NETRESOURCERETURN, lphEnum As Long) As Long
    Declare Function WNetOpenEnumNull Lib "mpr.dll" Alias "WNetOpenEnumA" (ByVal dwScope As Long, ByVal dwType As Long, ByVal dwUsage As Long, ByVal lpNetResource As String, lphEnum As Long) As Long
'*** End Enumeration Stuff ***


'****************************************************************
'Here is the network enumeration (netenum.cls) class:

Option Explicit

Function StrTok$(Srce$, Delim$)
    Dim iBegPos As Integer
    Dim iLn As Integer
    Dim iEndPos As Integer
   
   
    Static Start%, SaveStr$

    'If first call, make a copy of the string.
    If Srce$ <> "" Then
        Start% = 1:  SaveStr$ = Srce$
    End If

    iBegPos = Start%:  iLn = Len(SaveStr$)
    ' Look for start of a token (character that isn't delimiter).
    While iBegPos <= iLn And InStr(Delim$, Mid$(SaveStr$, iBegPos, 1)) <> 0
        iBegPos = iBegPos + 1
    Wend
    ' Test for token start found.
    If iBegPos > iLn Then
        StrTok$ = "":  Exit Function
    End If
    ' Find the end of the token.
    iEndPos = iBegPos
    While iEndPos <= iLn And InStr(Delim$, Mid$(SaveStr$, iEndPos, 1)) = 0
        iEndPos = iEndPos + 1
    Wend
    StrTok$ = Mid$(SaveStr$, iBegPos, iEndPos - iBegPos)
    ' Set starting point for search for next token.
    Start% = iEndPos
End Function

Public Function ExtractsProvider(sInput$)
    Dim iCounter As Integer
    Dim sTempsProvider As String

    'Look backward from the end of the string looking for the terminator
    ExtractsProvider = ""
    For iCounter = Len(sInput$) - 1 To 1 Step -1
        If Mid$(sInput$, iCounter, 1) = Chr$(0) Then
            sTempsProvider = Mid$(sInput$, iCounter + 1)
            Exit For
        End If
    Next
    If Len(sTempsProvider) Then ExtractsProvider = StrTok$(sTempsProvider, Chr$(0))
End Function

Public Function EnumerateResources(ResArray$(), ResType%, ResScope%, ResUsage%) As Long
    Dim dwScope As Long, dwType As Long, dwUsage As Long, lEnum As Long
    Dim lphEnum As Long, lphEnumMachine As Long, lphEnumDrive As Long, lphEnumDrive2 As Long
   
    Dim lpNumEntries As Long
    Dim lpBufferSize As Long
    Dim lpBuffer As NETRESOURCERETURN
    Dim lpBufferMachine As NETRESOURCERETURN
    Dim lpBufferDrive As NETRESOURCERETURN
    Dim lpBufferDrive2 As NETRESOURCERETURN
   
    Dim iDomains As Integer
    Dim lNetTop As Long
    Dim lEnumMain As Long
    Dim lEnumMach As Long
    Dim sNetProvider As String
    Dim lEnumDrive As Long
    Dim lEnumDrive2 As Long
    Dim sProvider As String
    Dim lNewTop As Long

   
    'Set the wait cursor
    Screen.MousePointer = 11
    iDomains = False
   
    'Set the size of the buffer used to hold net resources
    lpBufferSize& = Len(lpBuffer)
   
    'Clear the list box
    Erase ResArray$()
    lNetTop = 0
   
    'Open an enumeration of network services
    dwScope& = ResScope%
    dwType& = ResType%
    dwUsage& = ResUsage%
   
    lphEnum& = 0
    lEnum& = WNetOpenEnumNull(dwScope&, dwType&, dwUsage&, ByVal vbNullString, lphEnum&)
    If lEnum& = 0 Then  'no errors
        Do
            'Enumerate network services
            lpNumEntries& = 1
            lpBufferSize& = Len(lpBuffer)
            lEnumMain = WNetEnumResource(lphEnum&, lpNumEntries&, lpBuffer, lpBufferSize&)
           
            iDomains = False
            If lEnumMain = 0 Then
                'Set up the enumeration for machine resources on this net service
                lphEnumMachine& = 0
                lEnumMach = WNetOpenEnum(dwScope&, dwType&, dwUsage&, lpBuffer, lphEnumMachine&)
               
                'Find out if this is a microsoft network machine
                sNetProvider = ExtractsProvider(lpBuffer.lpStrings)
                If sNetProvider = "Microsoft Network" Then iDomains = True
               
                If lEnumMach = 0 Then  'no errors
                    Do
                        lpNumEntries& = 1
                        lEnumMach = WNetEnumResource(lphEnumMachine&, lpNumEntries&, lpBufferMachine, lpBufferSize&)
                        If lEnumMach = 0 Then
                            'Set up the enumeration for drive resources on this net service
                            lphEnumDrive& = 0
                            lEnumDrive = WNetOpenEnum(dwScope&, dwType&, dwUsage&, lpBufferMachine, lphEnumDrive&)
                            If lEnumDrive = 0 Then  'no errors
                                Do
                                    lpNumEntries& = 1
                                    lEnumDrive = WNetEnumResource(lphEnumDrive&, lpNumEntries&, lpBufferDrive, lpBufferSize&)
                                    If lEnumDrive = 0 Then
                                        'This is a machine if we're doing microsoft network
                                        If iDomains Then
                                            'Set up the enumeration for drive resources on this net service
                                            lphEnumDrive2& = 0
                                            lEnumDrive2 = WNetOpenEnum(dwScope&, dwType&, dwUsage&, lpBufferDrive, lphEnumDrive2&)
                                            If lEnumDrive2 = 0 Then  'no errors
                                                Do
                                                    lpNumEntries& = 1
                                                    lEnumDrive2 = WNetEnumResource(lphEnumDrive2&, lpNumEntries&, lpBufferDrive2, lpBufferSize&)
                                                    If lEnumDrive = 0 Then
                                                        sProvider = ExtractsProvider(lpBufferDrive2.lpStrings)
                                                        If Len(sProvider) Then
                                                            lNewTop = lNewTop + 1
                                                            ReDim Preserve ResArray$(1 To lNewTop)
                                                            ResArray$(lNewTop) = sProvider
                                                        End If
                                                    End If
                                                Loop While lEnumDrive2 = 0
                                            End If
                                            lEnum& = WNetCloseEnum(lphEnumDrive2&)
                                        Else
                                            sProvider = ExtractsProvider(lpBufferDrive.lpStrings)
                                            If Len(sProvider) Then
                                                lNewTop = lNewTop + 1
                                                ReDim Preserve ResArray$(1 To lNewTop)
                                                ResArray$(lNewTop) = sProvider
                                            End If
                                        End If
                                    End If
                                Loop While lEnumDrive = 0
                            End If
                            lEnum& = WNetCloseEnum(lphEnumDrive&)
                        End If
                    Loop While lEnumMach = 0
                End If
                lEnum& = WNetCloseEnum(lphEnumMachine&)
            End If
        Loop Until lEnumMain > 0
    End If
    lEnum& = WNetCloseEnum(lphEnum&)
   
    EnumerateResources = lNewTop
   
    Screen.MousePointer = 0
End Function


0
 

Author Comment

by:bubbles
Comment Utility
Ok, I have formatted all the code, and tried to get it to work, but it won't. I have already got code that will enumerate network resources, although it is nowhere near as lengthy as this. The code I have lists the network connections - eg:

e:    \\ComputerA\C\
f:    \\ComputerB\C\

And so on. I only want the computer names whether their drives have been mapped or not - eg.

ComputerA
ComputerB
ComputerC

I don't know if your code does what I want or not, but it won't work. Would you please email me a sample project, and I will increase the points to 100 if it works. You can email me at
d.taylor@clear.net.nz

Thanks,
Bubbles
0
 

Author Comment

by:bubbles
Comment Utility
I got your code, but it only displays either computers with resources that have been shared, or computers with connected resources. Is it possible just to get the computer names even if they have no shared resources, or if they do not have connected drives?
E.g. - can I list computer names of computers that are physically hooked into the network, but do not have drives that can be accessed?
0
 

Author Comment

by:bubbles
Comment Utility
Ok, so it looks like I can't exactly do what I wanted. Thanks anyway.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now