Names of computers on network. (1 Viewer)

arvindn

Registered User.
Local time
Today, 21:02
Joined
Oct 1, 2003
Messages
99
How do i get the names of the computers (the ones u see on entering network neighbourhood/ My network places) currently on the LAN using VBA code ?

Thanks in advance.
 

___

¯¯¯¯¯
Local time
Today, 16:32
Joined
Nov 27, 2003
Messages
595
If you only need the computer name, put the following code into a new module
Code:
Private Declare Function apiGetComputerName Lib "kernel32" Alias _
"GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Function fOSMachineName() As String
Dim lngLen As Long, lngX As Long
Dim strCompName As String

lngLen = 16
strCompName = String$(lngLen, 0)
lngX = apiGetComputerName(strCompName, lngLen)
    If lngX <> 0 Then
        fOSMachineName = Left$(strCompName, lngLen)
    Else
        fOSMachineName = ""
    End If
    
End Function
and place

=fOSMachineName()

in the control source of an unbound text box on your form.
 

ghudson

Registered User.
Local time
Today, 11:32
Joined
Jun 8, 2002
Messages
6,194
arvindn said:
How do i get the names of the computers (the ones u see on entering network neighbourhood/ My network places) currently on the LAN using VBA code?
If it is possible...you should search a Visual Basic site for some code to list all of the computer names that are logged onto a specific server.

The Environ() function is a simple way to grab information from a users computer. Here are a few examples...

MsgBox "ComputerName = " & Environ("ComputerName")
MsgBox "UserName = " & Environ("UserName")

I have a sample db posted somewhere that lists most of the Environ variables. Just search for "environ" and user "ghudson" to find it.
 
Last edited:

arvindn

Registered User.
Local time
Today, 21:02
Joined
Oct 1, 2003
Messages
99
Thanks everyone.

To clarify, i do not need only my computer's name. I will try searching the net as suggested by ghudson. By the way if u come across something please let me know.
 

ColinEssex

Old registered user
Local time
Today, 16:32
Joined
Feb 22, 2002
Messages
9,132
You can use the fosMachineName() as listed earlier (or a derivation of it) to log who logs in (network UserName) and when (Time and Date), and then log when they log out (Time) and also the PCID that they used - log it to a LoginTable and then you could display it on the switchboard form to show current users logged in. You can also show what form they are looking at if you want. Put it in the Timer event then it always gives up-to-date info by refreshing the query that scans the LogIn table.

I think I've answered this ages ago - do a search for fosUserName() to check - if you're interested.

Col
 

ghudson

Registered User.
Local time
Today, 11:32
Joined
Jun 8, 2002
Messages
6,194
arvindn said:
To clarify, i do not need only my computer's name.
arvindn is searching for a way to list ALL of the users computer names that are connected to a specific server [not just one user who is in his db].

Out of courisity I search VBnet for the key words "computer name" and there were a few interesting posts. Look at these and see if they are what you are after. Many of the codes listed for VB also work in VBA. If you get it to work in VBA I would apprecuiate it if you could post an Access 97 sample.

Here are two of the search results titles that looked interesting...
  • Enumerating Share Connection Information
  • Enumerating the User List and Obtaining User Info
Good luck!
 

ColinEssex

Old registered user
Local time
Today, 16:32
Joined
Feb 22, 2002
Messages
9,132
ghudson said:
arvindn is searching for a way to list ALL of the users computer names that are connected to a specific server [not just one user who is in his db].

Aha I see, I was only thinking of all the users logged into his database.

Col
 

arvindn

Registered User.
Local time
Today, 21:02
Joined
Oct 1, 2003
Messages
99
Thanks once again.

I will follow the lead by ghudson and let everyone know.
 

mr moe

Registered User.
Local time
Today, 16:32
Joined
Jul 24, 2003
Messages
332
Nice example. But how do you retrieve names for multi user application? Thanks.
 

pono1

Registered User.
Local time
Today, 08:32
Joined
Jun 23, 2002
Messages
1,186
Piling on... A Google or MSDN or MS Knowledge base search on IADS might also yield some helpful results.

