The other day a fellow programmer walked up to me and asked for help with a problem. He said that as part of an install, he needed a batch program to be able to usefully change an environment variable on an NT box. He said that given the way that a process inherits the environment, he had been unsuccessful so far and wondered if I knew of anyway to accomplish the task. Without a lot of thought, I said "No Problem!" Silly me. What I had in mind was a small program that I'd gotten out of Andrew Schullman's Undocumented DOS which allowed modifications to the master environment space. 'Silly', because that was then and this is of course now, further, NT is not DOS!

Well to make a long story short, here is the solution--or at least an example that demonstrates the necessary technique/incantations, and in Perl no less...

#!/perl/bin/perl # # Mset.pl -- Set master environment variable 'test' to arbitrary value + as demo... use strict; use warnings; use diagnostics; use Win32::Registry; use Win32::API; use constant HWND_BROADCAST => -1; use constant WM_SETTINGCHANGE => 0x1a; my $env; my $value; my $reserved; $::HKEY_LOCAL_MACHINE->Open("SYSTEM\\CurrentControlSet\\Control\\Sessi +on Manager\\Environment", $env) or die "Can't open env: $^E"; $env->SetValueEx("test",$reserved,'1',"C:\\Perl\\Perl_Dev\\mset") or die "No Environment variable test: $!\n"; $env->Close(); my $SendMessage = new Win32::API("user32", "SendMessage", 'NNNP', 'N') or die "Couldn't create SendMessage: $!\n"; my $RetVal = $SendMessage->Call(HWND_BROADCAST,WM_SETTINGCHANGE,0,'Env +ironment');

Simple and too the point. Muck about in the registry, using get/set etc. And when done, tell the rest of the world about it. Note--for those who play with this, changes won't be visible in the dos box from which the script is launched. Pop up another one and examine things with set.

–hsm

"Never try to teach a pig to sing…it wastes your time and it annoys the pig."

Edit by tye, don't put PRE tags around CODE tags


In reply to Master of your Environment (NT) by hsmyers

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.