On 2/2/21, Christopher Barker <pythonchb@gmail.com> wrote:
In the common case, folks have their environment variables set in an initialization file (or the registry? I've lost track of what Windows does these days)
It hasn't fundamentally changed since the mid 1990s. Configurable system variables are set in the regsitry key "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", and configurable user variables are set in "HKCU\Environment". A process is spawned with an environment that's sourced from the parent process. Either it's inherited from the parent's environment or it's a new environment that was passed to CreateProcessW(). The ancestor of most interactive processes in a desktop session is the graphical shell, Explorer. At startup, it calls an undocumented shell32 function (RegenerateUserEnvironment) to load a new environment from scratch. It also reloads its environment in response to a WM_SETTINGCHANGE "Environment" message. The documented way to reload the environment from scratch is CreateEnvironmentBlock(&env, htoken, FALSE) and SetEnvironmentStringsW(env).