Visual Basic > Windows and Controls
Obtaining the current user name
Obtaining the current user name The following routine returns the name of the current user's login name: Option Explicit Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long 'Purpose : Returns the name of the current user's logon name 'Inputs : N/A 'Outputs : The current username 'Revisions : As an alternative, can use Environ$("UserName") on NT/2000 Function UserName() As String Dim lLenUserName As Long Static ssUserName As String If Len(ssUserName) = 0 Then lLenUserName = 100 ssUserName = String$(lLenUserName, Chr$(0)) 'Get the username GetUserName ssUserName, lLenUserName 'strip the rest of the buffer ssUserName = Left$(ssUserName, lLenUserName - 1) End If UserName = ssUserName End Function
Visual Basic Codes
ActiveX
Miscellaneous
Applications
Code Snippets
Common Dialogs
Special Effects
Database Stuff
Date Time
Files Drives
Forms
Graphics Games
Internet Stuff
Multimedia
Other
Strings
Windows