Index: base/win/registry.cc |
diff --git a/base/win/registry.cc b/base/win/registry.cc |
index 7330d08aa277e0ab5a67d84492176556bdd9dde0..ae4ad43dc0f9c0f00ce4797940068827f2e8db46 100644 |
--- a/base/win/registry.cc |
+++ b/base/win/registry.cc |
@@ -10,8 +10,7 @@ |
#include "base/logging.h" |
#include "base/strings/string_util.h" |
#include "base/threading/thread_restrictions.h" |
- |
-#pragma comment(lib, "shlwapi.lib") // for SHDeleteKey |
+#include "base/win/windows_version.h" |
namespace base { |
namespace win { |
@@ -30,6 +29,9 @@ inline DWORD to_wchar_size(DWORD byte_size) { |
return (byte_size + sizeof(wchar_t) - 1) / sizeof(wchar_t); |
} |
+// Mask to pull WOW64 access flags out of REGSAM access. |
+const REGSAM kWow64AccessMask = KEY_WOW64_32KEY | KEY_WOW64_64KEY; |
+ |
} // namespace |
// RegKey ---------------------------------------------------------------------- |
@@ -76,6 +78,7 @@ LONG RegKey::CreateWithDisposition(HKEY rootkey, const wchar_t* subkey, |
if (result == ERROR_SUCCESS) { |
Close(); |
key_ = subhkey; |
+ wow64access_ = access & kWow64AccessMask; |
} |
return result; |
@@ -86,10 +89,10 @@ LONG RegKey::CreateKey(const wchar_t* name, REGSAM access) { |
HKEY subkey = NULL; |
LONG result = RegCreateKeyEx(key_, name, 0, NULL, REG_OPTION_NON_VOLATILE, |
access, NULL, &subkey, NULL); |
- if (result == ERROR_SUCCESS) { |
- Close(); |
- |
- key_ = subkey; |
+ if (result == ERROR_SUCCESS) { |
+ Close(); |
+ key_ = subkey; |
+ wow64access_ = access & kWow64AccessMask; |
} |
return result; |
@@ -103,6 +106,7 @@ LONG RegKey::Open(HKEY rootkey, const wchar_t* subkey, REGSAM access) { |
if (result == ERROR_SUCCESS) { |
Close(); |
key_ = subhkey; |
+ wow64access_ = access & kWow64AccessMask; |
} |
return result; |
@@ -118,6 +122,7 @@ LONG RegKey::OpenKey(const wchar_t* relative_key_name, REGSAM access) { |
if (result == ERROR_SUCCESS) { |
Close(); |
key_ = subkey; |
+ wow64access_ = access & kWow64AccessMask; |
} |
return result; |
} |
@@ -168,8 +173,18 @@ LONG RegKey::GetValueNameAt(int index, std::wstring* name) const { |
LONG RegKey::DeleteKey(const wchar_t* name) { |
DCHECK(key_); |
DCHECK(name); |
- LONG result = SHDeleteKey(key_, name); |
- return result; |
+ HKEY subkey; |
+ |
+ // Verify the key exists before attempting delete to replicate previous |
+ // behavior. |
+ LONG result = |
+ RegOpenKeyEx(key_, name, 0, READ_CONTROL | wow64access_, &subkey); |
+ if (result != ERROR_SUCCESS) |
+ return result; |
+ RegCloseKey(subkey); |
+ std::wstring keyname(name); |
grt (UTC plus 2)
2014/05/17 12:54:33
nit: put this inline with the recursive call:
re
Will Harris
2014/05/19 21:49:03
Done.
|
+ |
+ return RegDelRecurse(key_, keyname, wow64access_); |
} |
LONG RegKey::DeleteValue(const wchar_t* value_name) { |
@@ -342,6 +357,97 @@ LONG RegKey::StopWatching() { |
return result; |
} |
+// static |
+LONG RegKey::RegDeleteKeyExWrapper(HKEY hKey, |
+ const wchar_t* lpSubKey, |
+ REGSAM samDesired, |
+ DWORD Reserved) { |
+ typedef LSTATUS(WINAPI * RegDeleteKeyExPtr)( |
grt (UTC plus 2)
2014/05/17 12:54:33
WINAPI*
Will Harris
2014/05/19 21:49:03
this was git gl format :)
grt (UTC plus 2)
2014/05/20 15:28:52
Oh. How 'bout that. May as well do as it says, alt
|
+ HKEY hKey, LPCWSTR lpSubKey, REGSAM samDesired, DWORD Reserved); |
grt (UTC plus 2)
2014/05/17 12:54:33
nit: remove the parameter names
Will Harris
2014/05/19 21:49:03
Done.
|
+ |
+ RegDeleteKeyExPtr reg_delete_key_ex_func = |
+ reinterpret_cast<RegDeleteKeyExPtr>( |
+ GetProcAddress(GetModuleHandleA("advapi32.dll"), "RegDeleteKeyExW")); |
+ |
+ if (reg_delete_key_ex_func) |
+ return reg_delete_key_ex_func(hKey, lpSubKey, samDesired, Reserved); |
+ |
+ // Windows XP does not support RegDeleteKeyEx, so fallback to RegDeleteKey. |
+ return RegDeleteKey(hKey, lpSubKey); |
+} |
+ |
+// static |
+LONG RegKey::RegDelRecurse(HKEY root_key, |
+ std::wstring const& name, |
grt (UTC plus 2)
2014/05/17 12:54:33
const std::wstring&
Will Harris
2014/05/19 21:49:03
Done.
|
+ REGSAM access) { |
+ LONG result; |
+ HKEY target_key; |
+ |
+ size_t name_length = name.length(); |
grt (UTC plus 2)
2014/05/17 12:54:33
remove this and change line 388 to if (name.empty(
Will Harris
2014/05/19 21:49:03
Done.
|
+ |
+ if (name_length == 0) { |
+ NOTREACHED(); |
+ return ERROR_INVALID_PARAMETER; |
+ } |
+ |
+ // First, see if the key can be deleted without having to recurse. |
+ result = RegDeleteKeyExWrapper(root_key, name.c_str(), access, 0); |
grt (UTC plus 2)
2014/05/17 12:54:33
the style guide says to define local vars "as clos
Will Harris
2014/05/19 21:49:03
Done.
|
+ |
+ if (result == ERROR_SUCCESS) |
+ return result; |
+ |
+ result = RegOpenKeyEx( |
grt (UTC plus 2)
2014/05/17 12:54:33
put the definition of target_key just above this l
Will Harris
2014/05/19 21:49:03
Done.
|
+ root_key, name.c_str(), 0, KEY_ENUMERATE_SUB_KEYS | access, &target_key); |
+ |
+ if (result == ERROR_FILE_NOT_FOUND) |
+ return ERROR_SUCCESS; |
+ if (result != ERROR_SUCCESS) |
+ return result; |
+ |
+ // Copy string before modifying it. |
+ std::wstring subkey_name(name); |
+ |
+ // Check for an ending slash and add one if it is missing. |
+ if (subkey_name[name_length - 1] != L'\\') { |
+ name_length++; |
+ subkey_name += L"\\"; |
+ } |
+ |
+ // Enumerate the keys |
+ result = ERROR_SUCCESS; |
+ DWORD kMaxKeyNameLength = MAX_PATH; |
grt (UTC plus 2)
2014/05/17 12:54:33
const DWORD
Will Harris
2014/05/19 21:49:03
Done.
|
+ |
grt (UTC plus 2)
2014/05/17 12:54:33
define this here:
const size_t base_key_lenght =
Will Harris
2014/05/19 21:49:03
Done.
|
+ while (result == ERROR_SUCCESS) { |
+ std::wstring key_name; |
grt (UTC plus 2)
2014/05/17 12:54:33
move the definition of key_name out of the loop.
Will Harris
2014/05/19 21:49:03
Done.
|
+ DWORD key_size = kMaxKeyNameLength; |
+ result = RegEnumKeyEx(target_key, |
+ 0, |
+ WriteInto(&key_name, kMaxKeyNameLength), |
+ &key_size, |
+ NULL, |
+ NULL, |
+ NULL, |
+ NULL); |
+ |
+ if (result != ERROR_SUCCESS) |
+ break; |
+ |
+ key_name.resize(key_size); |
+ subkey_name.resize(name_length); |
grt (UTC plus 2)
2014/05/17 12:54:33
name_length -> base_key_lenght
Will Harris
2014/05/19 21:49:03
Done.
|
+ subkey_name += key_name; |
+ |
+ if (RegDelRecurse(root_key, subkey_name, access) != ERROR_SUCCESS) |
+ break; |
+ } |
+ |
+ RegCloseKey(target_key); |
+ |
+ // Try again to delete the key. |
+ result = RegDeleteKeyExWrapper(root_key, name.c_str(), access, 0); |
+ |
+ return result; |
+} |
+ |
// RegistryValueIterator ------------------------------------------------------ |
RegistryValueIterator::RegistryValueIterator(HKEY root_key, |