Homes32 Posted December 22, 2023 Share Posted December 22, 2023 For example, when a program Run by the script changed %TEMP% and calls SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, "Environment") EnvGet() and @TempDir do not update to the new value, causing the next couple programs Run by the script to receive the wrong environment. According to the help docs and forum searches @TempDir is determined by GetTempPath(). Clearly EnvGet() is also not actually querying the event vars each time it is called, and just uses the value as of script start. Is there a method/option to force a running script to update EnvVars when a WM_SETTINGCHANGE message is received other then registering the WM_SETTINGCHANGE message and directly reading the registry and maintaining my own $TempDir variable? (Remember EnvGet() returns the old value as well)? Thanks! Homes32 Link to comment Share on other sites More sharing options...
Subz Posted December 23, 2023 Share Posted December 23, 2023 The following will set the environment variable functions work for me, although @TempDir will not update until the script is re-run. Link to comment Share on other sites More sharing options...
spudw2k Posted December 24, 2023 Share Posted December 24, 2023 (edited) Looks like GetTempPath looks for the %TMP% variable first. If you update %TMP%, the @TempDir macro updates. _PrintTempVars("----------Environment vars at script start----------") EnvSet("Temp", "C:\temp") EnvSet("Tmp", "C:\tmp") _PrintTempVars("----------Environment vars after update----------") Func _PrintTempVars($sTitle = "") $sTempMacro = @TempDir $sTempEnv = EnvGet("Temp") ConsoleWrite($sTitle & @CRLF & "Env:" & $sTempEnv & @CRLF & "@TempDir:" & $sTempMacro & @CRLF & @CRLF) EndFunc or if you delete the %TMP% environment variable, GetTempPath/@TempDir will use %TEMP% EnvSet("Temp", "C:\temp") EnvSet("Tmp") Edited December 25, 2023 by spudw2k Homes32 1 Spoiler Things I've Made: Always On Top Tool ◊ AU History ◊ Deck of Cards ◊ HideIt ◊ ICU ◊ Icon Freezer ◊ Ipod Ejector ◊ Junos Configuration Explorer ◊ Link Downloader ◊ MD5 Folder Enumerator ◊ PassGen ◊ Ping Tool ◊ Quick NIC ◊ Read OCR ◊ RemoteIT ◊ SchTasksGui ◊ SpyCam ◊ System Scan Report Tool ◊ System UpTime ◊ Transparency Machine ◊ VMWare ESX Builder Misc Code Snippets: ADODB Example ◊ CheckHover ◊ Detect SafeMode ◊ DynEnumArray ◊ GetNetStatData ◊ HashArray ◊ IsBetweenDates ◊ Local Admins ◊ Make Choice ◊ Recursive File List ◊ Remove Sizebox Style ◊ Retrieve PNPDeviceID ◊ Retrieve SysListView32 Contents ◊ Set IE Homepage ◊ Tickle Expired Password ◊ Transpose Array Projects: Drive Space Usage GUI ◊ LEDkIT ◊ Plasma_kIt ◊ Scan Engine Builder ◊ SpeeDBurner ◊ SubnetCalc Cool Stuff: AutoItObject UDF ◊ Extract Icon From Proc ◊ GuiCtrlFontRotate ◊ Hex Edit Funcs ◊ Run binary ◊ Service_UDF Link to comment Share on other sites More sharing options...
ioa747 Posted December 25, 2023 Share Posted December 25, 2023 On 12/23/2023 at 12:08 AM, Homes32 said: Is there a method/option to force a running script to update EnvVars Refreshes the OS environment. EnvUpdate ( ) I know that I know nothing Link to comment Share on other sites More sharing options...
Homes32 Posted December 26, 2023 Author Share Posted December 26, 2023 15 hours ago, ioa747 said: Refreshes the OS environment. EnvUpdate ( ) Not quite. the question was specifically regarding updating the RUNNING script, not sub scripts. EnvUpdate() only updates child applications with the parent processes ENV, and in this case the PARENT is the ENV that needs to listen for Broadcasts from other applications. On 12/22/2023 at 4:08 PM, Homes32 said: update EnvVars when a WM_SETTINGCHANGE message is received Link to comment Share on other sites More sharing options...
Solution Homes32 Posted December 26, 2023 Author Solution Share Posted December 26, 2023 (edited) Thank you all for the replies. Since my script that needs to listen ENV updates from external applications and is the parent process for other applications restarting it is not an option (it's a shell loader), I ended up just listening for WM_SETTINGCHANGE myself and querying the registry directly in order to perform a EnvSet() and EnvUpate() to update the "Local process" environment and push down any Env changes to @TempDir and to child processes as well. GUIRegisterMsg($WM_SETTINGCHANGE, "WindowProc") Func WindowProc($hWnd, $iMsg, $wParam, $lParam) Switch $iMsg Case $WM_SETTINGCHANGE If $lParam <> 0 Then $ilParamStringLen = _WinAPI_StrLen($lParam) $lParamString = DllStructCreate("wchar[" & $ilParamStringLen & "]", $lParam) $sString = DllStructGetData($lParamString, 1) If $sString = "Environment" Then Local $sTmpDir = RegRead("HKLM\SYSTEM\ControlSet001\Control\Session Manager\Environment", "TMP") Local $sTempDir = RegRead("HKLM\SYSTEM\ControlSet001\Control\Session Manager\Environment", "TEMP") EnvSet("TMP", $sTmpDir) EnvSet("TEMP", $sTempDir) EnvUpdate() ConsoleWrite("Env Change: Temp=" & @TempDir) EndIf EndIf EndSwitch EndFunc Edited December 26, 2023 by Homes32 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now