xxpiper has asked for the wisdom of the Perl Monks concerning the following question:

Monks,
I would like to set an environment variable in the HKEY_CURRENT_USER key. Here is the code:

$username = getlogin(); $USERNAME = "\L$username\E"; $key = 'HKEY_CURRENT_USER\Environment\INTRALINK_DEFAULT_USER'; use Win32::TieRegistry; $Registry->{$key} = "$USERNAME";

This will set the variable "INTRALINK_DEFAULT_USER" in HKEY_CURRENT_USER and HKEY_USERS.
The problem I am faced with is:
Although the variable is set when I read it in "regedit" or in the "System Properties, user environment variables"
it does not take effect for the current session unless the "OK" button is selected in the System Properties panel
or unless I logout and log back in.
Is there a way to "push" the new values for the current session?

Thank you,
xxpiper
"Loud pipes save lives."

Replies are listed 'Best First'.
Re: User Keys - Environment Variables
by NetWallah (Canon) on Dec 09, 2003 at 22:13 UTC
    When you assign a value to an environment variable using %ENV, the value is effective only for that session. After the program that sets the variable terminates, the value is no longer available. To create a persistent environment variable, you must set the value in the Registry . One way to do that is uses a program in the Windows resource kit.

    Use the Setx resource kit utility. Setx is a powerful tool with which you can manage the environment variables in NT. This utility has three modes of operation: command line, Registry, and file. The command-line and Registry modes are the most common.

    setx LOCATION DALLAS -m

    The other option is to hack the registry .

    The HKEY_LOCAL_MACHINE\SYSTEM\ CurrentControlSet\Control\ Session Manager\ Environment Registry key stores static system variables.

    "We are enthusiastically pro-laugh, we are pro-choice as well. We respect each and every individual’s right not to laugh. If you want to be miserable, go right ahead. Whatever makes you happy."
      Thank you, this seems do what I require.
      xxpiper
Re: User Keys - Environment Variables
by Joost (Canon) on Dec 09, 2003 at 21:16 UTC
    I think in some(?) Win32 systems you can set $ENV{INTRALINK_DEFAULT_USER}, and it might work (don't have a windows system running right now). On unix-type systems the variable will only be seen by processes forked from your perl program (so, also for processes created with system and friends).

    If you want to set an environment variable, you might be better off sourcing some shell/DOS script at the start of your session / in autoexec.bat. See also perldoc -q env