blob: 6fa205319bf0fa667aaca39a32e85ef990fe5df8 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001:mod:`_winreg` -- Windows registry access
2=========================================
3
4.. module:: _winreg
5 :platform: Windows
6 :synopsis: Routines and objects for manipulating the Windows registry.
7.. sectionauthor:: Mark Hammond <MarkH@ActiveState.com>
8
Georg Brandlecd0ad32008-05-25 07:46:33 +00009.. note::
10 The :mod:`_winreg` module has been renamed to :mod:`winreg` in Python 3.0.
11 The :term:`2to3` tool will automatically adapt imports when converting your
12 sources to 3.0.
13
Georg Brandl8ec7f652007-08-15 14:28:01 +000014
15.. versionadded:: 2.0
16
17These functions expose the Windows registry API to Python. Instead of using an
18integer as the registry handle, a handle object is used to ensure that the
19handles are closed correctly, even if the programmer neglects to explicitly
20close them.
21
22This module exposes a very low-level interface to the Windows registry; it is
Ezio Melotti01fa86a2010-04-05 08:02:54 +000023expected that in the future a new ``winreg`` module will be created offering a
Georg Brandl8ec7f652007-08-15 14:28:01 +000024higher-level interface to the registry API.
25
26This module offers the following functions:
27
28
29.. function:: CloseKey(hkey)
30
31 Closes a previously opened registry key. The hkey argument specifies a
32 previously opened key.
33
34 Note that if *hkey* is not closed using this method (or via
35 :meth:`handle.Close`), it is closed when the *hkey* object is destroyed by
36 Python.
37
38
39.. function:: ConnectRegistry(computer_name, key)
40
Ezio Melotti01fa86a2010-04-05 08:02:54 +000041 Establishes a connection to a predefined registry handle on another computer,
42 and returns a :ref:`handle object <handle-object>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000043
Ezio Melotti01fa86a2010-04-05 08:02:54 +000044 *computer_name* is the name of the remote computer, of the form
Georg Brandl8ec7f652007-08-15 14:28:01 +000045 ``r"\\computername"``. If ``None``, the local computer is used.
46
47 *key* is the predefined handle to connect to.
48
Georg Brandlb945bbf2009-03-31 16:31:11 +000049 The return value is the handle of the opened key. If the function fails, a
Ezio Melotti01fa86a2010-04-05 08:02:54 +000050 :exc:`WindowsError` exception is raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +000051
52
53.. function:: CreateKey(key, sub_key)
54
Ezio Melotti01fa86a2010-04-05 08:02:54 +000055 Creates or opens the specified key, returning a
56 :ref:`handle object <handle-object>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000057
Ezio Melotti01fa86a2010-04-05 08:02:54 +000058 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl8ec7f652007-08-15 14:28:01 +000059 constants.
60
Ezio Melotti01fa86a2010-04-05 08:02:54 +000061 *sub_key* is a string that names the key this method opens or creates.
Georg Brandl8ec7f652007-08-15 14:28:01 +000062
Ezio Melotti01fa86a2010-04-05 08:02:54 +000063 If *key* is one of the predefined keys, *sub_key* may be ``None``. In that
64 case, the handle returned is the same key handle passed in to the function.
Georg Brandl8ec7f652007-08-15 14:28:01 +000065
66 If the key already exists, this function opens the existing key.
67
Georg Brandlb945bbf2009-03-31 16:31:11 +000068 The return value is the handle of the opened key. If the function fails, a
Ezio Melotti01fa86a2010-04-05 08:02:54 +000069 :exc:`WindowsError` exception is raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +000070
71
Ezio Melotti01fa86a2010-04-05 08:02:54 +000072.. function:: CreateKeyEx(key, sub_key, res=0, sam=KEY_ALL_ACCESS)
Brian Curtine33fa882010-04-02 21:18:14 +000073
Ezio Melotti01fa86a2010-04-05 08:02:54 +000074 Creates or opens the specified key, returning a
75 :ref:`handle object <handle-object>`.
Brian Curtine33fa882010-04-02 21:18:14 +000076
77 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
78 constants.
79
80 *sub_key* is a string that names the key this method opens or creates.
81
82 *res* is a reserved integer, and must be zero. The default is zero.
83
84 *sam* is an integer that specifies an access mask that describes the desired
85 security access for the key. Default is :const:`KEY_ALL_ACCESS`
86
Ezio Melotti01fa86a2010-04-05 08:02:54 +000087 If *key* is one of the predefined keys, *sub_key* may be ``None``. In that
88 case, the handle returned is the same key handle passed in to the function.
Brian Curtine33fa882010-04-02 21:18:14 +000089
90 If the key already exists, this function opens the existing key.
91
92 The return value is the handle of the opened key. If the function fails, a
93 :exc:`WindowsError` exception is raised.
94
95.. versionadded:: 2.7
96
97
Georg Brandl8ec7f652007-08-15 14:28:01 +000098.. function:: DeleteKey(key, sub_key)
99
100 Deletes the specified key.
101
Brian Curtine33fa882010-04-02 21:18:14 +0000102 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000103 constants.
104
Brian Curtine33fa882010-04-02 21:18:14 +0000105 *sub_key* is a string that must be a subkey of the key identified by the *key*
106 parameter. This value must not be ``None``, and the key may not have subkeys.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000107
108 *This method can not delete keys with subkeys.*
109
110 If the method succeeds, the entire key, including all of its values, is removed.
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000111 If the method fails, a :exc:`WindowsError` exception is raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000112
113
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000114.. function:: DeleteKeyEx(key, sub_key, sam=KEY_WOW64_64KEY, res=0)
Brian Curtine33fa882010-04-02 21:18:14 +0000115
116 Deletes the specified key.
117
118 .. note::
119 The :func:`DeleteKeyEx` function is implemented with the RegDeleteKeyEx
120 Windows API function, which is specific to 64-bit versions of Windows.
121 See http://msdn.microsoft.com/en-us/library/ms724847%28VS.85%29.aspx
122
123 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
124 constants.
125
126 *sub_key* is a string that must be a subkey of the key identified by the
127 *key* parameter. This value must not be ``None``, and the key may not have
128 subkeys.
129
130 *res* is a reserved integer, and must be zero. The default is zero.
131
132 *sam* is an integer that specifies an access mask that describes the
133 desired security access for the key. Default is :const:`KEY_WOW64_64KEY`
134
135 *This method can not delete keys with subkeys.*
136
137 If the method succeeds, the entire key, including all of its values, is
138 removed. If the method fails, a :exc:`WindowsError` exception is raised.
139
140 On unsupported Windows versions, :exc:`NotImplementedError` is raised.
141
142.. versionadded:: 2.7
143
144
Georg Brandl8ec7f652007-08-15 14:28:01 +0000145.. function:: DeleteValue(key, value)
146
147 Removes a named value from a registry key.
148
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000149 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000150 constants.
151
152 *value* is a string that identifies the value to remove.
153
154
155.. function:: EnumKey(key, index)
156
157 Enumerates subkeys of an open registry key, returning a string.
158
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000159 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000160 constants.
161
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000162 *index* is an integer that identifies the index of the key to retrieve.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000163
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000164 The function retrieves the name of one subkey each time it is called. It is
165 typically called repeatedly until a :exc:`WindowsError` exception is
Georg Brandl8ec7f652007-08-15 14:28:01 +0000166 raised, indicating, no more values are available.
167
168
169.. function:: EnumValue(key, index)
170
171 Enumerates values of an open registry key, returning a tuple.
172
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000173 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000174 constants.
175
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000176 *index* is an integer that identifies the index of the value to retrieve.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000177
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000178 The function retrieves the name of one subkey each time it is called. It is
179 typically called repeatedly, until a :exc:`WindowsError` exception is
180 raised, indicating no more values.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000181
182 The result is a tuple of 3 items:
183
184 +-------+--------------------------------------------+
185 | Index | Meaning |
186 +=======+============================================+
187 | ``0`` | A string that identifies the value name |
188 +-------+--------------------------------------------+
189 | ``1`` | An object that holds the value data, and |
190 | | whose type depends on the underlying |
191 | | registry type |
192 +-------+--------------------------------------------+
193 | ``2`` | An integer that identifies the type of the |
194 | | value data |
195 +-------+--------------------------------------------+
196
197
Christian Heimesb39a7562008-01-08 15:46:10 +0000198.. function:: ExpandEnvironmentStrings(unicode)
199
200 Expands environment strings %NAME% in unicode string like const:`REG_EXPAND_SZ`::
Georg Brandl502d6312008-01-08 16:18:26 +0000201
202 >>> ExpandEnvironmentStrings(u"%windir%")
203 u"C:\\Windows"
204
205 .. versionadded:: 2.6
Christian Heimesb39a7562008-01-08 15:46:10 +0000206
207
Georg Brandl8ec7f652007-08-15 14:28:01 +0000208.. function:: FlushKey(key)
209
210 Writes all the attributes of a key to the registry.
211
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000212 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000213 constants.
214
Georg Brandl51174092008-05-09 06:10:43 +0000215 It is not necessary to call :func:`FlushKey` to change a key. Registry changes are
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000216 flushed to disk by the registry using its lazy flusher. Registry changes are
217 also flushed to disk at system shutdown. Unlike :func:`CloseKey`, the
218 :func:`FlushKey` method returns only when all the data has been written to the
Georg Brandl8ec7f652007-08-15 14:28:01 +0000219 registry. An application should only call :func:`FlushKey` if it requires
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000220 absolute certainty that registry changes are on disk.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000221
222 .. note::
223
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000224 If you don't know whether a :func:`FlushKey` call is required, it probably
Georg Brandl8ec7f652007-08-15 14:28:01 +0000225 isn't.
226
227
Georg Brandl51174092008-05-09 06:10:43 +0000228.. function:: LoadKey(key, sub_key, file_name)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000229
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000230 Creates a subkey under the specified key and stores registration information
Georg Brandl8ec7f652007-08-15 14:28:01 +0000231 from a specified file into that subkey.
232
233 *key* is an already open key, or any of the predefined :const:`HKEY_\*`
234 constants.
235
236 *sub_key* is a string that identifies the sub_key to load.
237
238 *file_name* is the name of the file to load registry data from. This file must
239 have been created with the :func:`SaveKey` function. Under the file allocation
240 table (FAT) file system, the filename may not have an extension.
241
242 A call to LoadKey() fails if the calling process does not have the
243 :const:`SE_RESTORE_PRIVILEGE` privilege. Note that privileges are different than
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000244 permissions -- see the Win32 documentation for more details.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000245
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000246 If *key* is a handle returned by :func:`ConnectRegistry`, then the path
247 specified in *fileName* is relative to the remote computer.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000248
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000249 The Win32 documentation implies *key* must be in the :const:`HKEY_USER` or
Georg Brandl8ec7f652007-08-15 14:28:01 +0000250 :const:`HKEY_LOCAL_MACHINE` tree. This may or may not be true.
251
252
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000253.. function:: OpenKey(key, sub_key, res=0, sam=KEY_READ)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000254
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000255 Opens the specified key, returning a :ref:`handle object <handle-object>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000256
257 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
258 constants.
259
260 *sub_key* is a string that identifies the sub_key to open.
261
262 *res* is a reserved integer, and must be zero. The default is zero.
263
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000264 *sam* is an integer that specifies an access mask that describes the desired
Georg Brandl8ec7f652007-08-15 14:28:01 +0000265 security access for the key. Default is :const:`KEY_READ`
266
267 The result is a new handle to the specified key.
268
Georg Brandlb945bbf2009-03-31 16:31:11 +0000269 If the function fails, :exc:`WindowsError` is raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000270
271
272.. function:: OpenKeyEx()
273
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000274 The functionality of :func:`OpenKeyEx` is provided via :func:`OpenKey`,
275 by the use of default arguments.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000276
277
278.. function:: QueryInfoKey(key)
279
280 Returns information about a key, as a tuple.
281
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000282 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000283 constants.
284
285 The result is a tuple of 3 items:
286
287 +-------+---------------------------------------------+
288 | Index | Meaning |
289 +=======+=============================================+
290 | ``0`` | An integer giving the number of sub keys |
291 | | this key has. |
292 +-------+---------------------------------------------+
293 | ``1`` | An integer giving the number of values this |
294 | | key has. |
295 +-------+---------------------------------------------+
296 | ``2`` | A long integer giving when the key was last |
297 | | modified (if available) as 100's of |
298 | | nanoseconds since Jan 1, 1600. |
299 +-------+---------------------------------------------+
300
301
302.. function:: QueryValue(key, sub_key)
303
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000304 Retrieves the unnamed value for a key, as a string.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000305
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000306 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000307 constants.
308
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000309 *sub_key* is a string that holds the name of the subkey with which the value is
310 associated. If this parameter is ``None`` or empty, the function retrieves the
311 value set by the :func:`SetValue` method for the key identified by *key*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000312
Georg Brandl75f11072009-04-05 10:32:26 +0000313 Values in the registry have name, type, and data components. This method
Georg Brandl8ec7f652007-08-15 14:28:01 +0000314 retrieves the data for a key's first value that has a NULL name. But the
Georg Brandl75f11072009-04-05 10:32:26 +0000315 underlying API call doesn't return the type, so always use
316 :func:`QueryValueEx` if possible.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000317
318
319.. function:: QueryValueEx(key, value_name)
320
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000321 Retrieves the type and data for a specified value name associated with
322 an open registry key.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000323
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000324 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000325 constants.
326
327 *value_name* is a string indicating the value to query.
328
329 The result is a tuple of 2 items:
330
331 +-------+-----------------------------------------+
332 | Index | Meaning |
333 +=======+=========================================+
334 | ``0`` | The value of the registry item. |
335 +-------+-----------------------------------------+
336 | ``1`` | An integer giving the registry type for |
337 | | this value. |
338 +-------+-----------------------------------------+
339
340
341.. function:: SaveKey(key, file_name)
342
343 Saves the specified key, and all its subkeys to the specified file.
344
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000345 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000346 constants.
347
348 *file_name* is the name of the file to save registry data to. This file cannot
349 already exist. If this filename includes an extension, it cannot be used on file
350 allocation table (FAT) file systems by the :meth:`LoadKey`, :meth:`ReplaceKey`
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000351 or :meth:`RestoreKey` methods.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000352
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000353 If *key* represents a key on a remote computer, the path described by
Georg Brandl8ec7f652007-08-15 14:28:01 +0000354 *file_name* is relative to the remote computer. The caller of this method must
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000355 possess the :const:`SeBackupPrivilege` security privilege. Note that
356 privileges are different than permissions -- see the Win32 documentation for
Georg Brandl8ec7f652007-08-15 14:28:01 +0000357 more details.
358
359 This function passes NULL for *security_attributes* to the API.
360
361
362.. function:: SetValue(key, sub_key, type, value)
363
364 Associates a value with a specified key.
365
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000366 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000367 constants.
368
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000369 *sub_key* is a string that names the subkey with which the value is associated.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000370
371 *type* is an integer that specifies the type of the data. Currently this must be
372 :const:`REG_SZ`, meaning only strings are supported. Use the :func:`SetValueEx`
373 function for support for other data types.
374
375 *value* is a string that specifies the new value.
376
377 If the key specified by the *sub_key* parameter does not exist, the SetValue
378 function creates it.
379
380 Value lengths are limited by available memory. Long values (more than 2048
381 bytes) should be stored as files with the filenames stored in the configuration
382 registry. This helps the registry perform efficiently.
383
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000384 The key identified by the *key* parameter must have been opened with
Georg Brandl8ec7f652007-08-15 14:28:01 +0000385 :const:`KEY_SET_VALUE` access.
386
387
388.. function:: SetValueEx(key, value_name, reserved, type, value)
389
390 Stores data in the value field of an open registry key.
391
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000392 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000393 constants.
394
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000395 *value_name* is a string that names the subkey with which the value is
Georg Brandl8ec7f652007-08-15 14:28:01 +0000396 associated.
397
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000398 *type* is an integer that specifies the type of the data. This should be one
Georg Brandl8ec7f652007-08-15 14:28:01 +0000399 of the following constants defined in this module:
400
401 +----------------------------------+---------------------------------------------+
402 | Constant | Meaning |
403 +==================================+=============================================+
404 | :const:`REG_BINARY` | Binary data in any form. |
405 +----------------------------------+---------------------------------------------+
406 | :const:`REG_DWORD` | A 32-bit number. |
407 +----------------------------------+---------------------------------------------+
408 | :const:`REG_DWORD_LITTLE_ENDIAN` | A 32-bit number in little-endian format. |
409 +----------------------------------+---------------------------------------------+
410 | :const:`REG_DWORD_BIG_ENDIAN` | A 32-bit number in big-endian format. |
411 +----------------------------------+---------------------------------------------+
412 | :const:`REG_EXPAND_SZ` | Null-terminated string containing |
413 | | references to environment variables |
414 | | (``%PATH%``). |
415 +----------------------------------+---------------------------------------------+
416 | :const:`REG_LINK` | A Unicode symbolic link. |
417 +----------------------------------+---------------------------------------------+
418 | :const:`REG_MULTI_SZ` | A sequence of null-terminated strings, |
419 | | terminated by two null characters. (Python |
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000420 | | handles this termination automatically.) |
Georg Brandl8ec7f652007-08-15 14:28:01 +0000421 +----------------------------------+---------------------------------------------+
422 | :const:`REG_NONE` | No defined value type. |
423 +----------------------------------+---------------------------------------------+
424 | :const:`REG_RESOURCE_LIST` | A device-driver resource list. |
425 +----------------------------------+---------------------------------------------+
426 | :const:`REG_SZ` | A null-terminated string. |
427 +----------------------------------+---------------------------------------------+
428
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000429 *reserved* can be anything -- zero is always passed to the API.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000430
431 *value* is a string that specifies the new value.
432
433 This method can also set additional value and type information for the specified
434 key. The key identified by the key parameter must have been opened with
435 :const:`KEY_SET_VALUE` access.
436
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000437 To open the key, use the :func:`CreateKey` or :func:`OpenKey` methods.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000438
439 Value lengths are limited by available memory. Long values (more than 2048
440 bytes) should be stored as files with the filenames stored in the configuration
441 registry. This helps the registry perform efficiently.
442
443
Brian Curtine33fa882010-04-02 21:18:14 +0000444.. function:: DisableReflectionKey(key)
445
446 Disables registry reflection for 32-bit processes running on a 64-bit
447 Operating System.
448
449 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
450 constants.
451
452 Will generally raise :exc:`NotImplemented` if executed on a 32-bit
453 Operating System.
454
455 If the key is not on the reflection list, the function succeeds but has no
456 effect. Disabling reflection for a key does not affect reflection of any
457 subkeys.
458
459
460.. function:: EnableReflectionKey(key)
461
462 Restores registry reflection for the specified disabled key.
463
464 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
465 constants.
466
467 Will generally raise :exc:`NotImplemented` if executed on a 32-bit
468 Operating System.
469
470 Restoring reflection for a key does not affect reflection of any subkeys.
471
472
473.. function:: QueryReflectionKey(key)
474
475 Determines the reflection state for the specified key.
476
477 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
478 constants.
479
480 Returns ``True`` if reflection is disabled.
481
482 Will generally raise :exc:`NotImplemented` if executed on a 32-bit
483 Operating System.
484
485
Georg Brandl8ec7f652007-08-15 14:28:01 +0000486.. _handle-object:
487
488Registry Handle Objects
489-----------------------
490
491This object wraps a Windows HKEY object, automatically closing it when the
492object is destroyed. To guarantee cleanup, you can call either the
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000493:meth:`Close` method on the object, or the :func:`CloseKey` function.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000494
495All registry functions in this module return one of these objects.
496
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000497All registry functions in this module which accept a handle object also accept
498an integer, however, use of the handle object is encouraged.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000499
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000500Handle objects provide semantics for :meth:`__nonzero__` -- thus::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000501
502 if handle:
503 print "Yes"
504
505will print ``Yes`` if the handle is currently valid (has not been closed or
506detached).
507
508The object also support comparison semantics, so handle objects will compare
509true if they both reference the same underlying Windows handle value.
510
Georg Brandld7d4fd72009-07-26 14:37:28 +0000511Handle objects can be converted to an integer (e.g., using the built-in
Georg Brandl8ec7f652007-08-15 14:28:01 +0000512:func:`int` function), in which case the underlying Windows handle value is
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000513returned. You can also use the :meth:`Detach` method to return the integer
Georg Brandl8ec7f652007-08-15 14:28:01 +0000514handle, and also disconnect the Windows handle from the handle object.
515
516
517.. method:: PyHKEY.Close()
518
519 Closes the underlying Windows handle.
520
521 If the handle is already closed, no error is raised.
522
523
524.. method:: PyHKEY.Detach()
525
526 Detaches the Windows handle from the handle object.
527
528 The result is an integer (or long on 64 bit Windows) that holds the value of the
529 handle before it is detached. If the handle is already detached or closed, this
530 will return zero.
531
532 After calling this function, the handle is effectively invalidated, but the
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000533 handle is not closed. You would call this function when you need the
534 underlying Win32 handle to exist beyond the lifetime of the handle object.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000535
Christian Heimesb39a7562008-01-08 15:46:10 +0000536.. method:: PyHKEY.__enter__()
Georg Brandl502d6312008-01-08 16:18:26 +0000537 PyHKEY.__exit__(\*exc_info)
538
539 The HKEY object implements :meth:`__enter__` and :meth:`__exit__` and thus
540 supports the context protocol for the :keyword:`with` statement::
541
542 with OpenKey(HKEY_LOCAL_MACHINE, "foo") as key:
543 # ... work with key ...
544
545 will automatically close *key* when control leaves the :keyword:`with` block.
546
547 .. versionadded:: 2.6
Christian Heimesb39a7562008-01-08 15:46:10 +0000548