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');

In reply to Change %ENV globally on Win32 by runrig

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.