Jump to content
Sign in to follow this  
donhorn20

Update Environment Variables Based Off Of Combo Box Selection

Recommended Posts

donhorn20

Hi Guys,

I am hoping someone may be able to help me here. I have 3 individual scripts that I would like to combine into one by using a drop down list to execute each script. I would like to make a GUI that has a drop down list so that when the value in the drop down list is selected it will apply the proper function.

The background behind what I am trying to do has to do with updating my environment variables. Each selection from the drop down list is going to update 2 environment variables. It's always the same set of variables being updated just with different data depending on the selection.

Here is an example of the three scripts combined, I just don't know how to place into a proper drop down so that when one is selected it will execute the proper script. Any help would be greatly appriciated.

_1("HTTPSERVER", "test1.com","SYSCFG", "c:\test\test1.txt", 1, 0)
_2("HTTPSERVER", "test2.com","SYSCFG", "c:\test\test2.txt", 1, 0)
_3("HTTPSERVER", "test3.com","SYSCFG", "c:\test\test3.txt", 1, 0)

Func _1($HTTPSERVER, $Value1, $SYSCFG, $Value2, $Replace=1, $KeyVal=1)
If $HTTPSERVER = "" Then Return SetError(1)
If $Value1 = "" Then Return SetError(2)
If $SYSCFG = "" Then Return SetError(1)
If $Value2 = "" Then Return SetError(2)
If $Replace <> 0 And $Replace <> 1 Then Return SetError(3)
Local $ReadEnv
Local $SystemRegKey = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
Local $UserRegKey = "HKEY_CURRENT_USER\Environment"
Local $RegKey = $SystemRegKey
If $KeyVal = 1 Then $RegKey = $UserRegKey
If $Replace = 1 Then
     RegWrite($RegKey, $HTTPSERVER, "REG_SZ", $Value1)
RegWrite($RegKey, $SYSCFG, "REG_SZ", $Value2)
Else
     If RegRead($RegKey, $HTTPSERVER) = "" Then RegWrite($RegKey, $HTTPSERVER, "REG_SZ", $Value1)
If RegRead($RegKey, $SYSCFG) = "" Then RegWrite($RegKey, $SYSCFG, "REG_SZ", $Value2)
EndIf
EndFunc

Func _2($HTTPSERVER, $Value1, $SYSCFG, $Value2, $Replace=1, $KeyVal=1)
If $HTTPSERVER = "" Then Return SetError(1)
If $Value1 = "" Then Return SetError(2)
If $SYSCFG = "" Then Return SetError(1)
If $Value2 = "" Then Return SetError(2)
If $Replace <> 0 And $Replace <> 1 Then Return SetError(3)
Local $ReadEnv
Local $SystemRegKey = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
Local $UserRegKey = "HKEY_CURRENT_USER\Environment"
Local $RegKey = $SystemRegKey
If $KeyVal = 1 Then $RegKey = $UserRegKey
If $Replace = 1 Then
RegWrite($RegKey, $HTTPSERVER, "REG_SZ", $Value1)
RegWrite($RegKey, $SYSCFG, "REG_SZ", $Value2)
Else
If RegRead($RegKey, $HTTPSERVER) = "" Then RegWrite($RegKey, $HTTPSERVER, "REG_SZ", $Value1)
If RegRead($RegKey, $SYSCFG) = "" Then RegWrite($RegKey, $SYSCFG, "REG_SZ", $Value2)
EndIf
EndFunc

Func _3($HTTPSERVER, $Value1, $SYSCFG, $Value2, $Replace=1, $KeyVal=1)
If $HTTPSERVER = "" Then Return SetError(1)
If $Value1 = "" Then Return SetError(2)
If $SYSCFG = "" Then Return SetError(1)
If $Value2 = "" Then Return SetError(2)
If $Replace <> 0 And $Replace <> 1 Then Return SetError(3)
Local $ReadEnv
Local $SystemRegKey = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
Local $UserRegKey = "HKEY_CURRENT_USER\Environment"
Local $RegKey = $SystemRegKey
If $KeyVal = 1 Then $RegKey = $UserRegKey
If $Replace = 1 Then
RegWrite($RegKey, $HTTPSERVER, "REG_SZ", $Value1)
RegWrite($RegKey, $SYSCFG, "REG_SZ", $Value2)
Else
If RegRead($RegKey, $HTTPSERVER) = "" Then RegWrite($RegKey, $HTTPSERVER, "REG_SZ", $Value1)
If RegRead($RegKey, $SYSCFG) = "" Then RegWrite($RegKey, $SYSCFG, "REG_SZ", $Value2)
EndIf
EndFunc

Share this post


Link to post
Share on other sites
donhorn20

Ok, I got a working script. I was looking through some other forums and borrowed pieces from similar scripts and applied it to mine. Thanks to all the samples that are out there, it really helped. If I come across the posts that I got the code from I will be sure to include you in the credits.

Now that I have it working I was wondering if anyone had any ideas to make code look cleaner. I believe there is a way to not have all the various functions but not quite sure. This list is going to grow and the fact that the only two things changing amongst the functions is $Value1 and $Value2. Any input would be greatly appreciated. Here is the current code I have now. It will simply write two test environment variables to your system when selected from the drop down. Was even thinking about adding some code to remove these variables when done but haven't gotten there yet. Any suggestions are welcome.

#include 

Opt("GUIOnEventMode", 1)

$GUI = GUICreate("Environment Variable Change", 300, 200)
$COMBO = GUICtrlCreateCombo("", 70, 30, 150)
GUICtrlSetOnEvent($COMBO, "_Events")
GUISetOnEvent($GUI_EVENT_CLOSE, "_Events")
GUISetState()

