Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> Visual Basic >> Snippet

Get user name


Private Declare Function GetUserName Lib "advapi32.dll <API_ADVAPI32.html>" Alias "GetUserNameA" ( ByVal lpBuffer As String , nSize As Long ) As Long

Public Function UserName() As String
Dim cn As String , ls As Long , res As Long
cn = String (1024, 0)
ls = 1024
res = GetUserName(cn, ls)
If res <> 0 Then
   UserName = Mid (cn, 1, InStr(cn, Chr (0)) - 1)
Else
   UserName = ""
End If
End Function

'------------------------------------
'it is also in the registry at
HKLM \ System \ CurrentControlSet \ Control \ CurrentUser