Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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 # # 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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-16 10:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found