Reputation: 63190
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
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
Reputation: 221997
There are some errors in your code:
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.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).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.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
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