I got several requests by people asking to make my C++ WinReg code (which has been using C++17 in its main branch since many months) compile on older C++14 compilers (like VS 2017 in C++14 mode).
Now you can find the C++14-compatible version in this branch.
To make the code C++14-compatible, among other things, I removed the use of [[nodiscard]], removed std::variant (simply substituted with a couple of data members), and replaced the non-const std::wstring::data() method with &s.
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.