| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This API is a usability layer for direct registry access via NTDLL. | 5 // This API is a usability layer for direct registry access via NTDLL. |
| 6 // It allows for "advapi32-free" registry access, which is especially | 6 // It allows for "advapi32-free" registry access, which is especially |
| 7 // useful for accessing registy from DllMain (holding loader lock), | 7 // useful for accessing registy from DllMain (holding loader lock), |
| 8 // or if a dependency on/linkage of ADVAPI32.dll is not desired. | 8 // or if a dependency on/linkage of ADVAPI32.dll is not desired. |
| 9 | 9 |
| 10 // The implementation of this API should only use ntdll and kernel32 system | 10 // The implementation of this API should only use ntdll and kernel32 system |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 typedef enum _ROOT_KEY { AUTO = 0, HKLM, HKCU } ROOT_KEY; | 38 typedef enum _ROOT_KEY { AUTO = 0, HKLM, HKCU } ROOT_KEY; |
| 39 | 39 |
| 40 // Used with wrapper functions to request registry redirection override. | 40 // Used with wrapper functions to request registry redirection override. |
| 41 // Maps to KEY_WOW64_32KEY and KEY_WOW64_64KEY access flags. | 41 // Maps to KEY_WOW64_32KEY and KEY_WOW64_64KEY access flags. |
| 42 enum WOW64_OVERRIDE { | 42 enum WOW64_OVERRIDE { |
| 43 NONE = 0L, | 43 NONE = 0L, |
| 44 WOW6432 = KEY_WOW64_32KEY, | 44 WOW6432 = KEY_WOW64_32KEY, |
| 45 WOW6464 = KEY_WOW64_64KEY | 45 WOW6464 = KEY_WOW64_64KEY |
| 46 }; | 46 }; |
| 47 | 47 |
| 48 // ScopedHANDLE wraps a generic Windows HANDLE (with an invalid value of |
| 49 // nullptr), allowing it to be closed automatically (via NtClose) when the |
| 50 // object goes out of scope. Use is_valid() function to check validity of |
| 51 // internal HANDLE. This class may not be used with HANDLEs that have an invalid |
| 52 // value of INVALID_HANDLE_VALUE (e.g. CreateFile()). |
| 53 class ScopedHANDLE { |
| 54 public: |
| 55 ScopedHANDLE(); |
| 56 explicit ScopedHANDLE(HANDLE handle); |
| 57 ScopedHANDLE(ScopedHANDLE&& rvalue) : handle_(rvalue.release()) {} |
| 58 ScopedHANDLE(const ScopedHANDLE&) = delete; |
| 59 void operator=(const ScopedHANDLE&) = delete; |
| 60 ~ScopedHANDLE(); |
| 61 |
| 62 HANDLE get() { return handle_; } |
| 63 bool is_valid() const; |
| 64 |
| 65 private: |
| 66 // Transfers ownership. If the returned is non-null, it needs to be closed by |
| 67 // the caller. |
| 68 HANDLE release(); |
| 69 |
| 70 HANDLE handle_; |
| 71 }; |
| 72 |
| 48 //------------------------------------------------------------------------------ | 73 //------------------------------------------------------------------------------ |
| 49 // Create, open, delete, close functions | 74 // Create, open, delete, close functions |
| 50 //------------------------------------------------------------------------------ | 75 //------------------------------------------------------------------------------ |
| 51 | 76 |
| 52 // Create and/or open a registry key. | 77 // Create and/or open a registry key. |
| 53 // - This function will recursively create multiple sub-keys if required for | 78 // - This function will recursively create multiple sub-keys if required for |
| 54 // |key_path|. | 79 // |key_path|. |
| 55 // - If the key doesn't need to be left open, pass in nullptr for |out_handle|. | |
| 56 // - This function will happily succeed if the key already exists. | 80 // - This function will happily succeed if the key already exists. |
| 57 // - Optional |out_handle|. If nullptr, function will close handle when done. | 81 // - Optional |out_handle|. If nullptr, function will close handle when done. |
| 58 // Otherwise, will hold the open handle to the deepest subkey. | 82 // - On success, the returned object will hold the open handle to the deepest |
| 59 // - Caller must call CloseRegKey on returned handle (on success). | 83 // subkey. |
| 60 bool CreateRegKey(ROOT_KEY root, | 84 ScopedHANDLE CreateRegKey(ROOT_KEY root, |
| 61 const wchar_t* key_path, | 85 const wchar_t* key_path, |
| 62 ACCESS_MASK access, | 86 ACCESS_MASK access); |
| 63 HANDLE* out_handle OPTIONAL); | |
| 64 | 87 |
| 65 // Open existing registry key. | 88 // Open existing registry key. |
| 66 // - Caller must call CloseRegKey on returned handle (on success). | 89 // - Optional error code can be returned on failure for extra detail. This will |
| 67 // - Optional error code can be returned on failure for extra detail. | 90 // be set if the returned ScopedHANDLE.is_valid() == false. |
| 68 bool OpenRegKey(ROOT_KEY root, | 91 ScopedHANDLE OpenRegKey(ROOT_KEY root, |
| 69 const wchar_t* key_path, | 92 const wchar_t* key_path, |
| 70 ACCESS_MASK access, | 93 ACCESS_MASK access, |
| 71 HANDLE* out_handle, | 94 NTSTATUS* error_code OPTIONAL); |
| 72 NTSTATUS* error_code OPTIONAL); | |
| 73 | 95 |
| 74 // Delete a registry key. | 96 // Delete a registry key. |
| 75 // - Caller must still call CloseRegKey after the delete. | |
| 76 bool DeleteRegKey(HANDLE key); | 97 bool DeleteRegKey(HANDLE key); |
| 77 | 98 |
| 78 // Delete a registry key. | 99 // Delete a registry key. |
| 79 // - WRAPPER: Function opens and closes the target key for caller. | 100 // - WRAPPER: Function opens and closes the target key for caller. |
| 80 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE. | 101 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE. |
| 81 bool DeleteRegKey(ROOT_KEY root, | 102 bool DeleteRegKey(ROOT_KEY root, |
| 82 WOW64_OVERRIDE wow64_override, | 103 WOW64_OVERRIDE wow64_override, |
| 83 const wchar_t* key_path); | 104 const wchar_t* key_path); |
| 84 | 105 |
| 85 // Close a registry key handle that was opened with CreateRegKey or OpenRegKey. | |
| 86 void CloseRegKey(HANDLE key); | |
| 87 | |
| 88 //------------------------------------------------------------------------------ | 106 //------------------------------------------------------------------------------ |
| 89 // Getter functions | 107 // Getter functions |
| 90 //------------------------------------------------------------------------------ | 108 //------------------------------------------------------------------------------ |
| 91 | 109 |
| 92 // Main function to query a registry value. | 110 // Main function to query a registry value. |
| 93 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 111 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 94 // - Types defined in winnt.h. E.g.: REG_DWORD, REG_SZ. | 112 // - Types defined in winnt.h. E.g.: REG_DWORD, REG_SZ. |
| 95 // - Caller is responsible for calling "delete[] *out_buffer" (on success). | 113 // - Caller is responsible for calling "delete[] *out_buffer" (on success). |
| 96 bool QueryRegKeyValue(HANDLE key, | 114 bool QueryRegKeyValue(HANDLE key, |
| 97 const wchar_t* value_name, | 115 const wchar_t* value_name, |
| 98 ULONG* out_type, | 116 ULONG* out_type, |
| 99 BYTE** out_buffer, | 117 BYTE** out_buffer, |
| 100 DWORD* out_size); | 118 DWORD* out_size); |
| 101 | 119 |
| 102 // Query DWORD value. | 120 // Query DWORD value. |
| 103 // - WRAPPER: Function works with DWORD data type. | 121 // - WRAPPER: Function works with DWORD data type. |
| 104 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 122 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 105 // - Handle will be left open. Caller must still call CloseRegKey when done. | |
| 106 bool QueryRegValueDWORD(HANDLE key, | 123 bool QueryRegValueDWORD(HANDLE key, |
| 107 const wchar_t* value_name, | 124 const wchar_t* value_name, |
| 108 DWORD* out_dword); | 125 DWORD* out_dword); |
| 109 | 126 |
| 110 // Query DWORD value. | 127 // Query DWORD value. |
| 111 // - WRAPPER: Function opens and closes the target key for caller, and works | 128 // - WRAPPER: Function opens and closes the target key for caller, and works |
| 112 // with DWORD data type. | 129 // with DWORD data type. |
| 113 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE. | 130 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE. |
| 114 bool QueryRegValueDWORD(ROOT_KEY root, | 131 bool QueryRegValueDWORD(ROOT_KEY root, |
| 115 WOW64_OVERRIDE wow64_override, | 132 WOW64_OVERRIDE wow64_override, |
| 116 const wchar_t* key_path, | 133 const wchar_t* key_path, |
| 117 const wchar_t* value_name, | 134 const wchar_t* value_name, |
| 118 DWORD* out_dword); | 135 DWORD* out_dword); |
| 119 | 136 |
| 120 // Query SZ (string) value. | 137 // Query SZ (string) value. |
| 121 // - WRAPPER: Function works with SZ data type. | 138 // - WRAPPER: Function works with SZ data type. |
| 122 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 139 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 123 // - Handle will be left open. Caller must still call CloseRegKey when done. | |
| 124 bool QueryRegValueSZ(HANDLE key, | 140 bool QueryRegValueSZ(HANDLE key, |
| 125 const wchar_t* value_name, | 141 const wchar_t* value_name, |
| 126 std::wstring* out_sz); | 142 std::wstring* out_sz); |
| 127 | 143 |
| 128 // Query SZ (string) value. | 144 // Query SZ (string) value. |
| 129 // - WRAPPER: Function opens and closes the target key for caller, and works | 145 // - WRAPPER: Function opens and closes the target key for caller, and works |
| 130 // with SZ data type. | 146 // with SZ data type. |
| 131 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE. | 147 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE. |
| 132 bool QueryRegValueSZ(ROOT_KEY root, | 148 bool QueryRegValueSZ(ROOT_KEY root, |
| 133 WOW64_OVERRIDE wow64_override, | 149 WOW64_OVERRIDE wow64_override, |
| 134 const wchar_t* key_path, | 150 const wchar_t* key_path, |
| 135 const wchar_t* value_name, | 151 const wchar_t* value_name, |
| 136 std::wstring* out_sz); | 152 std::wstring* out_sz); |
| 137 | 153 |
| 138 // Query MULTI_SZ (multiple strings) value. | 154 // Query MULTI_SZ (multiple strings) value. |
| 139 // - WRAPPER: Function works with MULTI_SZ data type. | 155 // - WRAPPER: Function works with MULTI_SZ data type. |
| 140 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 156 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 141 // - Handle will be left open. Caller must still call CloseRegKey when done. | |
| 142 bool QueryRegValueMULTISZ(HANDLE key, | 157 bool QueryRegValueMULTISZ(HANDLE key, |
| 143 const wchar_t* value_name, | 158 const wchar_t* value_name, |
| 144 std::vector<std::wstring>* out_multi_sz); | 159 std::vector<std::wstring>* out_multi_sz); |
| 145 | 160 |
| 146 // Query MULTI_SZ (multiple strings) value. | 161 // Query MULTI_SZ (multiple strings) value. |
| 147 // - WRAPPER: Function opens and closes the target key for caller, and works | 162 // - WRAPPER: Function opens and closes the target key for caller, and works |
| 148 // with MULTI_SZ data type. | 163 // with MULTI_SZ data type. |
| 149 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE. | 164 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE. |
| 150 bool QueryRegValueMULTISZ(ROOT_KEY root, | 165 bool QueryRegValueMULTISZ(ROOT_KEY root, |
| 151 WOW64_OVERRIDE wow64_override, | 166 WOW64_OVERRIDE wow64_override, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 162 // - Types defined in winnt.h. E.g.: REG_DWORD, REG_SZ. | 177 // - Types defined in winnt.h. E.g.: REG_DWORD, REG_SZ. |
| 163 bool SetRegKeyValue(HANDLE key, | 178 bool SetRegKeyValue(HANDLE key, |
| 164 const wchar_t* value_name, | 179 const wchar_t* value_name, |
| 165 ULONG type, | 180 ULONG type, |
| 166 const BYTE* data, | 181 const BYTE* data, |
| 167 DWORD data_size); | 182 DWORD data_size); |
| 168 | 183 |
| 169 // Set DWORD value. | 184 // Set DWORD value. |
| 170 // - WRAPPER: Function works with DWORD data type. | 185 // - WRAPPER: Function works with DWORD data type. |
| 171 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 186 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 172 // - Handle will be left open. Caller must still call CloseRegKey when done. | |
| 173 bool SetRegValueDWORD(HANDLE key, const wchar_t* value_name, DWORD value); | 187 bool SetRegValueDWORD(HANDLE key, const wchar_t* value_name, DWORD value); |
| 174 | 188 |
| 175 // Set DWORD value. | 189 // Set DWORD value. |
| 176 // - WRAPPER: Function opens and closes the target key for caller, and works | 190 // - WRAPPER: Function opens and closes the target key for caller, and works |
| 177 // with DWORD data type. | 191 // with DWORD data type. |
| 178 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE. | 192 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE. |
| 179 bool SetRegValueDWORD(ROOT_KEY root, | 193 bool SetRegValueDWORD(ROOT_KEY root, |
| 180 WOW64_OVERRIDE wow64_override, | 194 WOW64_OVERRIDE wow64_override, |
| 181 const wchar_t* key_path, | 195 const wchar_t* key_path, |
| 182 const wchar_t* value_name, | 196 const wchar_t* value_name, |
| 183 DWORD value); | 197 DWORD value); |
| 184 | 198 |
| 185 // Set SZ (string) value. | 199 // Set SZ (string) value. |
| 186 // - WRAPPER: Function works with SZ data type. | 200 // - WRAPPER: Function works with SZ data type. |
| 187 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 201 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 188 // - Handle will be left open. Caller must still call CloseRegKey when done. | |
| 189 bool SetRegValueSZ(HANDLE key, | 202 bool SetRegValueSZ(HANDLE key, |
| 190 const wchar_t* value_name, | 203 const wchar_t* value_name, |
| 191 const std::wstring& value); | 204 const std::wstring& value); |
| 192 | 205 |
| 193 // Set SZ (string) value. | 206 // Set SZ (string) value. |
| 194 // - WRAPPER: Function opens and closes the target key for caller, and works | 207 // - WRAPPER: Function opens and closes the target key for caller, and works |
| 195 // with SZ data type. | 208 // with SZ data type. |
| 196 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE. | 209 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE. |
| 197 bool SetRegValueSZ(ROOT_KEY root, | 210 bool SetRegValueSZ(ROOT_KEY root, |
| 198 WOW64_OVERRIDE wow64_override, | 211 WOW64_OVERRIDE wow64_override, |
| 199 const wchar_t* key_path, | 212 const wchar_t* key_path, |
| 200 const wchar_t* value_name, | 213 const wchar_t* value_name, |
| 201 const std::wstring& value); | 214 const std::wstring& value); |
| 202 | 215 |
| 203 // Set MULTI_SZ (multiple strings) value. | 216 // Set MULTI_SZ (multiple strings) value. |
| 204 // - WRAPPER: Function works with MULTI_SZ data type. | 217 // - WRAPPER: Function works with MULTI_SZ data type. |
| 205 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 218 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 206 // - Handle will be left open. Caller must still call CloseRegKey when done. | |
| 207 bool SetRegValueMULTISZ(HANDLE key, | 219 bool SetRegValueMULTISZ(HANDLE key, |
| 208 const wchar_t* value_name, | 220 const wchar_t* value_name, |
| 209 const std::vector<std::wstring>& values); | 221 const std::vector<std::wstring>& values); |
| 210 | 222 |
| 211 // Set MULTI_SZ (multiple strings) value. | 223 // Set MULTI_SZ (multiple strings) value. |
| 212 // - WRAPPER: Function opens and closes the target key for caller, and works | 224 // - WRAPPER: Function opens and closes the target key for caller, and works |
| 213 // with MULTI_SZ data type. | 225 // with MULTI_SZ data type. |
| 214 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE. | 226 // - Use |wow64_override| to force redirection behaviour, or pass nt::NONE. |
| 215 bool SetRegValueMULTISZ(ROOT_KEY root, | 227 bool SetRegValueMULTISZ(ROOT_KEY root, |
| 216 WOW64_OVERRIDE wow64_override, | 228 WOW64_OVERRIDE wow64_override, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 230 | 242 |
| 231 // Setter function for test suites that use reg redirection. | 243 // Setter function for test suites that use reg redirection. |
| 232 bool SetTestingOverride(ROOT_KEY root, const std::wstring& new_path); | 244 bool SetTestingOverride(ROOT_KEY root, const std::wstring& new_path); |
| 233 | 245 |
| 234 // Getter function for test suites that use reg redirection. | 246 // Getter function for test suites that use reg redirection. |
| 235 std::wstring GetTestingOverride(ROOT_KEY root); | 247 std::wstring GetTestingOverride(ROOT_KEY root); |
| 236 | 248 |
| 237 }; // namespace nt | 249 }; // namespace nt |
| 238 | 250 |
| 239 #endif // CHROME_ELF_NT_REGISTRY_NT_REGISTRY_H_ | 251 #endif // CHROME_ELF_NT_REGISTRY_NT_REGISTRY_H_ |
| OLD | NEW |