blob: 5acb1f63794078ae5699f80c5eaf8650192fc137 [file] [log] [blame]
Georg Brandl38feaf02008-05-25 07:45:51 +00001:mod:`winreg` -- Windows registry access
Georg Brandl116aa622007-08-15 14:28:22 +00002=========================================
3
Georg Brandl38feaf02008-05-25 07:45:51 +00004.. module:: winreg
Georg Brandl116aa622007-08-15 14:28:22 +00005 :platform: Windows
6 :synopsis: Routines and objects for manipulating the Windows registry.
7.. sectionauthor:: Mark Hammond <MarkH@ActiveState.com>
8
9
Georg Brandl116aa622007-08-15 14:28:22 +000010These functions expose the Windows registry API to Python. Instead of using an
11integer as the registry handle, a handle object is used to ensure that the
12handles are closed correctly, even if the programmer neglects to explicitly
13close them.
14
Georg Brandl116aa622007-08-15 14:28:22 +000015This module offers the following functions:
16
17
18.. function:: CloseKey(hkey)
19
20 Closes a previously opened registry key. The hkey argument specifies a
21 previously opened key.
22
Brian Curtin2d067c82010-05-11 20:35:47 +000023 .. note::
24 If *hkey* is not closed using this method (or via :meth:`hkey.Close() <PyHKEY.Close>`),
25 it is closed when the *hkey* object is destroyed by Python.
Georg Brandl116aa622007-08-15 14:28:22 +000026
27
28.. function:: ConnectRegistry(computer_name, key)
29
Ezio Melottibc372982010-04-25 17:48:01 +000030 Establishes a connection to a predefined registry handle on another computer,
31 and returns a :ref:`handle object <handle-object>`.
Georg Brandl116aa622007-08-15 14:28:22 +000032
Ezio Melottibc372982010-04-25 17:48:01 +000033 *computer_name* is the name of the remote computer, of the form
Georg Brandl116aa622007-08-15 14:28:22 +000034 ``r"\\computername"``. If ``None``, the local computer is used.
35
36 *key* is the predefined handle to connect to.
37
Benjamin Petersond23f8222009-04-05 19:13:16 +000038 The return value is the handle of the opened key. If the function fails, a
Ezio Melottibc372982010-04-25 17:48:01 +000039 :exc:`WindowsError` exception is raised.
Georg Brandl116aa622007-08-15 14:28:22 +000040
41
42.. function:: CreateKey(key, sub_key)
43
Ezio Melottibc372982010-04-25 17:48:01 +000044 Creates or opens the specified key, returning a
45 :ref:`handle object <handle-object>`.
Georg Brandl116aa622007-08-15 14:28:22 +000046
Brian Curtin2d067c82010-05-11 20:35:47 +000047 *key* is an already open key, or one of the predefined
48 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +000049
Ezio Melottibc372982010-04-25 17:48:01 +000050 *sub_key* is a string that names the key this method opens or creates.
Georg Brandl116aa622007-08-15 14:28:22 +000051
Ezio Melottibc372982010-04-25 17:48:01 +000052 If *key* is one of the predefined keys, *sub_key* may be ``None``. In that
53 case, the handle returned is the same key handle passed in to the function.
Georg Brandl116aa622007-08-15 14:28:22 +000054
55 If the key already exists, this function opens the existing key.
56
Benjamin Petersond23f8222009-04-05 19:13:16 +000057 The return value is the handle of the opened key. If the function fails, a
Ezio Melottibc372982010-04-25 17:48:01 +000058 :exc:`WindowsError` exception is raised.
Georg Brandl116aa622007-08-15 14:28:22 +000059
60
Brian Curtind8559482010-04-24 17:21:31 +000061.. function:: CreateKeyEx(key, sub_key[, res[, sam]])
Brian Curtin3035c392010-04-21 23:56:21 +000062
Ezio Melottibc372982010-04-25 17:48:01 +000063 Creates or opens the specified key, returning a
64 :ref:`handle object <handle-object>`.
Brian Curtin3035c392010-04-21 23:56:21 +000065
Brian Curtin2d067c82010-05-11 20:35:47 +000066 *key* is an already open key, or one of the predefined
67 :ref:`HKEY_* constants <hkey-constants>`.
Brian Curtin3035c392010-04-21 23:56:21 +000068
69 *sub_key* is a string that names the key this method opens or creates.
70
71 *res* is a reserved integer, and must be zero. The default is zero.
72
73 *sam* is an integer that specifies an access mask that describes the desired
Brian Curtin2d067c82010-05-11 20:35:47 +000074 security access for the key. Default is :const:`KEY_ALL_ACCESS`. See
75 :ref:`Access Rights <access-rights>` for other allowed values.
Brian Curtin3035c392010-04-21 23:56:21 +000076
Ezio Melottibc372982010-04-25 17:48:01 +000077 If *key* is one of the predefined keys, *sub_key* may be ``None``. In that
78 case, the handle returned is the same key handle passed in to the function.
Brian Curtin3035c392010-04-21 23:56:21 +000079
80 If the key already exists, this function opens the existing key.
81
82 The return value is the handle of the opened key. If the function fails, a
83 :exc:`WindowsError` exception is raised.
84
Georg Brandl4c25cf32010-04-22 07:00:42 +000085 .. versionadded:: 3.2
Brian Curtin3035c392010-04-21 23:56:21 +000086
87
Georg Brandl116aa622007-08-15 14:28:22 +000088.. function:: DeleteKey(key, sub_key)
89
90 Deletes the specified key.
91
Brian Curtin2d067c82010-05-11 20:35:47 +000092 *key* is an already open key, or one of the predefined
93 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +000094
Brian Curtin3035c392010-04-21 23:56:21 +000095 *sub_key* is a string that must be a subkey of the key identified by the *key*
96 parameter. This value must not be ``None``, and the key may not have subkeys.
Georg Brandl116aa622007-08-15 14:28:22 +000097
98 *This method can not delete keys with subkeys.*
99
100 If the method succeeds, the entire key, including all of its values, is removed.
Ezio Melottibc372982010-04-25 17:48:01 +0000101 If the method fails, a :exc:`WindowsError` exception is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000102
103
Brian Curtind8559482010-04-24 17:21:31 +0000104.. function:: DeleteKeyEx(key, sub_key[, sam[, res]])
Brian Curtin3035c392010-04-21 23:56:21 +0000105
106 Deletes the specified key.
107
108 .. note::
109 The :func:`DeleteKeyEx` function is implemented with the RegDeleteKeyEx
110 Windows API function, which is specific to 64-bit versions of Windows.
Brian Curtin2d067c82010-05-11 20:35:47 +0000111 See the `RegDeleteKeyEx documentation
112 <http://msdn.microsoft.com/en-us/library/ms724847%28VS.85%29.aspx>`__.
Brian Curtin3035c392010-04-21 23:56:21 +0000113
Brian Curtin2d067c82010-05-11 20:35:47 +0000114 *key* is an already open key, or one of the predefined
115 :ref:`HKEY_* constants <hkey-constants>`.
Brian Curtin3035c392010-04-21 23:56:21 +0000116
117 *sub_key* is a string that must be a subkey of the key identified by the
118 *key* parameter. This value must not be ``None``, and the key may not have
119 subkeys.
120
121 *res* is a reserved integer, and must be zero. The default is zero.
122
123 *sam* is an integer that specifies an access mask that describes the
Brian Curtin2d067c82010-05-11 20:35:47 +0000124 security access for the key. Default is :const:`KEY_ALL_ACCESS`. See
125 :ref:`Access Rights <access-rights>` for other allowed values.
Brian Curtin3035c392010-04-21 23:56:21 +0000126
127 *This method can not delete keys with subkeys.*
128
129 If the method succeeds, the entire key, including all of its values, is
130 removed. If the method fails, a :exc:`WindowsError` exception is raised.
131
132 On unsupported Windows versions, :exc:`NotImplementedError` is raised.
133
Georg Brandl4c25cf32010-04-22 07:00:42 +0000134 .. versionadded:: 3.2
Brian Curtin3035c392010-04-21 23:56:21 +0000135
136
Georg Brandl116aa622007-08-15 14:28:22 +0000137.. function:: DeleteValue(key, value)
138
139 Removes a named value from a registry key.
140
Brian Curtin2d067c82010-05-11 20:35:47 +0000141 *key* is an already open key, or one of the predefined
142 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000143
144 *value* is a string that identifies the value to remove.
145
146
147.. function:: EnumKey(key, index)
148
149 Enumerates subkeys of an open registry key, returning a string.
150
Brian Curtin2d067c82010-05-11 20:35:47 +0000151 *key* is an already open key, or one of the predefined
152 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000153
Ezio Melottibc372982010-04-25 17:48:01 +0000154 *index* is an integer that identifies the index of the key to retrieve.
Georg Brandl116aa622007-08-15 14:28:22 +0000155
Ezio Melottibc372982010-04-25 17:48:01 +0000156 The function retrieves the name of one subkey each time it is called. It is
157 typically called repeatedly until a :exc:`WindowsError` exception is
Georg Brandl116aa622007-08-15 14:28:22 +0000158 raised, indicating, no more values are available.
159
160
161.. function:: EnumValue(key, index)
162
163 Enumerates values of an open registry key, returning a tuple.
164
Brian Curtin2d067c82010-05-11 20:35:47 +0000165 *key* is an already open key, or one of the predefined
166 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000167
Ezio Melottibc372982010-04-25 17:48:01 +0000168 *index* is an integer that identifies the index of the value to retrieve.
Georg Brandl116aa622007-08-15 14:28:22 +0000169
Ezio Melottibc372982010-04-25 17:48:01 +0000170 The function retrieves the name of one subkey each time it is called. It is
171 typically called repeatedly, until a :exc:`WindowsError` exception is
172 raised, indicating no more values.
Georg Brandl116aa622007-08-15 14:28:22 +0000173
174 The result is a tuple of 3 items:
175
176 +-------+--------------------------------------------+
177 | Index | Meaning |
178 +=======+============================================+
179 | ``0`` | A string that identifies the value name |
180 +-------+--------------------------------------------+
181 | ``1`` | An object that holds the value data, and |
182 | | whose type depends on the underlying |
183 | | registry type |
184 +-------+--------------------------------------------+
185 | ``2`` | An integer that identifies the type of the |
186 | | value data |
187 +-------+--------------------------------------------+
188
189
Ezio Melotti985e24d2009-09-13 07:54:02 +0000190.. function:: ExpandEnvironmentStrings(str)
Christian Heimes2380ac72008-01-09 00:17:24 +0000191
Ezio Melotti985e24d2009-09-13 07:54:02 +0000192 Expands environment strings %NAME% in unicode string like :const:`REG_EXPAND_SZ`::
Christian Heimes2380ac72008-01-09 00:17:24 +0000193
Ezio Melotti985e24d2009-09-13 07:54:02 +0000194 >>> ExpandEnvironmentStrings('%windir%')
195 'C:\\Windows'
Christian Heimes2380ac72008-01-09 00:17:24 +0000196
Christian Heimes2380ac72008-01-09 00:17:24 +0000197
Georg Brandl116aa622007-08-15 14:28:22 +0000198.. function:: FlushKey(key)
199
200 Writes all the attributes of a key to the registry.
201
Brian Curtin2d067c82010-05-11 20:35:47 +0000202 *key* is an already open key, or one of the predefined
203 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000204
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000205 It is not necessary to call :func:`FlushKey` to change a key. Registry changes are
Ezio Melottibc372982010-04-25 17:48:01 +0000206 flushed to disk by the registry using its lazy flusher. Registry changes are
207 also flushed to disk at system shutdown. Unlike :func:`CloseKey`, the
208 :func:`FlushKey` method returns only when all the data has been written to the
Georg Brandl116aa622007-08-15 14:28:22 +0000209 registry. An application should only call :func:`FlushKey` if it requires
Ezio Melottibc372982010-04-25 17:48:01 +0000210 absolute certainty that registry changes are on disk.
Georg Brandl116aa622007-08-15 14:28:22 +0000211
212 .. note::
213
Ezio Melottibc372982010-04-25 17:48:01 +0000214 If you don't know whether a :func:`FlushKey` call is required, it probably
Georg Brandl116aa622007-08-15 14:28:22 +0000215 isn't.
216
217
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000218.. function:: LoadKey(key, sub_key, file_name)
Georg Brandl116aa622007-08-15 14:28:22 +0000219
Ezio Melottibc372982010-04-25 17:48:01 +0000220 Creates a subkey under the specified key and stores registration information
Georg Brandl116aa622007-08-15 14:28:22 +0000221 from a specified file into that subkey.
222
Brian Curtin2d067c82010-05-11 20:35:47 +0000223 *key* is a handle returned by :func:`ConnectRegistry` or one of the constants
224 :const:`HKEY_USERS` or :const:`HKEY_LOCAL_MACHINE`.
Georg Brandl116aa622007-08-15 14:28:22 +0000225
226 *sub_key* is a string that identifies the sub_key to load.
227
228 *file_name* is the name of the file to load registry data from. This file must
229 have been created with the :func:`SaveKey` function. Under the file allocation
230 table (FAT) file system, the filename may not have an extension.
231
232 A call to LoadKey() fails if the calling process does not have the
233 :const:`SE_RESTORE_PRIVILEGE` privilege. Note that privileges are different than
Brian Curtin2d067c82010-05-11 20:35:47 +0000234 from permissions -- see the `RegLoadKey documentation
235 <http://msdn.microsoft.com/en-us/library/ms724889%28v=VS.85%29.aspx>`__ for
236 more details.
Georg Brandl116aa622007-08-15 14:28:22 +0000237
Ezio Melottibc372982010-04-25 17:48:01 +0000238 If *key* is a handle returned by :func:`ConnectRegistry`, then the path
239 specified in *fileName* is relative to the remote computer.
Georg Brandl116aa622007-08-15 14:28:22 +0000240
Ezio Melottibc372982010-04-25 17:48:01 +0000241 The Win32 documentation implies *key* must be in the :const:`HKEY_USER` or
Georg Brandl116aa622007-08-15 14:28:22 +0000242 :const:`HKEY_LOCAL_MACHINE` tree. This may or may not be true.
243
244
Brian Curtind8559482010-04-24 17:21:31 +0000245.. function:: OpenKey(key, sub_key[, res[, sam]])
Georg Brandl116aa622007-08-15 14:28:22 +0000246
Ezio Melottibc372982010-04-25 17:48:01 +0000247 Opens the specified key, returning a :ref:`handle object <handle-object>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000248
Brian Curtin2d067c82010-05-11 20:35:47 +0000249 *key* is an already open key, or one of the predefined
250 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000251
252 *sub_key* is a string that identifies the sub_key to open.
253
254 *res* is a reserved integer, and must be zero. The default is zero.
255
Ezio Melottibc372982010-04-25 17:48:01 +0000256 *sam* is an integer that specifies an access mask that describes the desired
Brian Curtin2d067c82010-05-11 20:35:47 +0000257 security access for the key. Default is :const:`KEY_READ`. See
258 :ref:`Access Rights <access-rights>` for other allowed values.
Georg Brandl116aa622007-08-15 14:28:22 +0000259
260 The result is a new handle to the specified key.
261
Benjamin Petersond23f8222009-04-05 19:13:16 +0000262 If the function fails, :exc:`WindowsError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000263
264
265.. function:: OpenKeyEx()
266
Ezio Melottibc372982010-04-25 17:48:01 +0000267 The functionality of :func:`OpenKeyEx` is provided via :func:`OpenKey`,
268 by the use of default arguments.
Georg Brandl116aa622007-08-15 14:28:22 +0000269
270
271.. function:: QueryInfoKey(key)
272
273 Returns information about a key, as a tuple.
274
Brian Curtin2d067c82010-05-11 20:35:47 +0000275 *key* is an already open key, or one of the predefined
276 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000277
278 The result is a tuple of 3 items:
279
280 +-------+---------------------------------------------+
281 | Index | Meaning |
282 +=======+=============================================+
283 | ``0`` | An integer giving the number of sub keys |
284 | | this key has. |
285 +-------+---------------------------------------------+
286 | ``1`` | An integer giving the number of values this |
287 | | key has. |
288 +-------+---------------------------------------------+
Georg Brandlba956ae2007-11-29 17:24:34 +0000289 | ``2`` | An integer giving when the key was last |
Georg Brandl116aa622007-08-15 14:28:22 +0000290 | | modified (if available) as 100's of |
291 | | nanoseconds since Jan 1, 1600. |
292 +-------+---------------------------------------------+
293
294
295.. function:: QueryValue(key, sub_key)
296
Ezio Melottibc372982010-04-25 17:48:01 +0000297 Retrieves the unnamed value for a key, as a string.
Georg Brandl116aa622007-08-15 14:28:22 +0000298
Brian Curtin2d067c82010-05-11 20:35:47 +0000299 *key* is an already open key, or one of the predefined
300 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000301
Ezio Melottibc372982010-04-25 17:48:01 +0000302 *sub_key* is a string that holds the name of the subkey with which the value is
303 associated. If this parameter is ``None`` or empty, the function retrieves the
304 value set by the :func:`SetValue` method for the key identified by *key*.
Georg Brandl116aa622007-08-15 14:28:22 +0000305
Benjamin Petersond23f8222009-04-05 19:13:16 +0000306 Values in the registry have name, type, and data components. This method
Georg Brandl116aa622007-08-15 14:28:22 +0000307 retrieves the data for a key's first value that has a NULL name. But the
Benjamin Petersond23f8222009-04-05 19:13:16 +0000308 underlying API call doesn't return the type, so always use
309 :func:`QueryValueEx` if possible.
Georg Brandl116aa622007-08-15 14:28:22 +0000310
311
312.. function:: QueryValueEx(key, value_name)
313
Ezio Melottibc372982010-04-25 17:48:01 +0000314 Retrieves the type and data for a specified value name associated with
315 an open registry key.
Georg Brandl116aa622007-08-15 14:28:22 +0000316
Brian Curtin2d067c82010-05-11 20:35:47 +0000317 *key* is an already open key, or one of the predefined
318 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000319
320 *value_name* is a string indicating the value to query.
321
322 The result is a tuple of 2 items:
323
324 +-------+-----------------------------------------+
325 | Index | Meaning |
326 +=======+=========================================+
327 | ``0`` | The value of the registry item. |
328 +-------+-----------------------------------------+
329 | ``1`` | An integer giving the registry type for |
330 | | this value. |
331 +-------+-----------------------------------------+
332
333
334.. function:: SaveKey(key, file_name)
335
336 Saves the specified key, and all its subkeys to the specified file.
337
Brian Curtin2d067c82010-05-11 20:35:47 +0000338 *key* is an already open key, or one of the predefined
339 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000340
341 *file_name* is the name of the file to save registry data to. This file cannot
342 already exist. If this filename includes an extension, it cannot be used on file
343 allocation table (FAT) file systems by the :meth:`LoadKey`, :meth:`ReplaceKey`
Ezio Melottibc372982010-04-25 17:48:01 +0000344 or :meth:`RestoreKey` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000345
Ezio Melottibc372982010-04-25 17:48:01 +0000346 If *key* represents a key on a remote computer, the path described by
Georg Brandl116aa622007-08-15 14:28:22 +0000347 *file_name* is relative to the remote computer. The caller of this method must
Ezio Melottibc372982010-04-25 17:48:01 +0000348 possess the :const:`SeBackupPrivilege` security privilege. Note that
Brian Curtin2d067c82010-05-11 20:35:47 +0000349 privileges are different than permissions -- see the
350 `Conflicts Between User Rights and Permissions documentation
351 <http://msdn.microsoft.com/en-us/library/ms724878%28v=VS.85%29.aspx>`__
352 for more details.
Georg Brandl116aa622007-08-15 14:28:22 +0000353
354 This function passes NULL for *security_attributes* to the API.
355
356
357.. function:: SetValue(key, sub_key, type, value)
358
359 Associates a value with a specified key.
360
Brian Curtin2d067c82010-05-11 20:35:47 +0000361 *key* is an already open key, or one of the predefined
362 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000363
Ezio Melottibc372982010-04-25 17:48:01 +0000364 *sub_key* is a string that names the subkey with which the value is associated.
Georg Brandl116aa622007-08-15 14:28:22 +0000365
366 *type* is an integer that specifies the type of the data. Currently this must be
367 :const:`REG_SZ`, meaning only strings are supported. Use the :func:`SetValueEx`
368 function for support for other data types.
369
370 *value* is a string that specifies the new value.
371
372 If the key specified by the *sub_key* parameter does not exist, the SetValue
373 function creates it.
374
375 Value lengths are limited by available memory. Long values (more than 2048
376 bytes) should be stored as files with the filenames stored in the configuration
377 registry. This helps the registry perform efficiently.
378
Ezio Melottibc372982010-04-25 17:48:01 +0000379 The key identified by the *key* parameter must have been opened with
Georg Brandl116aa622007-08-15 14:28:22 +0000380 :const:`KEY_SET_VALUE` access.
381
382
383.. function:: SetValueEx(key, value_name, reserved, type, value)
384
385 Stores data in the value field of an open registry key.
386
Brian Curtin2d067c82010-05-11 20:35:47 +0000387 *key* is an already open key, or one of the predefined
388 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000389
Ezio Melottibc372982010-04-25 17:48:01 +0000390 *value_name* is a string that names the subkey with which the value is
Georg Brandl116aa622007-08-15 14:28:22 +0000391 associated.
392
Brian Curtin2d067c82010-05-11 20:35:47 +0000393 *type* is an integer that specifies the type of the data. See
394 :ref:`Value Types <value-types>` for the available types.
Georg Brandl116aa622007-08-15 14:28:22 +0000395
Ezio Melottibc372982010-04-25 17:48:01 +0000396 *reserved* can be anything -- zero is always passed to the API.
Georg Brandl116aa622007-08-15 14:28:22 +0000397
398 *value* is a string that specifies the new value.
399
400 This method can also set additional value and type information for the specified
401 key. The key identified by the key parameter must have been opened with
402 :const:`KEY_SET_VALUE` access.
403
Ezio Melottibc372982010-04-25 17:48:01 +0000404 To open the key, use the :func:`CreateKey` or :func:`OpenKey` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000405
406 Value lengths are limited by available memory. Long values (more than 2048
407 bytes) should be stored as files with the filenames stored in the configuration
408 registry. This helps the registry perform efficiently.
409
410
Brian Curtin3035c392010-04-21 23:56:21 +0000411.. function:: DisableReflectionKey(key)
412
413 Disables registry reflection for 32-bit processes running on a 64-bit
414 Operating System.
415
Brian Curtin2d067c82010-05-11 20:35:47 +0000416 *key* is an already open key, or one of the predefined
417 :ref:`HKEY_* constants <hkey-constants>`.
Brian Curtin3035c392010-04-21 23:56:21 +0000418
419 Will generally raise :exc:`NotImplemented` if executed on a 32-bit
420 Operating System.
421
422 If the key is not on the reflection list, the function succeeds but has no
423 effect. Disabling reflection for a key does not affect reflection of any
424 subkeys.
425
426
427.. function:: EnableReflectionKey(key)
428
429 Restores registry reflection for the specified disabled key.
430
Brian Curtin2d067c82010-05-11 20:35:47 +0000431 *key* is an already open key, or one of the predefined
432 :ref:`HKEY_* constants <hkey-constants>`.
Brian Curtin3035c392010-04-21 23:56:21 +0000433
434 Will generally raise :exc:`NotImplemented` if executed on a 32-bit
435 Operating System.
436
437 Restoring reflection for a key does not affect reflection of any subkeys.
438
439
440.. function:: QueryReflectionKey(key)
441
442 Determines the reflection state for the specified key.
443
Brian Curtin2d067c82010-05-11 20:35:47 +0000444 *key* is an already open key, or one of the predefined
445 :ref:`HKEY_* constants <hkey-constants>`.
Brian Curtin3035c392010-04-21 23:56:21 +0000446
447 Returns ``True`` if reflection is disabled.
448
449 Will generally raise :exc:`NotImplemented` if executed on a 32-bit
450 Operating System.
451
452
Brian Curtin2d067c82010-05-11 20:35:47 +0000453.. _constants:
454
455Constants
456------------------
457
458The following constants are defined for use in many :mod:`_winreg` functions.
459
460.. _hkey-constants:
461
462HKEY_* Constants
463++++++++++++++++
464
465.. data:: HKEY_CLASSES_ROOT
466
467 Registry entries subordinate to this key define types (or classes) of
468 documents and the properties associated with those types. Shell and
469 COM applications use the information stored under this key.
470
471
472.. data:: HKEY_CURRENT_USER
473
474 Registry entries subordinate to this key define the preferences of
475 the current user. These preferences include the settings of
476 environment variables, data about program groups, colors, printers,
477 network connections, and application preferences.
478
479.. data:: HKEY_LOCAL_MACHINE
480
481 Registry entries subordinate to this key define the physical state
482 of the computer, including data about the bus type, system memory,
483 and installed hardware and software.
484
485.. data:: HKEY_USERS
486
487 Registry entries subordinate to this key define the default user
488 configuration for new users on the local computer and the user
489 configuration for the current user.
490
491.. data:: HKEY_PERFORMANCE_DATA
492
493 Registry entries subordinate to this key allow you to access
494 performance data. The data is not actually stored in the registry;
495 the registry functions cause the system to collect the data from
496 its source.
497
498
499.. data:: HKEY_CURRENT_CONFIG
500
501 Contains information about the current hardware profile of the
502 local computer system.
503
504.. data:: HKEY_DYN_DATA
505
506 This key is not used in versions of Windows after 98.
507
508
509.. _access-rights:
510
511Access Rights
512+++++++++++++
513
514For more information, see `Registry Key Security and Access
515<http://msdn.microsoft.com/en-us/library/ms724878%28v=VS.85%29.aspx>`__.
516
517.. data:: KEY_ALL_ACCESS
518
519 Combines the STANDARD_RIGHTS_REQUIRED, :const:`KEY_QUERY_VALUE`,
520 :const:`KEY_SET_VALUE`, :const:`KEY_CREATE_SUB_KEY`,
521 :const:`KEY_ENUMERATE_SUB_KEYS`, :const:`KEY_NOTIFY`,
522 and :const:`KEY_CREATE_LINK` access rights.
523
524.. data:: KEY_WRITE
525
526 Combines the STANDARD_RIGHTS_WRITE, :const:`KEY_SET_VALUE`, and
527 :const:`KEY_CREATE_SUB_KEY` access rights.
528
529.. data:: KEY_READ
530
531 Combines the STANDARD_RIGHTS_READ, :const:`KEY_QUERY_VALUE`,
532 :const:`KEY_ENUMERATE_SUB_KEYS`, and :const:`KEY_NOTIFY` values.
533
534.. data:: KEY_EXECUTE
535
536 Equivalent to :const:`KEY_READ`.
537
538.. data:: KEY_QUERY_VALUE
539
540 Required to query the values of a registry key.
541
542.. data:: KEY_SET_VALUE
543
544 Required to create, delete, or set a registry value.
545
546.. data:: KEY_CREATE_SUB_KEY
547
548 Required to create a subkey of a registry key.
549
550.. data:: KEY_ENUMERATE_SUB_KEYS
551
552 Required to enumerate the subkeys of a registry key.
553
554.. data:: KEY_NOTIFY
555
556 Required to request change notifications for a registry key or for
557 subkeys of a registry key.
558
559.. data:: KEY_CREATE_LINK
560
561 Reserved for system use.
562
563
564.. _64-bit-access-rights:
565
56664-bit Specific
567***************
568
569For more information, see `Accesing an Alternate Registry View
570<http://msdn.microsoft.com/en-us/library/aa384129(v=VS.85).aspx>`__.
571
572.. data:: KEY_WOW64_64KEY
573
574 Indicates that an application on 64-bit Windows should operate on
575 the 64-bit registry view.
576
577.. data:: KEY_WOW64_32KEY
578
579 Indicates that an application on 64-bit Windows should operate on
580 the 32-bit registry view.
581
582
583.. _value-types:
584
585Value Types
586+++++++++++
587
588For more information, see `Registry Value Types
589<http://msdn.microsoft.com/en-us/library/ms724884%28v=VS.85%29.aspx>`__.
590
591.. data:: REG_BINARY
592
593 Binary data in any form.
594
595.. data:: REG_DWORD
596
597 32-bit number.
598
599.. data:: REG_DWORD_LITTLE_ENDIAN
600
601 A 32-bit number in little-endian format.
602
603.. data:: REG_DWORD_BIG_ENDIAN
604
605 A 32-bit number in big-endian format.
606
607.. data:: REG_EXPAND_SZ
608
609 Null-terminated string containing references to environment
610 variables (``%PATH%``).
611
612.. data:: REG_LINK
613
614 A Unicode symbolic link.
615
616.. data:: REG_MULTI_SZ
617
618 A sequence of null-terminated strings, terminated by two null characters.
619 (Python handles this termination automatically.)
620
621.. data:: REG_NONE
622
623 No defined value type.
624
625.. data:: REG_RESOURCE_LIST
626
627 A device-driver resource list.
628
629.. data:: REG_FULL_RESOURCE_DESCRIPTOR
630
631 A hardware setting.
632
633.. data:: REG_RESOURCE_REQUIREMENTS_LIST
634
635 A hardware resource list.
636
637.. data:: REG_SZ
638
639 A null-terminated string.
640
641
Georg Brandl116aa622007-08-15 14:28:22 +0000642.. _handle-object:
643
644Registry Handle Objects
645-----------------------
646
647This object wraps a Windows HKEY object, automatically closing it when the
648object is destroyed. To guarantee cleanup, you can call either the
Ezio Melottibc372982010-04-25 17:48:01 +0000649:meth:`Close` method on the object, or the :func:`CloseKey` function.
Georg Brandl116aa622007-08-15 14:28:22 +0000650
651All registry functions in this module return one of these objects.
652
Ezio Melottibc372982010-04-25 17:48:01 +0000653All registry functions in this module which accept a handle object also accept
654an integer, however, use of the handle object is encouraged.
Georg Brandl116aa622007-08-15 14:28:22 +0000655
Ezio Melottibc372982010-04-25 17:48:01 +0000656Handle objects provide semantics for :meth:`__bool__` -- thus ::
Georg Brandl116aa622007-08-15 14:28:22 +0000657
658 if handle:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000659 print("Yes")
Georg Brandl116aa622007-08-15 14:28:22 +0000660
661will print ``Yes`` if the handle is currently valid (has not been closed or
662detached).
663
664The object also support comparison semantics, so handle objects will compare
665true if they both reference the same underlying Windows handle value.
666
Georg Brandl22b34312009-07-26 14:54:51 +0000667Handle objects can be converted to an integer (e.g., using the built-in
Georg Brandl116aa622007-08-15 14:28:22 +0000668:func:`int` function), in which case the underlying Windows handle value is
Ezio Melottibc372982010-04-25 17:48:01 +0000669returned. You can also use the :meth:`Detach` method to return the integer
Georg Brandl116aa622007-08-15 14:28:22 +0000670handle, and also disconnect the Windows handle from the handle object.
671
672
673.. method:: PyHKEY.Close()
674
675 Closes the underlying Windows handle.
676
677 If the handle is already closed, no error is raised.
678
679
680.. method:: PyHKEY.Detach()
681
682 Detaches the Windows handle from the handle object.
683
Georg Brandl5c106642007-11-29 17:41:05 +0000684 The result is an integer that holds the value of the handle before it is
685 detached. If the handle is already detached or closed, this will return
686 zero.
Georg Brandl116aa622007-08-15 14:28:22 +0000687
688 After calling this function, the handle is effectively invalidated, but the
Ezio Melottibc372982010-04-25 17:48:01 +0000689 handle is not closed. You would call this function when you need the
690 underlying Win32 handle to exist beyond the lifetime of the handle object.
Georg Brandl116aa622007-08-15 14:28:22 +0000691
Christian Heimes2380ac72008-01-09 00:17:24 +0000692.. method:: PyHKEY.__enter__()
693 PyHKEY.__exit__(\*exc_info)
694
695 The HKEY object implements :meth:`__enter__` and :meth:`__exit__` and thus
696 supports the context protocol for the :keyword:`with` statement::
697
698 with OpenKey(HKEY_LOCAL_MACHINE, "foo") as key:
699 # ... work with key ...
700
701 will automatically close *key* when control leaves the :keyword:`with` block.
702
Christian Heimes2380ac72008-01-09 00:17:24 +0000703