Environment_Variables

toc

Listing all
Below find a simple GUI to get the Windows environment variables with Just BASIC.

The example uses the //SET// DOS-command to get all environment variables and lists them in a list-box, if you select a variable its value will be displayed on the right side of the list-box.


 * **Windows 9x/ME** users: The SET command does not support displaying single variables, therefore the lower part of the GUI window is empty.
 * **Windows NT/2000/XP/Vista/7** users: The SET command can display the value of a single variable or a range of variables starting with the entered letter or letters. You will also notice the difference between the Command.COM and Cmd.EXE settings of some variables, this is due to the support of long file names.

The heart of the example is the **GetEnvVars** function, which runs the command prompt to create a file containing the current environment variable settings. The created file is parsed to generate a single string containing the variable names and values. The format of the string is **{Name.1}0{Value.1}00{Name.2}0{Value.2}00...{Name.n}0{Value.n}00**, each zero represents chr$(0). The function returns the number of variables found. The string is returned in the second argument, which is submitted by reference. The first argument to the function is the variable name or starting letter(s) (available only to Windows NT/2000/XP/Vista/7 users), ALL signals the function to return all variables found (the only value available to Windows 9x/ME users).

If you use the function in your own projects, make sure to **include** the **GetEnvVars, GetOSversion and FileExists** functions and set the **OS$** variable as shown in the example.

code format="lb" 'get environment variables from command prompt

'initialize the single variable check array dim SingleEnvVar$(1)

'global variable to check if running on Windows 9x/ME global OS$

'get all environment variables NumVarsFound = GetEnvVars("", EnvVarsArray$) dim EnvVars$(NumVarsFound) for index = 1 to NumVarsFound EnvVars$(index) = word$(EnvVarsArray$, index, chr$(0)+chr$(0))

if word$(EnvVars$(index), 1, chr$(0)) = "OS" then OS$ = word$(EnvVars$(index), 2, chr$(0)) end if   next

'start GUI nomainwin WindowWidth = 515 WindowHeight = 340 UpperLeftX=int((DisplayWidth-WindowWidth)/2) UpperLeftY=int((DisplayHeight-WindowHeight)/2)

listbox   #main.vars,  EnvVars$(, [DisplayValue],                5,   7, 230, 200    statictext #main.hint,  "Select Variable to Display its Value", 245,   7, 260,  20    statictext #main.hint1, "(command.com)",                        245,  27, 260,  20    statictext #main.value, "Nothing selected ...",                 245,  77, 260, 130

'Windows versions newer than 9x/ME can check for single variables if OS$ = "Windows_NT" then groupbox  #main.check,       "Check Single Variable (cmd.exe)",   5, 217, 495,  80 statictext #main.namehint,   "Name:",                            15, 239,  40,  20 combobox  #main.varname,     SingleEnvVar$(,[DisplaySingle],     65, 237, 320, 100        button     #main.default,     "Get Value",[GetVarValue], UL,     395, 237,  95,  25        statictext #main.valuehint,   "Value:",                           15, 272,  40,  20        statictext #main.singlevalue, "Nothing to check ...",             65, 272, 425,  20    end if

open "Get Environment Variables" for dialog as #main #main "trapclose [quit]" #main "font ms_sans_serif 10"

#main.vars "singleclickselect" wait

[DisplayValue] 'display value of selected variable (all variables) #main.vars "selectionindex? index" #main.value word$(EnvVars$(index), 2, chr$(0)) wait

[DisplaySingle] 'display value of selected variable (single variable) #main.varname "selectionindex? index" #main.singlevalue word$(SingleEnvVar$(index), 2, chr$(0)) wait

[GetVarValue] 'get value of single variable 'or variables beginning with the entered letter(s) #main.varname "contents? VarName$"

#main.default "!disable"

if instr(VarName$, " ") > 0 then #main.singlevalue "Invalid Variable Name" else VarsFound = GetEnvVars(VarName$, VarValue$)

