Retrieve Username

There comes a time in almost every program when you want to know who is the currently logged in user… here’s a little function to do that…

Declare Function GetUserName Lib “advapi32.dll” Alias _
“GetUserNameA” (ByVal lpBuffer As String, _
ByRef nSize As Integer) As Integer

Public Function retUserName() As String
Dim iReturn As Integer
Dim userName As String
userName = New String(CChar(” “), 50)
iReturn = GetUserName(userName, 50)
retUserName = userName.Substring(0, userName.IndexOf(Chr(0)))
End Function