windows C++ RegEnumValue() - 无法迭代到每个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8620394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:43:41  来源:igfitidea点击:

C++ RegEnumValue() - fail to iterate to each value

c++windowswinapiregistry

提问by Lufia

I want to get all the registry values under specific key path, but RegEnumValue() always returns back the error code 259 as ERROR_NO_MORE_ITEMS and sectionValue has nonsense value. I check the registry manually and there are values under the specified key.

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

For example.
key is MyTestApp

例如。
关键是 MyTestApp

key value is ManualTestCase = 10

关键值是 ManualTestCase = 10

key value is AutomationTestCase = 50

关键值是 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);

采纳答案by Gabe

My guess is your problem is with how you use lResult = RegEnumKeyEx(hKey, i, sectionName,...

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

You are trying to enumerate values of a subkey without actually opening that subkey.

您正在尝试枚举子项的值,而无需实际打开该子项。