VBNotebook Home | Functions | Get User Name
This routine provides the name of the user who's currently logged onto a system.
First, in General|Declarations, include the following...
__________________________________________________________________________________________
'
' API Calls
'
Private Declare Function WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA" _
(ByVal lpName As String, ByVal lpUserName As String, _
lpnLength As Long) As Long
__________________________________________________________________________________________
...and now the routine... __________________________________________________________________________________________
Public Function GetUserName() As String
On Error GoTo proc_err
Dim nLength As Long
Dim nStatus As Long
Dim sName As String
Dim sUserName As String
nLength = 255
sUserName = Space$(nLength + 1)
nStatus = WNetGetUser(sName, sUserName, nLength)
If nStatus = 0 Then
sUserName = Trim$(Replace(sUserName, Chr$(0), " "))
Else
sUserName = ""
End If
GetUserName = sUserName
Exit Function
proc_err:
GetUserName = ""
End Function
__________________________________________________________________________________________
Copyright 2000-2005, J. Frank Carr