0

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

Huzaifa
  • 345
  • 4
  • 15
  • 1
    Obviously what you need to do is 1) get the existing path with `RegGetValue`, 2) append your path to the path you got from step 1, 3) save the combined path with `RegSetValueEx`. Seems straightforward enough. Did you try something like that and it didn't work? If so you should post the code that you tried. – john Aug 22 '20 at 09:50

1 Answers1

0

I followed up to @john recommendations and got the working code as follows

void appendReg()
{
    // commons
        static const char sysVar[] = "Cur" ;    // Variable Name 
        const char key_name[] = "Environment";  
        
        
    // first read the current value of registery    
        static BYTE curRegVal[1000000]; // will store the reg value
        DWORD curRegValCP = sizeof(curRegVal) ; 
        
        HKEY readKey;
        RegOpenKeyExA( HKEY_CURRENT_USER, key_name, 0, KEY_QUERY_VALUE, &readKey);
        RegQueryValueExA( readKey, sysVar, nullptr, nullptr, curRegVal, &curRegValCP);
        RegCloseKey(readKey);
        
        // curRegVal now has the current reg value
        std::cout<<"\nCurrent Reg value is = > "<< reinterpret_cast<const char*>(curRegVal);
    
    
    // second append the new path value to current value
        const char appendedVal[1000000]="C:\\New\\Dir\\To\\Be\\Appended;";  // to be appended
        strcat(appendedVal,curRegVal); // concatenate the current and new values
        std::cout<<"\nAppended Reg value is => "<<appendedVal;


    // third set the new(appended) value for registery and broadcast changes to other processes
        HKEY writeKey;
        RegOpenKeyEx(HKEY_CURRENT_USER,key_name, 0, KEY_ALL_ACCESS, &writeKey); 
        RegSetValueEx(writeKey,(LPCSTR)sysVar,0,REG_EXPAND_SZ,(BYTE*) appendedVal, strlen(appendedVal)+1);  
        SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)key_name, SMTO_BLOCK, 100, NULL); // tell other process to update their env values
        RegCloseKey(writeKey);
}

This will append the new path to environment variable of current user.

Huzaifa
  • 345
  • 4
  • 15