I want to modify my system's environment variables on Windows. I can open the Control Panel's 'Environment Variables' 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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With