http://www.perlmonks.org?node_id=471822
Description:

Update: modules Win32::Env and Win32::Env::Path(note: WEP does not broadcast the change) make this code obsolete.

Everyone (I hope) knows that changing environment variables only affects current and (subsequently spawned) child processes and doesn't affect parent or other unrelated processes. But on Windows, when you go through Control Panel to change environment variables, that's exactly what does happen (it changes environment variables in other processes). I had to do a bit of searching to find out how to do this, so just in case you ever need to do this, here it is. Might it be a good idea to wrap this up in a module...maybe Win32::EnvVar??

This program broadcasts to other Win32 processes a message to reload their environment. Other processes do not have to listen to the message. "CMD.EXE" and "PERL.EXE", e.g., do not reload their environment.

Note: There is also Win32::AdminMisc::SetEnvVar(), but it crashed perl when I ran it on Windows XP with ActiveState perl 5.8.4 build 810. Although other functions in the package worked fine.

Updated: Path is a REG_EXPAND_SZ...see GetValue() method in Win32::TieRegistry to see how get the type of other registry values/environment variables if you want a more dynamic solution.

Note: I very nearly used ActiveState's ActiveTcl for this, because it comes with a "broadcast" function in the "registry" package.


use strict;
use warnings;
my $Registry;
use Win32::TieRegistry (
  Delimiter => "/",
  TiedRef => \$Registry,
  "REG_EXPAND_SZ"
);
use Win32::API;

my $dir = "c:\\another_directory";

# Registry key for $ENV{PATH}
my $path_key = "LMachine/SYSTEM/CurrentControlSet/Control/Session Mana
+ger/Environment/Path";
die "Can't find Path registry key\n"
  unless exists $Registry->{$path_key};

my $path = $Registry->{$path_key};

$path .= ";$dir";
$Registry->{$path_key} = [ $path, REG_EXPAND_SZ ];

use constant HWND_BROADCAST   => 0xFFFF;
use constant WM_SETTINGCHANGE => 0x001A;
my $SendMessage = Win32::API->new("user32", "SendMessage", 
  [qw(N N P P)], "N");
my $result = $SendMessage
  ->Call(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 'Environment');