views:

42

answers:

2

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);
    }
A: 

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.

Zafer
can not be lpData cause don't use it. lpValueName could be, but it already has a 8192 array buffer. It baffles me that could be too small???
Tony
Sometimes it seems to put a 0 byte in the middle of a string of the lpValueName and so it cuts off the remainder of the string...
Tony
You set namesize to 4096.Did you try to set it to the array size which is 8192?The documentation says:"A pointer to a variable that specifies the size of the buffer pointed to by the lpValueName parameter, in characters. When the function returns, the variable receives the number of characters stored in the buffer, not including the terminating null character."
Zafer
makes no difference, still throwing me the same error... It seems to be a problem with null bytes...
Tony
Zafer
+2  A: 

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".

Oleg