adambox
adambox

Reputation: 25501

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?

Upvotes: 11

Views: 43991

Answers (7)

Akshay Harugop
Akshay Harugop

Reputation: 11

'To Fetch the Logged in username

StrUser = CreateObject("WScript.Network").UserName

MsgBox StrUser

'To Fetch the Logged in userDomain

StrDomain = CreateObject("WScript.Network").UserDomain

MsgBox StrDomain

Upvotes: 1

bugBurger
bugBurger

Reputation: 6940

Alternative way to do that - probably the API you mention is a better way to get username.

For Each strComputer In arrComputers
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
        For Each objItem in colItems
        Wscript.Echo "UserName: " & objItem.UserName & " is logged in at computer " & strComputer
Next

Upvotes: 1

Harsono
Harsono

Reputation: 1

there are lots of way to get the current logged user name in WMI. my way is to get it through the username from process of 'explorer.exe' because when user login into window, the access of this file according to the current user.

WMI script would be look like this:

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strIP & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process")
For Each objprocess In colProcessList
   colProperties = objprocess.GetOwner(strNameOfUser, strUserDomain)
   If objprocess.Name = "explorer.exe" Then
      UsrName = strNameOfUser
      DmnName = strUserDomain
   End If
Next

for more detailcheck the link on :
http://msdn.microsoft.com/en-us/library/aa394599%28v=vs.85%29.aspx

Upvotes: 0

to StackOverflow
to StackOverflow

Reputation: 124696

Lots of alternative methods in other posts, but to answer the question: yes that is the best way to do it. Faster than creating a COM object or WMI if all you want is the username, and available in all versions of Windows from Win95 up.

Upvotes: 1

Knox
Knox

Reputation: 2919

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

Upvotes: 4

bobwienholt
bobwienholt

Reputation: 17610

You could also do this:

Set WshNetwork = CreateObject("WScript.Network")
Print WshNetwork.UserName

It also has a UserDomain property and a bunch of other things:

http://msdn.microsoft.com/en-us/library/907chf30(VS.85).aspx

Upvotes: 14

Ken
Ken

Reputation: 2092

You could also use Environ$ but the method specified by the question is better. Users/Applications can change the environment variables.

Upvotes: 7

Related Questions