From 2b47e180bea35cfcb9de482103145ecd6aa2fe30 Mon Sep 17 00:00:00 2001 From: GiovanniDicanio Date: Mon, 21 Mar 2022 21:01:43 +0100 Subject: [PATCH] Use LSTATUS instead of LONG for return code type Use the LSTATUS typedef (instead of LONG) to represent the type of return codes from Windows Registry APIs. --- README.md | 43 +++++++---- WinReg/WinReg.hpp | 163 +++++++++++++++++++++++------------------- WinReg/WinRegTest.cpp | 1 - 3 files changed, 118 insertions(+), 89 deletions(-) diff --git a/README.md b/README.md index df13f00..ce98ec1 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,17 @@ -# WinReg v5.0.0 +# WinReg v5.0.1 ## High-level C++ Wrapper Around the Low-level Windows Registry C-interface API by Giovanni Dicanio The Windows Registry C-interface API is _very low-level_ and _hard_ to use. -I developed some **C++ wrappers** around this low-level Win32 API, to raise the semantic level, using C++ classes like `std::wstring`, `std::vector`, etc. instead of raw C-style buffers and low-level mechanisms. +I developed some **C++ wrappers** around this low-level Win32 API, to raise the semantic level, +using C++ classes like `std::wstring`, `std::vector`, etc. instead of raw C-style buffers and +low-level mechanisms. -For example, the `REG_MULTI_SZ` registry type associated to double-NUL-terminated C-style strings is handled using a much easier higher-level `vector`. My C++ code does the _translation_ between high-level C++ STL-based stuff and the low-level Win32 C-interface API. +For example, the `REG_MULTI_SZ` registry type associated to double-NUL-terminated C-style strings +is handled using a much easier higher-level `vector`. My C++ code does the _translation_ +between high-level C++ STL-based stuff and the low-level Win32 C-interface API. Moreover, Win32 error codes are translated to C++ exceptions. @@ -23,9 +27,12 @@ The Win32 registry value types are mapped to C++ higher-level types according th | `REG_BINARY` | `std::vector` | -This code is currently developed using **Visual Studio 2019** with **C++17** features enabled (`/std:c++17`). I have no longer tested the code with previous compilers. The code compiles cleanly at warning level 4 (`/W4`) in both 32-bit and 64-bit builds. +This code is currently developed using **Visual Studio 2019** with **C++17** features enabled +(`/std:c++17`). I have no longer tested the code with previous compilers. +The code compiles cleanly at warning level 4 (`/W4`) in both 32-bit and 64-bit builds. -This is a **header-only** library, implemented in the **[`WinReg.hpp`](WinReg/WinReg.hpp)** header file. +This is a **header-only** library, implemented in the **[`WinReg.hpp`](WinReg/WinReg.hpp)** +header file. `WinRegTest.cpp` contains some demo/test code for the library: check it out for some sample usage. @@ -33,9 +40,11 @@ The library exposes three main classes: * `RegKey`: a tiny efficient wrapper around raw Win32 `HKEY` handles * `RegException`: an exception class to signal error conditions -* `RegResult`: a tiny wrapper around Windows Registry API `LONG` error codes, returned by some `Try` methods (like `RegKey::TryOpen`) +* `RegResult`: a tiny wrapper around Windows Registry API `LSTATUS` error codes, +returned by some `Try` methods (like `RegKey::TryOpen`) -There are many member functions inside the `RegKey` class, that wrap many parts of the native C-interface Windows Registry API, in a convenient C++ way. +There are many member functions inside the `RegKey` class, that wrap many parts of the native +C-interface Windows Registry API, in a convenient higher-level C++ way. For example, you can simply open a registry key and get registry values with C++ code like this: @@ -53,7 +62,8 @@ RegKey key{}; key.Open(HKEY_CURRENT_USER, L"SOFTWARE\\SomeKey"); ``` -The above code will throw an exception on error. If you prefer to check return codes, you can do that as well: +The above code will throw an exception on error. If you prefer to check return codes, you can do +that as well: ```c++ RegKey key; @@ -87,7 +97,8 @@ for (const auto & v : values) } ``` -In addition, you can also use the `RegKey::TryGet...Value` methods, that return `std::optional` instead of throwing on errors: +In addition, you can also use the `RegKey::TryGet...Value` methods, that return `std::optional` +instead of throwing on errors: ```c++ // RegKey::TryGetDwordValue() returns a std::optional; @@ -103,10 +114,12 @@ else } ``` -Note that many methods are available in _two forms_: one that _throws an exception_ of type `RegException` on error (e.g. `RegKey::Open`), -and another that _returns an error status object_ of type `RegResult` (e.g. `RegKey::TryOpen`) instead of throwing an exception. -In addition, as indicated above, some methods like the `RegKey::TryGet...Value` ones return `std::optional` instead of throwing exceptions; -in case of errors, the returned `std::optional` _does not contain_ any value. +Note that many methods are available in _two forms_: one that _throws an exception_ of type +`RegException` on error (e.g. `RegKey::Open`), and another that _returns an error status object_ +of type `RegResult` (e.g. `RegKey::TryOpen`) instead of throwing an exception. +In addition, as indicated above, some methods like the `RegKey::TryGet...Value` ones return +`std::optional` instead of throwing exceptions; in case of errors, the returned `std::optional` +_does not contain_ any value. You can take a look at the test code in `WinRegTest.cpp` for some sample usage. @@ -114,5 +127,5 @@ The library stuff lives under the `winreg` namespace. See the [**`WinReg.hpp`**](WinReg/WinReg.hpp) header for more details and **documentation**. -Thanks to everyone who contributed to this project with some additional features and constructive comments and suggestions. - +Thanks to everyone who contributed to this project with some additional features and constructive +comments and suggestions. \ No newline at end of file diff --git a/WinReg/WinReg.hpp b/WinReg/WinReg.hpp index 6b2c976..3a80e73 100644 --- a/WinReg/WinReg.hpp +++ b/WinReg/WinReg.hpp @@ -9,7 +9,7 @@ // Copyright (C) by Giovanni Dicanio // // First version: 2017, January 22nd -// Last update: 2022, March 4th +// Last update: 2022, March 21th // // E-mail: . AT REMOVE_THIS gmail.com // @@ -33,9 +33,10 @@ // ATL's CString is not used, to avoid dependencies from ATL or MFC. // // Compiler: Visual Studio 2019 -// Code compiles cleanly at /W4 on both 32-bit and 64-bit builds. +// C++ Language Standard: C++17 (/std:c++17) +// Code compiles cleanly at warning level 4 (/W4) on both 32-bit and 64-bit builds. // -// Requires building in Unicode mode (which is the default since VS2005). +// Requires building in Unicode mode (which has been the default since VS2005). // // =========================================================================== // @@ -314,8 +315,11 @@ class RegKey ExpandStringOption expandOption = ExpandStringOption::DontExpand ) const; - [[nodiscard]] std::optional> TryGetMultiStringValue(const std::wstring& valueName) const; - [[nodiscard]] std::optional> TryGetBinaryValue(const std::wstring& valueName) const; + [[nodiscard]] std::optional> + TryGetMultiStringValue(const std::wstring& valueName) const; + + [[nodiscard]] std::optional> + TryGetBinaryValue(const std::wstring& valueName) const; // @@ -418,13 +422,23 @@ class RegKey [[nodiscard]] RegResult TryDeleteValue(const std::wstring& valueName) noexcept; [[nodiscard]] RegResult TryDeleteKey(const std::wstring& subKey, REGSAM desiredAccess) noexcept; [[nodiscard]] RegResult TryDeleteTree(const std::wstring& subKey) noexcept; - [[nodiscard]] RegResult TryCopyTree(const std::wstring& sourceSubKey, const RegKey& destKey) noexcept; + + [[nodiscard]] RegResult TryCopyTree(const std::wstring& sourceSubKey, + const RegKey& destKey) noexcept; + [[nodiscard]] RegResult TryFlushKey() noexcept; - [[nodiscard]] RegResult TryLoadKey(const std::wstring& subKey, const std::wstring& filename) noexcept; - [[nodiscard]] RegResult TrySaveKey(const std::wstring& filename, SECURITY_ATTRIBUTES* securityAttributes) const noexcept; + + [[nodiscard]] RegResult TryLoadKey(const std::wstring& subKey, + const std::wstring& filename) noexcept; + + [[nodiscard]] RegResult TrySaveKey(const std::wstring& filename, + SECURITY_ATTRIBUTES* securityAttributes) const noexcept; + [[nodiscard]] RegResult TryEnableReflectionKey() noexcept; [[nodiscard]] RegResult TryDisableReflectionKey() noexcept; - [[nodiscard]] RegResult TryConnectRegistry(const std::wstring& machineName, HKEY hKeyPredefined) noexcept; + + [[nodiscard]] RegResult TryConnectRegistry(const std::wstring& machineName, + HKEY hKeyPredefined) noexcept; // Return a string representation of Windows registry types @@ -454,13 +468,13 @@ class RegException : public std::system_error { public: - RegException(LONG errorCode, const char* message); - RegException(LONG errorCode, const std::string& message); + RegException(LSTATUS errorCode, const char* message); + RegException(LSTATUS errorCode, const std::string& message); }; //------------------------------------------------------------------------------ -// A tiny wrapper around LONG return codes used by the Windows Registry API. +// A tiny wrapper around LSTATUS return codes used by the Windows Registry API. //------------------------------------------------------------------------------ class RegResult { @@ -472,7 +486,7 @@ class RegResult // Conversion constructor, *not* marked "explicit" on purpose, // allows easy and convenient conversion from Win32 API return code type // to this C++ wrapper. - RegResult(LONG result) noexcept; + RegResult(LSTATUS result) noexcept; // Is the wrapped code a success code? [[nodiscard]] bool IsOk() const noexcept; @@ -484,7 +498,7 @@ class RegResult [[nodiscard]] explicit operator bool() const noexcept; // Get the wrapped Win32 code - [[nodiscard]] LONG Code() const noexcept; + [[nodiscard]] LSTATUS Code() const noexcept; // Return the system error message associated to the current error code [[nodiscard]] std::wstring ErrorMessage() const; @@ -496,7 +510,7 @@ class RegResult private: // Error code returned by Windows Registry C API; // default initialized to success code. - LONG m_result{ ERROR_SUCCESS }; + LSTATUS m_result{ ERROR_SUCCESS }; }; @@ -619,9 +633,7 @@ class ScopedLocalFree // E.g.: // Hello\0World\0\0 //------------------------------------------------------------------------------ -[[nodiscard]] inline std::vector BuildMultiString( - const std::vector& data -) +[[nodiscard]] inline std::vector BuildMultiString(const std::vector& data) { // Special case of the empty multi-string if (data.empty()) @@ -693,9 +705,7 @@ class ScopedLocalFree // // Also supports embedded empty strings in the sequence. //------------------------------------------------------------------------------ -[[nodiscard]] inline std::vector ParseMultiString( - const std::vector& data -) +[[nodiscard]] inline std::vector ParseMultiString(const std::vector& data) { // Make sure that there are two terminating L'\0's at the end of the sequence if (!IsDoubleNullTerminated(data)) @@ -953,7 +963,7 @@ inline void RegKey::Create( ) { HKEY hKey = nullptr; - LONG retCode = ::RegCreateKeyExW( + LSTATUS retCode = ::RegCreateKeyExW( hKeyParent, subKey.c_str(), 0, // reserved @@ -984,7 +994,7 @@ inline void RegKey::Open( ) { HKEY hKey = nullptr; - LONG retCode = ::RegOpenKeyExW( + LSTATUS retCode = ::RegOpenKeyExW( hKeyParent, subKey.c_str(), REG_NONE, // default options @@ -1090,7 +1100,7 @@ inline void RegKey::SetDwordValue(const std::wstring& valueName, const DWORD dat { _ASSERTE(IsValid()); - LONG retCode = ::RegSetValueExW( + LSTATUS retCode = ::RegSetValueExW( m_hKey, valueName.c_str(), 0, // reserved @@ -1109,7 +1119,7 @@ inline void RegKey::SetQwordValue(const std::wstring& valueName, const ULONGLONG { _ASSERTE(IsValid()); - LONG retCode = ::RegSetValueExW( + LSTATUS retCode = ::RegSetValueExW( m_hKey, valueName.c_str(), 0, // reserved @@ -1131,7 +1141,7 @@ inline void RegKey::SetStringValue(const std::wstring& valueName, const std::wst // String size including the terminating NUL, in bytes const DWORD dataSize = static_cast((data.length() + 1) * sizeof(wchar_t)); - LONG retCode = ::RegSetValueExW( + LSTATUS retCode = ::RegSetValueExW( m_hKey, valueName.c_str(), 0, // reserved @@ -1153,7 +1163,7 @@ inline void RegKey::SetExpandStringValue(const std::wstring& valueName, const st // String size including the terminating NUL, in bytes const DWORD dataSize = static_cast((data.length() + 1) * sizeof(wchar_t)); - LONG retCode = ::RegSetValueExW( + LSTATUS retCode = ::RegSetValueExW( m_hKey, valueName.c_str(), 0, // reserved @@ -1181,7 +1191,7 @@ inline void RegKey::SetMultiStringValue( // Total size, in bytes, of the whole multi-string structure const DWORD dataSize = static_cast(multiString.size() * sizeof(wchar_t)); - LONG retCode = ::RegSetValueExW( + LSTATUS retCode = ::RegSetValueExW( m_hKey, valueName.c_str(), 0, // reserved @@ -1203,7 +1213,7 @@ inline void RegKey::SetBinaryValue(const std::wstring& valueName, const std::vec // Total data size, in bytes const DWORD dataSize = static_cast(data.size()); - LONG retCode = ::RegSetValueExW( + LSTATUS retCode = ::RegSetValueExW( m_hKey, valueName.c_str(), 0, // reserved @@ -1226,7 +1236,7 @@ inline void RegKey::SetBinaryValue( { _ASSERTE(IsValid()); - LONG retCode = ::RegSetValueExW( + LSTATUS retCode = ::RegSetValueExW( m_hKey, valueName.c_str(), 0, // reserved @@ -1249,7 +1259,7 @@ inline DWORD RegKey::GetDwordValue(const std::wstring& valueName) const DWORD dataSize = sizeof(data); // size of data, in bytes constexpr DWORD flags = RRF_RT_REG_DWORD; - LONG retCode = ::RegGetValueW( + LSTATUS retCode = ::RegGetValueW( m_hKey, nullptr, // no subkey valueName.c_str(), @@ -1275,7 +1285,7 @@ inline ULONGLONG RegKey::GetQwordValue(const std::wstring& valueName) const DWORD dataSize = sizeof(data); // size of data, in bytes constexpr DWORD flags = RRF_RT_REG_QWORD; - LONG retCode = ::RegGetValueW( + LSTATUS retCode = ::RegGetValueW( m_hKey, nullptr, // no subkey valueName.c_str(), @@ -1300,7 +1310,7 @@ inline std::wstring RegKey::GetStringValue(const std::wstring& valueName) const // Get the size of the result string DWORD dataSize = 0; // size of data, in bytes constexpr DWORD flags = RRF_RT_REG_SZ; - LONG retCode = ::RegGetValueW( + LSTATUS retCode = ::RegGetValueW( m_hKey, nullptr, // no subkey valueName.c_str(), @@ -1358,7 +1368,7 @@ inline std::wstring RegKey::GetExpandStringValue( // Get the size of the result string DWORD dataSize = 0; // size of data, in bytes - LONG retCode = ::RegGetValueW( + LSTATUS retCode = ::RegGetValueW( m_hKey, nullptr, // no subkey valueName.c_str(), @@ -1406,7 +1416,7 @@ inline std::vector RegKey::GetMultiStringValue(const std::wstring& // Request the size of the multi-string, in bytes DWORD dataSize = 0; constexpr DWORD flags = RRF_RT_REG_MULTI_SZ; - LONG retCode = ::RegGetValueW( + LSTATUS retCode = ::RegGetValueW( m_hKey, nullptr, // no subkey valueName.c_str(), @@ -1417,7 +1427,8 @@ inline std::vector RegKey::GetMultiStringValue(const std::wstring& ); if (retCode != ERROR_SUCCESS) { - throw RegException{ retCode, "Cannot get size of multi-string value: RegGetValueW failed." }; + throw RegException{ retCode, + "Cannot get size of multi-string value: RegGetValueW failed." }; } // Allocate room for the result multi-string. @@ -1458,7 +1469,7 @@ inline std::vector RegKey::GetBinaryValue(const std::wstring& valueName) c // Get the size of the binary data DWORD dataSize = 0; // size of data, in bytes constexpr DWORD flags = RRF_RT_REG_BINARY; - LONG retCode = ::RegGetValueW( + LSTATUS retCode = ::RegGetValueW( m_hKey, nullptr, // no subkey valueName.c_str(), @@ -1502,7 +1513,7 @@ inline std::optional RegKey::TryGetDwordValue(const std::wstring& valueNa DWORD dataSize = sizeof(data); // size of data, in bytes constexpr DWORD flags = RRF_RT_REG_DWORD; - LONG retCode = ::RegGetValueW( + LSTATUS retCode = ::RegGetValueW( m_hKey, nullptr, // no subkey valueName.c_str(), @@ -1528,7 +1539,7 @@ inline std::optional RegKey::TryGetQwordValue(const std::wstring& val DWORD dataSize = sizeof(data); // size of data, in bytes constexpr DWORD flags = RRF_RT_REG_QWORD; - LONG retCode = ::RegGetValueW( + LSTATUS retCode = ::RegGetValueW( m_hKey, nullptr, // no subkey valueName.c_str(), @@ -1553,7 +1564,7 @@ inline std::optional RegKey::TryGetStringValue(const std::wstring& // Get the size of the result string DWORD dataSize = 0; // size of data, in bytes constexpr DWORD flags = RRF_RT_REG_SZ; - LONG retCode = ::RegGetValueW( + LSTATUS retCode = ::RegGetValueW( m_hKey, nullptr, // no subkey valueName.c_str(), @@ -1611,7 +1622,7 @@ inline std::optional RegKey::TryGetExpandStringValue( // Get the size of the result string DWORD dataSize = 0; // size of data, in bytes - LONG retCode = ::RegGetValueW( + LSTATUS retCode = ::RegGetValueW( m_hKey, nullptr, // no subkey valueName.c_str(), @@ -1652,14 +1663,15 @@ inline std::optional RegKey::TryGetExpandStringValue( } -inline std::optional> RegKey::TryGetMultiStringValue(const std::wstring& valueName) const +inline std::optional> + RegKey::TryGetMultiStringValue(const std::wstring& valueName) const { _ASSERTE(IsValid()); // Request the size of the multi-string, in bytes DWORD dataSize = 0; constexpr DWORD flags = RRF_RT_REG_MULTI_SZ; - LONG retCode = ::RegGetValueW( + LSTATUS retCode = ::RegGetValueW( m_hKey, nullptr, // no subkey valueName.c_str(), @@ -1704,14 +1716,15 @@ inline std::optional> RegKey::TryGetMultiStringValue(c } -inline std::optional> RegKey::TryGetBinaryValue(const std::wstring& valueName) const +inline std::optional> + RegKey::TryGetBinaryValue(const std::wstring& valueName) const { _ASSERTE(IsValid()); // Get the size of the binary data DWORD dataSize = 0; // size of data, in bytes constexpr DWORD flags = RRF_RT_REG_BINARY; - LONG retCode = ::RegGetValueW( + LSTATUS retCode = ::RegGetValueW( m_hKey, nullptr, // no subkey valueName.c_str(), @@ -1755,7 +1768,7 @@ inline std::vector RegKey::EnumSubKeys() const // and the maximum length of the subkey names DWORD subKeyCount = 0; DWORD maxSubKeyNameLen = 0; - LONG retCode = ::RegQueryInfoKeyW( + LSTATUS retCode = ::RegQueryInfoKeyW( m_hKey, nullptr, // no user-defined class nullptr, // no user-defined class size @@ -1830,7 +1843,7 @@ inline std::vector> RegKey::EnumValues() const // and the maximum length of the value names DWORD valueCount = 0; DWORD maxValueNameLen = 0; - LONG retCode = ::RegQueryInfoKeyW( + LSTATUS retCode = ::RegQueryInfoKeyW( m_hKey, nullptr, // no user-defined class nullptr, // no user-defined class size @@ -1909,7 +1922,7 @@ inline std::optional> RegKey::TryEnumSubKeys() const // and the maximum length of the subkey names DWORD subKeyCount = 0; DWORD maxSubKeyNameLen = 0; - LONG retCode = ::RegQueryInfoKeyW( + LSTATUS retCode = ::RegQueryInfoKeyW( m_hKey, nullptr, // no user-defined class nullptr, // no user-defined class size @@ -1981,7 +1994,7 @@ inline std::optional>> RegKey::TryEnu // and the maximum length of the value names DWORD valueCount = 0; DWORD maxValueNameLen = 0; - LONG retCode = ::RegQueryInfoKeyW( + LSTATUS retCode = ::RegQueryInfoKeyW( m_hKey, nullptr, // no user-defined class nullptr, // no user-defined class size @@ -2055,7 +2068,7 @@ inline DWORD RegKey::QueryValueType(const std::wstring& valueName) const DWORD typeId = 0; // will be returned by RegQueryValueEx - LONG retCode = ::RegQueryValueExW( + LSTATUS retCode = ::RegQueryValueExW( m_hKey, valueName.c_str(), nullptr, // reserved @@ -2079,7 +2092,7 @@ inline std::optional RegKey::TryQueryValueType(const std::wstring& valueN DWORD typeId = 0; // will be returned by RegQueryValueEx - LONG retCode = ::RegQueryValueExW( + LSTATUS retCode = ::RegQueryValueExW( m_hKey, valueName.c_str(), nullptr, // reserved @@ -2102,7 +2115,7 @@ inline RegKey::InfoKey RegKey::QueryInfoKey() const _ASSERTE(IsValid()); InfoKey infoKey{}; - LONG retCode = ::RegQueryInfoKeyW( + LSTATUS retCode = ::RegQueryInfoKeyW( m_hKey, nullptr, nullptr, @@ -2130,7 +2143,7 @@ inline std::optional RegKey::TryQueryInfoKey() const _ASSERTE(IsValid()); InfoKey infoKey{}; - LONG retCode = ::RegQueryInfoKeyW( + LSTATUS retCode = ::RegQueryInfoKeyW( m_hKey, nullptr, nullptr, @@ -2156,7 +2169,7 @@ inline std::optional RegKey::TryQueryInfoKey() const inline RegKey::KeyReflection RegKey::QueryReflectionKey() const { BOOL isReflectionDisabled = FALSE; - LONG retCode = ::RegQueryReflectionKey(m_hKey, &isReflectionDisabled); + LSTATUS retCode = ::RegQueryReflectionKey(m_hKey, &isReflectionDisabled); if (retCode != ERROR_SUCCESS) { throw RegException{ retCode, "RegQueryReflectionKey failed." }; @@ -2170,7 +2183,7 @@ inline RegKey::KeyReflection RegKey::QueryReflectionKey() const inline std::optional RegKey::TryQueryReflectionKey() const { BOOL isReflectionDisabled = FALSE; - LONG retCode = ::RegQueryReflectionKey(m_hKey, &isReflectionDisabled); + LSTATUS retCode = ::RegQueryReflectionKey(m_hKey, &isReflectionDisabled); if (retCode != ERROR_SUCCESS) { return {}; @@ -2186,7 +2199,7 @@ inline void RegKey::DeleteValue(const std::wstring& valueName) { _ASSERTE(IsValid()); - LONG retCode = ::RegDeleteValueW(m_hKey, valueName.c_str()); + LSTATUS retCode = ::RegDeleteValueW(m_hKey, valueName.c_str()); if (retCode != ERROR_SUCCESS) { throw RegException{ retCode, "RegDeleteValueW failed." }; @@ -2206,7 +2219,7 @@ inline void RegKey::DeleteKey(const std::wstring& subKey, const REGSAM desiredAc { _ASSERTE(IsValid()); - LONG retCode = ::RegDeleteKeyExW(m_hKey, subKey.c_str(), desiredAccess, 0); + LSTATUS retCode = ::RegDeleteKeyExW(m_hKey, subKey.c_str(), desiredAccess, 0); if (retCode != ERROR_SUCCESS) { throw RegException{ retCode, "RegDeleteKeyExW failed." }; @@ -2214,7 +2227,8 @@ inline void RegKey::DeleteKey(const std::wstring& subKey, const REGSAM desiredAc } -inline RegResult RegKey::TryDeleteKey(const std::wstring& subKey, const REGSAM desiredAccess) noexcept +inline RegResult RegKey::TryDeleteKey(const std::wstring& subKey, + const REGSAM desiredAccess) noexcept { _ASSERTE(IsValid()); @@ -2226,7 +2240,7 @@ inline void RegKey::DeleteTree(const std::wstring& subKey) { _ASSERTE(IsValid()); - LONG retCode = ::RegDeleteTreeW(m_hKey, subKey.c_str()); + LSTATUS retCode = ::RegDeleteTreeW(m_hKey, subKey.c_str()); if (retCode != ERROR_SUCCESS) { throw RegException{ retCode, "RegDeleteTreeW failed." }; @@ -2246,7 +2260,7 @@ inline void RegKey::CopyTree(const std::wstring& sourceSubKey, const RegKey& des { _ASSERTE(IsValid()); - LONG retCode = ::RegCopyTreeW(m_hKey, sourceSubKey.c_str(), destKey.Get()); + LSTATUS retCode = ::RegCopyTreeW(m_hKey, sourceSubKey.c_str(), destKey.Get()); if (retCode != ERROR_SUCCESS) { throw RegException{ retCode, "RegCopyTreeW failed." }; @@ -2254,7 +2268,8 @@ inline void RegKey::CopyTree(const std::wstring& sourceSubKey, const RegKey& des } -inline RegResult RegKey::TryCopyTree(const std::wstring& sourceSubKey, const RegKey& destKey) noexcept +inline RegResult RegKey::TryCopyTree(const std::wstring& sourceSubKey, + const RegKey& destKey) noexcept { _ASSERTE(IsValid()); @@ -2266,7 +2281,7 @@ inline void RegKey::FlushKey() { _ASSERTE(IsValid()); - LONG retCode = ::RegFlushKey(m_hKey); + LSTATUS retCode = ::RegFlushKey(m_hKey); if (retCode != ERROR_SUCCESS) { throw RegException{ retCode, "RegFlushKey failed." }; @@ -2286,7 +2301,7 @@ inline void RegKey::LoadKey(const std::wstring& subKey, const std::wstring& file { Close(); - LONG retCode = ::RegLoadKeyW(m_hKey, subKey.c_str(), filename.c_str()); + LSTATUS retCode = ::RegLoadKeyW(m_hKey, subKey.c_str(), filename.c_str()); if (retCode != ERROR_SUCCESS) { throw RegException{ retCode, "RegLoadKeyW failed." }; @@ -2294,7 +2309,8 @@ inline void RegKey::LoadKey(const std::wstring& subKey, const std::wstring& file } -inline RegResult RegKey::TryLoadKey(const std::wstring& subKey, const std::wstring& filename) noexcept +inline RegResult RegKey::TryLoadKey(const std::wstring& subKey, + const std::wstring& filename) noexcept { Close(); @@ -2309,7 +2325,7 @@ inline void RegKey::SaveKey( { _ASSERTE(IsValid()); - LONG retCode = ::RegSaveKeyW(m_hKey, filename.c_str(), securityAttributes); + LSTATUS retCode = ::RegSaveKeyW(m_hKey, filename.c_str(), securityAttributes); if (retCode != ERROR_SUCCESS) { throw RegException{ retCode, "RegSaveKeyW failed." }; @@ -2330,7 +2346,7 @@ inline RegResult RegKey::TrySaveKey( inline void RegKey::EnableReflectionKey() { - LONG retCode = ::RegEnableReflectionKey(m_hKey); + LSTATUS retCode = ::RegEnableReflectionKey(m_hKey); if (retCode != ERROR_SUCCESS) { throw RegException{ retCode, "RegEnableReflectionKey failed." }; @@ -2346,7 +2362,7 @@ inline RegResult RegKey::TryEnableReflectionKey() noexcept inline void RegKey::DisableReflectionKey() { - LONG retCode = ::RegDisableReflectionKey(m_hKey); + LSTATUS retCode = ::RegDisableReflectionKey(m_hKey); if (retCode != ERROR_SUCCESS) { throw RegException{ retCode, "RegDisableReflectionKey failed." }; @@ -2366,7 +2382,7 @@ inline void RegKey::ConnectRegistry(const std::wstring& machineName, const HKEY Close(); HKEY hKeyResult = nullptr; - LONG retCode = ::RegConnectRegistryW(machineName.c_str(), hKeyPredefined, &hKeyResult); + LSTATUS retCode = ::RegConnectRegistryW(machineName.c_str(), hKeyPredefined, &hKeyResult); if (retCode != ERROR_SUCCESS) { throw RegException{ retCode, "RegConnectRegistryW failed." }; @@ -2377,7 +2393,8 @@ inline void RegKey::ConnectRegistry(const std::wstring& machineName, const HKEY } -inline RegResult RegKey::TryConnectRegistry(const std::wstring& machineName, const HKEY hKeyPredefined) noexcept +inline RegResult RegKey::TryConnectRegistry(const std::wstring& machineName, + const HKEY hKeyPredefined) noexcept { // Safely close any previously opened key Close(); @@ -2417,12 +2434,12 @@ inline std::wstring RegKey::RegTypeToString(const DWORD regType) // RegException Inline Methods //------------------------------------------------------------------------------ -inline RegException::RegException(const LONG errorCode, const char* const message) +inline RegException::RegException(const LSTATUS errorCode, const char* const message) : std::system_error{ errorCode, std::system_category(), message } {} -inline RegException::RegException(const LONG errorCode, const std::string& message) +inline RegException::RegException(const LSTATUS errorCode, const std::string& message) : std::system_error{ errorCode, std::system_category(), message } {} @@ -2431,7 +2448,7 @@ inline RegException::RegException(const LONG errorCode, const std::string& messa // RegResult Inline Methods //------------------------------------------------------------------------------ -inline RegResult::RegResult(const LONG result) noexcept +inline RegResult::RegResult(const LSTATUS result) noexcept : m_result{ result } {} @@ -2454,7 +2471,7 @@ inline RegResult::operator bool() const noexcept } -inline LONG RegResult::Code() const noexcept +inline LSTATUS RegResult::Code() const noexcept { return m_result; } diff --git a/WinReg/WinRegTest.cpp b/WinReg/WinRegTest.cpp index 1328083..648f706 100644 --- a/WinReg/WinRegTest.cpp +++ b/WinReg/WinRegTest.cpp @@ -269,4 +269,3 @@ int main() return kExitOk; } -