Find User Name

By using Winsock control u can findout the Computer Name.My Question is that How to Find out  the User Name (of Windows that is Login Name).
Who is Participating?
romualdtConnect With a Mentor Commented:
Try this out

Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpbuffer As String, nSize As Long) As Long


Private Sub Command1_Click()
x = GetUser
Debug.Print x

End Sub

Function GetUser()
    ' This function uses a windows dll to qu
    '     ery the registry automatically ti return
    '     the user name
    Dim sBuffer As String
    Dim lSize As Long
    ' Parameters for the dll declaration are
    '     set
    sBuffer = Space$(255)
    lSize = Len(sBuffer)
    Call GetUserName(sBuffer, lSize) ' Call the declared dll Function

    If lSize > 0 Then
        GetUser = Left$(sBuffer, lSize) ' Remove empty spaces
        GetUser = vbNullString ' Return empty If no user is found
    End If
End Function
Add reference to Windows Scripting Host
'(VB menu, project, reference)
Private Sub Command1_Click()
  Dim wsh As New IWshNetwork_Class
  Text1 = wsh.UserName
End Sub
If you dont have access to the scripting hosts its:

Option Explicit

Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Private Sub Form_Load()
  Dim s$, x&
  s = String$(255, 0)
  x = GetUserName(s, Len(s))
  Debug.Print Left$(s, x)
End Sub

Good Luck!  :)

The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

wpsjr1. You came close but u might choose to print debub.print s. that's all. y all those left functions. Otherwise, atreya, u got the answer.
Guy Hengel [angelIII / a3]Billing EngineerCommented:
here, much easier :
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Same for computer name:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.