diff --git a/Example/main.cpp b/Example/main.cpp index 30022d7..7584886 100644 --- a/Example/main.cpp +++ b/Example/main.cpp @@ -1,74 +1,73 @@ #include "RegistryHelper.h" #include -int main() +int +main() { - try - { - RegistryHelper registryHelper; - std::wstring subKey = L"SYSTEM\\CurrentControlSet\\Control"; - HKEY hKey = HKEY_LOCAL_MACHINE; + try { + RegistryHelper registryHelper; + std::wstring subKey = L"SYSTEM\\CurrentControlSet\\Control"; + HKEY hKey = HKEY_LOCAL_MACHINE; - // - // Example 1: Reading DWORD value (REG_DWORD) - // - std::wstring valueNameDword = L"BootDriverFlags"; + // + // Example 1: Reading DWORD value (REG_DWORD) + // + std::wstring valueNameDword = L"BootDriverFlags"; - DWORD dwordValue = registryHelper.RegGetDword(hKey, subKey, valueNameDword); - std::wcout << L"DWORD Value: " << dwordValue << std::endl; + DWORD dwordValue = registryHelper.RegGetDword(hKey, subKey, valueNameDword); + std::wcout << L"DWORD Value: " << dwordValue << std::endl; - // - // Example 2: Reading String value (REG_SZ) - // - std::wstring valueNameString = L"CurrentUser"; + // + // Example 2: Reading String value (REG_SZ) + // + std::wstring valueNameString = L"CurrentUser"; - std::wstring stringValue = registryHelper.RegGetString(hKey, subKey, valueNameString); - std::wcout << L"String Value: " << stringValue << std::endl; + std::wstring stringValue = + registryHelper.RegGetString(hKey, subKey, valueNameString); + std::wcout << L"String Value: " << stringValue << std::endl; - // - // Example 3: Reading Multi-String value (REG_MULTI_SZ) - // - std::wstring valueNameMultiString = L"PreshutdownOrder"; + // + // Example 3: Reading Multi-String value (REG_MULTI_SZ) + // + std::wstring valueNameMultiString = L"PreshutdownOrder"; - std::vector multiStringValue = - registryHelper.RegGetMultiString(hKey, subKey, valueNameMultiString); + std::vector multiStringValue = + registryHelper.RegGetMultiString(hKey, subKey, valueNameMultiString); - std::wcout << L"Multi-String Values: " << std::endl; - for (const auto& str : multiStringValue) - { - std::wcout << L" " << str << std::endl; - } + std::wcout << L"Multi-String Values: " << std::endl; + for (const auto& str : multiStringValue) { + std::wcout << L" " << str << std::endl; + } - // - // Example 4: Enumerate sub-keys - // - std::vector> subKeys = registryHelper.RegEnumSubKeys(hKey, subKey); + // + // Example 4: Enumerate sub-keys + // + std::vector> subKeys = + registryHelper.RegEnumSubKeys(hKey, subKey); - std::wcout << L"Sub-Keys: " << std::endl; - for (const auto& subKeyPair : subKeys) - { - std::wcout << L" " << subKeyPair.first << std::endl; - } + std::wcout << L"Sub-Keys: " << std::endl; + for (const auto& subKeyPair : subKeys) { + std::wcout << L" " << subKeyPair.first << std::endl; + } - // - // Example 5: Enumerate values - // - std::vector> values = registryHelper.RegEnumValues(hKey, subKey); + // + // Example 5: Enumerate values + // + std::vector> values = + registryHelper.RegEnumValues(hKey, subKey); - std::wcout << L"Values: " << std::endl; - for (const auto& valuePair : values) - { - std::wcout << L" " << valuePair.first << L" (Type: " << valuePair.second << L")" << std::endl; - } - } - catch (const RegistryError& ex) - { - std::cerr << "Registry Error: " << ex.what() << " (Error Code: " << ex.ErrorCode() << ")" << std::endl; - } - catch (const std::exception& ex) - { - std::cerr << "Error: " << ex.what() << std::endl; - } + std::wcout << L"Values: " << std::endl; + for (const auto& valuePair : values) { + std::wcout << L" " << valuePair.first << L" (Type: " << valuePair.second + << L")" << std::endl; + } - return 0; + } catch (const RegistryError& ex) { + std::cerr << "Registry Error: " << ex.what() + << " (Error Code: " << ex.ErrorCode() << ")" << std::endl; + } catch (const std::exception& ex) { + std::cerr << "Error: " << ex.what() << std::endl; + } + + return 0; } diff --git a/README.md b/README.md index e476aba..aaf1567 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,18 @@ std::vector> subKeys = // Example 5: Enumerate values std::vector> values = registryHelper.RegEnumValues(hKey, subKey); + +// Set DWORD value under an existing key +registryHelper.RegSetDword(HKEY_LOCAL_MACHINE, + L"SOFTWARE\\YourApp", + L"ExistingDWORD", + 42); + +// Set DWORD value under a non-existing key (will create the key first) +registryHelper.RegSetDword(HKEY_LOCAL_MACHINE, + L"SOFTWARE\\NewApp", + L"NewDWORD", + 123); ``` Adjust the registry keys, subkeys, and value names based on your specific use case. diff --git a/RegistryHelper/RegistryHelper.cpp b/RegistryHelper/RegistryHelper.cpp index f1aa810..ef629bd 100644 --- a/RegistryHelper/RegistryHelper.cpp +++ b/RegistryHelper/RegistryHelper.cpp @@ -283,3 +283,61 @@ RegistryHelper::RegEnumValues(HKEY hKey, const std::wstring& subKey) return valueInfo; } + +void +RegistryHelper::RegSetDword(HKEY hKey, + const std::wstring& subKey, + const std::wstring& value, + DWORD data) +{ + m_errorCode = ::RegSetKeyValue( + hKey, subKey.c_str(), value.c_str(), REG_DWORD, &data, sizeof(data)); + if (m_errorCode != ERROR_SUCCESS) { + throw RegistryError("Cannot set DWORD value in registry.", m_errorCode); + } +} + +void +RegistryHelper::RegSetString(HKEY hKey, + const std::wstring& subKey, + const std::wstring& value, + const std::wstring& data) +{ + m_errorCode = + ::RegSetKeyValue(hKey, + subKey.c_str(), + value.c_str(), + REG_SZ, + data.c_str(), + static_cast((data.length() + 1) * sizeof(wchar_t))); + if (m_errorCode != ERROR_SUCCESS) { + throw RegistryError("Cannot set string value in registry.", m_errorCode); + } +} + +void +RegistryHelper::RegSetMultiString(HKEY hKey, + const std::wstring& subKey, + const std::wstring& value, + const std::vector& data) +{ + // Concatenate the strings and add an extra null character at the end + std::wstring multiString; + for (const auto& str : data) { + multiString += str; + multiString.push_back(L'\0'); + } + multiString.push_back(L'\0'); // Extra null character at the end + + m_errorCode = ::RegSetKeyValue( + hKey, + subKey.c_str(), + value.c_str(), + REG_MULTI_SZ, + multiString.c_str(), + static_cast(multiString.length() * sizeof(wchar_t))); + if (m_errorCode != ERROR_SUCCESS) { + throw RegistryError("Cannot set multi-string value in registry.", + m_errorCode); + } +} diff --git a/RegistryHelper/RegistryHelper.h b/RegistryHelper/RegistryHelper.h index 68e8dcf..7124c57 100644 --- a/RegistryHelper/RegistryHelper.h +++ b/RegistryHelper/RegistryHelper.h @@ -45,6 +45,21 @@ class RegistryHelper HKEY hKey, const std::wstring& subKey); + void RegSetDword(HKEY hKey, + const std::wstring& subKey, + const std::wstring& value, + DWORD data); + + void RegSetString(HKEY hKey, + const std::wstring& subKey, + const std::wstring& value, + const std::wstring& data); + + void RegSetMultiString(HKEY hKey, + const std::wstring& subKey, + const std::wstring& value, + const std::vector& data); + private: LONG m_errorCode; // store the last error code };