How can I get the currently logged-in windows user

2019-01-19 01:00发布

问题:

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?

回答1:

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



回答2:

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



回答3:

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


回答4:

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.



回答5:

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


回答6:

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