Getting user's full username

Hello,
I know next to nothing of Windows’ environment.
By full username I mean Carlo Rubini (opposite to carlorubini).

At present I have two ways to get it, one by simply calling System.EnvironmentVariable(“USERNAME”)

The second one, by declares taken from WFS. It works, but since it is pretty old and I’ll be addressing Windows 10 only, I’d like an advice whether I should use it or look for a more up-to-date code if ever exists.

Eventually, since my PC’s usename consists of a single word (Carlo), I wonder what I’d get if the username consisted of two words. Would the two codes return “Carlo Rubini” or carlorubini?
(Sorry, but I don’t know how to change the username in a PC; if I knew I’d not have made any noise).

No plugins, please. Thank you for any assistance.

[code]Protected Function GetLoggedInUserName() as String
#if TargetWin32
Soft Declare Sub GetUserNameA Lib “AdvApi32” ( name as Ptr, ByRef size as Integer )
Soft Declare Sub GetUserNameW Lib “AdvApi32” ( name as Ptr, ByRef size as Integer )

dim mb as new MemoryBlock( 256 )
dim size as Integer = mb.Size()

if System.IsFunctionAvailable( "GetUserNameW", "AdvApi32" ) then
  GetUserNameW( mb, size )
  
  return mb.WString( 0 )
else
  GetUserNameA( mb, size )
  
  return mb.CString( 0 )
end if

#endif

End Function
[/code]

SystemInformationMBS module also provides a UserName function if you use MBS Plugin.

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 :slight_smile:

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]

Hi,
thank you for answering.

Strange, but on my Windows 10 machine only this one works, returning “Home…\Carlo”

All the other constants return “”
Tested with 19r1.1 and 19r2

’ A legacy account name (for example, Engineering\JSmith).
’ The domain-only version includes trailing backslashes (\\).
const ExtendedNameFormat_SamCompatible = 2

You have be logged on with a domain account (i.e. not a local account) for the other ones.

@Andrew Lambert [quote]You have be logged on with a domain account (i.e. not a local account) for the other ones.[/quote]

Sorry if I’m not very smart; as I said, my experience on a Windows PC is very limited.

So, here is it what I do: I turn on the PC and since there isn’t any password I get the Welcome Carlo. And that’s all.
After that I open the app.

As for the modifications to the original code, I just encapsulated all the constants in order for the machine to pick up the first that returns a value. Like this:

dim result as string

const ExtendedNameFormat_FullyQualifiedDN = 1 Dim mbUserName As New MemoryBlock(1024) Dim mbUserNameSize As UInt32 = mbUserName.Size if GetUserNameExW(ExtendedNameFormat_FullyQualifiedDN, mbUserName, mbUserNameSize) then result = mbUserName.WString(0) end if if result <> "" then Return result else const ExtendedNameFormat_SamCompatible = 2