I am trying to read registry's "(Default)" values in Windows 7 in c++, and following is the code I am using:
string GetSZValueUnique( HKEY openKey, const char* regkey, const char* keyName )
{
   HKEY hKey = 0;
   BYTE data[512] ;
   DWORD szsize = 512 ;
   string value ;
   LONG retValue = RegOpenKeyEx( openKey, regkey, 0, KEY_READ, &hKey ) ;
   if ( retValue == ERROR_SUCCESS )
   {
        LONG retV = RegQueryValueEx( hKey, keyName, 0, 0, data, &szsize ) ;
        if ( retV == ERROR_SUCCESS )
        {
           char* _value = reinterpret_cast<char*>(data) ;
           value = _value ;
           RegCloseKey (hKey) ;
           return value ;
        }
        else
        {
            char msg[512] ;
            FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,(DWORD)retV,0,&msg[0],512,0) ;
            error_string = &msg[0];
            MessageBox( 0, error_string.c_str(), "Query : GetSZValueUnique", 0 );
        }
    }
    else
    {
        char msg[512] ;
        FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,(DWORD)retV,0,&msg[0],512,0) ;
        error_string = &msg[0];
        MessageBox( 0, error_string.c_str(), "Open : GetSZValueUnique", 0 );
    }
    RegCloseKey (hKey) ;
    return "" ;
}
And this is how I am calling the above function :
string ts3 = GetSZValueUnique( HKEY_LOCAL_MACHINE, "SOFTWARE\\TeamSpeak 3 Client\\", "" );
if ( !ts3.empty() )
    MessageBox( 0, ts3.c_str(), "GetSZValueUnique", 0 );
For some Keys it works for some it doesn't : For example, it works for "Adobe", "TrendMicro", "CheckPoint", "RegisteredApplications" but not for "7-Zip", "RTLSetup", "Sonic", "TeamSpeak 3 Client"
I am out of ideas now, can somebody point out what's wrong ?
EDIT: I have checked the code with "(Default)" values and other values as well, for keys its not working it never goes past the *"if ( retValue == ERROR_SUCCESS )"* check and I always get "Specified file not found" error. For keys its working, it gets past the "*if ( retValue == ERROR_SUCCESS )*" check and returns the value if its present, if its not present it simply displays the error message "Specified file not found".
EDIT 2: I Checked again : and it seems the keys it works for have their corresponding clone in "Wow6432Node" subkey under SOFTWARE... hmmm... so how do I get it working ?
You can specify the flag::
Note:: Your doubt has already been cleared by @WhozCraig in comments with the suitable links. If he answers, do accept his answer over mine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With