I often get some fun challenges from coders looking for help in the #winapi channel I frequent on freenode. The coder today was looking for a way to get the output from RegEnumValue and values of type REG_SZ into a vector. Start by creating a Win32 project. In your stdafx.h here's what we need to include:
Next we need a structure that's used to hold the name and data for each registry value. You could make this more generic to handle other types such as a vector of bytes or even turn this into a class with functions that will convert the data into the format you want. I'm keeping it simple and to the point, we're after the strings. Notice I'm using unicode here (explicitly).
The next part is the biggest, the part where we do all of the work! Again I am using unicode explicitly so it's not going to be RegEnumValue, but RegEnumValueW. You can easily convert this to multibyte (but why would you?) by using char instead of wchar_t and std::string instead of std::wstring. Here's the code:
Now you might ask why would I allocate 32 KB for the value name? Why not allocate just the amount of bytes that is needed like we do for the value data? Well, RegEnumValue doesn't support getting the size of the value name without copying it to a buffer like it does the data. If you try and set the third parameter () to NULL RegEnumValue will return 87 (ERROR_INVALID_PARAMETER) and your value name length will be unchanged. For the sake of performance and for the reason above we allocate the maximum amount of characters a value name could be (as specified in MSDN) only once before the loop.
Why am I using vectors for buffers? It's an old practice of mine and I'm sure there are even more modern ways to do this. If you have one comment below. Wherever I can, I want to have my types automatically delete themselves when they go out of scope. This can save you a lot of trouble. Now, I don't have to remember to delete anything wherever the loop ends. When a vector goes out of scope, it will deallocate what it has.
And if you're interested in the implementation (you may have to change the registry key), here yah go: