http://qs321.pair.com?node_id=171505

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