Alex Rivera | Logout

Is there a way to set the environment path programmatically in C++ on Windows?

Asked 2009-02-10T11:50:15.500
19

Is there a way to set the global windows path environment variable programmatically (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?

Edit
Report

2 Answers

26

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.

answered 2009-02-10T12:35:12.193
12

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\
answered 2009-02-11T02:39:16.563

Your Answer