Jump to content

Windows Environment Variables


jaja714
 Share

Recommended Posts

1.  I am running C:\Windows\Temp\1.AU3 (below) from SciTE to define a user-level Windows environment variable.

RegWrite('HKEY_CURRENT_USER\Environment', 'MENU_HOME', 'REG_SZ', 'C:\Windows\Temp')
EnvUpdate()

2.  When I check Windows System Properties=>Environment Variables, MENU_HOME is there in the list of user variables.

3.  When I run C:\Windows\Temp\2.BAT (below) from directly from Windows, the MENU_HOME environment variable appears in the SET listing (line 2) and then again in the explicit ECHO %MENU_HOME% (line 4).

@ECHO OFF
SET
ECHO.
ECHO MENU_HOME: %MENU_HOME%
PAUSE

4.  When I run C:\Windows\Temp\3.AU3 (below) from SciTE , the MENU_HOME environment variable DOES NOT appear in the SET listing or in the ECHO %MENU_HOME%.  However, other variables present in the the list of user variables from Step 2 (above) still appear in the SET listing!

$iPID = Run(@ComSpec & ' /k' & Chr(34) & 'C:\Windows\Temp\TWO.BAT' & Chr(34), 'C:\Windows\Temp\')

 

Am I missing something?  Is something wrong with my RegWrite key?

Link to comment
Share on other sites

Don't believe you would get the SET results from CMD in the same session that the variable is added/updated.  If you run your batch file manually afterwards it will show the correct results though.  Normally I use the following to set and check environment variables.

;~ #RequireAdmin ;~ Required when writing to System Environment Variable

; #VARIABLES# ===================================================================================================================
Global $MAX_VALUE_NAME = 1024
Global $HWND_BROADCAST = 0xffff
Global $WM_SETTINGCHANGE = 0x001A
Global $SMTO_ABORTIFHUNG = 0x0002
Global $SMTO_NORMAL = 0x0000
Global $MSG_TIMEOUT = 5000

;~ ============================== Begin Example ===============================
    _EnvVarSet("MENU_HOME", "C:\Windows\Temp", 3)
    ConsoleWrite(_EnvVarGet("MENU_HOME") & @CRLF)
;~ =============================== End Example ================================

; #FUNCTION# ====================================================================================================================
; Name ..........: _EnvVarGet
; Description ...: Get Environment Variables
; Syntax ........: _EnvVarGet($_sEnvVarGet[, $_iEnvVarType = 3[, $_bExpVarValue = False]])
; Parameters ....: $_sEnvVarGet         - Environment variable name
;                : $_iEnvVarType        - [optional] Environment variable type. Default is 3.
;                :  - 0 - Returns Enviroment variable from all profile types
;                :  - 1 - Process Enviornment Variable
;                :  - 2 - System Enviornment Variable
;                :  - 3 - User Enviornment Variable
;                :  - 4 - Volatile Enviornment Variable
;                : $_bExpVarValue       - [optional] Expand Environment Variables within result. Default is False.
; Return values .:
;                : Flag: $_iEnvVarType
;                :  - 0 - Returns Enviroment variable from all profile types as an array
;                :  - 1 - Process Enviornment Variable as a string
;                :  - 2 - System Enviornment Variable as a string
;                :  - 3 - User Enviornment Variable as a string
;                :  - 4 - Volatile Enviornment Variable as a string
; Author ........: Subz
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _EnvVarGet($_sEnvVarGet, $_iEnvVarType = 3, $_bExpVarValue = False)
    Local $iExpandEnvStrings = Opt("ExpandEnvStrings")
    Local $sEnvVarValue, $aEnvVarValue[5] = [4]
    Local $oEnvVarType, $aEnvVarType[5] = [4, "PROCESS", "SYSTEM", "USER", "VOLATILE"]
    Local $oWshShell = ObjCreate("WScript.Shell")
    Switch $_iEnvVarType
        Case 0
            If $_bExpVarValue == True Then Opt("ExpandEnvStrings", 1)
            For $i = 1 To $aEnvVarType[0]
                $oEnvVarType = $oWshShell.Environment($aEnvVarType[$i])
                $aEnvVarValue[$i] = $oEnvVarType($_sEnvVarGet)
            Next
            Opt("ExpandEnvStrings", $iExpandEnvStrings)
            Return $aEnvVarValue
        Case 1 To 4
            If $_bExpVarValue == True Then Opt("ExpandEnvStrings", 1)
            $oEnvVarType = $oWshShell.Environment($aEnvVarType[$_iEnvVarType])
            $sEnvVarValue = $oEnvVarType($_sEnvVarGet)
            Opt("ExpandEnvStrings", $iExpandEnvStrings)
            Return $sEnvVarValue
        Case Else
            Return SetError(1, 0, "")
    EndSwitch
EndFunc

; #FUNCTION# ====================================================================================================================
; Name ..........: _EnvVarSet
; Description ...: Set Environment Variables
; Syntax ........: _EnvVarSet($_sEnvVarName, $_sEnvVarValue, [, $_iEnvVarType = 3])
; Parameters ....: $_sEnvVarName         - Environment variable name.
;                : $_iEnvVarValue        - Environment variable value.
;                : $_iEnvVarType        - [optional] Environment variable type. Default is 3.
;                :  - 0 - Set Enviroment variable for all profile types
;                :  - 1 - Set Process Enviornment Variable
;                :  - 2 - Set System Enviornment Variable
;                :  - 3 - Set User Enviornment Variable
;                :  - 4 - Set Volatile Enviornment Variable
; Return values .: 0 - Success
;                : 1 - Failed
; Author ........: Subz
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _EnvVarSet($_sEnvVarName, $_sEnvVarValue, $_iEnvVarType = 3)
    Local $oEnvVarType, $aEnvVarType[5] = [4, "PROCESS", "SYSTEM", "USER", "VOLATILE"]
    Local $oWshShell = ObjCreate("WScript.Shell")
    Switch $_iEnvVarType
        Case 0
            For $i = 1 To $aEnvVarType[0]
                $oEnvVarType = $oWshShell.Environment($aEnvVarType[$i])
                $oEnvVarType($_sEnvVarName) = $_sEnvVarValue
            Next
        Case 1 To 4
            $oEnvVarType = $oWshShell.Environment($aEnvVarType[$_iEnvVarType])
             $oEnvVarType($_sEnvVarName) = $_sEnvVarValue
        Case Else
            Return SetError(1)
    EndSwitch
    _EnvVarRefresh()
    If @error Then Return SetError(1)
    SetError(0)
EndFunc

; #FUNCTION# ====================================================================================================================
; Name ..........: _EnvVarDelete
; Description ...: Delete Environment Variables
; Syntax ........: _EnvVarDelete($_sEnvVarName, [$_iEnvVarType = 3])
; Parameters ....: $_sEnvVarName         - Environment variable name to delete
;                : $_iEnvVarType        - [optional] Environment variable type. Default is 3.
;                :  - 0 - Returns Enviroment variable from all profile types
;                :  - 1 - Process Enviornment Variable
;                :  - 2 - System Enviornment Variable
;                :  - 3 - User Enviornment Variable
;                :  - 4 - Volatile Enviornment Variable
; Return values .: 0 - Success
;                : 1 - Failed
; Author ........: Subz
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _EnvVarDelete($_sEnvVarName, $_iEnvVarType = 3)
    Local $oEnvVarType, $aEnvVarType[5] = [4, "PROCESS", "SYSTEM", "USER", "VOLATILE"]
    Local $oWshShell = ObjCreate("WScript.Shell")
    Switch $_iEnvVarType
        Case 0
            For $i = 1 To $aEnvVarType[0]
                $oEnvVarType = $oWshShell.Environment($aEnvVarType[$i])
                $oEnvVarType.Remove($_sEnvVarName)
            Next
        Case 1 To 4
            $oEnvVarType = $oWshShell.Environment($aEnvVarType[$_iEnvVarType])
             $oEnvVarType.Remove($_sEnvVarName)
        Case Else
            Return SetError(1, 0, "")
    EndSwitch
    _EnvVarRefresh()
    If @error Then Return SetError(1)
    SetError(0)
EndFunc

Func _EnvVarRefresh()
    ; http://msdn.microsoft.com/en-us/library/ms644952%28VS.85%29.aspx
    $iRet2 = DllCall("user32.dll", "lresult", "SendMessageTimeoutW", _
            "hwnd", $HWND_BROADCAST, _
            "dword", $WM_SETTINGCHANGE, _
            "ptr", 0, _
            "wstr", "Environment", _
            "dword", $SMTO_ABORTIFHUNG, _
            "dword", $MSG_TIMEOUT, _
            "dword_ptr*", 0)
    If $iRet2[0] = 0 Then Return SetError(1)
EndFunc

 

Link to comment
Share on other sites

I'd suggest saving Subz's file as 'EnvVar' in your (personal) AutoIt Include directory instead. Add "#include-once" at the top, then you can use it in any script with #include <EnvVar>

It's probably a good idea to include a link to the forum post in a comment as well, then you can find this later :)

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...