blob: 5cf30ee03c2df35ac5381f0c58e910fd0397a6da [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
Georg Brandl8173fb32010-05-19 21:03:51 +000011integer as the registry handle, a :ref:`handle object <handle-object>` is used
12to ensure that the handles are closed correctly, even if the programmer neglects
13to explicitly close them.
Georg Brandl116aa622007-08-15 14:28:22 +000014
Georg Brandl116aa622007-08-15 14:28:22 +000015This module offers the following functions:
16
17
18.. function:: CloseKey(hkey)
19
Georg Brandl8173fb32010-05-19 21:03:51 +000020 Closes a previously opened registry key. The *hkey* argument specifies a
Georg Brandl116aa622007-08-15 14:28:22 +000021 previously opened key.
22
Brian Curtin2d067c82010-05-11 20:35:47 +000023 .. note::
Georg Brandl8173fb32010-05-19 21:03:51 +000024
25 If *hkey* is not closed using this method (or via :meth:`hkey.Close()
26 <PyHKEY.Close>`), it is closed when the *hkey* object is destroyed by
27 Python.
Georg Brandl116aa622007-08-15 14:28:22 +000028
29
30.. function:: ConnectRegistry(computer_name, key)
31
Ezio Melottibc372982010-04-25 17:48:01 +000032 Establishes a connection to a predefined registry handle on another computer,
33 and returns a :ref:`handle object <handle-object>`.
Georg Brandl116aa622007-08-15 14:28:22 +000034
Ezio Melottibc372982010-04-25 17:48:01 +000035 *computer_name* is the name of the remote computer, of the form
Georg Brandl116aa622007-08-15 14:28:22 +000036 ``r"\\computername"``. If ``None``, the local computer is used.
37
38 *key* is the predefined handle to connect to.
39
Benjamin Petersond23f8222009-04-05 19:13:16 +000040 The return value is the handle of the opened key. If the function fails, a
Ezio Melottibc372982010-04-25 17:48:01 +000041 :exc:`WindowsError` exception is raised.
Georg Brandl116aa622007-08-15 14:28:22 +000042
43
44.. function:: CreateKey(key, sub_key)
45
Ezio Melottibc372982010-04-25 17:48:01 +000046 Creates or opens the specified key, returning a
47 :ref:`handle object <handle-object>`.
Georg Brandl116aa622007-08-15 14:28:22 +000048
Brian Curtin2d067c82010-05-11 20:35:47 +000049 *key* is an already open key, or one of the predefined
50 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +000051
Ezio Melottibc372982010-04-25 17:48:01 +000052 *sub_key* is a string that names the key this method opens or creates.
Georg Brandl116aa622007-08-15 14:28:22 +000053
Ezio Melottibc372982010-04-25 17:48:01 +000054 If *key* is one of the predefined keys, *sub_key* may be ``None``. In that
55 case, the handle returned is the same key handle passed in to the function.
Georg Brandl116aa622007-08-15 14:28:22 +000056
57 If the key already exists, this function opens the existing key.
58
Benjamin Petersond23f8222009-04-05 19:13:16 +000059 The return value is the handle of the opened key. If the function fails, a
Ezio Melottibc372982010-04-25 17:48:01 +000060 :exc:`WindowsError` exception is raised.
Georg Brandl116aa622007-08-15 14:28:22 +000061
62
Brian Curtin1771b542010-09-27 17:56:36 +000063.. function:: CreateKeyEx(key, sub_key, reserved=0, access=KEY_ALL_ACCESS)
Brian Curtin3035c392010-04-21 23:56:21 +000064
Ezio Melottibc372982010-04-25 17:48:01 +000065 Creates or opens the specified key, returning a
66 :ref:`handle object <handle-object>`.
Brian Curtin3035c392010-04-21 23:56:21 +000067
Brian Curtin2d067c82010-05-11 20:35:47 +000068 *key* is an already open key, or one of the predefined
69 :ref:`HKEY_* constants <hkey-constants>`.
Brian Curtin3035c392010-04-21 23:56:21 +000070
71 *sub_key* is a string that names the key this method opens or creates.
72
73 *res* is a reserved integer, and must be zero. The default is zero.
74
75 *sam* is an integer that specifies an access mask that describes the desired
Brian Curtin2d067c82010-05-11 20:35:47 +000076 security access for the key. Default is :const:`KEY_ALL_ACCESS`. See
77 :ref:`Access Rights <access-rights>` for other allowed values.
Brian Curtin3035c392010-04-21 23:56:21 +000078
Ezio Melottibc372982010-04-25 17:48:01 +000079 If *key* is one of the predefined keys, *sub_key* may be ``None``. In that
80 case, the handle returned is the same key handle passed in to the function.
Brian Curtin3035c392010-04-21 23:56:21 +000081
82 If the key already exists, this function opens the existing key.
83
84 The return value is the handle of the opened key. If the function fails, a
85 :exc:`WindowsError` exception is raised.
86
Georg Brandl4c25cf32010-04-22 07:00:42 +000087 .. versionadded:: 3.2
Brian Curtin3035c392010-04-21 23:56:21 +000088
89
Georg Brandl116aa622007-08-15 14:28:22 +000090.. function:: DeleteKey(key, sub_key)
91
92 Deletes the specified key.
93
Brian Curtin2d067c82010-05-11 20:35:47 +000094 *key* is an already open key, or one of the predefined
95 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +000096
Brian Curtin3035c392010-04-21 23:56:21 +000097 *sub_key* is a string that must be a subkey of the key identified by the *key*
98 parameter. This value must not be ``None``, and the key may not have subkeys.
Georg Brandl116aa622007-08-15 14:28:22 +000099
100 *This method can not delete keys with subkeys.*
101
102 If the method succeeds, the entire key, including all of its values, is removed.
Ezio Melottibc372982010-04-25 17:48:01 +0000103 If the method fails, a :exc:`WindowsError` exception is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000104
105
Brian Curtin1771b542010-09-27 17:56:36 +0000106.. function:: DeleteKeyEx(key, sub_key, access=KEY_ALL_ACCESS, reserved=0)
Brian Curtin3035c392010-04-21 23:56:21 +0000107
108 Deletes the specified key.
109
110 .. note::
111 The :func:`DeleteKeyEx` function is implemented with the RegDeleteKeyEx
112 Windows API function, which is specific to 64-bit versions of Windows.
Brian Curtin2d067c82010-05-11 20:35:47 +0000113 See the `RegDeleteKeyEx documentation
Benjamin Petersoncc1f5972010-06-06 02:41:24 +0000114 <http://msdn.microsoft.com/en-us/library/ms724847%28VS.85%29.aspx>`__.
Brian Curtin3035c392010-04-21 23:56:21 +0000115
Brian Curtin2d067c82010-05-11 20:35:47 +0000116 *key* is an already open key, or one of the predefined
117 :ref:`HKEY_* constants <hkey-constants>`.
Brian Curtin3035c392010-04-21 23:56:21 +0000118
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
Georg Brandl8173fb32010-05-19 21:03:51 +0000125 *sam* is an integer that specifies an access mask that describes the desired
Brian Curtin2d067c82010-05-11 20:35:47 +0000126 security access for the key. Default is :const:`KEY_ALL_ACCESS`. See
127 :ref:`Access Rights <access-rights>` for other allowed values.
Brian Curtin3035c392010-04-21 23:56:21 +0000128
129 *This method can not delete keys with subkeys.*
130
131 If the method succeeds, the entire key, including all of its values, is
132 removed. If the method fails, a :exc:`WindowsError` exception is raised.
133
134 On unsupported Windows versions, :exc:`NotImplementedError` is raised.
135
Georg Brandl4c25cf32010-04-22 07:00:42 +0000136 .. versionadded:: 3.2
Brian Curtin3035c392010-04-21 23:56:21 +0000137
138
Georg Brandl116aa622007-08-15 14:28:22 +0000139.. function:: DeleteValue(key, value)
140
141 Removes a named value from a registry key.
142
Brian Curtin2d067c82010-05-11 20:35:47 +0000143 *key* is an already open key, or one of the predefined
144 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000145
146 *value* is a string that identifies the value to remove.
147
148
149.. function:: EnumKey(key, index)
150
151 Enumerates subkeys of an open registry key, returning a string.
152
Brian Curtin2d067c82010-05-11 20:35:47 +0000153 *key* is an already open key, or one of the predefined
154 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000155
Ezio Melottibc372982010-04-25 17:48:01 +0000156 *index* is an integer that identifies the index of the key to retrieve.
Georg Brandl116aa622007-08-15 14:28:22 +0000157
Ezio Melottibc372982010-04-25 17:48:01 +0000158 The function retrieves the name of one subkey each time it is called. It is
159 typically called repeatedly until a :exc:`WindowsError` exception is
Georg Brandl116aa622007-08-15 14:28:22 +0000160 raised, indicating, no more values are available.
161
162
163.. function:: EnumValue(key, index)
164
165 Enumerates values of an open registry key, returning a tuple.
166
Brian Curtin2d067c82010-05-11 20:35:47 +0000167 *key* is an already open key, or one of the predefined
168 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000169
Ezio Melottibc372982010-04-25 17:48:01 +0000170 *index* is an integer that identifies the index of the value to retrieve.
Georg Brandl116aa622007-08-15 14:28:22 +0000171
Ezio Melottibc372982010-04-25 17:48:01 +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
174 raised, indicating no more values.
Georg Brandl116aa622007-08-15 14:28:22 +0000175
176 The result is a tuple of 3 items:
177
178 +-------+--------------------------------------------+
179 | Index | Meaning |
180 +=======+============================================+
181 | ``0`` | A string that identifies the value name |
182 +-------+--------------------------------------------+
183 | ``1`` | An object that holds the value data, and |
184 | | whose type depends on the underlying |
185 | | registry type |
186 +-------+--------------------------------------------+
187 | ``2`` | An integer that identifies the type of the |
Georg Brandl8173fb32010-05-19 21:03:51 +0000188 | | value data (see table in docs for |
189 | | :meth:`SetValueEx`) |
Georg Brandl116aa622007-08-15 14:28:22 +0000190 +-------+--------------------------------------------+
191
192
Ezio Melotti985e24d2009-09-13 07:54:02 +0000193.. function:: ExpandEnvironmentStrings(str)
Christian Heimes2380ac72008-01-09 00:17:24 +0000194
Georg Brandl8173fb32010-05-19 21:03:51 +0000195 Expands environment variable placeholders ``%NAME%`` in strings like
196 :const:`REG_EXPAND_SZ`::
Christian Heimes2380ac72008-01-09 00:17:24 +0000197
Ezio Melotti985e24d2009-09-13 07:54:02 +0000198 >>> ExpandEnvironmentStrings('%windir%')
199 'C:\\Windows'
Christian Heimes2380ac72008-01-09 00:17:24 +0000200
Christian Heimes2380ac72008-01-09 00:17:24 +0000201
Georg Brandl116aa622007-08-15 14:28:22 +0000202.. function:: FlushKey(key)
203
204 Writes all the attributes of a key to the registry.
205
Brian Curtin2d067c82010-05-11 20:35:47 +0000206 *key* is an already open key, or one of the predefined
207 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000208
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000209 It is not necessary to call :func:`FlushKey` to change a key. Registry changes are
Ezio Melottibc372982010-04-25 17:48:01 +0000210 flushed to disk by the registry using its lazy flusher. Registry changes are
211 also flushed to disk at system shutdown. Unlike :func:`CloseKey`, the
212 :func:`FlushKey` method returns only when all the data has been written to the
Georg Brandl116aa622007-08-15 14:28:22 +0000213 registry. An application should only call :func:`FlushKey` if it requires
Ezio Melottibc372982010-04-25 17:48:01 +0000214 absolute certainty that registry changes are on disk.
Georg Brandl116aa622007-08-15 14:28:22 +0000215
216 .. note::
217
Ezio Melottibc372982010-04-25 17:48:01 +0000218 If you don't know whether a :func:`FlushKey` call is required, it probably
Georg Brandl116aa622007-08-15 14:28:22 +0000219 isn't.
220
221
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000222.. function:: LoadKey(key, sub_key, file_name)
Georg Brandl116aa622007-08-15 14:28:22 +0000223
Ezio Melottibc372982010-04-25 17:48:01 +0000224 Creates a subkey under the specified key and stores registration information
Georg Brandl116aa622007-08-15 14:28:22 +0000225 from a specified file into that subkey.
226
Brian Curtin2d067c82010-05-11 20:35:47 +0000227 *key* is a handle returned by :func:`ConnectRegistry` or one of the constants
228 :const:`HKEY_USERS` or :const:`HKEY_LOCAL_MACHINE`.
Georg Brandl116aa622007-08-15 14:28:22 +0000229
Georg Brandl8173fb32010-05-19 21:03:51 +0000230 *sub_key* is a string that identifies the subkey to load.
Georg Brandl116aa622007-08-15 14:28:22 +0000231
232 *file_name* is the name of the file to load registry data from. This file must
233 have been created with the :func:`SaveKey` function. Under the file allocation
234 table (FAT) file system, the filename may not have an extension.
235
Georg Brandl8173fb32010-05-19 21:03:51 +0000236 A call to :func:`LoadKey` fails if the calling process does not have the
237 :const:`SE_RESTORE_PRIVILEGE` privilege. Note that privileges are different
Brian Curtin2d067c82010-05-11 20:35:47 +0000238 from permissions -- see the `RegLoadKey documentation
239 <http://msdn.microsoft.com/en-us/library/ms724889%28v=VS.85%29.aspx>`__ for
240 more details.
Georg Brandl116aa622007-08-15 14:28:22 +0000241
Ezio Melottibc372982010-04-25 17:48:01 +0000242 If *key* is a handle returned by :func:`ConnectRegistry`, then the path
Georg Brandl8173fb32010-05-19 21:03:51 +0000243 specified in *file_name* is relative to the remote computer.
Georg Brandl116aa622007-08-15 14:28:22 +0000244
245
Brian Curtin1771b542010-09-27 17:56:36 +0000246.. function:: OpenKey(key, sub_key, reserved=0, access=KEY_ALL_ACCESS)
Georg Brandl116aa622007-08-15 14:28:22 +0000247
Ezio Melottibc372982010-04-25 17:48:01 +0000248 Opens the specified key, returning a :ref:`handle object <handle-object>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000249
Brian Curtin2d067c82010-05-11 20:35:47 +0000250 *key* is an already open key, or one of the predefined
251 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000252
253 *sub_key* is a string that identifies the sub_key to open.
254
255 *res* is a reserved integer, and must be zero. The default is zero.
256
Ezio Melottibc372982010-04-25 17:48:01 +0000257 *sam* is an integer that specifies an access mask that describes the desired
Georg Brandl8173fb32010-05-19 21:03:51 +0000258 security access for the key. Default is :const:`KEY_READ`. See :ref:`Access
259 Rights <access-rights>` for other allowed values.
Georg Brandl116aa622007-08-15 14:28:22 +0000260
261 The result is a new handle to the specified key.
262
Benjamin Petersond23f8222009-04-05 19:13:16 +0000263 If the function fails, :exc:`WindowsError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000264
Brian Curtin1771b542010-09-27 17:56:36 +0000265 .. versionchanged:: 3.2 Allow the use of named arguments.
266
Georg Brandl116aa622007-08-15 14:28:22 +0000267
268.. function:: OpenKeyEx()
269
Ezio Melottibc372982010-04-25 17:48:01 +0000270 The functionality of :func:`OpenKeyEx` is provided via :func:`OpenKey`,
271 by the use of default arguments.
Georg Brandl116aa622007-08-15 14:28:22 +0000272
273
274.. function:: QueryInfoKey(key)
275
276 Returns information about a key, as a tuple.
277
Brian Curtin2d067c82010-05-11 20:35:47 +0000278 *key* is an already open key, or one of the predefined
279 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000280
281 The result is a tuple of 3 items:
282
283 +-------+---------------------------------------------+
284 | Index | Meaning |
285 +=======+=============================================+
286 | ``0`` | An integer giving the number of sub keys |
287 | | this key has. |
288 +-------+---------------------------------------------+
289 | ``1`` | An integer giving the number of values this |
290 | | key has. |
291 +-------+---------------------------------------------+
Georg Brandlba956ae2007-11-29 17:24:34 +0000292 | ``2`` | An integer giving when the key was last |
Georg Brandl116aa622007-08-15 14:28:22 +0000293 | | modified (if available) as 100's of |
294 | | nanoseconds since Jan 1, 1600. |
295 +-------+---------------------------------------------+
296
297
298.. function:: QueryValue(key, sub_key)
299
Ezio Melottibc372982010-04-25 17:48:01 +0000300 Retrieves the unnamed value for a key, as a string.
Georg Brandl116aa622007-08-15 14:28:22 +0000301
Brian Curtin2d067c82010-05-11 20:35:47 +0000302 *key* is an already open key, or one of the predefined
303 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000304
Ezio Melottibc372982010-04-25 17:48:01 +0000305 *sub_key* is a string that holds the name of the subkey with which the value is
306 associated. If this parameter is ``None`` or empty, the function retrieves the
307 value set by the :func:`SetValue` method for the key identified by *key*.
Georg Brandl116aa622007-08-15 14:28:22 +0000308
Benjamin Petersond23f8222009-04-05 19:13:16 +0000309 Values in the registry have name, type, and data components. This method
Georg Brandl116aa622007-08-15 14:28:22 +0000310 retrieves the data for a key's first value that has a NULL name. But the
Benjamin Petersond23f8222009-04-05 19:13:16 +0000311 underlying API call doesn't return the type, so always use
312 :func:`QueryValueEx` if possible.
Georg Brandl116aa622007-08-15 14:28:22 +0000313
314
315.. function:: QueryValueEx(key, value_name)
316
Ezio Melottibc372982010-04-25 17:48:01 +0000317 Retrieves the type and data for a specified value name associated with
318 an open registry key.
Georg Brandl116aa622007-08-15 14:28:22 +0000319
Brian Curtin2d067c82010-05-11 20:35:47 +0000320 *key* is an already open key, or one of the predefined
321 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000322
323 *value_name* is a string indicating the value to query.
324
325 The result is a tuple of 2 items:
326
327 +-------+-----------------------------------------+
328 | Index | Meaning |
329 +=======+=========================================+
330 | ``0`` | The value of the registry item. |
331 +-------+-----------------------------------------+
332 | ``1`` | An integer giving the registry type for |
Georg Brandl8173fb32010-05-19 21:03:51 +0000333 | | this value (see table in docs for |
334 | | :meth:`SetValueEx`) |
Georg Brandl116aa622007-08-15 14:28:22 +0000335 +-------+-----------------------------------------+
336
337
338.. function:: SaveKey(key, file_name)
339
340 Saves the specified key, and all its subkeys to the specified file.
341
Brian Curtin2d067c82010-05-11 20:35:47 +0000342 *key* is an already open key, or one of the predefined
343 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000344
Georg Brandl8173fb32010-05-19 21:03:51 +0000345 *file_name* is the name of the file to save registry data to. This file
346 cannot already exist. If this filename includes an extension, it cannot be
347 used on file allocation table (FAT) file systems by the :meth:`LoadKey`
348 method.
Georg Brandl116aa622007-08-15 14:28:22 +0000349
Ezio Melottibc372982010-04-25 17:48:01 +0000350 If *key* represents a key on a remote computer, the path described by
Georg Brandl116aa622007-08-15 14:28:22 +0000351 *file_name* is relative to the remote computer. The caller of this method must
Ezio Melottibc372982010-04-25 17:48:01 +0000352 possess the :const:`SeBackupPrivilege` security privilege. Note that
Brian Curtin2d067c82010-05-11 20:35:47 +0000353 privileges are different than permissions -- see the
354 `Conflicts Between User Rights and Permissions documentation
355 <http://msdn.microsoft.com/en-us/library/ms724878%28v=VS.85%29.aspx>`__
356 for more details.
Georg Brandl116aa622007-08-15 14:28:22 +0000357
358 This function passes NULL for *security_attributes* to the API.
359
360
361.. function:: SetValue(key, sub_key, type, value)
362
363 Associates a value with a specified key.
364
Brian Curtin2d067c82010-05-11 20:35:47 +0000365 *key* is an already open key, or one of the predefined
366 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000367
Ezio Melottibc372982010-04-25 17:48:01 +0000368 *sub_key* is a string that names the subkey with which the value is associated.
Georg Brandl116aa622007-08-15 14:28:22 +0000369
370 *type* is an integer that specifies the type of the data. Currently this must be
371 :const:`REG_SZ`, meaning only strings are supported. Use the :func:`SetValueEx`
372 function for support for other data types.
373
374 *value* is a string that specifies the new value.
375
376 If the key specified by the *sub_key* parameter does not exist, the SetValue
377 function creates it.
378
379 Value lengths are limited by available memory. Long values (more than 2048
380 bytes) should be stored as files with the filenames stored in the configuration
381 registry. This helps the registry perform efficiently.
382
Ezio Melottibc372982010-04-25 17:48:01 +0000383 The key identified by the *key* parameter must have been opened with
Georg Brandl116aa622007-08-15 14:28:22 +0000384 :const:`KEY_SET_VALUE` access.
385
386
387.. function:: SetValueEx(key, value_name, reserved, type, value)
388
389 Stores data in the value field of an open registry key.
390
Brian Curtin2d067c82010-05-11 20:35:47 +0000391 *key* is an already open key, or one of the predefined
392 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000393
Ezio Melottibc372982010-04-25 17:48:01 +0000394 *value_name* is a string that names the subkey with which the value is
Georg Brandl116aa622007-08-15 14:28:22 +0000395 associated.
396
Brian Curtin2d067c82010-05-11 20:35:47 +0000397 *type* is an integer that specifies the type of the data. See
398 :ref:`Value Types <value-types>` for the available types.
Georg Brandl116aa622007-08-15 14:28:22 +0000399
Ezio Melottibc372982010-04-25 17:48:01 +0000400 *reserved* can be anything -- zero is always passed to the API.
Georg Brandl116aa622007-08-15 14:28:22 +0000401
402 *value* is a string that specifies the new value.
403
404 This method can also set additional value and type information for the specified
405 key. The key identified by the key parameter must have been opened with
406 :const:`KEY_SET_VALUE` access.
407
Ezio Melottibc372982010-04-25 17:48:01 +0000408 To open the key, use the :func:`CreateKey` or :func:`OpenKey` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000409
410 Value lengths are limited by available memory. Long values (more than 2048
411 bytes) should be stored as files with the filenames stored in the configuration
412 registry. This helps the registry perform efficiently.
413
414
Brian Curtin3035c392010-04-21 23:56:21 +0000415.. function:: DisableReflectionKey(key)
416
417 Disables registry reflection for 32-bit processes running on a 64-bit
Georg Brandl8173fb32010-05-19 21:03:51 +0000418 operating system.
Brian Curtin3035c392010-04-21 23:56:21 +0000419
Georg Brandl8173fb32010-05-19 21:03:51 +0000420 *key* is an already open key, or one of the predefined :ref:`HKEY_* constants
421 <hkey-constants>`.
Brian Curtin3035c392010-04-21 23:56:21 +0000422
Georg Brandl8173fb32010-05-19 21:03:51 +0000423 Will generally raise :exc:`NotImplemented` if executed on a 32-bit operating
424 system.
Brian Curtin3035c392010-04-21 23:56:21 +0000425
426 If the key is not on the reflection list, the function succeeds but has no
Georg Brandl8173fb32010-05-19 21:03:51 +0000427 effect. Disabling reflection for a key does not affect reflection of any
Brian Curtin3035c392010-04-21 23:56:21 +0000428 subkeys.
429
430
431.. function:: EnableReflectionKey(key)
432
433 Restores registry reflection for the specified disabled key.
434
Georg Brandl8173fb32010-05-19 21:03:51 +0000435 *key* is an already open key, or one of the predefined :ref:`HKEY_* constants
436 <hkey-constants>`.
Brian Curtin3035c392010-04-21 23:56:21 +0000437
Georg Brandl8173fb32010-05-19 21:03:51 +0000438 Will generally raise :exc:`NotImplemented` if executed on a 32-bit operating
439 system.
Brian Curtin3035c392010-04-21 23:56:21 +0000440
441 Restoring reflection for a key does not affect reflection of any subkeys.
442
443
444.. function:: QueryReflectionKey(key)
445
446 Determines the reflection state for the specified key.
447
Brian Curtin2d067c82010-05-11 20:35:47 +0000448 *key* is an already open key, or one of the predefined
449 :ref:`HKEY_* constants <hkey-constants>`.
Brian Curtin3035c392010-04-21 23:56:21 +0000450
451 Returns ``True`` if reflection is disabled.
452
453 Will generally raise :exc:`NotImplemented` if executed on a 32-bit
Georg Brandl8173fb32010-05-19 21:03:51 +0000454 operating system.
Brian Curtin3035c392010-04-21 23:56:21 +0000455
456
Brian Curtin2d067c82010-05-11 20:35:47 +0000457.. _constants:
458
459Constants
460------------------
461
462The following constants are defined for use in many :mod:`_winreg` functions.
463
464.. _hkey-constants:
465
466HKEY_* Constants
467++++++++++++++++
468
469.. data:: HKEY_CLASSES_ROOT
470
471 Registry entries subordinate to this key define types (or classes) of
472 documents and the properties associated with those types. Shell and
473 COM applications use the information stored under this key.
474
475
476.. data:: HKEY_CURRENT_USER
477
478 Registry entries subordinate to this key define the preferences of
479 the current user. These preferences include the settings of
480 environment variables, data about program groups, colors, printers,
481 network connections, and application preferences.
482
483.. data:: HKEY_LOCAL_MACHINE
484
485 Registry entries subordinate to this key define the physical state
486 of the computer, including data about the bus type, system memory,
487 and installed hardware and software.
488
489.. data:: HKEY_USERS
490
491 Registry entries subordinate to this key define the default user
492 configuration for new users on the local computer and the user
493 configuration for the current user.
494
495.. data:: HKEY_PERFORMANCE_DATA
496
497 Registry entries subordinate to this key allow you to access
498 performance data. The data is not actually stored in the registry;
499 the registry functions cause the system to collect the data from
500 its source.
501
502
503.. data:: HKEY_CURRENT_CONFIG
504
505 Contains information about the current hardware profile of the
506 local computer system.
507
508.. data:: HKEY_DYN_DATA
509
510 This key is not used in versions of Windows after 98.
511
512
513.. _access-rights:
514
515Access Rights
516+++++++++++++
517
518For more information, see `Registry Key Security and Access
519<http://msdn.microsoft.com/en-us/library/ms724878%28v=VS.85%29.aspx>`__.
520
521.. data:: KEY_ALL_ACCESS
522
523 Combines the STANDARD_RIGHTS_REQUIRED, :const:`KEY_QUERY_VALUE`,
524 :const:`KEY_SET_VALUE`, :const:`KEY_CREATE_SUB_KEY`,
525 :const:`KEY_ENUMERATE_SUB_KEYS`, :const:`KEY_NOTIFY`,
526 and :const:`KEY_CREATE_LINK` access rights.
527
528.. data:: KEY_WRITE
529
530 Combines the STANDARD_RIGHTS_WRITE, :const:`KEY_SET_VALUE`, and
531 :const:`KEY_CREATE_SUB_KEY` access rights.
532
533.. data:: KEY_READ
534
535 Combines the STANDARD_RIGHTS_READ, :const:`KEY_QUERY_VALUE`,
536 :const:`KEY_ENUMERATE_SUB_KEYS`, and :const:`KEY_NOTIFY` values.
537
538.. data:: KEY_EXECUTE
539
540 Equivalent to :const:`KEY_READ`.
541
542.. data:: KEY_QUERY_VALUE
543
544 Required to query the values of a registry key.
545
546.. data:: KEY_SET_VALUE
547
548 Required to create, delete, or set a registry value.
549
550.. data:: KEY_CREATE_SUB_KEY
551
552 Required to create a subkey of a registry key.
553
554.. data:: KEY_ENUMERATE_SUB_KEYS
555
556 Required to enumerate the subkeys of a registry key.
557
558.. data:: KEY_NOTIFY
559
560 Required to request change notifications for a registry key or for
561 subkeys of a registry key.
562
563.. data:: KEY_CREATE_LINK
564
565 Reserved for system use.
566
567
568.. _64-bit-access-rights:
569
57064-bit Specific
571***************
572
Georg Brandl6faee4e2010-09-21 14:48:28 +0000573For more information, see `Accessing an Alternate Registry View
Brian Curtin2d067c82010-05-11 20:35:47 +0000574<http://msdn.microsoft.com/en-us/library/aa384129(v=VS.85).aspx>`__.
575
576.. data:: KEY_WOW64_64KEY
577
578 Indicates that an application on 64-bit Windows should operate on
579 the 64-bit registry view.
580
581.. data:: KEY_WOW64_32KEY
582
583 Indicates that an application on 64-bit Windows should operate on
584 the 32-bit registry view.
585
586
587.. _value-types:
588
589Value Types
590+++++++++++
591
592For more information, see `Registry Value Types
593<http://msdn.microsoft.com/en-us/library/ms724884%28v=VS.85%29.aspx>`__.
594
595.. data:: REG_BINARY
596
597 Binary data in any form.
598
599.. data:: REG_DWORD
600
601 32-bit number.
602
603.. data:: REG_DWORD_LITTLE_ENDIAN
604
605 A 32-bit number in little-endian format.
606
607.. data:: REG_DWORD_BIG_ENDIAN
608
609 A 32-bit number in big-endian format.
610
611.. data:: REG_EXPAND_SZ
612
613 Null-terminated string containing references to environment
614 variables (``%PATH%``).
615
616.. data:: REG_LINK
617
618 A Unicode symbolic link.
619
620.. data:: REG_MULTI_SZ
621
622 A sequence of null-terminated strings, terminated by two null characters.
623 (Python handles this termination automatically.)
624
625.. data:: REG_NONE
626
627 No defined value type.
628
629.. data:: REG_RESOURCE_LIST
630
631 A device-driver resource list.
632
633.. data:: REG_FULL_RESOURCE_DESCRIPTOR
634
635 A hardware setting.
636
637.. data:: REG_RESOURCE_REQUIREMENTS_LIST
638
639 A hardware resource list.
640
641.. data:: REG_SZ
642
643 A null-terminated string.
644
645
Georg Brandl116aa622007-08-15 14:28:22 +0000646.. _handle-object:
647
648Registry Handle Objects
649-----------------------
650
651This object wraps a Windows HKEY object, automatically closing it when the
652object is destroyed. To guarantee cleanup, you can call either the
Georg Brandl8173fb32010-05-19 21:03:51 +0000653:meth:`~PyHKEY.Close` method on the object, or the :func:`CloseKey` function.
Georg Brandl116aa622007-08-15 14:28:22 +0000654
655All registry functions in this module return one of these objects.
656
Ezio Melottibc372982010-04-25 17:48:01 +0000657All registry functions in this module which accept a handle object also accept
658an integer, however, use of the handle object is encouraged.
Georg Brandl116aa622007-08-15 14:28:22 +0000659
Ezio Melottibc372982010-04-25 17:48:01 +0000660Handle objects provide semantics for :meth:`__bool__` -- thus ::
Georg Brandl116aa622007-08-15 14:28:22 +0000661
662 if handle:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000663 print("Yes")
Georg Brandl116aa622007-08-15 14:28:22 +0000664
665will print ``Yes`` if the handle is currently valid (has not been closed or
666detached).
667
668The object also support comparison semantics, so handle objects will compare
669true if they both reference the same underlying Windows handle value.
670
Georg Brandl22b34312009-07-26 14:54:51 +0000671Handle objects can be converted to an integer (e.g., using the built-in
Georg Brandl116aa622007-08-15 14:28:22 +0000672:func:`int` function), in which case the underlying Windows handle value is
Georg Brandl8173fb32010-05-19 21:03:51 +0000673returned. You can also use the :meth:`~PyHKEY.Detach` method to return the
674integer handle, and also disconnect the Windows handle from the handle object.
Georg Brandl116aa622007-08-15 14:28:22 +0000675
676
677.. method:: PyHKEY.Close()
678
679 Closes the underlying Windows handle.
680
681 If the handle is already closed, no error is raised.
682
683
684.. method:: PyHKEY.Detach()
685
686 Detaches the Windows handle from the handle object.
687
Georg Brandl5c106642007-11-29 17:41:05 +0000688 The result is an integer that holds the value of the handle before it is
689 detached. If the handle is already detached or closed, this will return
690 zero.
Georg Brandl116aa622007-08-15 14:28:22 +0000691
692 After calling this function, the handle is effectively invalidated, but the
Ezio Melottibc372982010-04-25 17:48:01 +0000693 handle is not closed. You would call this function when you need the
694 underlying Win32 handle to exist beyond the lifetime of the handle object.
Georg Brandl116aa622007-08-15 14:28:22 +0000695
Christian Heimes2380ac72008-01-09 00:17:24 +0000696.. method:: PyHKEY.__enter__()
697 PyHKEY.__exit__(\*exc_info)
698
Georg Brandl8173fb32010-05-19 21:03:51 +0000699 The HKEY object implements :meth:`~object.__enter__` and
700 :meth:`~object.__exit__` and thus supports the context protocol for the
701 :keyword:`with` statement::
Christian Heimes2380ac72008-01-09 00:17:24 +0000702
703 with OpenKey(HKEY_LOCAL_MACHINE, "foo") as key:
Georg Brandl8173fb32010-05-19 21:03:51 +0000704 ... # work with key
Christian Heimes2380ac72008-01-09 00:17:24 +0000705
706 will automatically close *key* when control leaves the :keyword:`with` block.
707
Christian Heimes2380ac72008-01-09 00:17:24 +0000708