blob: 67c4c6b07b041d3ba4de0f6c3da5bdafbcc9b341 [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
Georg Brandl9bfb78d2010-04-25 10:54:42 +000018integer as the registry handle, a :ref:`handle object <handle-object>` is used
19to ensure that the handles are closed correctly, even if the programmer neglects
20to explicitly close them.
Georg Brandl8ec7f652007-08-15 14:28:01 +000021
22This module exposes a very low-level interface to the Windows registry; it is
Georg Brandla3f1e4c2010-04-25 10:55:16 +000023expected that in the future a new module will be created offering a higher-level
24interface to the registry API.
Georg Brandl8ec7f652007-08-15 14:28:01 +000025
26This module offers the following functions:
27
28
29.. function:: CloseKey(hkey)
30
Georg Brandlbb091e72010-04-25 10:55:58 +000031 Closes a previously opened registry key. The *hkey* argument specifies a
Georg Brandl8ec7f652007-08-15 14:28:01 +000032 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
Brian Curtin5fa9fb42010-04-24 17:10:22 +000072.. function:: CreateKeyEx(key, sub_key[, res[, sam]])
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
Georg Brandl9bfb78d2010-04-25 10:54:42 +000085 security access for the key. Default is :const:`KEY_ALL_ACCESS`. See the
86 `Win32 documentation
87 <http://msdn.microsoft.com/en-us/library/ms724878%28v=VS.85%29.aspx>`_ for
88 other allowed values.
89
Brian Curtine33fa882010-04-02 21:18:14 +000090
Ezio Melotti01fa86a2010-04-05 08:02:54 +000091 If *key* is one of the predefined keys, *sub_key* may be ``None``. In that
92 case, the handle returned is the same key handle passed in to the function.
Brian Curtine33fa882010-04-02 21:18:14 +000093
94 If the key already exists, this function opens the existing key.
95
96 The return value is the handle of the opened key. If the function fails, a
97 :exc:`WindowsError` exception is raised.
98
99.. versionadded:: 2.7
100
101
Georg Brandl8ec7f652007-08-15 14:28:01 +0000102.. function:: DeleteKey(key, sub_key)
103
104 Deletes the specified key.
105
Brian Curtine33fa882010-04-02 21:18:14 +0000106 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000107 constants.
108
Brian Curtine33fa882010-04-02 21:18:14 +0000109 *sub_key* is a string that must be a subkey of the key identified by the *key*
110 parameter. This value must not be ``None``, and the key may not have subkeys.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000111
112 *This method can not delete keys with subkeys.*
113
114 If the method succeeds, the entire key, including all of its values, is removed.
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000115 If the method fails, a :exc:`WindowsError` exception is raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000116
117
Brian Curtin5fa9fb42010-04-24 17:10:22 +0000118.. function:: DeleteKeyEx(key, sub_key[, sam[, res]])
Brian Curtine33fa882010-04-02 21:18:14 +0000119
120 Deletes the specified key.
121
122 .. note::
123 The :func:`DeleteKeyEx` function is implemented with the RegDeleteKeyEx
124 Windows API function, which is specific to 64-bit versions of Windows.
125 See http://msdn.microsoft.com/en-us/library/ms724847%28VS.85%29.aspx
126
127 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
128 constants.
129
130 *sub_key* is a string that must be a subkey of the key identified by the
131 *key* parameter. This value must not be ``None``, and the key may not have
132 subkeys.
133
134 *res* is a reserved integer, and must be zero. The default is zero.
135
Georg Brandl9bfb78d2010-04-25 10:54:42 +0000136 *sam* is an integer that specifies an access mask that describes the desired
137 security access for the key. Default is :const:`KEY_WOW64_64KEY`. See the
138 `Win32 documentation
139 <http://msdn.microsoft.com/en-us/library/ms724878%28v=VS.85%29.aspx>`_ for
140 other allowed values.
141
Brian Curtine33fa882010-04-02 21:18:14 +0000142
143 *This method can not delete keys with subkeys.*
144
145 If the method succeeds, the entire key, including all of its values, is
146 removed. If the method fails, a :exc:`WindowsError` exception is raised.
147
148 On unsupported Windows versions, :exc:`NotImplementedError` is raised.
149
150.. versionadded:: 2.7
151
152
Georg Brandl8ec7f652007-08-15 14:28:01 +0000153.. function:: DeleteValue(key, value)
154
155 Removes a named value from a registry key.
156
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000157 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000158 constants.
159
160 *value* is a string that identifies the value to remove.
161
162
163.. function:: EnumKey(key, index)
164
165 Enumerates subkeys of an open registry key, returning a string.
166
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000167 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000168 constants.
169
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000170 *index* is an integer that identifies the index of the key to retrieve.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000171
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000172 The function retrieves the name of one subkey each time it is called. It is
173 typically called repeatedly until a :exc:`WindowsError` exception is
Georg Brandl8ec7f652007-08-15 14:28:01 +0000174 raised, indicating, no more values are available.
175
176
177.. function:: EnumValue(key, index)
178
179 Enumerates values of an open registry key, returning a tuple.
180
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000181 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000182 constants.
183
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000184 *index* is an integer that identifies the index of the value to retrieve.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000185
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000186 The function retrieves the name of one subkey each time it is called. It is
187 typically called repeatedly, until a :exc:`WindowsError` exception is
188 raised, indicating no more values.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000189
190 The result is a tuple of 3 items:
191
192 +-------+--------------------------------------------+
193 | Index | Meaning |
194 +=======+============================================+
195 | ``0`` | A string that identifies the value name |
196 +-------+--------------------------------------------+
197 | ``1`` | An object that holds the value data, and |
198 | | whose type depends on the underlying |
199 | | registry type |
200 +-------+--------------------------------------------+
201 | ``2`` | An integer that identifies the type of the |
Georg Brandl9bfb78d2010-04-25 10:54:42 +0000202 | | value data (see table in docs for |
203 | | :meth:`SetValueEx`) |
Georg Brandl8ec7f652007-08-15 14:28:01 +0000204 +-------+--------------------------------------------+
205
206
Christian Heimesb39a7562008-01-08 15:46:10 +0000207.. function:: ExpandEnvironmentStrings(unicode)
208
Georg Brandl9bfb78d2010-04-25 10:54:42 +0000209 Expands environment variable placeholders ``%NAME%`` in unicode strings like
210 :const:`REG_EXPAND_SZ`::
Georg Brandl502d6312008-01-08 16:18:26 +0000211
212 >>> ExpandEnvironmentStrings(u"%windir%")
213 u"C:\\Windows"
214
215 .. versionadded:: 2.6
Christian Heimesb39a7562008-01-08 15:46:10 +0000216
217
Georg Brandl8ec7f652007-08-15 14:28:01 +0000218.. function:: FlushKey(key)
219
220 Writes all the attributes of a key to the registry.
221
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000222 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000223 constants.
224
Georg Brandl51174092008-05-09 06:10:43 +0000225 It is not necessary to call :func:`FlushKey` to change a key. Registry changes are
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000226 flushed to disk by the registry using its lazy flusher. Registry changes are
227 also flushed to disk at system shutdown. Unlike :func:`CloseKey`, the
228 :func:`FlushKey` method returns only when all the data has been written to the
Georg Brandl8ec7f652007-08-15 14:28:01 +0000229 registry. An application should only call :func:`FlushKey` if it requires
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000230 absolute certainty that registry changes are on disk.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000231
232 .. note::
233
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000234 If you don't know whether a :func:`FlushKey` call is required, it probably
Georg Brandl8ec7f652007-08-15 14:28:01 +0000235 isn't.
236
237
Georg Brandl51174092008-05-09 06:10:43 +0000238.. function:: LoadKey(key, sub_key, file_name)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000239
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000240 Creates a subkey under the specified key and stores registration information
Georg Brandl8ec7f652007-08-15 14:28:01 +0000241 from a specified file into that subkey.
242
Georg Brandl9bfb78d2010-04-25 10:54:42 +0000243 *key* is a handle returned by :func:`ConnectRegistry` or one of the constants
244 :const:`HKEY_USER` or :const:`HKEY_LOCAL_MACHINE`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000245
Georg Brandl9bfb78d2010-04-25 10:54:42 +0000246 *sub_key* is a string that identifies the subkey to load.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000247
248 *file_name* is the name of the file to load registry data from. This file must
249 have been created with the :func:`SaveKey` function. Under the file allocation
250 table (FAT) file system, the filename may not have an extension.
251
Georg Brandl9bfb78d2010-04-25 10:54:42 +0000252 A call to :func:`LoadKey` fails if the calling process does not have the
253 :const:`SE_RESTORE_PRIVILEGE` privilege. Note that privileges are different
254 from permissions -- see the `Win32 documentation
255 <http://msdn.microsoft.com/en-us/library/ms724889%28v=VS.85%29.aspx>`_ for
256 more details.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000257
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000258 If *key* is a handle returned by :func:`ConnectRegistry`, then the path
Georg Brandl9bfb78d2010-04-25 10:54:42 +0000259 specified in *file_name* is relative to the remote computer.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000260
261
Brian Curtin5fa9fb42010-04-24 17:10:22 +0000262.. function:: OpenKey(key, sub_key[, res[, sam]])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000263
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000264 Opens the specified key, returning a :ref:`handle object <handle-object>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000265
266 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
267 constants.
268
269 *sub_key* is a string that identifies the sub_key to open.
270
271 *res* is a reserved integer, and must be zero. The default is zero.
272
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000273 *sam* is an integer that specifies an access mask that describes the desired
Georg Brandl9bfb78d2010-04-25 10:54:42 +0000274 security access for the key. Default is :const:`KEY_READ`. See the `Win32
275 documentation
276 <http://msdn.microsoft.com/en-us/library/ms724878%28v=VS.85%29.aspx>`_ for
277 other allowed values.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000278
279 The result is a new handle to the specified key.
280
Georg Brandlb945bbf2009-03-31 16:31:11 +0000281 If the function fails, :exc:`WindowsError` is raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000282
283
284.. function:: OpenKeyEx()
285
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000286 The functionality of :func:`OpenKeyEx` is provided via :func:`OpenKey`,
287 by the use of default arguments.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000288
289
290.. function:: QueryInfoKey(key)
291
292 Returns information about a key, as a tuple.
293
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000294 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000295 constants.
296
297 The result is a tuple of 3 items:
298
299 +-------+---------------------------------------------+
300 | Index | Meaning |
301 +=======+=============================================+
302 | ``0`` | An integer giving the number of sub keys |
303 | | this key has. |
304 +-------+---------------------------------------------+
305 | ``1`` | An integer giving the number of values this |
306 | | key has. |
307 +-------+---------------------------------------------+
308 | ``2`` | A long integer giving when the key was last |
309 | | modified (if available) as 100's of |
310 | | nanoseconds since Jan 1, 1600. |
311 +-------+---------------------------------------------+
312
313
314.. function:: QueryValue(key, sub_key)
315
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000316 Retrieves the unnamed value for a key, as a string.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000317
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000318 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000319 constants.
320
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000321 *sub_key* is a string that holds the name of the subkey with which the value is
322 associated. If this parameter is ``None`` or empty, the function retrieves the
323 value set by the :func:`SetValue` method for the key identified by *key*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000324
Georg Brandl75f11072009-04-05 10:32:26 +0000325 Values in the registry have name, type, and data components. This method
Georg Brandl8ec7f652007-08-15 14:28:01 +0000326 retrieves the data for a key's first value that has a NULL name. But the
Georg Brandl75f11072009-04-05 10:32:26 +0000327 underlying API call doesn't return the type, so always use
328 :func:`QueryValueEx` if possible.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000329
330
331.. function:: QueryValueEx(key, value_name)
332
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000333 Retrieves the type and data for a specified value name associated with
334 an open registry key.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000335
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000336 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000337 constants.
338
339 *value_name* is a string indicating the value to query.
340
341 The result is a tuple of 2 items:
342
343 +-------+-----------------------------------------+
344 | Index | Meaning |
345 +=======+=========================================+
346 | ``0`` | The value of the registry item. |
347 +-------+-----------------------------------------+
348 | ``1`` | An integer giving the registry type for |
Georg Brandl9bfb78d2010-04-25 10:54:42 +0000349 | | this value (see table in docs for |
350 | | :meth:`SetValueEx`) |
Georg Brandl8ec7f652007-08-15 14:28:01 +0000351 +-------+-----------------------------------------+
352
353
354.. function:: SaveKey(key, file_name)
355
356 Saves the specified key, and all its subkeys to the specified file.
357
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000358 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000359 constants.
360
Georg Brandl9bfb78d2010-04-25 10:54:42 +0000361 *file_name* is the name of the file to save registry data to. This file
362 cannot already exist. If this filename includes an extension, it cannot be
363 used on file allocation table (FAT) file systems by the :meth:`LoadKey`
364 method.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000365
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000366 If *key* represents a key on a remote computer, the path described by
Georg Brandl8ec7f652007-08-15 14:28:01 +0000367 *file_name* is relative to the remote computer. The caller of this method must
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000368 possess the :const:`SeBackupPrivilege` security privilege. Note that
369 privileges are different than permissions -- see the Win32 documentation for
Georg Brandl8ec7f652007-08-15 14:28:01 +0000370 more details.
371
372 This function passes NULL for *security_attributes* to the API.
373
374
375.. function:: SetValue(key, sub_key, type, value)
376
377 Associates a value with a specified key.
378
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000379 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000380 constants.
381
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000382 *sub_key* is a string that names the subkey with which the value is associated.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000383
384 *type* is an integer that specifies the type of the data. Currently this must be
385 :const:`REG_SZ`, meaning only strings are supported. Use the :func:`SetValueEx`
386 function for support for other data types.
387
388 *value* is a string that specifies the new value.
389
390 If the key specified by the *sub_key* parameter does not exist, the SetValue
391 function creates it.
392
393 Value lengths are limited by available memory. Long values (more than 2048
394 bytes) should be stored as files with the filenames stored in the configuration
395 registry. This helps the registry perform efficiently.
396
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000397 The key identified by the *key* parameter must have been opened with
Georg Brandl8ec7f652007-08-15 14:28:01 +0000398 :const:`KEY_SET_VALUE` access.
399
400
401.. function:: SetValueEx(key, value_name, reserved, type, value)
402
403 Stores data in the value field of an open registry key.
404
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000405 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000406 constants.
407
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000408 *value_name* is a string that names the subkey with which the value is
Georg Brandl8ec7f652007-08-15 14:28:01 +0000409 associated.
410
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000411 *type* is an integer that specifies the type of the data. This should be one
Georg Brandl8ec7f652007-08-15 14:28:01 +0000412 of the following constants defined in this module:
413
414 +----------------------------------+---------------------------------------------+
415 | Constant | Meaning |
416 +==================================+=============================================+
417 | :const:`REG_BINARY` | Binary data in any form. |
418 +----------------------------------+---------------------------------------------+
419 | :const:`REG_DWORD` | A 32-bit number. |
420 +----------------------------------+---------------------------------------------+
421 | :const:`REG_DWORD_LITTLE_ENDIAN` | A 32-bit number in little-endian format. |
422 +----------------------------------+---------------------------------------------+
423 | :const:`REG_DWORD_BIG_ENDIAN` | A 32-bit number in big-endian format. |
424 +----------------------------------+---------------------------------------------+
425 | :const:`REG_EXPAND_SZ` | Null-terminated string containing |
426 | | references to environment variables |
427 | | (``%PATH%``). |
428 +----------------------------------+---------------------------------------------+
429 | :const:`REG_LINK` | A Unicode symbolic link. |
430 +----------------------------------+---------------------------------------------+
431 | :const:`REG_MULTI_SZ` | A sequence of null-terminated strings, |
432 | | terminated by two null characters. (Python |
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000433 | | handles this termination automatically.) |
Georg Brandl8ec7f652007-08-15 14:28:01 +0000434 +----------------------------------+---------------------------------------------+
435 | :const:`REG_NONE` | No defined value type. |
436 +----------------------------------+---------------------------------------------+
437 | :const:`REG_RESOURCE_LIST` | A device-driver resource list. |
438 +----------------------------------+---------------------------------------------+
439 | :const:`REG_SZ` | A null-terminated string. |
440 +----------------------------------+---------------------------------------------+
441
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000442 *reserved* can be anything -- zero is always passed to the API.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000443
444 *value* is a string that specifies the new value.
445
446 This method can also set additional value and type information for the specified
447 key. The key identified by the key parameter must have been opened with
448 :const:`KEY_SET_VALUE` access.
449
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000450 To open the key, use the :func:`CreateKey` or :func:`OpenKey` methods.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000451
452 Value lengths are limited by available memory. Long values (more than 2048
453 bytes) should be stored as files with the filenames stored in the configuration
454 registry. This helps the registry perform efficiently.
455
456
Brian Curtine33fa882010-04-02 21:18:14 +0000457.. function:: DisableReflectionKey(key)
458
459 Disables registry reflection for 32-bit processes running on a 64-bit
Georg Brandlbb091e72010-04-25 10:55:58 +0000460 operating system.
Brian Curtine33fa882010-04-02 21:18:14 +0000461
462 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
463 constants.
464
465 Will generally raise :exc:`NotImplemented` if executed on a 32-bit
Georg Brandlbb091e72010-04-25 10:55:58 +0000466 operating system.
Brian Curtine33fa882010-04-02 21:18:14 +0000467
468 If the key is not on the reflection list, the function succeeds but has no
469 effect. Disabling reflection for a key does not affect reflection of any
470 subkeys.
471
472
473.. function:: EnableReflectionKey(key)
474
475 Restores registry reflection for the specified disabled key.
476
477 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
478 constants.
479
480 Will generally raise :exc:`NotImplemented` if executed on a 32-bit
Georg Brandlbb091e72010-04-25 10:55:58 +0000481 operating system.
Brian Curtine33fa882010-04-02 21:18:14 +0000482
483 Restoring reflection for a key does not affect reflection of any subkeys.
484
485
486.. function:: QueryReflectionKey(key)
487
488 Determines the reflection state for the specified key.
489
490 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
491 constants.
492
493 Returns ``True`` if reflection is disabled.
494
495 Will generally raise :exc:`NotImplemented` if executed on a 32-bit
Georg Brandlbb091e72010-04-25 10:55:58 +0000496 operating system.
Brian Curtine33fa882010-04-02 21:18:14 +0000497
498
Georg Brandl8ec7f652007-08-15 14:28:01 +0000499.. _handle-object:
500
501Registry Handle Objects
502-----------------------
503
504This object wraps a Windows HKEY object, automatically closing it when the
505object is destroyed. To guarantee cleanup, you can call either the
Georg Brandl9bfb78d2010-04-25 10:54:42 +0000506:meth:`~PyHKEY.Close` method on the object, or the :func:`CloseKey` function.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000507
508All registry functions in this module return one of these objects.
509
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000510All registry functions in this module which accept a handle object also accept
511an integer, however, use of the handle object is encouraged.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000512
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000513Handle objects provide semantics for :meth:`__nonzero__` -- thus::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000514
515 if handle:
516 print "Yes"
517
518will print ``Yes`` if the handle is currently valid (has not been closed or
519detached).
520
521The object also support comparison semantics, so handle objects will compare
522true if they both reference the same underlying Windows handle value.
523
Georg Brandld7d4fd72009-07-26 14:37:28 +0000524Handle objects can be converted to an integer (e.g., using the built-in
Georg Brandl8ec7f652007-08-15 14:28:01 +0000525:func:`int` function), in which case the underlying Windows handle value is
Georg Brandl9bfb78d2010-04-25 10:54:42 +0000526returned. You can also use the :meth:`~PyHKEY.Detach` method to return the
527integer handle, and also disconnect the Windows handle from the handle object.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000528
529
530.. method:: PyHKEY.Close()
531
532 Closes the underlying Windows handle.
533
534 If the handle is already closed, no error is raised.
535
536
537.. method:: PyHKEY.Detach()
538
539 Detaches the Windows handle from the handle object.
540
541 The result is an integer (or long on 64 bit Windows) that holds the value of the
542 handle before it is detached. If the handle is already detached or closed, this
543 will return zero.
544
545 After calling this function, the handle is effectively invalidated, but the
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000546 handle is not closed. You would call this function when you need the
547 underlying Win32 handle to exist beyond the lifetime of the handle object.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000548
Christian Heimesb39a7562008-01-08 15:46:10 +0000549.. method:: PyHKEY.__enter__()
Georg Brandl502d6312008-01-08 16:18:26 +0000550 PyHKEY.__exit__(\*exc_info)
551
Georg Brandl9bfb78d2010-04-25 10:54:42 +0000552 The HKEY object implements :meth:`~object.__enter__` and
553 :meth:`~object.__exit__` and thus supports the context protocol for the
554 :keyword:`with` statement::
Georg Brandl502d6312008-01-08 16:18:26 +0000555
556 with OpenKey(HKEY_LOCAL_MACHINE, "foo") as key:
Georg Brandl1e518252010-04-25 10:56:41 +0000557 ... # work with key
Georg Brandl502d6312008-01-08 16:18:26 +0000558
559 will automatically close *key* when control leaves the :keyword:`with` block.
560
561 .. versionadded:: 2.6
Christian Heimesb39a7562008-01-08 15:46:10 +0000562