CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2010
    Posts
    5

    Question System Process : Get currently logged on user?

    We have a small windows application that we need to run as a system process. However, somewhere in the application code, we need to get the login ID of the currently logged on user.

    If we use the GetUserName function, it will return SYSTEM since this is running as a system application. So, is there a way for an application running as a system process to get the login ID / username of the currently logged-on user?

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: System Process : Get currently logged on user?

    Try this:
    Code:
    Option Explicit
    
    Private Enum EXTENDED_NAME_FORMAT
        NameUnknown = 0
        NameFullyQualifiedDN = 1
        NameSamCompatible = 2
        NameDisplay = 3
        NameUniqueId = 6
        NameCanonical = 7
        NameUserPrincipal = 8
        NameCanonicalEx = 9
        NameServicePrincipal = 10
    End Enum
    
    Private Declare Function GetUserNameEx Lib "secur32.dll" Alias "GetUserNameExA" (ByVal NameFormat As EXTENDED_NAME_FORMAT, ByVal lpNameBuffer As String, ByRef nSize As Long) As Long
    
    Private Sub Form_Load()
        'KPD-Team 2001
        'URL: http://www.allapi.net/
        'E-Mail: KPDTeam@allapi.net
        Dim sBuffer As String, Ret As Long
        sBuffer = String(256, 0)
        Ret = Len(sBuffer)
        If GetUserNameEx(NameSamCompatible, sBuffer, Ret) <> 0 Then
            MsgBox "Username: " + Left$(sBuffer, Ret)
        Else
            MsgBox "Error while retrieving the username"
        End If
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Oct 2010
    Posts
    5

    Re: System Process : Get currently logged on user?

    Quote Originally Posted by dglienna View Post
    Try this:
    Code:
    Option Explicit
    
    Private Enum EXTENDED_NAME_FORMAT
        NameUnknown = 0
        NameFullyQualifiedDN = 1
        NameSamCompatible = 2
        NameDisplay = 3
        NameUniqueId = 6
        NameCanonical = 7
        NameUserPrincipal = 8
        NameCanonicalEx = 9
        NameServicePrincipal = 10
    End Enum
    
    Private Declare Function GetUserNameEx Lib "secur32.dll" Alias "GetUserNameExA" (ByVal NameFormat As EXTENDED_NAME_FORMAT, ByVal lpNameBuffer As String, ByRef nSize As Long) As Long
    
    Private Sub Form_Load()
        'KPD-Team 2001
        'URL: http://www.allapi.net/
        'E-Mail: KPDTeam@allapi.net
        Dim sBuffer As String, Ret As Long
        sBuffer = String(256, 0)
        Ret = Len(sBuffer)
        If GetUserNameEx(NameSamCompatible, sBuffer, Ret) <> 0 Then
            MsgBox "Username: " + Left$(sBuffer, Ret)
        Else
            MsgBox "Error while retrieving the username"
        End If
    End Sub

    This returns NTADMIN\<computer name>$


  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: System Process : Get currently logged on user?

    Not for me:
    ASUSVISTA\David
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    May 2004
    Location
    Michigan, United States
    Posts
    457

    Re: System Process : Get currently logged on user?

    You can use the WSH library, if you want.
    Code:
    strUser = CreateObject("WScript.Network").UserName
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

  6. #6
    Join Date
    Dec 2001
    Posts
    6,332

    Re: System Process : Get currently logged on user?

    There's also the GetUserName API, plus a few other ways:
    http://www.vbforums.com/showthread.php?t=357723
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured