Sub TstGetUserName() 'This will test the use of GetUserName. Dim UName As String Dim UNameLen As Long Dim X as Long UName = Space(255) UNameLen = 255 X = GetUserName(UName, UNameLen) If X <> 0 Then 'We got the user's name UName = Left(UName, UNameLen - 1) 'Get all except the last character MsgBox "The current user is: " & vbCr & UName Else MsgBox "Unable to get the user name." End If End Sub
定义: 'General Declarations area of a Code Module Public Declare Function GetUserName& Lib "advapi32.dll" Alias "GetUserNameA" _ (ByVal lpBuffer As String, nSize As Long)
Dim UName As String
Dim UNameLen As Long
Dim X as Long
UName = Space(255)
UNameLen = 255
X = GetUserName(UName, UNameLen)
If X <> 0 Then 'We got the user's name
UName = Left(UName, UNameLen - 1) 'Get all except the last character
MsgBox "The current user is: " & vbCr & UName
Else
MsgBox "Unable to get the user name."
End If
End Sub
'General Declarations area of a Code Module
Public Declare Function GetUserName& Lib "advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long)