Reading Registry in Windows 7 behaving strangely

北战南征 提交于 2019-12-10 13:17:29

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/15084380/reading-registry-in-windows-7-behaving-strangely

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!