Link to home
Start Free TrialLog in
Avatar of RobK6364
RobK6364

asked on

Get the Current User of Remote Computer?

Anyone know if there is a way to obtain the name of the user currently logged in on a computer on your LAN?

I know how to get the local machine's current user, and i have the ip address, computer name, and domain of all the computers on my LAN... but I would also like to get the current user of all these computers too.

Anyone got any ideas?
Avatar of vinnyd79
vinnyd79

This example will get the netbios info from a remote computer from the IP. It will get the logged on user as well as mac address and computer name:

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=1&txtCodeId=495
Disregard the above link,It looks like it will only work on the local machine.
Maybe not the best way,but this should work:

Private Declare Function OpenProcess Lib "Kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "Kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)

Private Sub ShellWait(ByVal JobToDo As String)
        Dim hProcess As Long
        Dim RetVal As Long
        hProcess = OpenProcess(&H400, False, Shell(JobToDo, vbHide))
        Do
            GetExitCodeProcess hProcess, RetVal
            DoEvents: Sleep 100
        Loop While RetVal = &H103
End Sub

Private Function GetTheUser(IpAddress As String) As String
Dim TmpFile As String, Ln As String
Dim ff As Integer, Cnt As Integer, pos As Integer
TmpFile = Environ("Temp") & "\TmpUsr.tmp"
Cnt = 0
ShellWait Environ("Comspec") & " /c nbtstat -A " & IpAddress & " > " & TmpFile
ff = FreeFile
Open TmpFile For Input As #ff
Do Until EOF(ff)
Line Input #ff, Ln
pos = InStr(Ln, "<03>")
If pos > 0 Then Cnt = Cnt + 1
If Cnt = 2 Then
    GetTheUser = Trim$(Left$(Ln, pos - 1))
    Close #ff
    Kill TmpFile
    Exit Function
End If
Loop
Close #ff
GetTheUser = "Host Not Found"
Kill TmpFile
End Function

Private Sub Command1_Click()
MsgBox GetTheUser("192.168.1.100")
End Sub
Avatar of RobK6364

ASKER

vinny, that code returns the computer name not the logged in user.  also it seems all my network computers return "Host Not Found"
i modified the GetTheUser function and removed the count (Cnt) so that it would fire on the first occurance of "<03>" instead of the second.  However, this has me concerned that the code may not work on all versions of Windows the same way.  I am running Windows 2000 Pro.  What version are you running vinny?  does my altered code work on your OS?

'==============================================
Private Function GetTheUser(IpAddress As String) As String
Dim TmpFile As String, Ln As String
Dim ff As Integer, Cnt As Integer, pos As Integer

TmpFile = Environ("Temp") & "\TmpUsr.tmp"
Cnt = 0
ShellWait Environ("Comspec") & " /c nbtstat -A " & IpAddress & " > " & TmpFile
ff = FreeFile
Open TmpFile For Input As #ff
    Do Until EOF(ff)
        Line Input #ff, Ln
        pos = InStr(Ln, "<03>")
        If pos > 0 Then
            GetTheUser = Trim$(Left$(Ln, pos - 1))
            Close #ff
            Kill TmpFile
            Exit Function
        End If
    Loop
Close #ff
GetTheUser = "Host Not Found"
Kill TmpFile
End Function
'===============================================
ASKER CERTIFIED SOLUTION
Avatar of Glen A.
Glen A.
Flag of United States of America image

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
thanks AlbertaBeef, that seems to be a more consistent way to do this... worked perfectly.  

although i find it hard to believe it's that easy to access a remote registry... is there a way to set the permissions on the remote computer so that you wouldn't be able to read the registry remotely?  or will this always work?  i just find it hard to believe that with nothing more than a computer name... i can read/write any registry key on anyone's computer in my LAN?  seems like a big security threat.
only admins can read it.  So that's the security, is only an admin on the workstation can read / write info.