This repository has been archived by the owner on Dec 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
5 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<DWORD, std::wstring> 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<ServiceInfo> | ||
RegistryInterface::EnumerateServices() | ||
{ | ||
std::vector<ServiceInfo> services; | ||
|
||
RegistryHelper registryHelper; | ||
|
||
// Specify the registry key for services | ||
const std::wstring servicesKey = L"SYSTEM\\CurrentControlSet\\Services"; | ||
|
||
try { | ||
// Enumerate subkeys | ||
std::vector<std::pair<std::wstring, DWORD>> 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<std::pair<std::wstring, DWORD>> 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<RegistryValue> values; | ||
}; | ||
|
||
class RegistryInterface | ||
{ | ||
public: | ||
RegistryInterface(); | ||
|
||
// Methods related to service information | ||
std::vector<ServiceInfo> 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(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#pragma once | ||
|
||
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers | ||
|
||
#include <Windows.h> | ||
#include <iostream> | ||
#include <memory> | ||
#include <stdexcept> | ||
#include <string> | ||
#include <unordered_map> | ||
#include <vector> | ||
|
||
#include "RegistryHelper.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |