Tony The Lion
Tony The Lion

Reputation: 63190

Registry problem - deleting key/values with C++

The following piece of code seems to unreliably execute and after and undeterministic time it will fail with error code 234 at the RegEnumValue function.

I have not written this code, I am merely trying to debug it. I know there is an issue with doing RegEnumValue and then deleting keys in the while loop.

I am trying to figure out first, why it is throwing this 234 error at seemingly random points, as in, it is never after a consistent number of loop iterations or anything like that.

From what I have seen it fails to fill its name buffer, but this buffer is by no means too small for its purpose, so I don't understand how it could fail??

Could someone please advice on getting rid of this 234 error thrown by the RegEnumValue funciton?

    HKEY key;
    DWORD dw;
    int idx;
    char name[8192];
    DWORD namesize=4096;
    std::string m_path = "SOFTWARE\\Company\\Server 4.0";

    if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,m_path.c_str(),0,KEY_ALL_ACCESS,&key) == ERROR_SUCCESS)
    {
        bool error=false;
        idx=0;
        long result;
        long delresult;
        while (true)
        {
            result = RegEnumValue(key,idx,(char*)name,&namesize,NULL,NULL,NULL,NULL);

            if (result == ERROR_SUCCESS && !error){

                delresult = RegDeleteValue(key,name);
                if (delresult != ERROR_SUCCESS)
                    error = true;

                idx++;
            }
            else
            {
                break;
            }
        }
        RegCloseKey(key);
    }

Upvotes: 1

Views: 3033

Answers (3)

Kantesh N
Kantesh N

Reputation: 133

just change the value of your fourth parameter i.e namesize from 4096 to 8192 .Always MakeSure that it should be always equal to buffer size.

Upvotes: 1

Oleg
Oleg

Reputation: 221997

There are some errors in your code:

  1. The 4-th parameter of RegEnumValue (the namesize) is in-out parameter. So you have to reset namesize to sizeof(name)/sizeof(name[0]) (in case of the usage char type it is just sizeof(name)) inside the while loop before every call of RegEnumValue. It's the main error in your program.
  2. If you don't want to have ERROR_MORE_DATA error any time you have the buffer having 32,767 characters. It is the maximum size of name the the regitry value (see documentation of RegEnumValue).
  3. It is not good to use KEY_ALL_ACCESS in the RegOpenKeyEx. I'll recomend you to change it to KEY_QUERY_VALUE | KEY_SET_VALUE. It is not a real error, but depends on your environment it could be.
  4. It if better to use UNICODE version of all this functions to speed-up a little the code.

UPDATED: Only small comment about the usage of the UNICODE version. Intern Windows work with UNICODE characters. So usage of non-Unicode version of RegEnumValue si more slow because at the evry call a new UICODE memeory block will be allocated and converted to ANSI/Multi-byte. Moreover if you will has a value name written in a language which can't be converted in you Windows ANSI code page (Chinese, Japanese and so on) and some characters will be replaced to '?' (see WC_DEFAULTCHAR flag of WideCharToMultiByte), then it can be that the function RegDeleteValue will fail with the error code like "the value with the name is not exist".

Upvotes: 4

Zafer
Zafer

Reputation: 2190

The answer is at the bottom of that page: http://msdn.microsoft.com/en-us/library/ms724865(VS.85).aspx

Please read the answer of "ERROR_MORE_DATA: lpData too small, or lpValueName too small?" question.

Upvotes: 0

Related Questions