Skip to content

Commit

Permalink
Merge pull request #52 from GiovanniDicanio/add-try-set-value-methods
Browse files Browse the repository at this point in the history
Added Error-Returning TrySetXxxValue Methods
  • Loading branch information
GiovanniDicanio authored Mar 29, 2022
2 parents d2cae6b + 5ec924d commit 4ac74bf
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# WinReg v5.0.1
# WinReg v5.1.0
## High-level C++ Wrapper Around the Low-level Windows Registry C-interface API

by Giovanni Dicanio
Expand Down
164 changes: 163 additions & 1 deletion WinReg/WinReg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// Copyright (C) by Giovanni Dicanio
//
// First version: 2017, January 22nd
// Last update: 2022, March 21th
// Last update: 2022, March 29th
//
// E-mail: <first name>.<last name> AT REMOVE_THIS gmail.com
//
Expand Down Expand Up @@ -278,6 +278,36 @@ class RegKey
void SetBinaryValue(const std::wstring& valueName, const void* data, DWORD dataSize);


//
// Registry Value Setters Returning RegResult
// (instead of throwing RegException on error)
//

[[nodiscard]] RegResult TrySetDwordValue(const std::wstring& valueName, DWORD data) noexcept;

[[nodiscard]] RegResult TrySetQwordValue(const std::wstring& valueName,
const ULONGLONG& data) noexcept;

[[nodiscard]] RegResult TrySetStringValue(const std::wstring& valueName,
const std::wstring& data) noexcept;

[[nodiscard]] RegResult TrySetExpandStringValue(const std::wstring& valueName,
const std::wstring& data) noexcept;

[[nodiscard]] RegResult TrySetMultiStringValue(const std::wstring& valueName,
const std::vector<std::wstring>& data);
// Note: The TrySetMultiStringValue method CANNOT be marked noexcept,
// because internally the method *dynamically allocates memory* for creating the multi-string
// that will be stored in the Registry.

[[nodiscard]] RegResult TrySetBinaryValue(const std::wstring& valueName,
const std::vector<BYTE>& data) noexcept;

[[nodiscard]] RegResult TrySetBinaryValue(const std::wstring& valueName,
const void* data,
DWORD dataSize) noexcept;


//
// Registry Value Getters
//
Expand Down Expand Up @@ -1251,6 +1281,138 @@ inline void RegKey::SetBinaryValue(
}


inline RegResult RegKey::TrySetDwordValue(const std::wstring& valueName, const DWORD data) noexcept
{
_ASSERTE(IsValid());

return RegResult{ ::RegSetValueExW(
m_hKey,
valueName.c_str(),
0, // reserved
REG_DWORD,
reinterpret_cast<const BYTE*>(&data),
sizeof(data)
) };
}


inline RegResult RegKey::TrySetQwordValue(const std::wstring& valueName,
const ULONGLONG& data) noexcept
{
_ASSERTE(IsValid());

return RegResult{ ::RegSetValueExW(
m_hKey,
valueName.c_str(),
0, // reserved
REG_QWORD,
reinterpret_cast<const BYTE*>(&data),
sizeof(data)
) };
}


inline RegResult RegKey::TrySetStringValue(const std::wstring& valueName,
const std::wstring& data) noexcept
{
_ASSERTE(IsValid());

// String size including the terminating NUL, in bytes
const DWORD dataSize = static_cast<DWORD>((data.length() + 1) * sizeof(wchar_t));

return RegResult{ ::RegSetValueExW(
m_hKey,
valueName.c_str(),
0, // reserved
REG_SZ,
reinterpret_cast<const BYTE*>(data.c_str()),
dataSize
) };
}


inline RegResult RegKey::TrySetExpandStringValue(const std::wstring& valueName,
const std::wstring& data) noexcept
{
_ASSERTE(IsValid());

// String size including the terminating NUL, in bytes
const DWORD dataSize = static_cast<DWORD>((data.length() + 1) * sizeof(wchar_t));

return RegResult{ ::RegSetValueExW(
m_hKey,
valueName.c_str(),
0, // reserved
REG_EXPAND_SZ,
reinterpret_cast<const BYTE*>(data.c_str()),
dataSize
) };
}


inline RegResult RegKey::TrySetMultiStringValue(const std::wstring& valueName,
const std::vector<std::wstring>& data)
{
_ASSERTE(IsValid());

// First, we have to build a double-NUL-terminated multi-string from the input data.
//
// NOTE: This is the reason why I *cannot* mark this method noexcept,
// since a *dynamic allocation* happens for creating the std::vector in BuildMultiString.
// And, if dynamic memory allocations fail, an exception is thrown.
//
const std::vector<wchar_t> multiString = detail::BuildMultiString(data);

// Total size, in bytes, of the whole multi-string structure
const DWORD dataSize = static_cast<DWORD>(multiString.size() * sizeof(wchar_t));

return RegResult{ ::RegSetValueExW(
m_hKey,
valueName.c_str(),
0, // reserved
REG_MULTI_SZ,
reinterpret_cast<const BYTE*>(&multiString[0]),
dataSize
) };
}


