Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify system environment variables

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?

like image 874
MD XF Avatar asked Apr 17 '26 17:04

MD XF


1 Answers

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.

like image 50
IInspectable Avatar answered Apr 19 '26 06:04

IInspectable