There is the “username” (loginname, usually the same as the name of the user’s home directory). And there is the “friendly” display name. In my case:
- username: juerg.otter
- friendly display name: Jürg Otter
System.EnvironmentVariable("USERNAME")
returns obviously: juerg.otter
The same goes for the GetUserNameW
Declare above - it expectedly returns: juerg.otter
What you are probably looking for is to get “extended information” about the user.
The following code should work (not fully tested; you could/should probably add error handling, …). You can shorten it - I’ve just included all comments/constants when you probably just want a single one
This will return “Jürg Otter” on my Windows machine (well, if I’m logged in as ‘juerg.otter’):
[code]Public Function GetUserNameEx() as String
#if TargetWindows then
if System.IsFunctionAvailable(“GetUserNameExW”, “Secur32”) then
Soft Declare Function GetUserNameExW Lib "Secur32" ( NameFormat As UInt8, lpNameBuffer As Ptr, ByRef nSize As UInt32 ) As Boolean
' The fully qualified distinguished name
' (for example, CN=Jeff Smith,OU=Users,DC=Engineering,DC=Microsoft,DC=Com).
const ExtendedNameFormat_FullyQualifiedDN = 1
' A legacy account name (for example, Engineering\\JSmith).
' The domain-only version includes trailing backslashes (\\\\).
const ExtendedNameFormat_SamCompatible = 2
' A "friendly" display name (for example, Jeff Smith).
' The display name is not necessarily the defining relative distinguished name (RDN).
const ExtendedNameFormat_Display = 3
' A GUID string that the IIDFromString function returns
' (for example, {4fa050f0-f561-11cf-bdd9-00aa003a77b6}).
const ExtendedNameFormat_UniqueId = 6
' The complete canonical name (for example, engineering.microsoft.com/software/someone).
' The domain-only version includes a trailing forward slash (/).
const ExtendedNameFormat_Canonical = 7
' The user principal name (for example, someone@example.com).
const ExtendedNameFormat_UserPrincipal = 8
' The same as NameCanonical except that the rightmost forward slash (/)
' is replaced with a new line character (\
), even in a domain-only case
’ (for example, engineering.microsoft.com/software
JSmith).
const ExtendedNameFormat_CanonicalEx = 9
' The generalized service principal name
' (for example, www/www.microsoft.com@microsoft.com).
const ExtendedNameFormat_ServicePrincipal = 10
' The DNS domain name followed by a backward-slash and the SAM user name.
const ExtendedNameFormat_DnsDomain = 12
Dim mbUserName As New MemoryBlock(1024)
Dim mbUserNameSize As UInt32 = mbUserName.Size
if GetUserNameExW(ExtendedNameFormat_Display, mbUserName, mbUserNameSize) then
return mbUserName.WString(0)
end if
end if
#endif
End Function[/code]