|
Post by angelo2449 on Jan 4, 2023 11:02:01 GMT -5
Hi guys,
I have to extract the PC User Name, has anyone solved this problem yet?
Thank you very much!
|
|
|
Post by xxgeek on Jan 4, 2023 13:03:26 GMT -5
One way is to use cmd with the windows env var %username% Save this to file before executing. A list of other environment variables you may need in future www.tenforums.com/tutorials/3234-environment-variables-windows-10-a.html call getUserName end
sub getUserName run "cmd.exe /c echo %USERNAME% >username.txt", HIDE do scan loop until fileExists(DefaultDir$, "username.txt") open "username.txt" for input as #1 username$ = input$(#1, lof(#1)) close #1 if username$ = "" then notice "Sorry, can't find username " : end print username$ kill DefaultDir$;"\username.txt" end sub
'function for checking file existence function fileExists(path$, filename$) dim info$(0, 0) files path$, filename$, info$() fileExists = val(info$(0, 0)) 'non zero is true end function
|
|
|
Post by metro on Jan 4, 2023 19:25:09 GMT -5
This may be of use
'Demo of API call to get Environment Variable 'Placed into the Public domain on 10/15/04 'Original author: Brad Moore 'Created using Liberty Basic Workshop - thanks Alyce! 'Check out Liberty Basic at http://www.libertybasic.com 'create the array that will contain the values for the combobox DIM Combo1$(10) a$ = "UserName UserProfile ProgramFiles windir OS ALLUSERSPROFILE COMPUTERNAME" for x = 1 to 7 Combo1$(x) = word$(a$, x)
next x 'Setup the window NOMAINWIN WindowWidth = 529 : WindowHeight = 166 'we want it centered UpperLeftX = INT((DisplayWidth-WindowWidth)/2) UpperLeftY = INT((DisplayHeight-WindowHeight)/2) 'setup the controls groupbox #main.group1, "Current Value", 20, 50, 480, 60 statictext #main.st1, "", 35, 75, 440, 25 statictext #main.st2, "Choose an environment variable:", 25, 20, 205, 20 button #main.go, "Go!",[go],UL, 450, 20, 45, 25 combobox #main.combo,Combo1$(),[loop], 240, 20, 200, 300 Open "Environment Variables" for Window as #main #main "trapclose [quit]" #main.combo "selectindex 1" #main "font ms_sans_serif 11" #main.combo "font ms_sans_serif 11" [loop] Wait [quit] close #main : END [go] #main.combo "selection? selected$" if selected$ <> "" then #main.st1 GetEnvVar$(selected$) end if wait function GetEnvVar$(name$) 'calls the api GetEnvironmentVariable to get the names environment variable lbuf$ = space$(255) + chr$(0) name$ = name$ + chr$(0) calldll #kernel32, "GetEnvironmentVariableA", name$ as ptr, _ lbuf$ as ptr, _ 256 as long, _ result as long if result = 0 then GetEnvVar$ = "Environment Variable Not Found" else GetEnvVar$ = left$(lbuf$,result) end if end function
|
|
|
Post by Brandon Parker on Jan 4, 2023 21:54:50 GMT -5
For completeness sake, you can get the current user's name using this function. You can also change what is returned if the function fails to return successfully.
'Create the Generic Size Struct Struct Size, value As long
Print CurrentUserName$() Wait
Function CurrentUserName$() lpBuffer$ = Space$(_MAX_PATH) + chr$(0) Size.value.struct = Len(lpBuffer$)
Open "advapi32.dll" For DLL As #ADVAPI32 CallDLL #ADVAPI32, "GetUserNameA", lpBuffer$ As ptr, _ Size As struct, _ result As long Close #ADVAPI32
If result Then CurrentUserName$ = Trim$(Left$(lpBuffer$, Size.value.struct)) Else CurrentUserName$ = "UNKNOWN USER" End If End Function
{:0)
Brandon Parker
|
|
|
Post by metro on Jan 4, 2023 22:04:19 GMT -5
G'Day Brandon, just curious, I also have that code in my database, what is the advantage of using the advapi32.dll over kernel32
metro (API novice)
|
|
|
Post by Brandon Parker on Jan 5, 2023 22:06:52 GMT -5
There are two main reasons for using GetUserNameA over GetEnvironmentVariableA:
1. The user can change the value of environment variables very easily. 2. The user can delete environment variables easily as well.
Another issue that should not plague too many people these days is that the environment variable that holds the username was not always initially set on Windows version prior to XP...I believe...
The GetUserNameA function is the most reliable method for retrieving the current user name IMO.
Either method is perfectly acceptable. There is typically more than one way to do one thing in Windows using the vast API.
{:0)
Brandon Parker
|
|
|
Post by metro on Jan 6, 2023 0:37:18 GMT -5
Either method is perfectly acceptable. There is typically more than one way to do one thing in Windows using the vast API. {:0) Brandon Parker Thanks Brandon, I have found the API universe to be vast and somewhat confusing. need more time to study it.
Who'd a thought I get less free time now I'm retired.
once again, I appreciate your input
|
|