Get XP Username
BlitzMax Forums/BlitzMax Programming/Get XP Username
| ||
Is it possible or is there a command available to find out the name of current user that is logged into Windows XP and running your BlitzMax App? |
| ||
this is a start http://blitzbasic.com/Community/posts.php?topic=48299 youll have to port it though.... |
| ||
This should do it...' Function stuff... Global GetUserName (buff:Byte Ptr, size:Byte Ptr) adv32 = LoadLibraryA ("advapi32.dll") If Not adv32 Notify "Can't open advapi32.dll!"; End GetUserName = GetProcAddress (adv32, "GetUserNameA") If Not GetUserName Notify "Can't find GetUserNameA function!"; End ' Test... Local name:Byte [256] Local size = 256 If GetUserName (name, Varptr (size)) Print StringFromArray (name) EndIf ' Conversion to/from C strings... Function ArrayFromString:Byte [] (source$) Local newarray:Byte [Len (source$)] MemCopy (newarray, source.ToCString (), Len (source$)) Return newarray End Function Function StringFromArray$ (source:Byte []) Return String.FromCString (source) End Function |
| ||
Or a one-liner:Print getenv_("username") Windows automatically keeps a bunch of environment variables that can be read by any program. This includes username as mentioned above, but also other useful information such as: - installation directory for windows ("windir") - your personal directory ("userprofile") - the location of your program file directory ("programfiles") and a bunch of other info. To see the full list, open a DOS command prompt (cmd.exe), and simply type: set |
| ||
Got one that's cross-platform? |
| ||
Got one that's cross-platform? name$=Input("What's your name?") Sorry... couldn't resist :) |
| ||
Cross platform... system_ "who am i" This will print the username under unix/Mac os.Parse out the other info.To ensure cross platform ability a comparison should do the job: |
| ||
@all thank you very much, I tried the easiest solution Print getenv_("username") and it works fine for me. If the getenv_("username") commands results an empty string I will prompt the user name via a requester. I hope this will keep my source cross platfrom compatible. |