What's new

Changing environment variables in Windows.

Poobah

New member
I was recently trying to make a program that appends a directory to the PATH environment variable, but I'm having trouble. I've been using the SET command, but it doesn't actually change the environment variables when it's called from within another program. So if I do something like:
Code:
SET "PATH=%PATH%;C:\Program Files"
within the program using C's system() function, the environment variable will not change at all, despite the fact that SET returns 0 to the program. Calling the exact same command in Windows' command prompt works fine. I've tried writing the command to a batch file, which didn't work, and I've also tried sending it to cmd.exe, which also didn't work.

So could someone recommend an alternative to the SET comand for changing environment variables?
 

Warcon

New member
Setting environment variable is a little bit tricky because it only change it for the current process. In your example, you started a 2nd process that did the change but the change 'died' when that process closed. There was no modification to the system or your own process.

If you only need the change to be made in your own process, use the 'C' function "putenv". Or, if you are programming in .Net, use System.Environment.SetEnvironmentVariable (only available in framework 2.0).

On the other hand, if you need the change to be permanent, you need to modify the registry key:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path

After the change, you need to reboot the computer to make the change available to all process.

Hope this help.
 
OP
Poobah

Poobah

New member
Warcon said:
Setting environment variable is a little bit tricky because it only change it for the current process. In your example, you started a 2nd process that did the change but the change 'died' when that process closed. There was no modification to the system or your own process.

If you only need the change to be made in your own process, use the 'C' function "putenv". Or, if you are programming in .Net, use System.Environment.SetEnvironmentVariable (only available in framework 2.0).

On the other hand, if you need the change to be permanent, you need to modify the registry key:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path

After the change, you need to reboot the computer to make the change available to all process.

Hope this help.
I don't think Putenv() or Setenv() are ANSI-compliant, because they aren't available in MinGW. I was hoping there'd be an alternative to editing the registry, because I don't know much about Windows programming. Thanks for the help, though.

Falcon4ever said:
just go to your system settings (rightclick on the This Computer icon in the explorer) choose tab advanced, then you see a button ...variables (i'm on a dutch system win xp pro... so i guess it's named a bit different) and you can add your path there... (don't mess with the reg ;)

ah found a general one:
http://www.phys.ufl.edu/~coldwell/cl..._home_path.htm
I was looking for a method to change it in C, not through My Computer. :)
 

smcd

Active member
There is no standards-compliant method (to my knowledge) so you'll have to break portability in order to do so. As mentioned above, using "set xxx=yyy" it doesn't make permanent changes and is only in effect for the current process, and perhaps spawned child processes depending on how they're launched. Editing it in the registry I've not done, but used the "My Computer" properties dialog and editing it there should put it into the registry after you save the changes and I've never had to restart in order for the changes to take effect.
 

Top