Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Find User Name

Posted on 2000-03-31
6
Medium Priority
?
236 Views
Last Modified: 2013-11-13
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).
0
Comment
Question by:atreya
6 Comments
 
LVL 28

Expert Comment

by:Ark
ID: 2675522
Hi
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
0
 
LVL 3

Expert Comment

by:wpsjr1
ID: 2675539
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!  :)

Paul
0
 

Expert Comment

by:AshokKumar
ID: 2675609
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2675688
here, much easier :
ENVIRON("USERNAME")
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2675691
Same for computer name:
ENVIRON("COMPUTERNAME")
0
 

Accepted Solution

by:
romualdt earned 20 total points
ID: 2675767
Try this out


'Declerations
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
    Else
        GetUser = vbNullString ' Return empty If no user is found
    End If
End Function
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question