3

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.

For example.
key is MyTestApp

key value is ManualTestCase = 10

key value is 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);

1 Answer 1

2

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

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

Sign up to request clarification or add additional context in comments.

3 Comments

thank you that is it. After calling RegOpenKeyEx(HKEY_LOCAL_MACHINE, rootPath + "\\" + sectionName, NULL, KEY_ALL_ACCESS, &hKey); before calling RegEnumValue(..), can get the correct values.
Also I need to create KHEY variable hKey1 and pass into the RegEnumValue() referencing from RegOpenKeyEx(,,,,,&hKey1). After finish iterating through the value, I have to close hKey1 else I can't get to the next subkey.
You also forgot to reinitialize sectionNameSize in the value enumeration loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.