From c9153c2308878618ee178423c367601bdddd6f4c Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 13 Feb 2024 11:14:36 +0100 Subject: [PATCH] Refactor RegistryHelper.cpp for better error handling and code readability. - 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. --- RegistryHelper/RegistryHelper.cpp | 67 +++++++++++++++---------------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/RegistryHelper/RegistryHelper.cpp b/RegistryHelper/RegistryHelper.cpp index 841ac47..6cd986b 100644 --- a/RegistryHelper/RegistryHelper.cpp +++ b/RegistryHelper/RegistryHelper.cpp @@ -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(maxSubKeyNameLen); + auto nameBuffer = std::make_unique(maxSubKeyNameLen); -// Vector to store pairs of subkey names and types -std::vector> subKeys; + // Vector to store pairs of subkey names and types + std::vector> 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> @@ -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}; } @@ -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}; }