From 5827516d395f7511ad780baba7fd36f48ffa73f9 Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 16 Feb 2024 15:42:56 +0100 Subject: [PATCH] feat: Add RegistryInterface code This commit adds the necessary code files for the RegistryInterface module. The added files include `framework.h`, `pch.cpp`, `pch.h`, `RegistryInterface.cpp`, and `RegistryInterface.h`. These files define the functions and structures required for interacting with the Windows registry. The code includes methods for enumerating services, retrieving recovery settings, and getting data type names. --- RegistryInterface/RegistryInterface.cpp | 130 ++++++++++++++++++++++++ RegistryInterface/RegistryInterface.h | 58 +++++++++++ RegistryInterface/framework.h | 13 +++ RegistryInterface/pch.cpp | 6 ++ RegistryInterface/pch.h | 16 +++ 5 files changed, 223 insertions(+) create mode 100644 RegistryInterface/RegistryInterface.cpp create mode 100644 RegistryInterface/RegistryInterface.h create mode 100644 RegistryInterface/framework.h create mode 100644 RegistryInterface/pch.cpp create mode 100644 RegistryInterface/pch.h diff --git a/RegistryInterface/RegistryInterface.cpp b/RegistryInterface/RegistryInterface.cpp new file mode 100644 index 0000000..eddf8cb --- /dev/null +++ b/RegistryInterface/RegistryInterface.cpp @@ -0,0 +1,130 @@ +// RegistryInterface.cpp : Defines the functions for the static library. +// +#include "pch.h" + +#include "RegistryInterface.h" + +std::wstring +RegistryInterface::GetDataTypeName(DWORD dataType) noexcept +{ + static const std::unordered_map dataTypeNames = { + { REG_SZ, L"REG_SZ" }, + { REG_EXPAND_SZ, L"REG_EXPAND_SZ" }, + { REG_BINARY, L"REG_BINARY" }, + { REG_DWORD, L"REG_DWORD" }, + }; + + auto it = dataTypeNames.find(dataType); + return (it != dataTypeNames.end()) ? it->second : L"Unknown"; +} + +const wchar_t* RegistryInterface::CrashControlKey = + L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Recovery"; + +// get AutoReboot +inline std::wstring +RegistryInterface::GetAutoRebootSetting() +{ + return registryHelper.RegGetString( + HKEY_LOCAL_MACHINE, CrashControlKey, L"AutoReboot"); +} + +// get MiniDumpDir +inline std::wstring +RegistryInterface::GetMiniDumpDirSetting() +{ + return registryHelper.RegGetString( + HKEY_LOCAL_MACHINE, CrashControlKey, L"MiniDumpDir"); +} + +RecoverySettings +RegistryInterface::GetAllSettings() +{ + RecoverySettings settings; + + // Populate settings using the private helper functions + settings.recoveryOption = GetAutoRebootSetting(); + settings.dumpFileLocation = GetMiniDumpDirSetting(); + + return settings; +} + +std::vector +RegistryInterface::EnumerateServices() +{ + std::vector services; + + RegistryHelper registryHelper; + + // Specify the registry key for services + const std::wstring servicesKey = L"SYSTEM\\CurrentControlSet\\Services"; + + try { + // Enumerate subkeys + std::vector> subKeys = + registryHelper.RegEnumSubKeys(HKEY_LOCAL_MACHINE, servicesKey); + + // Retrieve additional information for each service + for (const auto& subKey : subKeys) { + ServiceInfo service; + service.serviceName = subKey.first; + + try { + // Retrieve the service type + std::vector> values = + registryHelper.RegEnumValues(HKEY_LOCAL_MACHINE, + (servicesKey + L"\\" + subKey.first)); + + // Retrieve additional information for each service + for (const auto& value : values) { + std::wstring valueName = value.first; + DWORD valueType = value.second; + + // Handle different value types + RegistryValue registryValue; + registryValue.name = valueName; + + if (valueType == REG_SZ) { + registryValue.stringValue = + registryHelper.RegGetString(HKEY_LOCAL_MACHINE, + (servicesKey + L"\\" + subKey.first), + valueName); + registryValue.dwordValue = 0; // Default value for non-DWORD types + } else if (valueType == REG_DWORD) { + registryValue.stringValue = + L""; // Default value for non-STRING types + registryValue.dwordValue = + registryHelper.RegGetDword(HKEY_LOCAL_MACHINE, + (servicesKey + L"\\" + subKey.first), + valueName); + } else { + // Handle other value types + registryValue.stringValue = L""; // Default value for unknown types + registryValue.dwordValue = 0; // Default value for unknown types + } + + registryValue.dataTypeName = GetDataTypeName(valueType); + + // Add the value information to the service structure + service.values.push_back(registryValue); + } + } catch (const std::exception& ex) { + // Handle the exception (e.g., log or report) + std::wcerr << L"Exception while enumerating values for service " + << service.serviceName << ": " << ex.what() << std::endl; + // Continue with the next service + continue; + } + + services.push_back(service); + } + } catch (const std::exception& ex) { + // Handle the exception (e.g., log or report) + std::wcerr << L"Exception while enumerating services: " << ex.what() + << std::endl; + // Return an empty vector on exception + return {}; + } + + return services; +} diff --git a/RegistryInterface/RegistryInterface.h b/RegistryInterface/RegistryInterface.h new file mode 100644 index 0000000..4a60754 --- /dev/null +++ b/RegistryInterface/RegistryInterface.h @@ -0,0 +1,58 @@ +#pragma once + +#include "pch.h" + +struct RecoverySettings +{ + std::wstring recoveryOption; + std::wstring dumpFileLocation; + // Add other settings as needed... +}; + +// Define a struct to hold registry value information +struct RegistryValue +{ + std::wstring name; + std::wstring stringValue; + DWORD dwordValue; + std::wstring dataTypeName; + + // Constructor to initialize members + RegistryValue() + : name() + , stringValue() + , dwordValue(0) + , dataTypeName() + { + } +}; + +struct ServiceInfo +{ + std::wstring serviceName; + std::vector values; +}; + +class RegistryInterface +{ +public: + RegistryInterface(); + + // Methods related to service information + std::vector EnumerateServices(); + + // Methods related to recovery settings + RecoverySettings GetAllSettings(); + + std::wstring GetDataTypeName(DWORD dataType) noexcept; + +private: + RegistryHelper registryHelper; // Member variable + + // Constants for registry keys + static const wchar_t* CrashControlKey; + + // Private helper functions + inline std::wstring GetAutoRebootSetting(); + inline std::wstring GetMiniDumpDirSetting(); +}; diff --git a/RegistryInterface/framework.h b/RegistryInterface/framework.h new file mode 100644 index 0000000..2c88a72 --- /dev/null +++ b/RegistryInterface/framework.h @@ -0,0 +1,13 @@ +#pragma once + +#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers + +#include +#include +#include +#include +#include +#include +#include + +#include "RegistryHelper.h" diff --git a/RegistryInterface/pch.cpp b/RegistryInterface/pch.cpp new file mode 100644 index 0000000..00df68a --- /dev/null +++ b/RegistryInterface/pch.cpp @@ -0,0 +1,6 @@ +// pch.cpp: source file corresponding to the pre-compiled header + +#include "pch.h" + +// When you are using pre-compiled headers, this source file is necessary for +// compilation to succeed. diff --git a/RegistryInterface/pch.h b/RegistryInterface/pch.h new file mode 100644 index 0000000..53c3e52 --- /dev/null +++ b/RegistryInterface/pch.h @@ -0,0 +1,16 @@ +// pch.h: This is a precompiled header file. +// Files listed below are compiled only once, improving build performance for +// future builds. This also affects IntelliSense performance, including code +// completion and many code browsing features. However, files listed here are +// ALL re-compiled if any one of them is updated between builds. Do not add +// files here that you will be updating frequently as this negates the +// performance advantage. + +#ifndef PCH_H +#define PCH_H + +// add headers that you want to pre-compile here + +#include "framework.h" + +#endif // PCH_H