Home > OS >  C does not alter registry even though running without errors
C does not alter registry even though running without errors

Time:12-20

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

CodePudding user response:

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).

CodePudding user response:

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 only way that can happen is if either:

  1. You are not updating the GUI after making the changes.

  2. you are modifying a different area of the Registry then you are viewing, ie if you are modifying the 32bit Registry but viewing the 64bit Registry, or vice versa. Read up about the Registry Redirector, Registry Keys Affected by WOW64 and Accessing an Alternate Registry View on MSDN for more details about working with the 32bit and 64bit Registry views.

That being said, there are a number of other mistakes in your code.

Try something more like this instead:

HKEY open_reg()
{
    HKEY hKey = NULL;
    int result = RegOpenKeyExA( HKEY_LOCAL_MACHINE,
                             "Software\\Microsoft\\Windows\\CurrentVersion\\Run",
                             0,
                             KEY_QUERY_VALUE | KEY_SET_VALUE /* | KEY_WOW64_(32|64)KEY if needed */,
                             &hKey );

    if ( result != 0 )
    {
        cout << " Failed to open Registry, Error " << result << endl;
        return NULL;
    }
    else
    {
        cout << "Opened Registry key" << endl;
        return hKey;
    }
}

void query_reg_value(HKEY handle)
{
    DWORD cbBuffer = TOTALBYTES;
    std::vector<char> buffer(cbBuffer);

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

    int result = RegQueryValueExA( handle,
                             "SecurityHealth",
                             NULL,
                             NULL,
                             reinterpret_cast<LPBYTE>(buffer.data()),
                             &cbBuffer );

    if ( result == 0 ) 
    { 
        cout << "Successfully quered: ";
        while (cbBuffer != 0 && buffer[cbBuffer-1] == '\0') --cbBuffer; // ignore null terminator(s)
        cout.write(buffer.data(), cbBuffer);
        cout << endl;
    }
    else 
    {
        cout << "Failed to query, Error " << result << endl;
    }
}

void set_reg_value(HKEY handle)
{
    std::string file = "C:\\Windows\\System32\\cmd.exe";

    int result = RegSetValueExA( handle,
                             "SecurityHealth",
                             0,
                             REG_EXPAND_SZ,
                             reinterpret_cast<LPCBYTE>(file.c_str()),
                             file.size() 1);
    
    if ( result == 0 ) 
    { 
        cout << "Successfully changed value" << endl;
    }
    else 
    {
        cout << "Failed to change value, Error " << result << endl;
    }
}

int main()
{
    cout << "testing Windows Registry" << endl;
    HKEY hKey = open_reg();
    if (hKey) {
        query_reg_value(hKey);
        set_reg_value(hKey);
        RegCloseKey(hKey);
    }
    system("PAUSE");
    return 0;   
}

However, it should be noted that only admin users have write access to HKLM keys by default, most users have read-only access. As such, it is not a good idea to open a key under HKLM for both reading and writing at the time same, unless you know what you are doing. You should open a key for reading only, read from it, and close it. Same for writing. For instance:

HKEY open_reg(bool isWriting)
{
    HKEY hKey = NULL;
    int result = RegOpenKeyExA( HKEY_LOCAL_MACHINE,
                             "Software\\Microsoft\\Windows\\CurrentVersion\\Run",
                             0,
                             (isWriting ? KEY_SET_VALUE : KEY_QUERY_VALUE) /* | KEY_WOW64_(32|64)KEY if needed */,
                             &hKey );

    if ( result != 0 )
    {
        cout << " Failed to open Registry, Error " << result << endl;
        return NULL;
    }
    else
    {
        cout << "Opened registry key" << endl;
        return hKey;
    }
}

void query_reg_value()
{
    HKEY hKey = open_reg(false);
    if (!hKey) return;

    DWORD cbBuffer = TOTALBYTES;
    std::vector<char> buffer(cbBuffer);

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

    int result = RegQueryValueExA( hKey,
                             "SecurityHealth",
                             NULL,
                             NULL,
                             reinterpret_cast<LPBYTE>(buffer.data()),
                             &cbBuffer );

    if ( result == 0 ) 
    { 
        cout << "Successfully quered: ";
        while (cbBuffer != 0 && buffer[cbBuffer-1] == '\0') --cbData; // ignore null terminator(s)
        cout.write(buffer.data(), cbBuffer);
        cout << endl;
    }
    else 
    {
        cout << "Failed to query, Error " << result << endl;
    }

    RegCloseKey(hKey);
}

void set_reg_value()
{
    HKEY hKey = open_reg(true);
    if (!hKey) return;

    std::string file = "C:\\Windows\\System32\\cmd.exe";

    int result = RegSetValueExA( hKey,
                             "SecurityHealth",
                             0,
                             REG_EXPAND_SZ,
                             reinterpret_cast<LPCBYTE>(file.c_str()),
                             file.size() 1);
    
    if ( result == 0 ) 
    { 
        cout << "Successfully changed value" << endl;
    }
    else 
    {
        cout << "Failed to change value, Error " << result << endl;
    }

    RegCloseKey(hKey);
}

int main()
{
    cout << "testing Windows Registry" << endl;
    query_reg_value();
    set_reg_value();
    system("PAUSE");
    return 0;   
}

CodePudding user response:

Take a look at the Remarks section of the official documentation, and see if any of the caveats apply to your situation.

  • Related