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 18 matching lines...) Expand all Loading... | |
29 namespace nt { | 29 namespace nt { |
30 | 30 |
31 // These globals are only used in test suites that use reg redirection | 31 // These globals are only used in test suites that use reg redirection |
32 // of HKLM and/or HKCU. | 32 // of HKLM and/or HKCU. |
33 extern const size_t g_kRegMaxPathLen; | 33 extern const size_t g_kRegMaxPathLen; |
34 extern wchar_t HKLM_override[]; | 34 extern wchar_t HKLM_override[]; |
35 extern wchar_t HKCU_override[]; | 35 extern wchar_t HKCU_override[]; |
36 | 36 |
37 // AUTO will choose depending on system install or not. | 37 // AUTO will choose depending on system install or not. |
38 // Use HKLM or HKCU to override. | 38 // Use HKLM or HKCU to override. |
39 typedef enum _ROOT_KEY { AUTO = 0, HKLM, HKCU } ROOT_KEY; | 39 typedef enum _ROOT_KEY { AUTO = 0, HKLM, HKCU } ROOT_KEY; |
robertshield
2016/09/28 23:49:23
thoughts re: AUTO: it seems to be used in only onc
penny
2016/10/01 01:44:25
Wow. Way to swoop in with the profound thinking R
robertshield
2016/10/04 05:22:09
Ok. My line of reasoning was based on separation o
penny
2016/10/04 17:55:53
Thank you! I hear you.
| |
40 | 40 |
41 // Used with wrapper functions to request registry redirection override. | |
42 // Maps to KEY_WOW64_32KEY and KEY_WOW64_64KEY access flags. | |
43 enum WOW64_OVERRIDE { | |
44 NONE = 0L, | |
45 WOW6432 = KEY_WOW64_32KEY, | |
46 WOW6464 = KEY_WOW64_64KEY | |
47 }; | |
48 | |
49 //------------------------------------------------------------------------------ | |
50 // Create, open, delete, close functions | |
51 //------------------------------------------------------------------------------ | |
52 | |
41 // Create and/or open a registry key. | 53 // Create and/or open a registry key. |
42 // - This function will recursively create multiple sub-keys if required for | 54 // - This function will recursively create multiple sub-keys if required for |
43 // |key_path|. | 55 // |key_path|. |
44 // - If the key doesn't need to be left open, pass in nullptr for |out_handle|. | 56 // - If the key doesn't need to be left open, pass in nullptr for |out_handle|. |
45 // - This function will happily succeed if the key already exists. | 57 // - This function will happily succeed if the key already exists. |
46 // - Optional |out_handle|. If nullptr, function will close handle when done. | 58 // - Optional |out_handle|. If nullptr, function will close handle when done. |
47 // Otherwise, will hold the open handle to the deepest subkey. | 59 // Otherwise, will hold the open handle to the deepest subkey. |
48 // - Caller must call CloseRegKey on returned handle (on success). | 60 // - Caller must call CloseRegKey on returned handle (on success). |
49 bool CreateRegKey(ROOT_KEY root, | 61 bool CreateRegKey(ROOT_KEY root, |
50 const wchar_t* key_path, | 62 const wchar_t* key_path, |
51 ACCESS_MASK access, | 63 ACCESS_MASK access, |
52 HANDLE* out_handle OPTIONAL); | 64 HANDLE* out_handle OPTIONAL); |
53 | 65 |
54 // Open existing registry key. | 66 // Open existing registry key. |
55 // - Caller must call CloseRegKey on returned handle (on success). | 67 // - Caller must call CloseRegKey on returned handle (on success). |
56 // - Optional error code can be returned on failure for extra detail. | 68 // - Optional error code can be returned on failure for extra detail. |
57 bool OpenRegKey(ROOT_KEY root, | 69 bool OpenRegKey(ROOT_KEY root, |
58 const wchar_t* key_path, | 70 const wchar_t* key_path, |
59 ACCESS_MASK access, | 71 ACCESS_MASK access, |
60 HANDLE* out_handle, | 72 HANDLE* out_handle, |
61 NTSTATUS* error_code OPTIONAL); | 73 NTSTATUS* error_code OPTIONAL); |
62 | 74 |
63 // Delete a registry key. | 75 // Delete a registry key. |
64 // - Caller must still call CloseRegKey after the delete. | 76 // - Caller must still call CloseRegKey after the delete. |
65 bool DeleteRegKey(HANDLE key); | 77 bool DeleteRegKey(HANDLE key); |
66 | 78 |
67 // Delete a registry key. | 79 // Delete a registry key. |
68 // - WRAPPER: Function opens and closes the target key for caller. | 80 // - WRAPPER: Function opens and closes the target key for caller. |
69 bool DeleteRegKey(ROOT_KEY root, const wchar_t* key_path); | 81 // - Use |wow64_override| to force redirection behaviour, or pass 0/nt::NONE. |
82 bool DeleteRegKey(ROOT_KEY root, | |
83 WOW64_OVERRIDE wow64_override, | |
84 const wchar_t* key_path); | |
70 | 85 |
71 // Close a registry key handle that was opened with CreateRegKey or OpenRegKey. | 86 // Close a registry key handle that was opened with CreateRegKey or OpenRegKey. |
72 void CloseRegKey(HANDLE key); | 87 void CloseRegKey(HANDLE key); |
73 | 88 |
74 //------------------------------------------------------------------------------ | 89 //------------------------------------------------------------------------------ |
75 // Getter functions | 90 // Getter functions |
76 //------------------------------------------------------------------------------ | 91 //------------------------------------------------------------------------------ |
77 | 92 |
78 // Main function to query a registry value. | 93 // Main function to query a registry value. |
79 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 94 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
80 // - Types defined in winnt.h. E.g.: REG_DWORD, REG_SZ. | 95 // - Types defined in winnt.h. E.g.: REG_DWORD, REG_SZ. |
81 // - Caller is responsible for calling "delete[] *out_buffer" (on success). | 96 // - Caller is responsible for calling "delete[] *out_buffer" (on success). |
82 bool QueryRegKeyValue(HANDLE key, | 97 bool QueryRegKeyValue(HANDLE key, |
83 const wchar_t* value_name, | 98 const wchar_t* value_name, |
84 ULONG* out_type, | 99 ULONG* out_type, |
85 BYTE** out_buffer, | 100 BYTE** out_buffer, |
86 DWORD* out_size); | 101 DWORD* out_size); |
87 | 102 |
88 // Query DWORD value. | 103 // Query DWORD value. |
89 // - WRAPPER: Function works with DWORD data type. | 104 // - WRAPPER: Function works with DWORD data type. |
90 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 105 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
91 // - Handle will be left open. Caller must still call CloseRegKey when done. | 106 // - Handle will be left open. Caller must still call CloseRegKey when done. |
92 bool QueryRegValueDWORD(HANDLE key, | 107 bool QueryRegValueDWORD(HANDLE key, |
93 const wchar_t* value_name, | 108 const wchar_t* value_name, |
94 DWORD* out_dword); | 109 DWORD* out_dword); |
95 | 110 |
96 // Query DWORD value. | 111 // Query DWORD value. |
97 // - WRAPPER: Function opens and closes the target key for caller, and works | 112 // - WRAPPER: Function opens and closes the target key for caller, and works |
98 // with DWORD data type. | 113 // with DWORD data type. |
114 // - Use |wow64_override| to force redirection behaviour, or pass 0/nt::NONE. | |
robertshield
2016/09/28 23:49:23
nit: figure we should express a stronger opinion t
grt (UTC plus 2)
2016/09/29 10:30:22
yes, please
penny
2016/10/01 01:44:25
Done.
| |
99 bool QueryRegValueDWORD(ROOT_KEY root, | 115 bool QueryRegValueDWORD(ROOT_KEY root, |
116 WOW64_OVERRIDE wow64_override, | |
100 const wchar_t* key_path, | 117 const wchar_t* key_path, |
101 const wchar_t* value_name, | 118 const wchar_t* value_name, |
102 DWORD* out_dword); | 119 DWORD* out_dword); |
103 | 120 |
104 // Query SZ (string) value. | 121 // Query SZ (string) value. |
105 // - WRAPPER: Function works with SZ data type. | 122 // - WRAPPER: Function works with SZ data type. |
106 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 123 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
107 // - Handle will be left open. Caller must still call CloseRegKey when done. | 124 // - Handle will be left open. Caller must still call CloseRegKey when done. |
108 bool QueryRegValueSZ(HANDLE key, | 125 bool QueryRegValueSZ(HANDLE key, |
109 const wchar_t* value_name, | 126 const wchar_t* value_name, |
110 std::wstring* out_sz); | 127 std::wstring* out_sz); |
111 | 128 |
112 // Query SZ (string) value. | 129 // Query SZ (string) value. |
113 // - WRAPPER: Function opens and closes the target key for caller, and works | 130 // - WRAPPER: Function opens and closes the target key for caller, and works |
114 // with SZ data type. | 131 // with SZ data type. |
132 // - Use |wow64_override| to force redirection behaviour, or pass 0/nt::NONE. | |
115 bool QueryRegValueSZ(ROOT_KEY root, | 133 bool QueryRegValueSZ(ROOT_KEY root, |
134 WOW64_OVERRIDE wow64_override, | |
116 const wchar_t* key_path, | 135 const wchar_t* key_path, |
117 const wchar_t* value_name, | 136 const wchar_t* value_name, |
118 std::wstring* out_sz); | 137 std::wstring* out_sz); |
119 | 138 |
120 // Query MULTI_SZ (multiple strings) value. | 139 // Query MULTI_SZ (multiple strings) value. |
121 // - WRAPPER: Function works with MULTI_SZ data type. | 140 // - WRAPPER: Function works with MULTI_SZ data type. |
122 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 141 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
123 // - Handle will be left open. Caller must still call CloseRegKey when done. | 142 // - Handle will be left open. Caller must still call CloseRegKey when done. |
124 bool QueryRegValueMULTISZ(HANDLE key, | 143 bool QueryRegValueMULTISZ(HANDLE key, |
125 const wchar_t* value_name, | 144 const wchar_t* value_name, |
126 std::vector<std::wstring>* out_multi_sz); | 145 std::vector<std::wstring>* out_multi_sz); |
127 | 146 |
128 // Query MULTI_SZ (multiple strings) value. | 147 // Query MULTI_SZ (multiple strings) value. |
129 // - WRAPPER: Function opens and closes the target key for caller, and works | 148 // - WRAPPER: Function opens and closes the target key for caller, and works |
130 // with MULTI_SZ data type. | 149 // with MULTI_SZ data type. |
150 // - Use |wow64_override| to force redirection behaviour, or pass 0/nt::NONE. | |
131 bool QueryRegValueMULTISZ(ROOT_KEY root, | 151 bool QueryRegValueMULTISZ(ROOT_KEY root, |
152 WOW64_OVERRIDE wow64_override, | |
132 const wchar_t* key_path, | 153 const wchar_t* key_path, |
133 const wchar_t* value_name, | 154 const wchar_t* value_name, |
134 std::vector<std::wstring>* out_multi_sz); | 155 std::vector<std::wstring>* out_multi_sz); |
135 | 156 |
136 //------------------------------------------------------------------------------ | 157 //------------------------------------------------------------------------------ |
137 // Setter functions | 158 // Setter functions |
138 //------------------------------------------------------------------------------ | 159 //------------------------------------------------------------------------------ |
139 | 160 |
140 // Main function to set a registry value. | 161 // Main function to set a registry value. |
141 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 162 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
142 // - Types defined in winnt.h. E.g.: REG_DWORD, REG_SZ. | 163 // - Types defined in winnt.h. E.g.: REG_DWORD, REG_SZ. |
143 bool SetRegKeyValue(HANDLE key, | 164 bool SetRegKeyValue(HANDLE key, |
144 const wchar_t* value_name, | 165 const wchar_t* value_name, |
145 ULONG type, | 166 ULONG type, |
146 const BYTE* data, | 167 const BYTE* data, |
147 DWORD data_size); | 168 DWORD data_size); |
148 | 169 |
149 // Set DWORD value. | 170 // Set DWORD value. |
150 // - WRAPPER: Function works with DWORD data type. | 171 // - WRAPPER: Function works with DWORD data type. |
151 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 172 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
152 // - Handle will be left open. Caller must still call CloseRegKey when done. | 173 // - Handle will be left open. Caller must still call CloseRegKey when done. |
153 bool SetRegValueDWORD(HANDLE key, const wchar_t* value_name, DWORD value); | 174 bool SetRegValueDWORD(HANDLE key, const wchar_t* value_name, DWORD value); |
154 | 175 |
155 // Set DWORD value. | 176 // Set DWORD value. |
156 // - WRAPPER: Function opens and closes the target key for caller, and works | 177 // - WRAPPER: Function opens and closes the target key for caller, and works |
157 // with DWORD data type. | 178 // with DWORD data type. |
179 // - Use |wow64_override| to force redirection behaviour, or pass 0/nt::NONE. | |
158 bool SetRegValueDWORD(ROOT_KEY root, | 180 bool SetRegValueDWORD(ROOT_KEY root, |
181 WOW64_OVERRIDE wow64_override, | |
159 const wchar_t* key_path, | 182 const wchar_t* key_path, |
160 const wchar_t* value_name, | 183 const wchar_t* value_name, |
161 DWORD value); | 184 DWORD value); |
162 | 185 |
163 // Set SZ (string) value. | 186 // Set SZ (string) value. |
164 // - WRAPPER: Function works with SZ data type. | 187 // - WRAPPER: Function works with SZ data type. |
165 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 188 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
166 // - Handle will be left open. Caller must still call CloseRegKey when done. | 189 // - Handle will be left open. Caller must still call CloseRegKey when done. |
167 bool SetRegValueSZ(HANDLE key, | 190 bool SetRegValueSZ(HANDLE key, |
168 const wchar_t* value_name, | 191 const wchar_t* value_name, |
169 const std::wstring& value); | 192 const std::wstring& value); |
170 | 193 |
171 // Set SZ (string) value. | 194 // Set SZ (string) value. |
172 // - WRAPPER: Function opens and closes the target key for caller, and works | 195 // - WRAPPER: Function opens and closes the target key for caller, and works |
173 // with SZ data type. | 196 // with SZ data type. |
197 // - Use |wow64_override| to force redirection behaviour, or pass 0/nt::NONE. | |
174 bool SetRegValueSZ(ROOT_KEY root, | 198 bool SetRegValueSZ(ROOT_KEY root, |
199 WOW64_OVERRIDE wow64_override, | |
175 const wchar_t* key_path, | 200 const wchar_t* key_path, |
176 const wchar_t* value_name, | 201 const wchar_t* value_name, |
177 const std::wstring& value); | 202 const std::wstring& value); |
178 | 203 |
179 // Set MULTI_SZ (multiple strings) value. | 204 // Set MULTI_SZ (multiple strings) value. |
180 // - WRAPPER: Function works with MULTI_SZ data type. | 205 // - WRAPPER: Function works with MULTI_SZ data type. |
181 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | 206 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
182 // - Handle will be left open. Caller must still call CloseRegKey when done. | 207 // - Handle will be left open. Caller must still call CloseRegKey when done. |
183 bool SetRegValueMULTISZ(HANDLE key, | 208 bool SetRegValueMULTISZ(HANDLE key, |
184 const wchar_t* value_name, | 209 const wchar_t* value_name, |
185 const std::vector<std::wstring>& values); | 210 const std::vector<std::wstring>& values); |
186 | 211 |
187 // Set MULTI_SZ (multiple strings) value. | 212 // Set MULTI_SZ (multiple strings) value. |
188 // - WRAPPER: Function opens and closes the target key for caller, and works | 213 // - WRAPPER: Function opens and closes the target key for caller, and works |
189 // with MULTI_SZ data type. | 214 // with MULTI_SZ data type. |
215 // - Use |wow64_override| to force redirection behaviour, or pass 0/nt::NONE. | |
190 bool SetRegValueMULTISZ(ROOT_KEY root, | 216 bool SetRegValueMULTISZ(ROOT_KEY root, |
217 WOW64_OVERRIDE wow64_override, | |
191 const wchar_t* key_path, | 218 const wchar_t* key_path, |
192 const wchar_t* value_name, | 219 const wchar_t* value_name, |
193 const std::vector<std::wstring>& values); | 220 const std::vector<std::wstring>& values); |
194 | 221 |
195 //------------------------------------------------------------------------------ | 222 //------------------------------------------------------------------------------ |
196 // Utils | 223 // Utils |
197 //------------------------------------------------------------------------------ | 224 //------------------------------------------------------------------------------ |
198 | 225 |
199 // Returns the current user SID in string form. | 226 // Returns the current user SID in string form. |
200 const wchar_t* GetCurrentUserSidString(); | 227 const wchar_t* GetCurrentUserSidString(); |
201 | 228 |
202 }; // namespace nt | 229 }; // namespace nt |
203 | 230 |
204 #endif // CHROME_ELF_NT_REGISTRY_NT_REGISTRY_H_ | 231 #endif // CHROME_ELF_NT_REGISTRY_NT_REGISTRY_H_ |
OLD | NEW |