A.E.Veltstra
2004/05/03
Nederlands
EnglishPrivate Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" ( _ ByVal lpBuffer As String, nSize As Long) As Long Public Function UserNameApi() As String Dim strReturn As String, nSize As Long, lngRet As Long 'GetUserName fills the variable with a name and ends with a 0-char. strReturn = String(256, Chr(0)) nSize = Len(strReturn) lngRet = GetUserName(strReturn, nSize) 'will fail when nSize is too small If lngRet = 0 Then 'failure If nSize > 0 Then 'more data (ERROR_MORE_DATA) strReturn = String(nSize, Chr(0)) 'nSize contains necessary length lngRet = GetUserName(strReturn, nSize) End If End If 'If the variable contains a 0-char, the username is everything in front of it. nSize = InStr(1, strReturn, Chr(0)) If nSize > 0 Then UserNameApi = Left(strReturn, nSize - 1) Else UserNameApi = strReturn End If End Function