在Windows 7中的注册表行为奇怪 [英] Reading Registry in Windows 7 behaving strangely

查看:153
本文介绍了在Windows 7中的注册表行为奇怪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Windows 7中读取注册表的(默认)值在c ++,以下是我使用的代码:

  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);
返回值;
}
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;
}

这是我调用上述函数的方法:

  string ts3 = GetSZValueUnique(HKEY_LOCAL_MACHINE,SOFTWARE\\TeamSpeak 3 Client\\, ); 
if(!ts3.empty())
MessageBox(0,ts3.c_str(),GetSZValueUnique,0);

对于某些键,它适用于某些不适用:例如,它适用于Adobe ,TrendMicro,CheckPoint,RegisteredApplications而不是7-Zip,RTLSetup,Sonic,TeamSpeak 3 Client



I



编辑:我已经检查了带有(默认)值的代码,并且其他值,对于键不工作它从来没有经过*if(retValue == ERROR_SUCCESS)*检查,我总是得到指定的文件未找到错误。对于其工作的键,它通过* if(retValue == ERROR_SUCCESS)*检查并返回值,如果它的存在,如果它不存在它只是显示错误消息指定的文件未找到



编辑2:我再次检查:似乎它的工作原理的键在Wow6432Node SOFTWARE ... hmmm ...所以我如何使它工作?

解决方案

RegOpenKeyEx 参数中的 samDesired 参数中的 b> b> b b strong>如果您要从应用程序中访问Wow6432Node密钥,即 32位密钥

  • KEY_WOW64_64KEY 如果您想要从应用程式存取正常键(例如 64位元键),请选择 RegOpenKeyEx 的「strong> samDesired 」参数。

  • 注意:@WhozCraig在合适链接的评论中已经清除了您的疑问。如果他回答,请接受我的答案。


    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::

    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.

    这篇关于在Windows 7中的注册表行为奇怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