Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
Refactor code for improved readability and maintainability
Browse files Browse the repository at this point in the history
- Simplify indentation and spacing for better code organization
- Add error handling for registry operations
- Implement methods to set DWORD, string, and multi-string values in the registry
  • Loading branch information
Arteiii committed Feb 8, 2024
1 parent 43c1230 commit e34f012
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 57 deletions.
113 changes: 56 additions & 57 deletions Example/main.cpp
Original file line number Diff line number Diff line change
@@ -1,74 +1,73 @@
#include "RegistryHelper.h"
#include <iostream>

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<std::wstring> multiStringValue =
registryHelper.RegGetMultiString(hKey, subKey, valueNameMultiString);
std::vector<std::wstring> 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<std::pair<std::wstring, DWORD>> subKeys = registryHelper.RegEnumSubKeys(hKey, subKey);
//
// Example 4: Enumerate sub-keys
//
std::vector<std::pair<std::wstring, DWORD>> 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<std::pair<std::wstring, DWORD>> values = registryHelper.RegEnumValues(hKey, subKey);
//
// Example 5: Enumerate values
//
std::vector<std::pair<std::wstring, DWORD>> 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;
}
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ std::vector<std::pair<std::wstring, DWORD>> subKeys =
// Example 5: Enumerate values
std::vector<std::pair<std::wstring, DWORD>> 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.
Expand Down
58 changes: 58 additions & 0 deletions RegistryHelper/RegistryHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<DWORD>((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<std::wstring>& 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<DWORD>(multiString.length() * sizeof(wchar_t)));
if (m_errorCode != ERROR_SUCCESS) {
throw RegistryError("Cannot set multi-string value in registry.",
m_errorCode);
}
}
15 changes: 15 additions & 0 deletions RegistryHelper/RegistryHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::wstring>& data);

private:
LONG m_errorCode; // store the last error code
};

0 comments on commit e34f012

Please sign in to comment.