Link to home
Start Free TrialLog in
Avatar of namedpipes
namedpipes

asked on

Identify current user on a remote Win2k computer

I need to determine if a remote win2k computer is logged on or not, and if it is, the username of the user on the computer (or blank if it is powered on but logged off).

That is to say... not the local computer the program is running on, but across the network. I have administrator rights on both computers.

Any thoughts?
ASKER CERTIFIED SOLUTION
Avatar of m_renaud
m_renaud

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of namedpipes
namedpipes

ASKER


Very close... the routine I picked out will be close enough for , although it isn't exactly what I was looking for. There may be a more focused routine in the collection there.

I'll post the code and settle up in the AM.
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 Type WKSTA_USER_INFO_1
  wkui1_username As Long
  wkui1_logon_domain As Long
  wkui1_oth_domains As Long
  wkui1_logon_server As Long
End Type

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" (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

Public Function UserList(ByVal ComputerName As String) As String
Dim dwWorkstation   As Long
Dim bufptr          As Long
Dim dwEntriesread   As Long
Dim dwTotalentries  As Long
Dim dwResumehandle  As Long
Dim success         As Long
Dim uinfo           As WKSTA_USER_INFO_1
Dim i As Long
Dim whos As String
Dim s As String
Dim remote As String
   
    whos = ""
   
    remote = "\\" & ComputerName & vbNullString
    dwWorkstation = StrPtr(remote)
   
    success = NetWkstaUserEnum(dwWorkstation, 1, bufptr, MAX_PREFERRED_LENGTH, dwEntriesread, dwTotalentries, dwResumehandle)
       
    If success = NERR_SUCCESS And success <> ERROR_MORE_DATA Then
        If dwEntriesread > 0 Then
            For i = 0 To dwEntriesread - 1
                CopyMemory uinfo, ByVal bufptr, LenB(uinfo)
                s = GetPointerToByteStringW(uinfo.wkui1_logon_domain) & "\" & GetPointerToByteStringW(uinfo.wkui1_username)
                If InStr(1, s, ComputerName) < 1 Then whos = whos & s & ","
                bufptr = bufptr + 16
            Next i
        End If
    End If
   
    'clean up
    Call NetApiBufferFree(bufptr)
   
    If whos <> "" Then whos = Left$(whos, Len(whos) - 1)
    UserList = whos
End Function

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