Changing environment variables programmatically

Some programs communicate through environment variables.

Windows

System Environment variables are stored in the registry, in HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment. REG_SZ entries are copied as-is, whereas REG_EXPAND_SZ entries are interpolated first against boot variables like SystemRoot.

Note that cmd.exe windows load environment variables when the window is created. Temporary changes can be made with the cmd.exe SET command, but I’m not sure how you’d access that from code.

Actually, it seems like cmd.exe is supposed to respect WM_SETTINGCHANGE messages. Should try that.

http://us.generation-nt.com/changing-environment-variables-programatically-help-57874242.html

Mac OS X

Environment variables can be set in environment.plist for GUI applications, or .profile or .bash_profile ior .bash_rc for Terminal programs running in the bash shell. Changes to one of these files are not reflected until a new terminal is opened, or the affected file is sourced into the existing environment (e.g. “source .bash_profile”). But I’m not sure how to do that programmatically. Note that “. .bash_profile” is a shortcut for “source .bash_profile” — the wonders of magic shortcuts in Unix.

  • ~/.MacOSX/environment.plist
  • ~/.bash_profile
  • ~/.profile

http://stackoverflow.com/questions/7501678/set-environment-variables-on-mac-os-x-lion

Linux

Environment variables for shells would be set in ~/.profile etc. Still need a way to set it programmatically. For LSB compliant systems, you can create a script in /etc/profile.d/ named the same as your program, and it will be run first. And I think /etc/profile is run before all programs.

http://stackoverflow.com/questions/3416638/set-environment-variables-in-c

http://stackoverflow.com/questions/2891972/how-to-programatically-set-a-permanent-environment-variable-in-linux

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>