Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Two network type questions...

Status
Not open for further replies.

elziko

Programmer
Nov 7, 2000
486
GB
First one I'm sure will be very easy:

How do I get the machine name of every machine currently on my network?

And the second one, much harder, I would've thought:

For each machine name how do I get the username of the currently logged on user?

Thanks

elziko
 
thread222-92023

This will answer your second question, and possibly your first. However, on our network I discovered that this method only worked for about 50% of the logged on users because we have a large network and some major profile issues. I had to use a very roundabout way of doing this by looping with net.exe and nbtstat.exe so that I could reliably get each logged on user. But then again I don't have adminstrative priveliges, if you do, you could use SESSION_INFO_0 or 1, which is probably more reliable.
 
Part 2.
Do the following to get Current User

Create Litview (List1) and CommandButton[\color](Command1)
Code:
[COLOR=blue]
Option Explicit

Private Const NERR_SUCCESS As Long = 0&
Private Const MAX_PREFERRED_LENGTH As Long = -1
Private Const ERROR_MORE_DATA As Long = 234&
Private Const LB_SETTABSTOPS As Long = &H192

'for use on Win NT/2000 only
Private Type WKSTA_INFO_102
  wki102_platform_id As Long
  wki102_computername As Long
  wki102_langroup As Long
  wki102_ver_major As Long
  wki102_ver_minor As Long
  wki102_lanroot As Long
  wki102_logged_on_users As Long
End Type

Private Type WKSTA_USER_INFO_0
  wkui0_username  As Long
End Type

Private Declare Function NetWkstaGetInfo Lib "Netapi32" _
  (ByVal servername As Long, _
   ByVal level As Long, _
   bufptr As Long) As Long
   
Private Declare Function NetWkstaUserEnum Lib "Netapi32" _
  (ByVal servername As Long, _
   ByVal level As Long, _
   bufptr As Long, _
   ByVal prefmaxlen As Long, _
   entriesread As Long, _
   totalentries As Long, _
   resume_handle As Long) As Long
   
Private Declare Function NetApiBufferFree Lib "netapi32.dll" _
   (ByVal Buffer As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" _
   Alias "RtlMoveMemory" _
  (pTo As Any, _
   uFrom As Any, _
   ByVal lSize As Long)
   
Private Declare Function lstrlenW Lib "kernel32" _
  (ByVal lpString As Long) As Long

Private Declare Function SendMessage Lib "user32" _
   Alias "SendMessageA" _
  (ByVal hwnd As Long, _
   ByVal wMsg As Long, _
   ByVal wParam As Long, _
   lParam As Any) As Long

Private Sub Form_Load()

   ReDim TabArray(0 To 6) As Long
   
   TabArray(0) = 78
   TabArray(1) = 129
   TabArray(2) = 159
   TabArray(3) = 198
   TabArray(4) = 227
   TabArray(5) = 253
   TabArray(6) = 302

   Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 0&, ByVal 0&)
   Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 7&, TabArray(0))
   List1.Refresh

   Command1.Caption = "NetWkstaGetInfo"
   
   Label1.Caption = "call success (0) or error :"
   Label2.Caption = ""
      
End Sub


Private Sub Command1_Click()

   Dim bufptr          As Long
   Dim dwServer        As Long
   Dim success         As Long
   Dim nStructSize     As Long
   Dim bServer         As String
   Dim ws102           As WKSTA_INFO_102
   Dim x As ListItem
   
   bServer = "\\" & Environ$("COMPUTERNAME") & vbNullString

   List1.ListItems.Clear
   List1.ColumnHeaders.Clear
   List1.ColumnHeaders.Add , , "Computer Name"
   List1.ColumnHeaders.Add , , "WorkGroup"
   List1.ColumnHeaders.Add , , "Logged On Users"
   List1.ColumnHeaders.Add , , "Platform Id"
   List1.ColumnHeaders.Add , , "Version"
   List1.ColumnHeaders.Add , , "Server"
   
   Label2.Caption = success
   
   dwServer = StrPtr(bServer)
   
   success = NetWkstaGetInfo(dwServer, 102, bufptr)

   If success = NERR_SUCCESS And _
      success <> ERROR_MORE_DATA Then
         nStructSize = LenB(ws102)

         CopyMemory ws102, ByVal bufptr, nStructSize

         Set x = List1.ListItems.Add(, , GetPointerToByteStringW(ws102.wki102_computername))
         x.SubItems(1) = GetPointerToByteStringW(ws102.wki102_langroup)
         x.SubItems(2) = ws102.wki102_logged_on_users
         x.SubItems(3) = ws102.wki102_platform_id
         x.SubItems(4) = ws102.wki102_ver_major & &quot; v &quot; & _
                         ws102.wki102_ver_minor
         x.SubItems(5) = GetWorkstationUserName(dwServer)
                       
   End If
   
   Call NetApiBufferFree(bufptr)
      
End Sub


Private Function GetPointerToByteStringW(ByVal dwData As Long) As String
  
   Dim tmp() As Byte
   Dim tmplen As Long
   
   If dwData <> 0 Then
      tmplen = lstrlenW(dwData) * 2
      
      If tmplen <> 0 Then
         ReDim tmp(0 To (tmplen - 1)) As Byte
         CopyMemory tmp(0), ByVal dwData, tmplen
         GetPointerToByteStringW = tmp
     End If
   End If
    
End Function


Private Function GetWorkstationUserName(ByVal dwWorkstation As Long) As String

   Dim bufptr          As Long
   Dim dwEntriesread   As Long
   Dim dwTotalentries  As Long
   Dim dwResumehandle  As Long
   Dim success         As Long
   Dim nStructSize     As Long
   Dim wui0            As WKSTA_USER_INFO_0
   
   success = NetWkstaUserEnum(dwWorkstation, 0, _
                              bufptr, MAX_PREFERRED_LENGTH, _
                              dwEntriesread, dwTotalentries, _
                              dwResumehandle)

   If success = NERR_SUCCESS And _
      success <> ERROR_MORE_DATA Then
      
      nStructSize = LenB(wui0)
      
      If dwEntriesread > 0 Then
         CopyMemory wui0, ByVal bufptr, nStructSize
         
         GetWorkstationUserName = GetPointerToByteStringW(wui0.wkui0_username)
         
         Call NetApiBufferFree(bufptr)
         Exit Function
      End If

   End If
   
   GetWorkstationUserName = &quot;n\a on Win9x&quot;
   Call NetApiBufferFree(bufptr)
   
End Function
[\color]
[\code]

Ciao >(::O>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top