tags:

views:

1542

answers:

6

Is there a way to set the global windows path environment variable programatically (C++)?

As far as I can see, putenv sets it only for the current application.
Changing directly in the registry (HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment) is also an option though I would prefer API methods if there are?

+1  A: 

There is no API - changing the registry is the way to do it. The changed value will be used for processes starting after the change was made.

Also: Notice that running applications must actively process the settings changed message and many (most?) do not do so.

anon
A: 

If you want to do it through the registry, you might want to look at the source code of this program.

Microsoft also provides a small command line utility called setx with its resource toolkits, which will let you do this. By the way, the regular set command just lets you define local environment variables I think.

drby
+14  A: 

MSDN Says:

Calling SetEnvironmentVariable has no effect on the system environment variables. To programmatically add or modify system environment variables, add them to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment registry key, then broadcast a WM_SETTINGCHANGE message with lParam set to the string "Environment". This allows applications, such as the shell, to pick up your updates. Note that the values of the environment variables listed in this key are limited to 1024 characters.

Assaf Lavie
A: 

Many information for you here :
"Environment variables under Windows (how to use them!)"

www.samiam.org/alt.hackers.1995/[email protected]

lsalamon
Did you mean to put a link in there?
Michael Kohne
+1  A: 

Yes You are correct. You also need to effect these settings without logging off

Send Message of borad casting to all windows SETTINGCHANGE for the parameter (LPARAM) "Environment" with SMTO_ABORTIFHUNG for some milliseconds(5000) using SendMessageTimeout API.

This is what setX.exe provided with resource Kit does.

lakshmanaraj
+1  A: 

As was pointed out earlier, to change the PATH at the machine level just change this registry entry:

HLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

But you can also set the PATH at the user level by changing this registry entry:

HKEY_CURRENT_USER\Environment\Path

And you can also set the PATH at the application level by adding the application\Path details to this registry entry:

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\
jussij