Set Windows environment variables

Submitted by: Bret Hartwig Last Updated: 2008-09-20

I wrote the following which is little more than wrappers to winregistry functions, targeting string environment variables. Two functions: getEV( evName ) and setEV( evName, evValue ); all other parameters are optional where their default values are globals defined herein. Please review the global vars before use. The warning message below comes straight out of winregistry.acl. Tested against Epic 4.4K. Carefully enjoy :)

package ev_sample; require winregistry; global KEY = "HKEY_CURRENT_USER"; global SUBKEY = "Environment"; global MAX_LENGTH = 255; global VERBOSE = 0; function getEV(evName, subkey = ev_sample::SUBKEY, key = ev_sample::KEY, \  v = ev_sample::VERBOSE, maxLen = ev_sample::MAX_LENGTH ) { local evValue = ""; # Change verbose var if different the param value. local origV = winregistry::VERBOSE; if ( v != origV ) { winregistry::VERBOSE = v  } if ( winregistry::HasKey( "$key", "$subkey", "$evName" ) ) { evValue = winregistry::GetKey( "$key", "$subkey", "$evName", maxLen ); }  # Restore verbose var if necessary. if ( v != origV ) { winregistry::VERBOSE = origV }  return evValue; } function setEV( evName, evValue, subkey = ev_sample::SUBKEY, \  key = ev_sample::KEY, v = ev_sample::VERBOSE ) { local rtn; # Change verbose var if different the param value. local origV = winregistry::VERBOSE; if ( v != origV ) { winregistry::VERBOSE = v  } rtn = winregistry::SetKey( "$key", "$subkey", "$evName", "$evValue" ); # Restore verbose var if necessary. if ( v != origV ) { winregistry::VERBOSE = origV }  return rtn; }
 * 1) * WARNING: Damaging the Windows Registry can be absolutely fatal, not
 * 2) * just to the application, but to the entire system.  Use SetKey with
 * 3) * some caution.  Caveat user.
 * 1) * some caution.  Caveat user.