'Angemeldeten_Benutzer_ermitteln:
Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (Byval lpBuffer As String, nSize As Long) As Long
Declare Function GetLastError Lib "kernel32" () As Long
Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (Byval dwFlags As Long, lpSource As Any, Byval dwMessageId As Long, Byval dwLanguageId As Long, Byval lpBuffer As String, Byval nSize As Long, Arguments As Long) As Long
Sub Click(Source As Button)
Msgbox LNGetUserName
End Sub
Function LNGetUserName() As String
Dim RetUserName As Long
Dim lpUserName As String
Dim nSize As Long
Dim RetLastError As Long
Dim RetVal As Long
Dim lpMessageBuffer As String
Dim varia As String
lpUserName = Space(255)
nSize = Len(lpUserName)
RetUserName = GetUserName(lpUserName, nSize)
If RetUserName <> 0 Then 'UserName ausgeben.
varia = Left(lpUserName, nSize - 1)
Else 'Username nicht verfügbar.
RetLastError = GetLastError()
lpMessageBuffer = Space(255)
nSize = Len(lpMessageBuffer)
RetVal = FormatMessage(0, 0, RetLastError, 0, lpMessageBuffer, nSize, 0)
If RetVal <> 0 Then
varia = "Fehler " & RetLastError & ": " & lpMessageBuffer
Else
varia = "Fehler " & RetLastError & ": kein angemeldeter Benutzer"
End If
End If
LNGetUserName= Trim(varia)
End Function