;Declare the array that hold the controlID and the function to execute:
Global $aValues[6][2] = [["", ""], _
["Test 1", "_T1()"], _
["Test 2", "_T2()"], _
["Test 3", "_T3()"], _
["Test 4", "_T4()"], _
["Test 5", "_T5()"]]

;Fill the combo with the values:
Global $sComboValues

;Global Environment variables
Global $HTTPSERVER = "TEST_HTTPSERVER"
Global $CFG = "TEST_CFG"
Global $Replace = 1
Global $KeyVal = 0

For $i = 0 To UBound($aValues) -1
$sComboValues &= $aValues[$i][0] & "|"
Next
GUICtrlSetData($COMBO, $sComboValues, $aValues[0][0])

While 1
Sleep(250)
WEnd

Func _Events()
Switch @GUI_CtrlId
Case $GUI_EVENT_CLOSE
Exit

Case $COMBO
;read the value in the combo:
Local $ValueSelected = GUICtrlRead($COMBO)

;Search the value in the array:
For $i = 0 To UBound($aValues) -1
If $aValues[$i][0] = $ValueSelected Then
;Found then execute the function
Return Execute($aValues[$i][1])
EndIf
Next
EndSwitch
EndFunc

Func _T1()
Local $SystemRegKey = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
Local $UserRegKey = "HKEY_CURRENT_USER\Environment"
Local $RegKey = $SystemRegKey
Local $Value1 = "test1"
Local $Value2 = "c:\test1.txt"

If $KeyVal = 1 Then $RegKey = $UserRegKey

If $Replace = 1 Then
RegWrite($RegKey, $HTTPSERVER, "REG_SZ", $Value1)
RegWrite($RegKey, $CFG, "REG_SZ", $Value2)
Else
If RegRead($RegKey, $HTTPSERVER) = "" Then RegWrite($RegKey, $HTTPSERVER, "REG_SZ", $Value1)
If RegRead($RegKey, $CFG) = "" Then RegWrite($RegKey, $CFG, "REG_SZ", $Value2)
EndIf
MsgBox(0, "Environment", "Environment Variables for T1 applied")
EndFunc

Func _T2()
Local $SystemRegKey = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
Local $UserRegKey = "HKEY_CURRENT_USER\Environment"
Local $RegKey = $SystemRegKey
Local $Value1 = "test2"
Local $Value2 = "c:\test2.txt"

If $KeyVal = 1 Then $RegKey = $UserRegKey

If $Replace = 1 Then
RegWrite($RegKey, $HTTPSERVER, "REG_SZ", $Value1)
RegWrite($RegKey, $CFG, "REG_SZ", $Value2)
Else
If RegRead($RegKey, $HTTPSERVER) = "" Then RegWrite($RegKey, $HTTPSERVER, "REG_SZ", $Value1)
If RegRead($RegKey, $CFG) = "" Then RegWrite($RegKey, $CFG, "REG_SZ", $Value2)
EndIf
MsgBox(0, "Environment", "Environment Variables for T2 applied")
EndFunc

Func _T3()
Local $SystemRegKey = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
Local $UserRegKey = "HKEY_CURRENT_USER\Environment"
Local $RegKey = $SystemRegKey
Local $Value1 = "test3"
Local $Value2 = "c:\test3.txt"

If $KeyVal = 1 Then $RegKey = $UserRegKey

If $Replace = 1 Then
RegWrite($RegKey, $HTTPSERVER, "REG_SZ", $Value1)
RegWrite($RegKey, $CFG, "REG_SZ", $Value2)
Else
If RegRead($RegKey, $HTTPSERVER) = "" Then RegWrite($RegKey, $HTTPSERVER, "REG_SZ", $Value1)
If RegRead($RegKey, $CFG) = "" Then RegWrite($RegKey, $CFG, "REG_SZ", $Value2)
EndIf
MsgBox(0, "Environment", "Environment Variables for T3 applied")
EndFunc

Func _T4()
Local $SystemRegKey = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
Local $UserRegKey = "HKEY_CURRENT_USER\Environment"
Local $RegKey = $SystemRegKey
Local $Value1 = "test4"
Local $Value2 = "c:\test4.txt"

If $KeyVal = 1 Then $RegKey = $UserRegKey

If $Replace = 1 Then
RegWrite($RegKey, $HTTPSERVER, "REG_SZ", $Value1)
RegWrite($RegKey, $CFG, "REG_SZ", $Value2)
Else
If RegRead($RegKey, $HTTPSERVER) = "" Then RegWrite($RegKey, $HTTPSERVER, "REG_SZ", $Value1)
If RegRead($RegKey, $CFG) = "" Then RegWrite($RegKey, $CFG, "REG_SZ", $Value2)
EndIf
MsgBox(0, "Environment", "Environment Variables for T4 applied")
EndFunc

Func _T5()
Local $SystemRegKey = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
Local $UserRegKey = "HKEY_CURRENT_USER\Environment"
Local $RegKey = $SystemRegKey
Local $Value1 = "test5"
Local $Value2 = "c:\test5.txt"

If $KeyVal = 1 Then $RegKey = $UserRegKey

If $Replace = 1 Then
RegWrite($RegKey, $HTTPSERVER, "REG_SZ", $Value1)
RegWrite($RegKey, $CFG, "REG_SZ", $Value2)
Else
If RegRead($RegKey, $HTTPSERVER) = "" Then RegWrite($RegKey, $HTTPSERVER, "REG_SZ", $Value1)
If RegRead($RegKey, $CFG) = "" Then RegWrite($RegKey, $CFG, "REG_SZ", $Value2)
EndIf
MsgBox(0, "Environment", "Environment Variables for T5 applied")
EndFunc

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×