RegQueryValueEx()
basically requires you to take a wild guess at the size of the buffer you need to hold the registry value. If you mess it up, you get ERROR_MORE_DATA
for your troubles. You could obviously just call the function in a loop and increase the buffer size every time until you get lucky but that "solution" makes me nauseous. Am I missing a way to do it properly or is the API really as bad as it seems?
The RegQueryValueEx documentation says:
If lpData is NULL, and lpcbData is non-NULL, the function returns ERROR_SUCCESS and stores the size of the data, in bytes, in the variable pointed to by lpcbData. This enables an application to determine the best way to allocate a buffer for the value's data.
In other words, just specify NULL as the buffer, and the function can return the data size in the lpcbData.
So you just have to call the API twice, once to get the size, and once to get the actual data. This method is not uncommon in Windows APIs, by the way.