Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading Registry in Windows 7 behaving strangely

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 ?

like image 394
StudentX Avatar asked Feb 26 '13 08:02

StudentX


1 Answers

You can specify the flag::

  1. "KEY_WOW64_32KEY" in "samDesired" parameter of the RegOpenKeyEx if you want to access Wow6432Node Keys i.e., 32-bit keys from your app.
  2. "KEY_WOW64_64KEY" in "samDesired" parameter of the RegOpenKeyEx if you want to access normal Keys i.e., 64-bit keys from your app.

Note:: Your doubt has already been cleared by @WhozCraig in comments with the suitable links. If he answers, do accept his answer over mine.

like image 164
Abhineet Avatar answered Oct 28 '22 13:10

Abhineet