inline RegResult RegKey::TrySetBinaryValue(const std::wstring& valueName,
const std::vector<BYTE>& data) noexcept
{
_ASSERTE(IsValid());

// Total data size, in bytes
const DWORD dataSize = static_cast<DWORD>(data.size());

return RegResult{ ::RegSetValueExW(
m_hKey,
valueName.c_str(),
0, // reserved
REG_BINARY,
&data[0],
dataSize
) };
}


inline RegResult RegKey::TrySetBinaryValue(const std::wstring& valueName,
const void* const data,
const DWORD dataSize) noexcept
{
_ASSERTE(IsValid());

return RegResult{ ::RegSetValueExW(
m_hKey,
valueName.c_str(),
0, // reserved
REG_BINARY,
static_cast<const BYTE*>(data),
dataSize
) };
}


inline DWORD RegKey::GetDwordValue(const std::wstring& valueName) const
{
_ASSERTE(IsValid());
Expand Down
50 changes: 44 additions & 6 deletions WinReg/WinRegTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,44 @@ void Test()
key.SetMultiStringValue(L"TestValueMultiString", testMultiSz);
key.SetBinaryValue(L"TestValueBinary", testBinary);

if (key.TrySetDwordValue(L"TestTryValueDword", testDw).Failed())
{
wcout << L"RegKey::TrySetDwordValue failed.\n";
}

if (key.TrySetQwordValue(L"TestTryValueQword", testQw).Failed())
{
wcout << L"RegKey::TrySetQwordValue failed.\n";
}

if (key.TrySetStringValue(L"TestTryValueString", testSz).Failed())
{
wcout << L"RegKey::TrySetStringValue failed.\n";
}

if (key.TrySetExpandStringValue(L"TestTryValueExpandString", testExpandSz).Failed())
{
wcout << L"RegKey::TrySetExpandStringValue failed.\n";
}

if (key.TrySetMultiStringValue(L"TestTryValueMultiString", testMultiSz).Failed())
{
wcout << L"RegKey::TrySetMultiStringValue failed.\n";
}

if (key.TrySetBinaryValue(L"TestTryValueBinary", testBinary).Failed())
{
wcout << L"RegKey::TrySetBinaryValue failed.\n";
}


DWORD testDw1 = key.GetDwordValue(L"TestValueDword");
if (testDw1 != testDw)
{
wcout << L"RegKey::GetDwordValue failed.\n";
}

if (auto testDw2 = key.TryGetDwordValue(L"TestValueDword"))
if (auto testDw2 = key.TryGetDwordValue(L"TestTryValueDword"))
{
if (testDw2 != testDw)
{
Expand All @@ -112,7 +143,7 @@ void Test()
wcout << L"RegKey::GetQwordValue failed.\n";
}

if (auto testQw2 = key.TryGetQwordValue(L"TestValueQword"))
if (auto testQw2 = key.TryGetQwordValue(L"TestTryValueQword"))
{
if (testQw2 != testQw)
{
Expand All @@ -136,7 +167,7 @@ void Test()
wcout << L"RegKey::GetStringValue failed.\n";
}

if (auto testSz2 = key.TryGetStringValue(L"TestValueString"))
if (auto testSz2 = key.TryGetStringValue(L"TestTryValueString"))
{
if (testSz2 != testSz)
{
Expand All @@ -160,7 +191,7 @@ void Test()
wcout << L"RegKey::GetExpandStringValue failed.\n";
}

if (auto testExpandSz2 = key.TryGetExpandStringValue(L"TestValueExpandString"))
if (auto testExpandSz2 = key.TryGetExpandStringValue(L"TestTryValueExpandString"))
{
if (testExpandSz2 != testExpandSz)
{
Expand All @@ -184,7 +215,7 @@ void Test()
wcout << L"RegKey::GetMultiStringValue failed.\n";
}

if (auto testMultiSz2 = key.TryGetMultiStringValue(L"TestValueMultiString"))
if (auto testMultiSz2 = key.TryGetMultiStringValue(L"TestTryValueMultiString"))
{
if (testMultiSz2 != testMultiSz)
{
Expand All @@ -208,7 +239,7 @@ void Test()
wcout << L"RegKey::GetBinaryValue failed.\n";
}

if (auto testBinary2 = key.TryGetBinaryValue(L"TestValueBinary"))
if (auto testBinary2 = key.TryGetBinaryValue(L"TestTryValueBinary"))
{
if (testBinary2 != testBinary)
{
Expand Down Expand Up @@ -237,6 +268,13 @@ void Test()
key.DeleteValue(L"TestValueExpandString");
key.DeleteValue(L"TestValueMultiString");
key.DeleteValue(L"TestValueBinary");

key.DeleteValue(L"TestTryValueDword");
key.DeleteValue(L"TestTryValueQword");
key.DeleteValue(L"TestTryValueString");
key.DeleteValue(L"TestTryValueExpandString");
key.DeleteValue(L"TestTryValueMultiString");
key.DeleteValue(L"TestTryValueBinary");
}


Expand Down

0 comments on commit 4ac74bf

Please sign in to comment.