The native Windows Registry API is a C-interface API, that is low-level and kind of hard and cumbersome to use.
For example, suppose that you simply want to read a string value under a given key. You would end up writing code like this:
And this is just the part to query the destination string length. Then, you need to allocate a string object with proper size (and pay attention to proper size-in-bytes-to-size-in-wchar_ts conversion!), and after that you can finally read the actual string value into the local string object.
That’s definitely a lot of bug-prone C++ code, and this is just to query a string value!
Moreover, in modern C++ code you should prefer using nice higher-level resource manager classes with automatic resource cleanup, instead of raw HKEY handles.
Fortunately, it’s possible to hide that kind of complex and bug-prone code in a nice C++ library, that offers a much more programmer-friendly interface. This is basically what my C++ WinReg library does.
I uploaded on GitHub some C++ code of mine that wraps some Windows registry C-interface APIs, using RAII, STL classes like std::wstring and std::vector, and signals error conditions using exceptions.
Using these high-level C++ wrappers, you can easily access the Windows registry with simple code like this:
// Open a registry key
// Read a DWORD value
DWORD dw = key.GetDwordValue(L"MyValue1");
// Read a string value
wstring s = key.GetStringValue(L"MyValue2");
// Enumerate the values under the given key
auto values = key.EnumValues();
On the May issue of MSDN Magazine you’ll find an article describing some of the techniques applied in this code.