Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Master of your Environment (NT)

by hsmyers (Canon)
on Jun 04, 2002 at 15:17 UTC ( #171505=CUFP: print w/replies, xml ) Need Help??

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 # # -- 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.


"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

Replies are listed 'Best First'.
Re: Master of your Environment (NT)
by zakzebrowski (Curate) on Jun 05, 2002 at 12:11 UTC
    Just FYI, rather than reinventing the wheel, the NSIS installer (Nullsoft Scriptable Install System) is pretty good & easy to use for when needing to write an installer on windows... You can do regedits, check for existing installs, etc... (Not perl, but a good way to distribute perl in some cases... :) ) Zak

    "There is no room in this country for hyphenated Americanism" ~ Theodore Roosevelt (1915)
Re: Master of your Environment (NT)
by Rex(Wrecks) (Curate) on Jun 18, 2002 at 23:53 UTC
    Win32::AdminMisc Works great for this too. And it is very easy to do:
    #!/usr/bin/perl -w use strict ; use Win32; use Win32::AdminMisc; Win32::AdminMisc::SetEnvVar( "UUID", "TEST-TEST", ENV_SYSTEM ) ; Win32::AdminMisc::SetEnvVar( "UUID", "TEST-TEST", ENV_USER ) ;

    "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://171505]
Approved by ichimunki
Front-paged by jarich
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2017-01-18 14:06 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (161 votes). Check out past polls.