blob: 61207250aebb291df14b18e59bcb9d0e1a02b064 [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
15This module exposes a very low-level interface to the Windows registry; it is
Ezio Melottibc372982010-04-25 17:48:01 +000016expected that in the future a new ``winreg`` module will be created offering a
Georg Brandl116aa622007-08-15 14:28:22 +000017higher-level interface to the registry API.
18
19This module offers the following functions:
20
21
22.. function:: CloseKey(hkey)
23
24 Closes a previously opened registry key. The hkey argument specifies a
25 previously opened key.
26
27 Note that if *hkey* is not closed using this method (or via
28 :meth:`handle.Close`), it is closed when the *hkey* object is destroyed by
29 Python.
30
31
32.. function:: ConnectRegistry(computer_name, key)
33
Ezio Melottibc372982010-04-25 17:48:01 +000034 Establishes a connection to a predefined registry handle on another computer,
35 and returns a :ref:`handle object <handle-object>`.
Georg Brandl116aa622007-08-15 14:28:22 +000036
Ezio Melottibc372982010-04-25 17:48:01 +000037 *computer_name* is the name of the remote computer, of the form
Georg Brandl116aa622007-08-15 14:28:22 +000038 ``r"\\computername"``. If ``None``, the local computer is used.
39
40 *key* is the predefined handle to connect to.
41
Benjamin Petersond23f8222009-04-05 19:13:16 +000042 The return value is the handle of the opened key. If the function fails, a
Ezio Melottibc372982010-04-25 17:48:01 +000043 :exc:`WindowsError` exception is raised.
Georg Brandl116aa622007-08-15 14:28:22 +000044
45
46.. function:: CreateKey(key, sub_key)
47
Ezio Melottibc372982010-04-25 17:48:01 +000048 Creates or opens the specified key, returning a
49 :ref:`handle object <handle-object>`.
Georg Brandl116aa622007-08-15 14:28:22 +000050
Ezio Melottibc372982010-04-25 17:48:01 +000051 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl116aa622007-08-15 14:28:22 +000052 constants.
53
Ezio Melottibc372982010-04-25 17:48:01 +000054 *sub_key* is a string that names the key this method opens or creates.
Georg Brandl116aa622007-08-15 14:28:22 +000055
Ezio Melottibc372982010-04-25 17:48:01 +000056 If *key* is one of the predefined keys, *sub_key* may be ``None``. In that
57 case, the handle returned is the same key handle passed in to the function.
Georg Brandl116aa622007-08-15 14:28:22 +000058
59 If the key already exists, this function opens the existing key.
60
Benjamin Petersond23f8222009-04-05 19:13:16 +000061 The return value is the handle of the opened key. If the function fails, a
Ezio Melottibc372982010-04-25 17:48:01 +000062 :exc:`WindowsError` exception is raised.
Georg Brandl116aa622007-08-15 14:28:22 +000063
64
Brian Curtind8559482010-04-24 17:21:31 +000065.. function:: CreateKeyEx(key, sub_key[, res[, sam]])
Brian Curtin3035c392010-04-21 23:56:21 +000066
Ezio Melottibc372982010-04-25 17:48:01 +000067 Creates or opens the specified key, returning a
68 :ref:`handle object <handle-object>`.
Brian Curtin3035c392010-04-21 23:56:21 +000069
70 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
71 constants.
72
73 *sub_key* is a string that names the key this method opens or creates.
74
75 *res* is a reserved integer, and must be zero. The default is zero.
76
77 *sam* is an integer that specifies an access mask that describes the desired
78 security access for the key. Default is :const:`KEY_ALL_ACCESS`
79
Ezio Melottibc372982010-04-25 17:48:01 +000080 If *key* is one of the predefined keys, *sub_key* may be ``None``. In that
81 case, the handle returned is the same key handle passed in to the function.
Brian Curtin3035c392010-04-21 23:56:21 +000082
83 If the key already exists, this function opens the existing key.
84
85 The return value is the handle of the opened key. If the function fails, a
86 :exc:`WindowsError` exception is raised.
87
Georg Brandl4c25cf32010-04-22 07:00:42 +000088 .. versionadded:: 3.2
Brian Curtin3035c392010-04-21 23:56:21 +000089
90
Georg Brandl116aa622007-08-15 14:28:22 +000091.. function:: DeleteKey(key, sub_key)
92
93 Deletes the specified key.
94
Brian Curtin3035c392010-04-21 23:56:21 +000095 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
Georg Brandl116aa622007-08-15 14:28:22 +000096 constants.
97
Brian Curtin3035c392010-04-21 23:56:21 +000098 *sub_key* is a string that must be a subkey of the key identified by the *key*
99 parameter. This value must not be ``None``, and the key may not have subkeys.
Georg Brandl116aa622007-08-15 14:28:22 +0000100
101 *This method can not delete keys with subkeys.*
102
103 If the method succeeds, the entire key, including all of its values, is removed.
Ezio Melottibc372982010-04-25 17:48:01 +0000104 If the method fails, a :exc:`WindowsError` exception is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000105
106
Brian Curtind8559482010-04-24 17:21:31 +0000107.. function:: DeleteKeyEx(key, sub_key[, sam[, res]])
Brian Curtin3035c392010-04-21 23:56:21 +0000108
109 Deletes the specified key.
110
111 .. note::
112 The :func:`DeleteKeyEx` function is implemented with the RegDeleteKeyEx
113 Windows API function, which is specific to 64-bit versions of Windows.
114 See http://msdn.microsoft.com/en-us/library/ms724847%28VS.85%29.aspx
115
116 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
117 constants.
118
119 *sub_key* is a string that must be a subkey of the key identified by the
120 *key* parameter. This value must not be ``None``, and the key may not have
121 subkeys.
122
123 *res* is a reserved integer, and must be zero. The default is zero.
124
125 *sam* is an integer that specifies an access mask that describes the
126 desired security access for the key. Default is :const:`KEY_WOW64_64KEY`
127
128 *This method can not delete keys with subkeys.*
129
130 If the method succeeds, the entire key, including all of its values, is
131 removed. If the method fails, a :exc:`WindowsError` exception is raised.
132
133 On unsupported Windows versions, :exc:`NotImplementedError` is raised.
134
Georg Brandl4c25cf32010-04-22 07:00:42 +0000135 .. versionadded:: 3.2
Brian Curtin3035c392010-04-21 23:56:21 +0000136
137
Georg Brandl116aa622007-08-15 14:28:22 +0000138.. function:: DeleteValue(key, value)
139
140 Removes a named value from a registry key.
141
Ezio Melottibc372982010-04-25 17:48:01 +0000142 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl116aa622007-08-15 14:28:22 +0000143 constants.
144
145 *value* is a string that identifies the value to remove.
146
147
148.. function:: EnumKey(key, index)
149
150 Enumerates subkeys of an open registry key, returning a string.
151
Ezio Melottibc372982010-04-25 17:48:01 +0000152 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
Georg Brandl116aa622007-08-15 14:28:22 +0000153 constants.
154
Ezio Melottibc372982010-04-25 17:48:01 +0000155 *index* is an integer that identifies the index of the key to retrieve.
Georg Brandl116aa622007-08-15 14:28:22 +0000156
Ezio Melottibc372982010-04-25 17:48:01 +0000157 The function retrieves the name of one subkey each time it is called. It is
158 typically called repeatedly until a :exc:`WindowsError` exception is
Georg Brandl116aa622007-08-15 14:28:22 +0000159 raised, indicating, no more values are available.
160
161
162.. function:: EnumValue(key, index)
163
164 Enumerates values of an open registry key, returning a tuple.
165
Ezio Melottibc372982010-04-25 17:48:01 +0000166 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
Georg Brandl116aa622007-08-15 14:28:22 +0000167 constants.
168
Ezio Melottibc372982010-04-25 17:48:01 +0000169 *index* is an integer that identifies the index of the value to retrieve.
Georg Brandl116aa622007-08-15 14:28:22 +0000170
Ezio Melottibc372982010-04-25 17:48:01 +0000171 The function retrieves the name of one subkey each time it is called. It is
172 typically called repeatedly, until a :exc:`WindowsError` exception is
173 raised, indicating no more values.
Georg Brandl116aa622007-08-15 14:28:22 +0000174
175 The result is a tuple of 3 items:
176
177 +-------+--------------------------------------------+
178 | Index | Meaning |
179 +=======+============================================+
180 | ``0`` | A string that identifies the value name |
181 +-------+--------------------------------------------+
182 | ``1`` | An object that holds the value data, and |
183 | | whose type depends on the underlying |
184 | | registry type |
185 +-------+--------------------------------------------+
186 | ``2`` | An integer that identifies the type of the |
187 | | value data |
188 +-------+--------------------------------------------+
189
190
Ezio Melotti985e24d2009-09-13 07:54:02 +0000191.. function:: ExpandEnvironmentStrings(str)
Christian Heimes2380ac72008-01-09 00:17:24 +0000192
Ezio Melotti985e24d2009-09-13 07:54:02 +0000193 Expands environment strings %NAME% in unicode string like :const:`REG_EXPAND_SZ`::
Christian Heimes2380ac72008-01-09 00:17:24 +0000194
Ezio Melotti985e24d2009-09-13 07:54:02 +0000195 >>> ExpandEnvironmentStrings('%windir%')
196 'C:\\Windows'
Christian Heimes2380ac72008-01-09 00:17:24 +0000197
Christian Heimes2380ac72008-01-09 00:17:24 +0000198
Georg Brandl116aa622007-08-15 14:28:22 +0000199.. function:: FlushKey(key)
200
201 Writes all the attributes of a key to the registry.
202
Ezio Melottibc372982010-04-25 17:48:01 +0000203 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl116aa622007-08-15 14:28:22 +0000204 constants.
205
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000206 It is not necessary to call :func:`FlushKey` to change a key. Registry changes are
Ezio Melottibc372982010-04-25 17:48:01 +0000207 flushed to disk by the registry using its lazy flusher. Registry changes are
208 also flushed to disk at system shutdown. Unlike :func:`CloseKey`, the
209 :func:`FlushKey` method returns only when all the data has been written to the
Georg Brandl116aa622007-08-15 14:28:22 +0000210 registry. An application should only call :func:`FlushKey` if it requires
Ezio Melottibc372982010-04-25 17:48:01 +0000211 absolute certainty that registry changes are on disk.
Georg Brandl116aa622007-08-15 14:28:22 +0000212
213 .. note::
214
Ezio Melottibc372982010-04-25 17:48:01 +0000215 If you don't know whether a :func:`FlushKey` call is required, it probably
Georg Brandl116aa622007-08-15 14:28:22 +0000216 isn't.
217
218
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000219.. function:: LoadKey(key, sub_key, file_name)
Georg Brandl116aa622007-08-15 14:28:22 +0000220
Ezio Melottibc372982010-04-25 17:48:01 +0000221 Creates a subkey under the specified key and stores registration information
Georg Brandl116aa622007-08-15 14:28:22 +0000222 from a specified file into that subkey.
223
224 *key* is an already open key, or any of the predefined :const:`HKEY_\*`
225 constants.
226
227 *sub_key* is a string that identifies the sub_key to load.
228
229 *file_name* is the name of the file to load registry data from. This file must
230 have been created with the :func:`SaveKey` function. Under the file allocation
231 table (FAT) file system, the filename may not have an extension.
232
233 A call to LoadKey() fails if the calling process does not have the
234 :const:`SE_RESTORE_PRIVILEGE` privilege. Note that privileges are different than
Ezio Melottibc372982010-04-25 17:48:01 +0000235 permissions -- see the Win32 documentation for more details.
Georg Brandl116aa622007-08-15 14:28:22 +0000236
Ezio Melottibc372982010-04-25 17:48:01 +0000237 If *key* is a handle returned by :func:`ConnectRegistry`, then the path
238 specified in *fileName* is relative to the remote computer.
Georg Brandl116aa622007-08-15 14:28:22 +0000239
Ezio Melottibc372982010-04-25 17:48:01 +0000240 The Win32 documentation implies *key* must be in the :const:`HKEY_USER` or
Georg Brandl116aa622007-08-15 14:28:22 +0000241 :const:`HKEY_LOCAL_MACHINE` tree. This may or may not be true.
242
243
Brian Curtind8559482010-04-24 17:21:31 +0000244.. function:: OpenKey(key, sub_key[, res[, sam]])
Georg Brandl116aa622007-08-15 14:28:22 +0000245
Ezio Melottibc372982010-04-25 17:48:01 +0000246 Opens the specified key, returning a :ref:`handle object <handle-object>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000247
248 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
249 constants.
250
251 *sub_key* is a string that identifies the sub_key to open.
252
253 *res* is a reserved integer, and must be zero. The default is zero.
254
Ezio Melottibc372982010-04-25 17:48:01 +0000255 *sam* is an integer that specifies an access mask that describes the desired
Georg Brandl7f01a132009-09-16 15:58:14 +0000256 security access for the key. Default is :const:`KEY_READ`.
Georg Brandl116aa622007-08-15 14:28:22 +0000257
258 The result is a new handle to the specified key.
259
Benjamin Petersond23f8222009-04-05 19:13:16 +0000260 If the function fails, :exc:`WindowsError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000261
262
263.. function:: OpenKeyEx()
264
Ezio Melottibc372982010-04-25 17:48:01 +0000265 The functionality of :func:`OpenKeyEx` is provided via :func:`OpenKey`,
266 by the use of default arguments.
Georg Brandl116aa622007-08-15 14:28:22 +0000267
268
269.. function:: QueryInfoKey(key)
270
271 Returns information about a key, as a tuple.
272
Ezio Melottibc372982010-04-25 17:48:01 +0000273 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl116aa622007-08-15 14:28:22 +0000274 constants.
275
276 The result is a tuple of 3 items:
277
278 +-------+---------------------------------------------+
279 | Index | Meaning |
280 +=======+=============================================+
281 | ``0`` | An integer giving the number of sub keys |
282 | | this key has. |
283 +-------+---------------------------------------------+
284 | ``1`` | An integer giving the number of values this |
285 | | key has. |
286 +-------+---------------------------------------------+
Georg Brandlba956ae2007-11-29 17:24:34 +0000287 | ``2`` | An integer giving when the key was last |
Georg Brandl116aa622007-08-15 14:28:22 +0000288 | | modified (if available) as 100's of |
289 | | nanoseconds since Jan 1, 1600. |
290 +-------+---------------------------------------------+
291
292
293.. function:: QueryValue(key, sub_key)
294
Ezio Melottibc372982010-04-25 17:48:01 +0000295 Retrieves the unnamed value for a key, as a string.
Georg Brandl116aa622007-08-15 14:28:22 +0000296
Ezio Melottibc372982010-04-25 17:48:01 +0000297 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl116aa622007-08-15 14:28:22 +0000298 constants.
299
Ezio Melottibc372982010-04-25 17:48:01 +0000300 *sub_key* is a string that holds the name of the subkey with which the value is
301 associated. If this parameter is ``None`` or empty, the function retrieves the
302 value set by the :func:`SetValue` method for the key identified by *key*.
Georg Brandl116aa622007-08-15 14:28:22 +0000303
Benjamin Petersond23f8222009-04-05 19:13:16 +0000304 Values in the registry have name, type, and data components. This method
Georg Brandl116aa622007-08-15 14:28:22 +0000305 retrieves the data for a key's first value that has a NULL name. But the
Benjamin Petersond23f8222009-04-05 19:13:16 +0000306 underlying API call doesn't return the type, so always use
307 :func:`QueryValueEx` if possible.
Georg Brandl116aa622007-08-15 14:28:22 +0000308
309
310.. function:: QueryValueEx(key, value_name)
311
Ezio Melottibc372982010-04-25 17:48:01 +0000312 Retrieves the type and data for a specified value name associated with
313 an open registry key.
Georg Brandl116aa622007-08-15 14:28:22 +0000314
Ezio Melottibc372982010-04-25 17:48:01 +0000315 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl116aa622007-08-15 14:28:22 +0000316 constants.
317
318 *value_name* is a string indicating the value to query.
319
320 The result is a tuple of 2 items:
321
322 +-------+-----------------------------------------+
323 | Index | Meaning |
324 +=======+=========================================+
325 | ``0`` | The value of the registry item. |
326 +-------+-----------------------------------------+
327 | ``1`` | An integer giving the registry type for |
328 | | this value. |
329 +-------+-----------------------------------------+
330
331
332.. function:: SaveKey(key, file_name)
333
334 Saves the specified key, and all its subkeys to the specified file.
335
Ezio Melottibc372982010-04-25 17:48:01 +0000336 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl116aa622007-08-15 14:28:22 +0000337 constants.
338
339 *file_name* is the name of the file to save registry data to. This file cannot
340 already exist. If this filename includes an extension, it cannot be used on file
341 allocation table (FAT) file systems by the :meth:`LoadKey`, :meth:`ReplaceKey`
Ezio Melottibc372982010-04-25 17:48:01 +0000342 or :meth:`RestoreKey` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000343
Ezio Melottibc372982010-04-25 17:48:01 +0000344 If *key* represents a key on a remote computer, the path described by
Georg Brandl116aa622007-08-15 14:28:22 +0000345 *file_name* is relative to the remote computer. The caller of this method must
Ezio Melottibc372982010-04-25 17:48:01 +0000346 possess the :const:`SeBackupPrivilege` security privilege. Note that
347 privileges are different than permissions -- see the Win32 documentation for
Georg Brandl116aa622007-08-15 14:28:22 +0000348 more details.
349
350 This function passes NULL for *security_attributes* to the API.
351
352
353.. function:: SetValue(key, sub_key, type, value)
354
355 Associates a value with a specified key.
356
Ezio Melottibc372982010-04-25 17:48:01 +0000357 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl116aa622007-08-15 14:28:22 +0000358 constants.
359
Ezio Melottibc372982010-04-25 17:48:01 +0000360 *sub_key* is a string that names the subkey with which the value is associated.
Georg Brandl116aa622007-08-15 14:28:22 +0000361
362 *type* is an integer that specifies the type of the data. Currently this must be
363 :const:`REG_SZ`, meaning only strings are supported. Use the :func:`SetValueEx`
364 function for support for other data types.
365
366 *value* is a string that specifies the new value.
367
368 If the key specified by the *sub_key* parameter does not exist, the SetValue
369 function creates it.
370
371 Value lengths are limited by available memory. Long values (more than 2048
372 bytes) should be stored as files with the filenames stored in the configuration
373 registry. This helps the registry perform efficiently.
374
Ezio Melottibc372982010-04-25 17:48:01 +0000375 The key identified by the *key* parameter must have been opened with
Georg Brandl116aa622007-08-15 14:28:22 +0000376 :const:`KEY_SET_VALUE` access.
377
378
379.. function:: SetValueEx(key, value_name, reserved, type, value)
380
381 Stores data in the value field of an open registry key.
382
Ezio Melottibc372982010-04-25 17:48:01 +0000383 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl116aa622007-08-15 14:28:22 +0000384 constants.
385
Ezio Melottibc372982010-04-25 17:48:01 +0000386 *value_name* is a string that names the subkey with which the value is
Georg Brandl116aa622007-08-15 14:28:22 +0000387 associated.
388
Ezio Melottibc372982010-04-25 17:48:01 +0000389 *type* is an integer that specifies the type of the data. This should be one
Georg Brandl116aa622007-08-15 14:28:22 +0000390 of the following constants defined in this module:
391
392 +----------------------------------+---------------------------------------------+
393 | Constant | Meaning |
394 +==================================+=============================================+
395 | :const:`REG_BINARY` | Binary data in any form. |
396 +----------------------------------+---------------------------------------------+
397 | :const:`REG_DWORD` | A 32-bit number. |
398 +----------------------------------+---------------------------------------------+
399 | :const:`REG_DWORD_LITTLE_ENDIAN` | A 32-bit number in little-endian format. |
400 +----------------------------------+---------------------------------------------+
401 | :const:`REG_DWORD_BIG_ENDIAN` | A 32-bit number in big-endian format. |
402 +----------------------------------+---------------------------------------------+
403 | :const:`REG_EXPAND_SZ` | Null-terminated string containing |
404 | | references to environment variables |
405 | | (``%PATH%``). |
406 +----------------------------------+---------------------------------------------+
407 | :const:`REG_LINK` | A Unicode symbolic link. |
408 +----------------------------------+---------------------------------------------+
409 | :const:`REG_MULTI_SZ` | A sequence of null-terminated strings, |
410 | | terminated by two null characters. (Python |
Ezio Melottibc372982010-04-25 17:48:01 +0000411 | | handles this termination automatically.) |
Georg Brandl116aa622007-08-15 14:28:22 +0000412 +----------------------------------+---------------------------------------------+
413 | :const:`REG_NONE` | No defined value type. |
414 +----------------------------------+---------------------------------------------+
415 | :const:`REG_RESOURCE_LIST` | A device-driver resource list. |
416 +----------------------------------+---------------------------------------------+
417 | :const:`REG_SZ` | A null-terminated string. |
418 +----------------------------------+---------------------------------------------+
419
Ezio Melottibc372982010-04-25 17:48:01 +0000420 *reserved* can be anything -- zero is always passed to the API.
Georg Brandl116aa622007-08-15 14:28:22 +0000421
422 *value* is a string that specifies the new value.
423
424 This method can also set additional value and type information for the specified
425 key. The key identified by the key parameter must have been opened with
426 :const:`KEY_SET_VALUE` access.
427
Ezio Melottibc372982010-04-25 17:48:01 +0000428 To open the key, use the :func:`CreateKey` or :func:`OpenKey` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000429
430 Value lengths are limited by available memory. Long values (more than 2048
431 bytes) should be stored as files with the filenames stored in the configuration
432 registry. This helps the registry perform efficiently.
433
434
Brian Curtin3035c392010-04-21 23:56:21 +0000435.. function:: DisableReflectionKey(key)
436
437 Disables registry reflection for 32-bit processes running on a 64-bit
438 Operating System.
439
440 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
441 constants.
442
443 Will generally raise :exc:`NotImplemented` if executed on a 32-bit
444 Operating System.
445
446 If the key is not on the reflection list, the function succeeds but has no
447 effect. Disabling reflection for a key does not affect reflection of any
448 subkeys.
449
450
451.. function:: EnableReflectionKey(key)
452
453 Restores registry reflection for the specified disabled key.
454
455 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
456 constants.
457
458 Will generally raise :exc:`NotImplemented` if executed on a 32-bit
459 Operating System.
460
461 Restoring reflection for a key does not affect reflection of any subkeys.
462
463
464.. function:: QueryReflectionKey(key)
465
466 Determines the reflection state for the specified key.
467
468 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
469 constants.
470
471 Returns ``True`` if reflection is disabled.
472
473 Will generally raise :exc:`NotImplemented` if executed on a 32-bit
474 Operating System.
475
476
Georg Brandl116aa622007-08-15 14:28:22 +0000477.. _handle-object:
478
479Registry Handle Objects
480-----------------------
481
482This object wraps a Windows HKEY object, automatically closing it when the
483object is destroyed. To guarantee cleanup, you can call either the
Ezio Melottibc372982010-04-25 17:48:01 +0000484:meth:`Close` method on the object, or the :func:`CloseKey` function.
Georg Brandl116aa622007-08-15 14:28:22 +0000485
486All registry functions in this module return one of these objects.
487
Ezio Melottibc372982010-04-25 17:48:01 +0000488All registry functions in this module which accept a handle object also accept
489an integer, however, use of the handle object is encouraged.
Georg Brandl116aa622007-08-15 14:28:22 +0000490
Ezio Melottibc372982010-04-25 17:48:01 +0000491Handle objects provide semantics for :meth:`__bool__` -- thus ::
Georg Brandl116aa622007-08-15 14:28:22 +0000492
493 if handle:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000494 print("Yes")
Georg Brandl116aa622007-08-15 14:28:22 +0000495
496will print ``Yes`` if the handle is currently valid (has not been closed or
497detached).
498
499The object also support comparison semantics, so handle objects will compare
500true if they both reference the same underlying Windows handle value.
501
Georg Brandl22b34312009-07-26 14:54:51 +0000502Handle objects can be converted to an integer (e.g., using the built-in
Georg Brandl116aa622007-08-15 14:28:22 +0000503:func:`int` function), in which case the underlying Windows handle value is
Ezio Melottibc372982010-04-25 17:48:01 +0000504returned. You can also use the :meth:`Detach` method to return the integer
Georg Brandl116aa622007-08-15 14:28:22 +0000505handle, and also disconnect the Windows handle from the handle object.
506
507
508.. method:: PyHKEY.Close()
509
510 Closes the underlying Windows handle.
511
512 If the handle is already closed, no error is raised.
513
514
515.. method:: PyHKEY.Detach()
516
517 Detaches the Windows handle from the handle object.
518
Georg Brandl5c106642007-11-29 17:41:05 +0000519 The result is an integer that holds the value of the handle before it is
520 detached. If the handle is already detached or closed, this will return
521 zero.
Georg Brandl116aa622007-08-15 14:28:22 +0000522
523 After calling this function, the handle is effectively invalidated, but the
Ezio Melottibc372982010-04-25 17:48:01 +0000524 handle is not closed. You would call this function when you need the
525 underlying Win32 handle to exist beyond the lifetime of the handle object.
Georg Brandl116aa622007-08-15 14:28:22 +0000526
Christian Heimes2380ac72008-01-09 00:17:24 +0000527.. method:: PyHKEY.__enter__()
528 PyHKEY.__exit__(\*exc_info)
529
530 The HKEY object implements :meth:`__enter__` and :meth:`__exit__` and thus
531 supports the context protocol for the :keyword:`with` statement::
532
533 with OpenKey(HKEY_LOCAL_MACHINE, "foo") as key:
534 # ... work with key ...
535
536 will automatically close *key* when control leaves the :keyword:`with` block.
537
Christian Heimes2380ac72008-01-09 00:17:24 +0000538