I want to append a new path to an existing environment variable with c++. This should be for the current user (if you can provide an example for the Local_Machine, that will also be acceptable).
The path(new) and the variable(already existing) are
variable = Cur
path = E:\Softwares\Setup
What i have tried
void SetUserVariablePath(){
HKEY hkey;
long regOpenResult;
const char key_name[] = "Environment";
const char path[]="E:\\Softwares\\Setup;"; //new path
LPCSTR stuff = "Cur"; //Variable Name
// here we open the variable and set the value
regOpenResult = RegOpenKeyEx(HKEY_CURRENT_USER,key_name, 0, KEY_ALL_ACCESS, &hkey);
RegSetValueEx(hkey,stuff,0,REG_EXPAND_SZ,(BYTE*) path, strlen(path)+1);
// tell other process to update their env values
SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)"Environment", SMTO_BLOCK, 100, NULL);
// close the registery key
RegCloseKey(hkey);
}
I have followed how-to-add-environment-variable-in-c for the above code.The code does work and sets the environment variable for the current user but it also over-rides any existing paths already present, What i want to do is to append to the existing paths.
Things that dont answer my question
- I am not taking about setting the variable for the current/child process(that can be done with setenv() or _putenv() function).
- I have already looked at these questions and they don't answer my question. Update system environment variable from c++ and Set system variable from C++