Code: Select all
Private Declare Function GetUserName Lib "advapi32.dll" _
Alias "GetUserNameA" _
(ByVal lpbuffer As String, nsize As Long) As Long
Public Function GetUN() As String
Dim lpbuffer As String * 30
Dim nsize As Long
Dim result As Long
nsize = 30
result = GetUserName(lpbuffer, nsize)
If result Then
GetUN = Trim(lpbuffer)
Else
GetUN = "Error"
End If
End Function
Code: Select all
s = GetUN
For x = 1 To Len(s)
Debug.Print Asc(Mid(s, x, 1))
Next x
If I change the code by adding
lpbuffer = Space(30)
then the loop returning the ASCII coded returns 65,65,65,65,0. The API call is still returning one null character, but Trim does not trim it.
What did I miss. Thanks