Regards,
Tim
 

tembenite

Registered User.
Local time
Today, 11:32
Joined
Feb 10, 2005
Messages
38
I'm working on a strictly code version to do this (Which I will post when I have it done). However, right now I'm just using a shell execution of "net view" captured into a textfile.
E.G.
Shell "cmd /c ""net view > netview.txt""", 6

I then open the file as a textstream and process the info. It is a workaround, but it works.
 

ghudson

Registered User.
Local time
Today, 11:32
Joined
Jun 8, 2002
Messages
6,194
I had to put in a C:\ drive location for the file to be saved.

Shell "cmd /c ""net view > c:\netview.txt""", 6
 

tembenite

Registered User.
Local time
Today, 11:32
Joined
Feb 10, 2005
Messages
38
I Finalley Got it!!

Okay, after hours of trying to edit different code modules, aimed at different things then getting the computer names, I finalley found a solution. On the web I found a VB class module that enumerates the domain. I created a new class module in my DB (called cNetView), and pasted this code in. I had to comment out the first 10 lines or so, because they don't apply to VBA, but then it worked. Not only does it work, but it works great, and very quickly!

Credit goes to Powersoft Programming, as indicated in the comments. Powersoft appears to no longer exist (and the website referenced in the code is dead). However in my next post, I reference the sourcecode. The person who posted it, says he created it. He's now part of X2Software. They also have some code on their website for enumeration more than one domain. I assume a similiar modification to that class would also work. (It's also in my links post).
Code:
'VERSION 1.0 CLASS
'BEGIN
'  MultiUse = -1  'True
'  Persistable = 0  'NotPersistable
'  DataBindingBehavior = 0  'vbNone
'  DataSourceBehavior = 0   'vbNone
'  MTSTransactionMode = 0   'NotAnMTSObject
'End
'Attribute VB_Name = "clsComputers"
'Attribute VB_GlobalNameSpace = False
'Attribute VB_Creatable = True
'Attribute VB_PredeclaredId = False
'Attribute VB_Exposed = False
Option Explicit

'// This class was put together by Powersoft Programming.
'// You can find us at: [url]http://psprogramming.virtualave.net/[/url]
'//
'// This class will provide the functionality
'// to enumerate all computers in a domain of your network.
'//
'// Feel free to use this class in your own projects,
'// just make sure this description is also included.
Private cComputers As New Collection
Private sDomain As String

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, ByVal lpBuffer As Long, ByRef lpBufferSize As Long) As Long
Private Declare Function WNetCloseEnum Lib "mpr.dll" (ByVal hEnum As Long) As Long
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function StrLenA Lib "kernel32" Alias "lstrlenA" (ByVal Ptr As Long) As Long
Private Declare Function StrCopyA Lib "kernel32" Alias "lstrcpyA" (ByVal RetVal As String, ByVal Ptr As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)

Private Const RESOURCE_GLOBALNET = &H2
Private Const RESOURCETYPE_DISK = &H1
Private Const ERROR_MORE_DATA = 234
Private Const GMEM_FIXED = &H0
Private Const GMEM_ZEROINIT = &H40
Private Const GPTR = (GMEM_FIXED Or GMEM_ZEROINIT)
Private Type NETRESOURCE
    dwScope As Long
    dwType As Long
    dwDisplayType As Long
    dwUsage As Long
    lpLocalName As Long
    lpRemoteName As Long
    lpComment As Long
    lpProvider As Long
End Type
Private Type NETRES2
    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
Public Property Get Domain() As String
    Domain = sDomain
End Property

Public Property Let Domain(Value As String)
    sDomain = Value
End Property

Public Function GetCount() As Long
    '// Return the number of computers in
    '// the collection.
    GetCount = cComputers.Count
End Function
Public Function GetItem(Index As Long) As String
    '// Check for legal value
    If Index < 1 Or Index > cComputers.Count Then
        '// Raise an error
        Err.Raise 7771, , "The index is not within valid range!"
        Exit Function
    End If
    '// Return an item in the domains collection
    GetItem = cComputers.item(Index)
