Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the currently logged-in windows user

I found this via google: http://www.mvps.org/access/api/api0008.htm

'******************** Code Start **************************
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
    "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function fOSUserName() As String
' Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
    strUserName = String$(254, 0)
    lngLen = 255
    lngX = apiGetUserName(strUserName, lngLen)
    If ( lngX > 0 ) Then
        fOSUserName = Left$(strUserName, lngLen - 1)
    Else
        fOSUserName = vbNullString
    End If
End Function
'******************** Code End **************************

Is this the best way to do it?

like image 349
adambox Avatar asked Oct 03 '08 20:10

adambox


People also ask

How can I tell who is currently logged in Windows?

The “whoami” command displays the user you are currently logged in and using in Windows. Hold down the Windows Key, and press “R” to bring up the Run window. Type “CMD“, then press “Enter” to open a command prompt. The computer name or domain followed by the username is displayed.

Who is currently logged?

1 – Open up the Run window by pressing the Windows Key +R. Type the text cmd in the box provided and hit Ctrl + Shift + Enter keys at once. 2 – Once the command prompt opens up, you will have to type the command query user. On hitting the Enter button, you will get all the details associated with the user.

How can I see active users in cmd?

This method works both in the Command Prompt and PowerShell. Open the command-line app that you prefer, type net user, and press Enter. Net user lists the users that have accounts configured on a Windows PC, including hidden ones or disabled user accounts.

How can I tell who is remoted into my computer?

Click the Tools tab. In the Windows Tools section, click Remote Control. Click. against the name of a computer to view its remote-control history.


1 Answers

I generally use an environ from within VBA as in the following. I haven't had the problems that Ken mentions as possibilities.

Function UserNameWindows() As String
    UserNameWindows = VBA.Environ("USERNAME") & "@" & VBA.Environ("USERDOMAIN")
End Function
like image 88
Knox Avatar answered Oct 17 '22 13:10

Knox