Force EnvGet to get new env. variables created _after_ script launch

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ahk7
Posts: 577
Joined: 06 Nov 2013, 16:35

Force EnvGet to get new env. variables created _after_ script launch

28 Jan 2024, 10:22

It seems when an AutoHotkey script starts, it inherits the environment "at that moment", so if another program creates temporary environment variables EnvGet won't be able to retrieve them and you have to quit/restart the script.

Is there a way to "force" EnvGet (or regread, or DLLCall) to read newly (so after script launch) created environment variables?

I've tried RefreshEnvironment() from https://github.com/iseahound/Environment.ahk and also the SendMessage
; SendMessage 0x1A, 0, "Environment",, ahk_id 0xFFFF ; 0x1A is WM_SETTINGCHANGE
; - The above code will broadcast a message stating there has been a change of environment variables.
but to no avail, the EnvGet for my variable remains empty unless I quit/restart the script which is not an option.
User avatar
mikeyww
Posts: 27302
Joined: 09 Sep 2014, 18:38

Re: Force EnvGet to get new env. variables created _after_ script launch

28 Jan 2024, 14:36

Read environment variable from registry

Code: Select all

; This script checks the Windows registry for an environment variable
#Requires AutoHotkey v1.1.33
var := "TEMP"
If ("" != value := envGetEx(var))
     MsgBox 64, % var  , % value
Else MsgBox 48, Failure, % "Value was not found for " var

envGetEx(var) {  ; Read an environment variable from the Windows registry
 Static key1 := "HKEY_CURRENT_USER\Environment"
      , key2 := "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
 RegRead value, % key1, % var
 If ErrorLevel
  RegRead value, % key2, % var
 Return value
}
Last edited by mikeyww on 28 Jan 2024, 14:42, edited 1 time in total.
ahk7
Posts: 577
Joined: 06 Nov 2013, 16:35

Re: Force EnvGet to get new env. variables created _after_ script launch

28 Jan 2024, 14:42

"Value was not found for " the-temp-env-var, but when I do a simple echo %the-temp-env-var% on the command line it is displayed correctly, so it does exist.
User avatar
mikeyww
Posts: 27302
Joined: 09 Sep 2014, 18:38

Re: Force EnvGet to get new env. variables created _after_ script launch

28 Jan 2024, 14:45

As far as I know, if you are setting a new var in a command shell session, then the setting exists only within that session. If you use the Windows control panel to add a new var, then it will be recorded in one of the registry keys as shown.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AdsBot [Google] and 111 guests