Imports System.Runtime.InteropServices
Module MainModule
Function Main() As Int32
Dim n As Int32 = 255
Dim x As String = New String(" ", n)
Dim ret As Int32 = 0
ret = GetUserNameEx(EXTENDED_NAME_FORMAT.NameSamCompatible, x, n)
If (ret = 1) Then
Console.WriteLine("User: {0}", x)
Else
Console.WriteLine("GetUserNameEx failed.")
End If
End Function
Private Enum EXTENDED_NAME_FORMAT
NameUnknown = 0
NameFullyQualifiedDN = 1
NameSamCompatible = 2
NameDisplay = 3
NameUniqueId = 6
NameCanonical = 7
NameUserPrincipal = 8
NameCanonicalEx = 9
NameServicePrincipal = 10
NameDnsDomain = 12
End Enum
Private Declare Function GetUserNameEx Lib "secur32.dll" _
Alias "GetUserNameExA" (ByVal NameFormat As EXTENDED_NAME_FORMAT, _
ByVal NameBuffer As String, _
ByRef nSize As Int32) As Integer
End Module This was first published in January 2004