11#include "pch.h"
22#include "RegistryKey.h"
3+ #include "LoggingHelper.h"
34
45using namespace NppShell::Registry;
6+ using namespace NppShell::Helpers;
7+
8+ extern LoggingHelper g_loggingHelper;
59
610RegistryKey::RegistryKey(HKEY hKey, const wstring& subKey, REGSAM access, bool createIfMissing)
711 : m_hKey(nullptr), m_regsam(access), m_originalHKey(hKey), m_originalSubKey(subKey)
812{
913 if (RegOpenKeyExW(hKey, subKey.data(), 0, access, &m_hKey) == ERROR_SUCCESS)
1014 {
15+ g_loggingHelper.LogMessage(L"RegistryKey::ctor", L"Opened sub key: " + subKey);
1116 return;
1217 }
1318
@@ -20,8 +25,13 @@ RegistryKey::RegistryKey(HKEY hKey, const wstring& subKey, REGSAM access, bool c
2025
2126 if (RegCreateKeyExW(hKey, subKey.data(), 0, nullptr, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, nullptr, &m_hKey, &disposition) != ERROR_SUCCESS)
2227 {
28+ g_loggingHelper.LogMessage(L"RegistryKey::ctor", L"Failed to create sub key: " + subKey);
2329 throw runtime_error("Failed to create registry key.");
2430 }
31+ else
32+ {
33+ g_loggingHelper.LogMessage(L"RegistryKey::ctor", L"Created sub key: " + subKey);
34+ }
2535}
2636
2737RegistryKey::~RegistryKey()
@@ -102,6 +112,7 @@ DWORD RegistryKey::GetDwordValue(const wstring& valueName)
102112
103113 if (RegGetValueW(m_hKey, nullptr, valueName.empty() ? NULL : valueName.data(), RRF_RT_REG_DWORD, nullptr, &value, &dataSize) != ERROR_SUCCESS)
104114 {
115+ g_loggingHelper.LogMessage(L"RegistryKey::GetDwordValue", L"Failed to get DWORD value: " + valueName);
105116 throw runtime_error("Failed to get registry value.");
106117 }
107118
@@ -119,13 +130,15 @@ wstring RegistryKey::GetStringValue(const wstring& valueName)
119130
120131 if (RegGetValueW(m_hKey, nullptr, valueName.empty() ? NULL : valueName.data(), RRF_RT_REG_SZ, nullptr, nullptr, &dataSize) != ERROR_SUCCESS)
121132 {
133+ g_loggingHelper.LogMessage(L"RegistryKey::GetStringValue", L"Failed to get REG_SZ value: " + valueName);
122134 throw runtime_error("Failed to get registry value size.");
123135 }
124136
125137 wstring value(dataSize / sizeof(wchar_t), L'\0');
126138
127139 if (RegGetValueW(m_hKey, nullptr, valueName.empty() ? NULL : valueName.data(), RRF_RT_REG_SZ, nullptr, bit_cast<BYTE*>(value.data()), &dataSize) != ERROR_SUCCESS)
128140 {
141+ g_loggingHelper.LogMessage(L"RegistryKey::GetStringValue", L"Failed to get REG_SZ value: " + valueName);
129142 throw runtime_error("Failed to get registry value.");
130143 }
131144
@@ -141,8 +154,12 @@ void RegistryKey::SetDwordValue(const wstring& valueName, DWORD value)
141154
142155 if (RegSetValueExW(m_hKey, valueName.empty() ? NULL : valueName.data(), 0, REG_DWORD, bit_cast<const BYTE*>(&value), sizeof(DWORD)) != ERROR_SUCCESS)
143156 {
157+ g_loggingHelper.LogMessage(L"RegistryKey::SetDwordValue", L"Failed to set DWORD value: " + valueName);
144158 throw runtime_error("Failed to set registry value.");
145159 }
160+
161+ wstring valueNameLog = valueName.empty() ? L"(default)" : valueName.data();
162+ g_loggingHelper.LogMessage(L"RegistryKey::SetDwordValue", L"Setting DWORD name: " + valueNameLog + L" to: " + to_wstring(value));
146163}
147164
148165void RegistryKey::SetStringValue(const wstring& valueName, const wstring& value)
@@ -154,8 +171,12 @@ void RegistryKey::SetStringValue(const wstring& valueName, const wstring& value)
154171
155172 if (RegSetValueExW(m_hKey, valueName.empty() ? NULL : valueName.data(), 0, REG_SZ, bit_cast<const BYTE*>(value.data()), static_cast<DWORD>((value.length() + 1) * sizeof(wchar_t))) != ERROR_SUCCESS)
156173 {
174+ g_loggingHelper.LogMessage(L"RegistryKey::SetStringValue", L"Failed to set REG_SZ value: " + valueName);
157175 throw runtime_error("Failed to set registry value.");
158176 }
177+
178+ wstring valueNameLog = valueName.empty() ? L"(default)" : valueName.data();
179+ g_loggingHelper.LogMessage(L"RegistryKey::SetStringValue", L"Setting REG_SZ name: " + valueNameLog + L" to: \"" + value + L"\"");
159180}
160181
161182void RegistryKey::DeleteKey()
@@ -167,9 +188,12 @@ void RegistryKey::DeleteKey()
167188
168189 if (RegDeleteTreeW(m_originalHKey, m_originalSubKey.data()) != ERROR_SUCCESS)
169190 {
191+ g_loggingHelper.LogMessage(L"RegistryKey::DeleteKey", L"Failed to delete subkey: " + m_originalSubKey);
170192 throw runtime_error("Failed to delete registry key.");
171193 }
172194
195+ g_loggingHelper.LogMessage(L"RegistryKey::DeleteKey", L"Deleted subkey: " + m_originalSubKey);
196+
173197 m_hKey = nullptr;
174198 m_originalHKey = nullptr;
175199}
0 commit comments