Search Tools Links Login

Domain change in progress, site content is moving! See this post for more info

Get the current username

Visual Basic 6, or VB Classic

Get the username of the currently logged on user.

Option Explicit

Private Const UNKNOWN = "(Value Unknown Because System Call Failed)"
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Public Function GetCurrentUserName() As String

  Dim l As Long
  Dim sUser As String

  sUser = Space$(255)
  l = GetUserName(sUser, 255)
  ' strip null terminator

  If l <> 0 Then
    GetCurrentUserName = Left(sUser, InStr(sUser, Chr(0)) - 1)
  Else
    Err.Raise Err.LastDllError, , "A system call returned an error code of " & Err.LastDllError
  End If

End Function

About this post

Posted: 2007-05-22
By: FortyPoundHead
Viewed: 2,121 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

Sorry. Comments are frozen for this article. If you have a question or comment that relates to this article, please post it in the appropriate forum.