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

Commit

Permalink
Refactor RegistryHelper.cpp for better error handling and code readab…
Browse files Browse the repository at this point in the history
…ility.

- Allocate a buffer for storing subkey names.
- Enumerate subkeys under the registry key.
- Retrieve information about each subkey.
- Check if the retrieval was successful, handle errors if not.
- Add the subkey name and type to the vector.
- Close the key handle after enumeration is complete.
- Return the vector containing subkey names and types.

Refactor RegEnumValues function in RegistryHelper.cpp for better error handling and code readability.

- Prepare for value enumeration by retrieving information about the registry key.
- Check if the retrieval was successful, handle errors if not.
- Enumerate values under the registry key using RegEnumValue function.
  - Handle errors during enumeration by closing the key handle and throwing an exception.
  • Loading branch information
Arteiii committed Feb 13, 2024
1 parent f7fa2a1 commit c9153c2
Showing 1 changed file with 33 additions and 34 deletions.
67 changes: 33 additions & 34 deletions RegistryHelper/RegistryHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,48 +129,47 @@ RegistryHelper::RegEnumSubKeys(HKEY hKey, const std::wstring &subKey) {
// Handle error, close the key handle and return
RegCloseKey(keyHandle);
throw RegistryError{
"RegQueryInfoKey failed while preparing for value enumeration",
"RegQueryInfoKey failed while preparing for value enumeration.",
retCode};
};
}
}

// Allocate a buffer for storing subkey names
maxSubKeyNameLen++;
// Allocate a buffer for storing subkey names
maxSubKeyNameLen++;

auto nameBuffer = std::make_unique<wchar_t[]>(maxSubKeyNameLen);
auto nameBuffer = std::make_unique<wchar_t[]>(maxSubKeyNameLen);

// Vector to store pairs of subkey names and types
std::vector<std::pair<std::wstring, DWORD>> subKeys;
// Vector to store pairs of subkey names and types
std::vector<std::pair<std::wstring, DWORD>> subKeys;

// Enumerate subkeys under the registry key
for (DWORD index = 0; index < subKeyCount; index++) {
DWORD subKeyNameLen = maxSubKeyNameLen;
// Enumerate subkeys under the registry key
for (DWORD index = 0; index < subKeyCount; index++) {
DWORD subKeyNameLen = maxSubKeyNameLen;

// Retrieve information about the specified subkey
retCode = ::RegEnumKeyEx(keyHandle, index, nameBuffer.get(), &subKeyNameLen,
nullptr, // Reserved
nullptr, // No class information
nullptr, // No class size
nullptr // No last write time
);
// Retrieve information about the specified subkey
retCode = ::RegEnumKeyEx(keyHandle, index, nameBuffer.get(), &subKeyNameLen,
nullptr, // Reserved
nullptr, // No class information
nullptr, // No class size
nullptr // No last write time
);

// Check if the subkey information retrieval was successful
if (retCode != ERROR_SUCCESS) {
// Close the key handle and handle the error
RegCloseKey(keyHandle);
throw RegistryError{"Cannot get subkey info from the registry", retCode};
}
// Check if the subkey information retrieval was successful
if (retCode != ERROR_SUCCESS) {
// Close the key handle and handle the error
RegCloseKey(keyHandle);
throw RegistryError{"Cannot get subkey info from the registry", retCode};
}

// Add the subkey name and type to the vector
subKeys.push_back(
std::make_pair(std::wstring{nameBuffer.get(), subKeyNameLen}, 0));
}
// Add the subkey name and type to the vector
subKeys.push_back(
std::make_pair(std::wstring{nameBuffer.get(), subKeyNameLen}, 0));
}

// Close the key handle
RegCloseKey(keyHandle);
// Close the key handle
RegCloseKey(keyHandle);

// Return the vector containing subkey names and types
return subKeys;
// Return the vector containing subkey names and types
return subKeys;
}

std::vector<std::pair<std::wstring, DWORD>>
Expand Down Expand Up @@ -204,7 +203,7 @@ RegistryHelper::RegEnumValues(HKEY hKey, const std::wstring &subKey) {
// Handle error, close the key handle and return
RegCloseKey(keyHandle);
throw RegistryError{
"RegQueryInfoKey failed while preparing for value enumeration",
"RegQueryInfoKey failed while preparing for value enumeration.",
retCode};
}

Expand All @@ -226,7 +225,7 @@ RegistryHelper::RegEnumValues(HKEY hKey, const std::wstring &subKey) {
if (retCode != ERROR_SUCCESS) {
// Handle error, close the key handle and throw an exception
RegCloseKey(keyHandle);
throw RegistryError{"Cannot enumerate values: RegEnumValue failed",
throw RegistryError{"Cannot enumerate values: RegEnumValue failed.",
retCode};
}

Expand Down

0 comments on commit c9153c2

Please sign in to comment.