Search code examples
c++windowsregistrymingw

C++ does not alter registry even though running without errors


Trying to write code to change the registry keys with c++ after endless amount of time I reached this point but this code still does not edit the registry even when running as admin

to change the registry 4 functions are needed according to this question which I used and every single one of them returns a zero which means that the function completed without errors but still no values are changed in the registry gui

the SecurityHealth strartup service is running on my machine and has the path %windir%\system32\SecurityHealthSystray.exe and type REG_EXPAND_SZ

I even tried creating a new entry similar to the SecurityHealth and still nothing is changed

I am compiling as admin and running as admin

HKEY open_reg()
{
    int result;
    LPCSTR lpSubKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
    HKEY hKey; 

    result = RegOpenKeyExA(HKEY_LOCAL_MACHINE, lpSubKey, 0, KEY_QUERY_VALUE|KEY_WRITE|KEY_READ|KEY_SET_VALUE, &hKey);

    if ( result != 0)
    {
        cout << " Failed to open registry. - [ "<< result<< "]" <<endl;
    }
    else
    {
        cout << "Found registry key. - [" << result<<"]" << endl;
    }
    return hKey;
}




HKEY find_reg_value(HKEY handle)
{
    

    LPCSTR lpValueName = "SecurityHealth";
    DWORD BufferSize = TOTALBYTES;
    DWORD cbData;
    int dwRet;

    PPERF_DATA_BLOCK PerfData = (PPERF_DATA_BLOCK) malloc( BufferSize );
    cbData = BufferSize;

    cout << "\nRetrieving the data..." << endl;

    dwRet = RegQueryValueExA( handle,
                             lpValueName,
                             NULL,
                             NULL,
                             (LPBYTE) PerfData,
                             &cbData );

    if ( dwRet == 0 ) 
    { 
        cout << "Successfully quered [" << dwRet << "]"<<endl;
    }
    else 
    {
        cout << "Failed to query  Error code : [" << dwRet << "]"<<endl;
    } 

    return handle;
}






void set_reg_value(HKEY handle)
{
    
    int result;
    LPCSTR lpValueName = "SecurityHealth";
    std::string file = "C:\\Windows\\System32\\cmd.exe";
    
    const  char * sth = file.c_str();
    unsigned char m_Test[file.size()];
    strcpy((char*)m_Test, sth);

    DWORD DATA_SIZE = file.size()+1;

    result = RegSetValueExA(handle,lpValueName,0,REG_EXPAND_SZ,m_Test,DATA_SIZE);
    
    if ( result == 0 ) 
    { 
        cout << "Successfully changed value [" << result << "]"<<endl;
    }
    else 
    {
        cout << "Failed to change value  Error code : [" << result << "]"<<endl;
    } 
    RegCloseKey (handle);
}


int main()
{
    cout << "testing windows registry " << endl;
    HKEY reg_handle = open_reg();
    HKEY handler = find_reg_value(reg_handle);
    set_reg_value(handler);
    system("PAUSE");
    return 0;   
}

the compiled exe output in the terminal

testing windows registry
Found registry key. - [0]

Retrieving the data...
Successfully quered [0]
Successfully changed value [0]
Press any key to continue . . .

Compiled with g++ regutil.cpp


Solution

  • I suspect you are compiling as a 32-bit program but looking at a 64-bit registry. Switch to compiling as 64-bit instead. (There is a 32-bit registry instead, which can be found buried within the 64-bit hives but you likely want to change the actual 64-bit version).