if VarsFound then redim SingleEnvVar$(VarsFound)

for index = 1 to VarsFound SingleEnvVar$(index) = word$(VarValue$, index, chr$(0)+chr$(0)) next

#main.varname "reload" #main.varname "selectindex 1" #main.singlevalue word$(SingleEnvVar$(1), 2, chr$(0)) else #main.singlevalue "Variable not found ..." end if   end if

#main.default "!enable" wait

[quit] 'program exit close #main end

function GetEnvVars(VariableName$, byref Array$) 'VarableName$ ... name of variable to check '                or no name for all available variables 'Array$ ......... on return contains the variable name and value '                in the format: VarName.1; chr$(0); VarValue.1; chr$(0); chr$(0); _ '                               ...    '                                VarName.n; chr$(0); VarValue.n; chr$(0); chr$(0); ' returns the number of variables found

Array$ = ""

if OS$ = "Windows_NT" or GetOSversion >= 5 then ComSpec$ = "cmd.exe" else ComSpec$ = "command.com" VariableName$ = "" end if

run ComSpec$; " /c set "; VariableName$; " >output.tmp", hide

timer 1000, [continue] wait [continue] timer 0

if FileExists("output.tmp") then open "output.tmp" for input as #result while not(eof(#result)) line input #result, contents$ VarName$ = word$(contents$, 1, "=") VarValue$ = word$(contents$, 2, "=")

if VarName$ <> "" then Array$ = Array$; VarName$; chr$(0); VarValue$; chr$(0); chr$(0) GetEnvVars = GetEnvVars + 1 end if       wend close #result

kill "output.tmp" end if end function

function GetOSversion ' returns the operating system version number

Numbers$ = "1234567890"

open "output_ver.bat" for output as #bat #bat "ver.exe >output_ver.tmp" close #bat

if FileExists("output_ver.bat") then run "output_ver.bat", hide

timer 1000, [continue] wait [continue] timer 0 if FileExists("output_ver.bat") then kill "output_ver.bat"

if FileExists("output_ver.tmp") then open "output_ver.tmp" for input as #result while not(eof(#result)) line input #result, contents$

if len(contents$) > 7 then exit while wend close #result

kill "output_ver.tmp" end if

EndPos = instr(contents$,"]") StartPos = EndPos

for i = 1 to len(Numbers$) Pos = instr(contents$, mid$(Numbers$, i, 1)) if Pos > 0 and StartPos > Pos then StartPos = Pos next

VersionString$ = mid$(contents$, StartPos, EndPos-StartPos) MajorMinor$ = word$(VersionString$, 1, ".") + "." + word$(VersionString$, 2, ".")

GetOSversion = val(MajorMinor$) end function

function FileExists(FullPath$) ' checks for the existence of the given file ' returns true(any number) or false(0) dim FileExistsInfo$(1,1)

files "", FullPath$, FileExistsInfo$

FileExists = val(FileExistsInfo$(0,0)) end function code

Retrieving only one
code format="lb" VariableName$ = "APPDATA"

print VariableName$; " ... "; GetSingleEnvVar$(VariableName$) end

function GetSingleEnvVar$(VarName$) open "output.bat" for output as #bat #bat "echo %"; VarName$; "% >output.tmp" close #bat

if FileExists("output.bat") then run "output.bat", hide

timer 1000, [continue] wait [continue] timer 0 if FileExists("output.bat") then kill "output.bat"

if FileExists("output.tmp") then open "output.tmp" for input as #result while not(eof(#result)) line input #result, GetSingleEnvVar$ wend close #result

kill "output.tmp" end if end function

function FileExists(FullPath$) ' checks for the existence of the given file ' returns true(any number) or false(0) dim FileExistsInfo$(1,1)

files "", FullPath$, FileExistsInfo$

FileExists = val(FileExistsInfo$(0,0)) end function code