blob: 36271300cd72400cf8d2c4f0c856595cb5d1f9ce [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 Brandl34e44c42010-04-25 17:15:51 +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
Georg Brandl8ec7f652007-08-15 14:28:01 +000022This module offers the following functions:
23
24
25.. function:: CloseKey(hkey)
26
Georg Brandl34e44c42010-04-25 17:15:51 +000027 Closes a previously opened registry key. The *hkey* argument specifies a
Georg Brandl8ec7f652007-08-15 14:28:01 +000028 previously opened key.
29
Brian Curtin59170e32010-05-11 19:32:26 +000030 .. note::
31 If *hkey* is not closed using this method (or via :meth:`hkey.Close() <PyHKEY.Close>`),
32 it is closed when the *hkey* object is destroyed by Python.
Georg Brandl8ec7f652007-08-15 14:28:01 +000033
34
35.. function:: ConnectRegistry(computer_name, key)
36
Ezio Melotti463aa202010-04-05 08:08:36 +000037 Establishes a connection to a predefined registry handle on another computer,
38 and returns a :ref:`handle object <handle-object>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000039
Ezio Melotti463aa202010-04-05 08:08:36 +000040 *computer_name* is the name of the remote computer, of the form
Georg Brandl8ec7f652007-08-15 14:28:01 +000041 ``r"\\computername"``. If ``None``, the local computer is used.
42
43 *key* is the predefined handle to connect to.
44
Georg Brandl8943caf2009-04-05 21:11:43 +000045 The return value is the handle of the opened key. If the function fails, a
Ezio Melotti463aa202010-04-05 08:08:36 +000046 :exc:`WindowsError` exception is raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +000047
48
49.. function:: CreateKey(key, sub_key)
50
Ezio Melotti463aa202010-04-05 08:08:36 +000051 Creates or opens the specified key, returning a
52 :ref:`handle object <handle-object>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000053
Brian Curtin59170e32010-05-11 19:32:26 +000054 *key* is an already open key, or one of the predefined
55 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000056
Ezio Melotti463aa202010-04-05 08:08:36 +000057 *sub_key* is a string that names the key this method opens or creates.
Georg Brandl8ec7f652007-08-15 14:28:01 +000058
Ezio Melotti463aa202010-04-05 08:08:36 +000059 If *key* is one of the predefined keys, *sub_key* may be ``None``. In that
60 case, the handle returned is the same key handle passed in to the function.
Georg Brandl8ec7f652007-08-15 14:28:01 +000061
62 If the key already exists, this function opens the existing key.
63
Georg Brandl8943caf2009-04-05 21:11:43 +000064 The return value is the handle of the opened key. If the function fails, a
Ezio Melotti463aa202010-04-05 08:08:36 +000065 :exc:`WindowsError` exception is raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +000066
67
68.. function:: DeleteKey(key, sub_key)
69
70 Deletes the specified key.
71
Brian Curtin59170e32010-05-11 19:32:26 +000072 *key* is an already open key, or any one of the predefined
73 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000074
75 *sub_key* is a string that must be a subkey of the key identified by the *key*
76 parameter. This value must not be ``None``, and the key may not have subkeys.
77
78 *This method can not delete keys with subkeys.*
79
80 If the method succeeds, the entire key, including all of its values, is removed.
Ezio Melotti463aa202010-04-05 08:08:36 +000081 If the method fails, a :exc:`WindowsError` exception is raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +000082
83
84.. function:: DeleteValue(key, value)
85
86 Removes a named value from a registry key.
87
Brian Curtin59170e32010-05-11 19:32:26 +000088 *key* is an already open key, or one of the predefined
89 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000090
91 *value* is a string that identifies the value to remove.
92
93
94.. function:: EnumKey(key, index)
95
96 Enumerates subkeys of an open registry key, returning a string.
97
Brian Curtin59170e32010-05-11 19:32:26 +000098 *key* is an already open key, or any one of the predefined
99 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000100
Ezio Melotti463aa202010-04-05 08:08:36 +0000101 *index* is an integer that identifies the index of the key to retrieve.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000102
Ezio Melotti463aa202010-04-05 08:08:36 +0000103 The function retrieves the name of one subkey each time it is called. It is
104 typically called repeatedly until a :exc:`WindowsError` exception is
Georg Brandl8ec7f652007-08-15 14:28:01 +0000105 raised, indicating, no more values are available.
106
107
108.. function:: EnumValue(key, index)
109
110 Enumerates values of an open registry key, returning a tuple.
111
Brian Curtin59170e32010-05-11 19:32:26 +0000112 *key* is an already open key, or any one of the predefined
113 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000114
Ezio Melotti463aa202010-04-05 08:08:36 +0000115 *index* is an integer that identifies the index of the value to retrieve.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000116
Ezio Melotti463aa202010-04-05 08:08:36 +0000117 The function retrieves the name of one subkey each time it is called. It is
118 typically called repeatedly, until a :exc:`WindowsError` exception is
119 raised, indicating no more values.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000120
121 The result is a tuple of 3 items:
122
123 +-------+--------------------------------------------+
124 | Index | Meaning |
125 +=======+============================================+
126 | ``0`` | A string that identifies the value name |
127 +-------+--------------------------------------------+
128 | ``1`` | An object that holds the value data, and |
129 | | whose type depends on the underlying |
130 | | registry type |
131 +-------+--------------------------------------------+
132 | ``2`` | An integer that identifies the type of the |
Georg Brandl34e44c42010-04-25 17:15:51 +0000133 | | value data (see table in docs for |
134 | | :meth:`SetValueEx`) |
Georg Brandl8ec7f652007-08-15 14:28:01 +0000135 +-------+--------------------------------------------+
136
137
Christian Heimesb39a7562008-01-08 15:46:10 +0000138.. function:: ExpandEnvironmentStrings(unicode)
139
Georg Brandl34e44c42010-04-25 17:15:51 +0000140 Expands environment variable placeholders ``%NAME%`` in unicode strings like
141 :const:`REG_EXPAND_SZ`::
Georg Brandl502d6312008-01-08 16:18:26 +0000142
143 >>> ExpandEnvironmentStrings(u"%windir%")
144 u"C:\\Windows"
145
146 .. versionadded:: 2.6
Christian Heimesb39a7562008-01-08 15:46:10 +0000147
148
Georg Brandl8ec7f652007-08-15 14:28:01 +0000149.. function:: FlushKey(key)
150
151 Writes all the attributes of a key to the registry.
152
Brian Curtin59170e32010-05-11 19:32:26 +0000153 *key* is an already open key, or one of the predefined
154 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000155
Georg Brandl51174092008-05-09 06:10:43 +0000156 It is not necessary to call :func:`FlushKey` to change a key. Registry changes are
Ezio Melotti463aa202010-04-05 08:08:36 +0000157 flushed to disk by the registry using its lazy flusher. Registry changes are
158 also flushed to disk at system shutdown. Unlike :func:`CloseKey`, the
159 :func:`FlushKey` method returns only when all the data has been written to the
Georg Brandl8ec7f652007-08-15 14:28:01 +0000160 registry. An application should only call :func:`FlushKey` if it requires
Ezio Melotti463aa202010-04-05 08:08:36 +0000161 absolute certainty that registry changes are on disk.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000162
163 .. note::
164
Ezio Melotti463aa202010-04-05 08:08:36 +0000165 If you don't know whether a :func:`FlushKey` call is required, it probably
Georg Brandl8ec7f652007-08-15 14:28:01 +0000166 isn't.
167
168
Georg Brandl51174092008-05-09 06:10:43 +0000169.. function:: LoadKey(key, sub_key, file_name)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000170
Ezio Melotti463aa202010-04-05 08:08:36 +0000171 Creates a subkey under the specified key and stores registration information
Georg Brandl8ec7f652007-08-15 14:28:01 +0000172 from a specified file into that subkey.
173
Georg Brandl34e44c42010-04-25 17:15:51 +0000174 *key* is a handle returned by :func:`ConnectRegistry` or one of the constants
Brian Curtin59170e32010-05-11 19:32:26 +0000175 :const:`HKEY_USERS` or :const:`HKEY_LOCAL_MACHINE`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000176
Georg Brandl34e44c42010-04-25 17:15:51 +0000177 *sub_key* is a string that identifies the subkey to load.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000178
179 *file_name* is the name of the file to load registry data from. This file must
180 have been created with the :func:`SaveKey` function. Under the file allocation
181 table (FAT) file system, the filename may not have an extension.
182
Georg Brandl34e44c42010-04-25 17:15:51 +0000183 A call to :func:`LoadKey` fails if the calling process does not have the
184 :const:`SE_RESTORE_PRIVILEGE` privilege. Note that privileges are different
Brian Curtin59170e32010-05-11 19:32:26 +0000185 from permissions -- see the `RegLoadKey documentation
Georg Brandl34e44c42010-04-25 17:15:51 +0000186 <http://msdn.microsoft.com/en-us/library/ms724889%28v=VS.85%29.aspx>`_ for
187 more details.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000188
Ezio Melotti463aa202010-04-05 08:08:36 +0000189 If *key* is a handle returned by :func:`ConnectRegistry`, then the path
Georg Brandl34e44c42010-04-25 17:15:51 +0000190 specified in *file_name* is relative to the remote computer.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000191
192
Brian Curtin2ec87de2010-04-24 17:14:35 +0000193.. function:: OpenKey(key, sub_key[, res[, sam]])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000194
Ezio Melotti463aa202010-04-05 08:08:36 +0000195 Opens the specified key, returning a :ref:`handle object <handle-object>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000196
Brian Curtin59170e32010-05-11 19:32:26 +0000197 *key* is an already open key, or any one of the predefined
198 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000199
200 *sub_key* is a string that identifies the sub_key to open.
201
202 *res* is a reserved integer, and must be zero. The default is zero.
203
Ezio Melotti463aa202010-04-05 08:08:36 +0000204 *sam* is an integer that specifies an access mask that describes the desired
Brian Curtin59170e32010-05-11 19:32:26 +0000205 security access for the key. Default is :const:`KEY_READ`. See
206 :ref:`Access Rights <access-rights>` for other allowed values.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000207
208 The result is a new handle to the specified key.
209
Georg Brandl8943caf2009-04-05 21:11:43 +0000210 If the function fails, :exc:`WindowsError` is raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000211
212
213.. function:: OpenKeyEx()
214
Ezio Melotti463aa202010-04-05 08:08:36 +0000215 The functionality of :func:`OpenKeyEx` is provided via :func:`OpenKey`,
216 by the use of default arguments.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000217
218
219.. function:: QueryInfoKey(key)
220
221 Returns information about a key, as a tuple.
222
Brian Curtin59170e32010-05-11 19:32:26 +0000223 *key* is an already open key, or one of the predefined
224 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000225
226 The result is a tuple of 3 items:
227
228 +-------+---------------------------------------------+
229 | Index | Meaning |
230 +=======+=============================================+
231 | ``0`` | An integer giving the number of sub keys |
232 | | this key has. |
233 +-------+---------------------------------------------+
234 | ``1`` | An integer giving the number of values this |
235 | | key has. |
236 +-------+---------------------------------------------+
237 | ``2`` | A long integer giving when the key was last |
238 | | modified (if available) as 100's of |
239 | | nanoseconds since Jan 1, 1600. |
240 +-------+---------------------------------------------+
241
242
243.. function:: QueryValue(key, sub_key)
244
Ezio Melotti463aa202010-04-05 08:08:36 +0000245 Retrieves the unnamed value for a key, as a string.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000246
Brian Curtin59170e32010-05-11 19:32:26 +0000247 *key* is an already open key, or one of the predefined
248 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000249
Ezio Melotti463aa202010-04-05 08:08:36 +0000250 *sub_key* is a string that holds the name of the subkey with which the value is
251 associated. If this parameter is ``None`` or empty, the function retrieves the
252 value set by the :func:`SetValue` method for the key identified by *key*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000253
Georg Brandle9b91212009-04-05 21:26:31 +0000254 Values in the registry have name, type, and data components. This method
Georg Brandl8ec7f652007-08-15 14:28:01 +0000255 retrieves the data for a key's first value that has a NULL name. But the
Georg Brandle9b91212009-04-05 21:26:31 +0000256 underlying API call doesn't return the type, so always use
257 :func:`QueryValueEx` if possible.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000258
259
260.. function:: QueryValueEx(key, value_name)
261
Ezio Melotti463aa202010-04-05 08:08:36 +0000262 Retrieves the type and data for a specified value name associated with
263 an open registry key.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000264
Brian Curtin59170e32010-05-11 19:32:26 +0000265 *key* is an already open key, or one of the predefined
266 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000267
268 *value_name* is a string indicating the value to query.
269
270 The result is a tuple of 2 items:
271
272 +-------+-----------------------------------------+
273 | Index | Meaning |
274 +=======+=========================================+
275 | ``0`` | The value of the registry item. |
276 +-------+-----------------------------------------+
277 | ``1`` | An integer giving the registry type for |
Georg Brandl34e44c42010-04-25 17:15:51 +0000278 | | this value (see table in docs for |
279 | | :meth:`SetValueEx`) |
Georg Brandl8ec7f652007-08-15 14:28:01 +0000280 +-------+-----------------------------------------+
281
282
283.. function:: SaveKey(key, file_name)
284
285 Saves the specified key, and all its subkeys to the specified file.
286
Brian Curtin59170e32010-05-11 19:32:26 +0000287 *key* is an already open key, or one of the predefined
288 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000289
Georg Brandl34e44c42010-04-25 17:15:51 +0000290 *file_name* is the name of the file to save registry data to. This file
291 cannot already exist. If this filename includes an extension, it cannot be
292 used on file allocation table (FAT) file systems by the :meth:`LoadKey`
293 method.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000294
Ezio Melotti463aa202010-04-05 08:08:36 +0000295 If *key* represents a key on a remote computer, the path described by
Georg Brandl8ec7f652007-08-15 14:28:01 +0000296 *file_name* is relative to the remote computer. The caller of this method must
Ezio Melotti463aa202010-04-05 08:08:36 +0000297 possess the :const:`SeBackupPrivilege` security privilege. Note that
Brian Curtin59170e32010-05-11 19:32:26 +0000298 privileges are different than permissions -- see the
299 `Conflicts Between User Rights and Permissions documentation
300 <http://msdn.microsoft.com/en-us/library/ms724878%28v=VS.85%29.aspx>`__
301 for more details.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000302
303 This function passes NULL for *security_attributes* to the API.
304
305
306.. function:: SetValue(key, sub_key, type, value)
307
308 Associates a value with a specified key.
309
Brian Curtin59170e32010-05-11 19:32:26 +0000310 *key* is an already open key, or one of the predefined
311 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000312
Ezio Melotti463aa202010-04-05 08:08:36 +0000313 *sub_key* is a string that names the subkey with which the value is associated.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000314
315 *type* is an integer that specifies the type of the data. Currently this must be
316 :const:`REG_SZ`, meaning only strings are supported. Use the :func:`SetValueEx`
317 function for support for other data types.
318
319 *value* is a string that specifies the new value.
320
321 If the key specified by the *sub_key* parameter does not exist, the SetValue
322 function creates it.
323
324 Value lengths are limited by available memory. Long values (more than 2048
325 bytes) should be stored as files with the filenames stored in the configuration
326 registry. This helps the registry perform efficiently.
327
Ezio Melotti463aa202010-04-05 08:08:36 +0000328 The key identified by the *key* parameter must have been opened with
Georg Brandl8ec7f652007-08-15 14:28:01 +0000329 :const:`KEY_SET_VALUE` access.
330
331
332.. function:: SetValueEx(key, value_name, reserved, type, value)
333
334 Stores data in the value field of an open registry key.
335
Brian Curtin59170e32010-05-11 19:32:26 +0000336 *key* is an already open key, or one of the predefined
337 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000338
Ezio Melotti463aa202010-04-05 08:08:36 +0000339 *value_name* is a string that names the subkey with which the value is
Georg Brandl8ec7f652007-08-15 14:28:01 +0000340 associated.
341
Brian Curtin59170e32010-05-11 19:32:26 +0000342 *type* is an integer that specifies the type of the data. See
343 :ref:`Value Types <value-types>` for the available types.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000344
Ezio Melotti463aa202010-04-05 08:08:36 +0000345 *reserved* can be anything -- zero is always passed to the API.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000346
347 *value* is a string that specifies the new value.
348
349 This method can also set additional value and type information for the specified
350 key. The key identified by the key parameter must have been opened with
351 :const:`KEY_SET_VALUE` access.
352
Ezio Melotti463aa202010-04-05 08:08:36 +0000353 To open the key, use the :func:`CreateKey` or :func:`OpenKey` methods.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000354
355 Value lengths are limited by available memory. Long values (more than 2048
356 bytes) should be stored as files with the filenames stored in the configuration
357 registry. This helps the registry perform efficiently.
358
359
Brian Curtin59170e32010-05-11 19:32:26 +0000360.. function:: DisableReflectionKey(key)
361
362 Disables registry reflection for 32-bit processes running on a 64-bit
363 operating system.
364
365 *key* is an already open key, or one of the predefined
366 :ref:`HKEY_* constants <hkey-constants>`.
367
368 Will generally raise :exc:`NotImplemented` if executed on a 32-bit
369 operating system.
370
371 If the key is not on the reflection list, the function succeeds but has no
372 effect. Disabling reflection for a key does not affect reflection of any
373 subkeys.
374
375
376.. function:: EnableReflectionKey(key)
377
378 Restores registry reflection for the specified disabled key.
379
380 *key* is an already open key, or one of the predefined
381 :ref:`HKEY_* constants <hkey-constants>`.
382
383 Will generally raise :exc:`NotImplemented` if executed on a 32-bit
384 operating system.
385
386 Restoring reflection for a key does not affect reflection of any subkeys.
387
388
389.. function:: QueryReflectionKey(key)
390
391 Determines the reflection state for the specified key.
392
393 *key* is an already open key, or one of the predefined
394 :ref:`HKEY_* constants <hkey-constants>`.
395
396 Returns ``True`` if reflection is disabled.
397
398 Will generally raise :exc:`NotImplemented` if executed on a 32-bit
399 operating system.
400
401
402.. _constants:
403
404Constants
405------------------
406
407The following constants are defined for use in many :mod:`_winreg` functions.
408
409.. _hkey-constants:
410
411HKEY_* Constants
412++++++++++++++++
413
414.. data:: HKEY_CLASSES_ROOT
415
416 Registry entries subordinate to this key define types (or classes) of
417 documents and the properties associated with those types. Shell and
418 COM applications use the information stored under this key.
419
420
421.. data:: HKEY_CURRENT_USER
422
423 Registry entries subordinate to this key define the preferences of
424 the current user. These preferences include the settings of
425 environment variables, data about program groups, colors, printers,
426 network connections, and application preferences.
427
428.. data:: HKEY_LOCAL_MACHINE
429
430 Registry entries subordinate to this key define the physical state
431 of the computer, including data about the bus type, system memory,
432 and installed hardware and software.
433
434.. data:: HKEY_USERS
435
436 Registry entries subordinate to this key define the default user
437 configuration for new users on the local computer and the user
438 configuration for the current user.
439
440.. data:: HKEY_PERFORMANCE_DATA
441
442 Registry entries subordinate to this key allow you to access
443 performance data. The data is not actually stored in the registry;
444 the registry functions cause the system to collect the data from
445 its source.
446
447
448.. data:: HKEY_CURRENT_CONFIG
449
450 Contains information about the current hardware profile of the
451 local computer system.
452
453.. data:: HKEY_DYN_DATA
454
455 This key is not used in versions of Windows after 98.
456
457
458.. _access-rights:
459
460Access Rights
461+++++++++++++
462
463For more information, see `Registry Key Security and Access
464<http://msdn.microsoft.com/en-us/library/ms724878%28v=VS.85%29.aspx>`__.
465
466.. data:: KEY_ALL_ACCESS
467
468 Combines the STANDARD_RIGHTS_REQUIRED, :const:`KEY_QUERY_VALUE`,
469 :const:`KEY_SET_VALUE`, :const:`KEY_CREATE_SUB_KEY`,
470 :const:`KEY_ENUMERATE_SUB_KEYS`, :const:`KEY_NOTIFY`,
471 and :const:`KEY_CREATE_LINK` access rights.
472
473.. data:: KEY_WRITE
474
475 Combines the STANDARD_RIGHTS_WRITE, :const:`KEY_SET_VALUE`, and
476 :const:`KEY_CREATE_SUB_KEY` access rights.
477
478.. data:: KEY_READ
479
480 Combines the STANDARD_RIGHTS_READ, :const:`KEY_QUERY_VALUE`,
481 :const:`KEY_ENUMERATE_SUB_KEYS`, and :const:`KEY_NOTIFY` values.
482
483.. data:: KEY_EXECUTE
484
485 Equivalent to :const:`KEY_READ`.
486
487.. data:: KEY_QUERY_VALUE
488
489 Required to query the values of a registry key.
490
491.. data:: KEY_SET_VALUE
492
493 Required to create, delete, or set a registry value.
494
495.. data:: KEY_CREATE_SUB_KEY
496
497 Required to create a subkey of a registry key.
498
499.. data:: KEY_ENUMERATE_SUB_KEYS
500
501 Required to enumerate the subkeys of a registry key.
502
503.. data:: KEY_NOTIFY
504
505 Required to request change notifications for a registry key or for
506 subkeys of a registry key.
507
508.. data:: KEY_CREATE_LINK
509
510 Reserved for system use.
511
512
513.. _64-bit-access-rights:
514
51564-bit Specific
516***************
517
518For more information, see `Accesing an Alternate Registry View
519<http://msdn.microsoft.com/en-us/library/aa384129(v=VS.85).aspx>`__.
520
521.. data:: KEY_WOW64_64KEY
522
523 Indicates that an application on 64-bit Windows should operate on
524 the 64-bit registry view.
525
526.. data:: KEY_WOW64_32KEY
527
528 Indicates that an application on 64-bit Windows should operate on
529 the 32-bit registry view.
530
531
532.. _value-types:
533
534Value Types
535+++++++++++
536
537For more information, see `Registry Value Types
538<http://msdn.microsoft.com/en-us/library/ms724884%28v=VS.85%29.aspx>`__.
539
540.. data:: REG_BINARY
541
542 Binary data in any form.
543
544.. data:: REG_DWORD
545
546 32-bit number.
547
548.. data:: REG_DWORD_LITTLE_ENDIAN
549
550 A 32-bit number in little-endian format.
551
552.. data:: REG_DWORD_BIG_ENDIAN
553
554 A 32-bit number in big-endian format.
555
556.. data:: REG_EXPAND_SZ
557
558 Null-terminated string containing references to environment
559 variables (``%PATH%``).
560
561.. data:: REG_LINK
562
563 A Unicode symbolic link.
564
565.. data:: REG_MULTI_SZ
566
567 A sequence of null-terminated strings, terminated by two null characters.
568 (Python handles this termination automatically.)
569
570.. data:: REG_NONE
571
572 No defined value type.
573
574.. data:: REG_RESOURCE_LIST
575
576 A device-driver resource list.
577
578.. data:: REG_FULL_RESOURCE_DESCRIPTOR
579
580 A hardware setting.
581
582.. data:: REG_RESOURCE_REQUIREMENTS_LIST
583
584 A hardware resource list.
585
586.. data:: REG_SZ
587
588 A null-terminated string.
589
590
Georg Brandl8ec7f652007-08-15 14:28:01 +0000591.. _handle-object:
592
593Registry Handle Objects
594-----------------------
595
596This object wraps a Windows HKEY object, automatically closing it when the
597object is destroyed. To guarantee cleanup, you can call either the
Georg Brandl34e44c42010-04-25 17:15:51 +0000598:meth:`~PyHKEY.Close` method on the object, or the :func:`CloseKey` function.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000599
600All registry functions in this module return one of these objects.
601
Ezio Melotti463aa202010-04-05 08:08:36 +0000602All registry functions in this module which accept a handle object also accept
603an integer, however, use of the handle object is encouraged.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000604
Ezio Melotti463aa202010-04-05 08:08:36 +0000605Handle objects provide semantics for :meth:`__nonzero__` -- thus::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000606
607 if handle:
608 print "Yes"
609
610will print ``Yes`` if the handle is currently valid (has not been closed or
611detached).
612
613The object also support comparison semantics, so handle objects will compare
614true if they both reference the same underlying Windows handle value.
615
Georg Brandl4ae4f872009-10-27 14:37:48 +0000616Handle objects can be converted to an integer (e.g., using the built-in
Georg Brandl8ec7f652007-08-15 14:28:01 +0000617:func:`int` function), in which case the underlying Windows handle value is
Georg Brandl34e44c42010-04-25 17:15:51 +0000618returned. You can also use the :meth:`~PyHKEY.Detach` method to return the
619integer handle, and also disconnect the Windows handle from the handle object.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000620
621
622.. method:: PyHKEY.Close()
623
624 Closes the underlying Windows handle.
625
626 If the handle is already closed, no error is raised.
627
628
629.. method:: PyHKEY.Detach()
630
631 Detaches the Windows handle from the handle object.
632
633 The result is an integer (or long on 64 bit Windows) that holds the value of the
634 handle before it is detached. If the handle is already detached or closed, this
635 will return zero.
636
637 After calling this function, the handle is effectively invalidated, but the
Ezio Melotti463aa202010-04-05 08:08:36 +0000638 handle is not closed. You would call this function when you need the
639 underlying Win32 handle to exist beyond the lifetime of the handle object.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000640
Christian Heimesb39a7562008-01-08 15:46:10 +0000641.. method:: PyHKEY.__enter__()
Georg Brandl502d6312008-01-08 16:18:26 +0000642 PyHKEY.__exit__(\*exc_info)
643
Georg Brandl34e44c42010-04-25 17:15:51 +0000644 The HKEY object implements :meth:`~object.__enter__` and
645 :meth:`~object.__exit__` and thus supports the context protocol for the
646 :keyword:`with` statement::
Georg Brandl502d6312008-01-08 16:18:26 +0000647
648 with OpenKey(HKEY_LOCAL_MACHINE, "foo") as key:
Georg Brandl34e44c42010-04-25 17:15:51 +0000649 ... # work with key
Georg Brandl502d6312008-01-08 16:18:26 +0000650
651 will automatically close *key* when control leaves the :keyword:`with` block.
652
653 .. versionadded:: 2.6
Christian Heimesb39a7562008-01-08 15:46:10 +0000654