Repository: microsoft/terminal
File: src/inc/til/env.h (C/C++)

Code before:
                }
            });

            currentEnvVars = ::GetEnvironmentStringsW();
            THROW_HR_IF_NULL(E_OUTOFMEMORY, currentEnvVars);

            return til::env{ currentEnvVars };
        }

        void set_user_environment_var(std::wstring_view var, std::wstring_view value)
        {
            auto valueString = expand_environment_strings(value);
            valueString = check_for_temp(var, valueString);
            save_to_map(std::wstring{ var }, std::move(valueString));
        }

        void regenerate()
        {
            // Generally replicates the behavior of shell32!RegenerateUserEnvironment
            // The difference is that we don't
            // * handle autoexec.bat (intentionally unsupported).
            // * call LookupAccountSidW to get the USERNAME/USERDOMAIN variables
            //   These are available as standard environment variables and they're "volatile"
            //   (= computed dynamically at each login and then constant until logout).
            //   We don't expect our process to impersonate another user so we can simplify this.
            // * call GetComputerNameW to get the COMPUTERNAME variable, for the same reason.
            get(til::details::vars::system_root);
            get(til::details::vars::system_drive);
            get(til::details::vars::all_users_profile);
            get(til::details::vars::public_var);
            get(til::details::vars::program_data);
            get(til::details::vars::computer_name);
            get(til::details::vars::user_name);
            get(til::details::vars::user_domain);
            get(til::details::vars::user_dns_domain);
            get(til::details::vars::home_drive);
            get(til::details::vars::home_share);
            get(til::details::vars::home_path);
            get(til::details::vars::user_profile);
            get(til::details::vars::app_data);
            get(til::details::vars::local_app_data);
            get_program_files();
            get_vars_from_registry(HKEY_LOCAL_MACHINE, til::details::vars::reg::system_env_var_root);
            // not processing autoexec.bat
            get_vars_from_registry(HKEY_CURRENT_USER, til::details::vars::reg::user_env_var_root);
            get_vars_from_registry(HKEY_CURRENT_USER, til::details::vars::reg::user_volatile_env_var_root);
            get_vars_from_registry(HKEY_CURRENT_USER, fmt::format(til::details::vars::reg::user_volatile_session_env_var_root_pattern, NtCurrentTeb()->ProcessEnvironmentBlock->SessionId));
        }

        std::wstring to_string() const
        {

Review comment (suggestion):
This description of "volatile" seems a bit off, if it covers `CLIENTNAME`, which I'd expect to change if the user reconnects to the same Remote Desktop session from a different client computer.

I have assumed that the "HKEY_CURRENT_USER\Volatile Environment" key is created with RegCreateKeyExW REG_OPTION_VOLATILE or the NT API equivalent, so that it is never saved to the user profile, but I haven't verified whether this is actually true.

Code after:
                }
            });

            currentEnvVars = ::GetEnvironmentStringsW();
            THROW_HR_IF_NULL(E_OUTOFMEMORY, currentEnvVars);

            return til::env{ currentEnvVars };
        }

        void set_user_environment_var(std::wstring_view var, std::wstring_view value)
        {
            auto valueString = expand_environment_strings(value);
            valueString = check_for_temp(var, valueString);
            save_to_map(std::wstring{ var }, std::move(valueString));
        }

        void regenerate()
        {
            // Generally replicates the behavior of shell32!RegenerateUserEnvironment
            // The difference is that we don't
            // * handle autoexec.bat (intentionally unsupported).
            // * call LookupAccountSidW to get the USERNAME/USERDOMAIN variables.
            //   Windows sets these as values of the "Volatile Environment" key at each login.
            //   We don't expect our process to impersonate another user so we can get them from the PEB.
            // * call GetComputerNameW to get the COMPUTERNAME variable, for the same reason.
            get(til::details::vars::system_root);
            get(til::details::vars::system_drive);
            get(til::details::vars::all_users_profile);
            get(til::details::vars::public_var);
            get(til::details::vars::program_data);
            get(til::details::vars::computer_name);
            get(til::details::vars::user_name);
            get(til::details::vars::user_domain);
            get(til::details::vars::user_dns_domain);
            get(til::details::vars::home_drive);
            get(til::details::vars::home_share);
            get(til::details::vars::home_path);
            get(til::details::vars::user_profile);
            get(til::details::vars::app_data);
            get(til::details::vars::local_app_data);
            get_program_files();
            get_vars_from_registry(HKEY_LOCAL_MACHINE, til::details::vars::reg::system_env_var_root);
            // not processing autoexec.bat
            get_vars_from_registry(HKEY_CURRENT_USER, til::details::vars::reg::user_env_var_root);
            get_vars_from_registry(HKEY_CURRENT_USER, til::details::vars::reg::user_volatile_env_var_root);
            get_vars_from_registry(HKEY_CURRENT_USER, fmt::format(til::details::vars::reg::user_volatile_session_env_var_root_pattern, NtCurrentTeb()->ProcessEnvironmentBlock->SessionId));
        }

        std::wstring to_string() const
        {
            std::wstring result;