From 85075cd419df9488e13f5ebc8aaaf1d65c72ed66 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Mon, 29 Jun 2020 15:15:36 +0200 Subject: [PATCH] Introduce reg_key::delete_subkey() method Signed-off-by: Simon Rozman --- include/WinStd/Win.h | 11 +++++++++++ src/Win.cpp | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/include/WinStd/Win.h b/include/WinStd/Win.h index 90b8c19e..397a0ed9 100644 --- a/include/WinStd/Win.h +++ b/include/WinStd/Win.h @@ -1069,6 +1069,17 @@ namespace winstd } } + /// + /// Deletes the specified registry subkey. + /// + /// \param[in] szSubkey Name of the subkey to delete + /// + /// \return + /// - true when creation succeeds; + /// - false when creation fails. For extended error information, call `GetLastError()`. + /// + bool delete_subkey(_In_z_ LPCTSTR szSubkey); + protected: /// /// Closes a handle to the registry key. diff --git a/src/Win.cpp b/src/Win.cpp index cfe42e24..69d85b07 100644 --- a/src/Win.cpp +++ b/src/Win.cpp @@ -346,6 +346,43 @@ winstd::reg_key::~reg_key() } +bool winstd::reg_key::delete_subkey(_In_z_ LPCTSTR szSubkey) +{ + LSTATUS s; + + s = RegDeleteKey(m_h, szSubkey); + if (s == ERROR_SUCCESS || s == ERROR_FILE_NOT_FOUND) + return true; + + { + reg_key k; + if (!k.open(m_h, szSubkey, 0, KEY_ENUMERATE_SUB_KEYS)) + return false; + for (;;) { + TCHAR szName[MAX_PATH]; + DWORD dwSize = _countof(szName); + s = RegEnumKeyEx(k, 0, szName, &dwSize, NULL, NULL, NULL, NULL); + if (s == ERROR_SUCCESS) + k.delete_subkey(szName); + else if (s == ERROR_NO_MORE_ITEMS) + break; + else { + SetLastError(s); + return false; + } + } + } + + s = RegDeleteKey(m_h, szSubkey); + if (s == ERROR_SUCCESS) + return true; + else { + SetLastError(s); + return false; + } +} + + void winstd::reg_key::free_internal() noexcept { RegCloseKey(m_h);