How to find Windows user name

It has taken me a lot of time to find a way to get Windows user name.
I think there is GetUserName API in VB. Is there any way to get it?

Thanks in advance.

Code from this link was used for the answer: Xojo Forums - how to determine current user

[code]Dim Registered_NAME 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 )
  Registered_NAME = mb.WString( 0 )
  GetUserNameA( mb, size )
  Registered_NAME = mb.CString( 0 )
end if


MsgBox Registered_NAME[/code]

Great. It works well.

Thank you so much.

And if you are using MBS Plugins, it’s jiust:

strRegisteredNAME  = SystemInformationMBS.UserName

Sorry for the stupid question:

what if the user name looks like some we can read on the Internet ?
Say Rocket88 or Toto232 or… (your guess is as good as mine)

Of course, it is the user’s responsibility to set a correct user name, but this is a valid question and show a limit.

I don’t think anyone needs GetUserNameA any more. You can go to GetUserNameW always.

At least I do that for SystemInformationMBS.UserName for Windows.

Is this possible in Xojo? I used it in Real Studio 2012 .

Dim f as FolderItem = SpecialFolder.UserHome
Dim userName as String = f.Name

that will most likely not get the right name.

There is a short ascii name often for folder and a full name for display.