【问题标题】:C++ RegEnumValue() - fail to iterate to each valueC++ RegEnumValue() - 无法迭代每个值
【发布时间】:2011-12-23 20:35:54
【问题描述】:

我想获取特定键路径下的所有注册表值,但 RegEnumValue() 总是返回错误代码 259 作为 ERROR_NO_MORE_ITEMS 并且 sectionValue 具有无意义的值。我手动检查注册表,指定键下有值。

例如。
关键是 MyTestApp

键值为 ManualTestCase = 10

键值为 AutomationTestCase = 50

    HKEY hKey;      //registry key handle
    LONG lResult;   //result of registry operations
    DWORD dwType, dwSize=0;

    //try to open the key that we are currently pointing at with rootPath
    lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, rootPath, NULL, KEY_ALL_ACCESS, &hKey);

    if (lResult == ERROR_SUCCESS)
    {
        LPTSTR className = NULL;
        DWORD classNameSize = MAX_PATH;
        DWORD subKey = 0; 
        DWORD maxSubKey;
        DWORD maxClass;
        DWORD value;
        DWORD maxValue;
        DWORD maxValueData;
        DWORD securityDescriptor;
        FILETIME ftLastWriteTime;
        DWORD sectionNameSize;
        int j;

        //to get total keys for the specified path
        lResult = RegQueryInfoKey(hKey, className, &classNameSize, NULL, 
                                    &subKey, &maxSubKey, &maxClass, &value, &maxValue, 
                                    &maxValueData, &securityDescriptor, &ftLastWriteTime);

        if(lResult == ERROR_SUCCESS)
        {
            for(int i = 0; i < subKey; i++)
            {                   
                LPTSTR sectionName = new TCHAR[1096];
                sectionNameSize = 1096;
                ftLastWriteTime.dwHighDateTime = 0;
                ftLastWriteTime.dwLowDateTime = 0;

                //enumerate all the registry key names for specified path
                lResult = RegEnumKeyEx(hKey, i, sectionName, 
                                &sectionNameSize, NULL, NULL,
                                NULL, &ftLastWriteTime);

                CString testStr = sectionName;
                if(lResult == ERROR_SUCCESS)
                {
                    j = 0;
                    do
                    {
                        LPTSTR sectionValue;
                        DWORD sectionValueSize = 4096;
                        DWORD dwType;

                        //enumerate all the values for specified key
                        lResult = RegEnumValue(hKey, j, sectionName, 
                                                    &sectionNameSize, NULL, &dwType, 
                                                    (LPBYTE)sectionValue, &sectionValueSize); 

                        //
                        if(lResult == ERROR_SUCCESS) 
                        {
                            //do something to the data
                            bool whatever = true;                               
                        }
                        else if(lResult == ERROR_MORE_DATA)
                        {
                            //
                            bool yeahSure = true;
                        }
                        j++;

                    }while(lResult != ERROR_NO_MORE_ITEMS);
                }

                delete[] sectionName;
            }
        }
    }

    RegCloseKey(hKey);

【问题讨论】:

    标签: c++ windows winapi registry


    【解决方案1】:

    我猜你的问题在于你如何使用lResult = RegEnumKeyEx(hKey, i, sectionName,...

    您正在尝试枚举子键的值而不实际打开该子键。

    【讨论】:

    • 谢谢,就是这样。调用 RegOpenKeyEx(HKEY_LOCAL_MACHINE, rootPath + "\\" + sectionName, NULL, KEY_ALL_ACCESS, &hKey);在调用 RegEnumValue(..) 之前,可以得到正确的值。
    • 我还需要创建 KHEY 变量 hKey1 并传入 RegOpenKeyEx(,,,,&hKey1) 引用的 RegEnumValue()。完成对值的迭代后,我必须关闭 hKey1 否则我无法进入下一个子键。
    • 你也忘了在值枚举循环中重新初始化sectionNameSize。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    • 2014-12-08
    • 2013-03-10
    • 1970-01-01
    • 2016-08-23
    相关资源
    最近更新 更多