Xoph
Xoph

Reputation: 37

RegEnumValue always throws ERROR_MORE_DATA for last value

No matter what, my call to RegEnumValue is always returning ERROR_MORE_DATA (234) on the last value for a key.

I am getting the size of the buffer necessary by using:

retCode = RegQueryInfoKey(hSubKey,          // key handle
                                      NULL,             // buffer for class name
                                      NULL,             // size of class string
                                      NULL,             // reserved
                                      NULL,             // number of subkeys
                                      NULL,             // longest subkey length
                                      NULL,             // longest class string 
                                      &valueCount,      // number of values for this key 
                                      &cLongestValue,   // longest value name 
                                      NULL,             // longest value data 
                                      NULL,             // security descriptor 
                                      NULL);            // last write time

Then I allocate my array:

currentValueName = new WCHAR[(int)cLongestValue];

Then I try to get the value:

retCode = RegEnumValue(hSubKey, j, currentValueName, &cLongestValue, NULL, NULL, NULL, NULL);

and retCode is always 234 when j = 2 (the last indexed value).

Why am I always getting ERROR_MORE_DATA? It's working fine for the first two values in the key.

Any help is greatly appreciated. I am very new to C++ so there could be something very obvious I am overlooking.

Upvotes: 2

Views: 1181

Answers (1)

Youka
Youka

Reputation: 2715

RegEnumValue, 3rd parameter (lpValueName [out]):

A pointer to a buffer that receives the name of the value as a null-terminated string. This buffer must be large enough to include the terminating null character.

RegQueryInfoKey returns just the value size in characters, so plain data, no string termination.

A pointer to a variable that receives the size of the key's longest value name, in Unicode characters. The size does not include the terminating null character.

ERROR_MORE_DATA tells you that a buffer is too small.

There you are: increase your buffer for one more character to make space for the additional termination.

Upvotes: 1

Related Questions