Getting the currently logged-in windows user
Asked Answered
U

7

11

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?

Unicuspid answered 3/10, 2008 at 20:7 Comment(3)
I tried to reproduce steps mentioned here(answer by ken) but I was not able to change any value of the Environ object. The only way to do this in straight VBA is then the one proposed here(answer by knox) and I cannot understand why this answer was downgraded!Disclamation
This question is almost exact duplicate of #9552Molest
Possible duplicate of Is there a way for MS Access to grab the current Active Directory user?Chair
K
7

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

Karlik answered 3/10, 2008 at 20:10 Comment(0)
S
14

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

Shamefaced answered 3/10, 2008 at 20:15 Comment(2)
You don't have to if you use CreateObject(). The code above will work without a reference.Shamefaced
You do have to have the WshNetwork variable defined as a generic object, instead of as the FSO's native data type (whatever that is -- I never use the FSO except with late binding, so don't know any of its data types at all).Lines
K
7

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

Karlik answered 3/10, 2008 at 20:10 Comment(0)
E
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
Earhart answered 3/10, 2008 at 21:31 Comment(1)
This was usefulJuniper
E
1

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
Enameling answered 3/10, 2008 at 20:14 Comment(0)
T
1

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.

Territory answered 4/10, 2008 at 7:2 Comment(0)
W
1
'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
Whoremaster answered 12/3, 2023 at 7:48 Comment(0)
D
0

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

Delores answered 1/11, 2013 at 2:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.