3

I want to modify my system's environment variables on Windows. I can open the Control Panel's 'Environment Variables' dialog:

dialog

However, I want to do this programmatically in C. AFAIK, in Unix I can modify the global variable environ, but I doubt this would work in Windows.

So, how can I programmatically modify the Windows system environment variables?

MD XF
  • 7,860
  • 7
  • 40
  • 71
  • In what language? – Robert Columbia Feb 25 '17 at 03:38
  • @RobertColumbia tagged [tag:c] and *However, I want to do this programmatically in C.* – MD XF Feb 25 '17 at 03:38
  • 2
    A Google search for *windows set environment variable* turns up [this answer](https://msdn.microsoft.com/en-us/library/windows/desktop/ms686206(v=vs.85).aspx) as the third result, and that page includes a [link to a code example](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682009(v=vs.85).aspx). – Ken White Feb 25 '17 at 03:39
  • You're asking for a C solution to a Windows issue and don't want to use `windows.h`? Good luck. The entire API depends on declarations in that file. It's foolish to expect to do anything programmatically with Windows without using it, and if you decide that's what you want to do then you should expect to have to figure it out yourself. – Ken White Feb 25 '17 at 03:41
  • @KenWhite Yeah. I rethought my entire life halfway through writing that comment and deleted it. Sorry. – MD XF Feb 25 '17 at 03:43
  • Related: http://stackoverflow.com/q/573817/694576 – alk Feb 25 '17 at 07:46
  • @KenWhite: You have linked to ways to change the calling process' environment. That's not what the UI the OP posted modifies. That UI modifies the global, non-transient environment, stored in the system registry. – IInspectable Feb 25 '17 at 10:54
  • Does this answer your question? [Setting Windows PowerShell environment variables](https://stackoverflow.com/questions/714877/setting-windows-powershell-environment-variables) – TechDogLover OR kiaNasirzadeh Apr 05 '23 at 00:45
  • dupplicate of https://stackoverflow.com/a/64148075/6791254 – TechDogLover OR kiaNasirzadeh Apr 05 '23 at 00:46

2 Answers2

2

Details on how to change the system environment variables are documented under Environment Variables:

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 a process needs to be elevated to change values under the HKEY_LOCAL_MACHINE hive.

IInspectable
  • 46,945
  • 8
  • 85
  • 181
0

If you're using MinGW/TDM-GCC you can probably use the same functions used in Unix, getenv to get the content of a variable and setenv or putenv to write or modify a variable.

If you want to go native you can do this by using GetEnvironmentVariable/SetEnvironmentVariable. An example on how to use these is also provided. Though they are marked as C++ you can either use a C++ compiler or link using extern "C".

MD XF
  • 7,860
  • 7
  • 40
  • 71
agxxvi
  • 39
  • 6
  • Be aware that any of those functions change the environment for the *calling process **only***. – alk Feb 25 '17 at 07:44
  • Completely misses the question. The OP is trying to **permanently** alter the environment. Your proposed solution affects the calling process' environment only, and is transient. Once that process dies, the changes are lost. Sorry, -1. – IInspectable Feb 25 '17 at 10:36