Jump to content

Env does not respond to WM_SETTINGCHANGE


Homes32
 Share

Go to solution Solved by Homes32,

Recommended Posts

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

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 by spudw2k
Link to comment
Share on other sites

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

  • Solution

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 by Homes32
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...