End Function
Public Sub Refresh()
    Dim lBufferPtrTemp As Long
    Dim sComputer As String
    Dim tNetRes As NETRES2
    Dim tNR As NETRESOURCE
    Dim lBufferPtr As Long
    Dim lEnumHwnd As Long
    Dim lReturn As Long
    Dim lBuffer As Long
    Dim lCount As Long
    Dim lK As Long
    '// Check if domain is already set
    If Len(sDomain) = 0 Then
        '// Raise an error
        Err.Raise 7772, , "The domain has not been set!"
        Exit Sub
    End If
    With tNetRes
        .lpRemoteName = sDomain
        .dwDisplayType = 1
    End With
    lReturn = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, tNetRes, lEnumHwnd)
    If lReturn <> 0 Then
        '// Raise error
        Err.Raise 7773, , "Could not enumerate computers in domain!"
        Exit Sub
    End If
    
    lBuffer = 1024 * 10
    lBufferPtr = GlobalAlloc(GPTR, lBuffer)
    Do
        '// Number of entries to return from enumeration:
        '// -1 causes all objects to be returned
        lCount = -1
        lReturn = WNetEnumResource(lEnumHwnd, lCount, lBufferPtr, lBuffer)
        
        If lReturn = ERROR_MORE_DATA Then
            '// Enumeration indicates that the lBufferPtr
            '// is not big enough to hold all of the
            '// information in the NETRESOURCE structure.
            '// lBuffer has been updated to hold the required
            '// amount of space.
            
            '//Free up memory
            GlobalFree lBufferPtr
            
            '// Allocate a new space for the lBuffer
            '// requested by the enumeration
            lBufferPtr = GlobalAlloc(GPTR, lBuffer)
        Else
            If lReturn = 0 Then
                lBufferPtrTemp = lBufferPtr
                For lK = 1 To lCount
                    CopyMemory tNR, ByVal lBufferPtrTemp, LenB(tNR)
                    sComputer = PointerToAsciiStr(tNR.lpRemoteName)
                    
                    If sComputer <> "" Then
                        sComputer = Mid(sComputer, InStr(sComputer, "\\") + 2)
                        
                        '// Add computer to the collection
                        cComputers.Add sComputer
                    End If
                    
                    '// Step forward in the buffer by
                    '// the length of the copied structure
                    lBufferPtrTemp = lBufferPtrTemp + LenB(tNR)
                Next
            End If
        End If
    Loop Until lCount = 0
    
    If lEnumHwnd <> 0 Then
        lReturn = WNetCloseEnum(lEnumHwnd)
    End If
    
    '// Free up memory
    GlobalFree lBufferPtr
End Sub
Private Function PointerToAsciiStr(ByVal lPtrToString As Long) As String
    On Local Error Resume Next
    Dim lLength As Long
    Dim sStringValue As String
    Dim lNullPos As Long
    Dim lReturn As Long
    
    lLength = StrLenA(lPtrToString)
    
    If lPtrToString > 0 And lLength > 0 Then
        '// Copy Pointer to String
        sStringValue = Space$(lLength + 1)
        lReturn = StrCopyA(sStringValue, lPtrToString)
        
        '// Find null terminator
        lNullPos = InStr(1, sStringValue, Chr$(0))
        
        If lNullPos > 0 Then
            '// Lose the null terminator
            PointerToAsciiStr = Left$(sStringValue, lNullPos - 1)
        Else
            '// Just pass the string
            PointerToAsciiStr = sStringValue
        End If
    Else
        PointerToAsciiStr = ""
    End If
End Function

The following code prints all the Computers in the domain into the Immediate window.
Code:
Function classtest()
    Dim nvCompList As New cNetView
    Dim x As Long
    nvCompList.Domain = "MYDOMAINNAME"
    nvCompList.Refresh
    For x = 1 To nvCompList.GetCount
        Debug.Print nvCompList.GetItem(x)
    Next x
End Function

Code Found Here (after a TON of searching)
 
Last edited:

Users who are viewing this thread

Top Bottom