blob: fa8e3681f3d794e7a0d56f8b50a618c984e40f0d [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 Curtind8559482010-04-24 17:21:31 +000063.. function:: CreateKeyEx(key, sub_key[, res[, sam]])
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 Curtind8559482010-04-24 17:21:31 +0000106.. function:: DeleteKeyEx(key, sub_key[, sam[, res]])
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 Curtind8559482010-04-24 17:21:31 +0000246.. function:: OpenKey(key, sub_key[, res[, sam]])
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
265
266.. function:: OpenKeyEx()
267
Ezio Melottibc372982010-04-25 17:48:01 +0000268 The functionality of :func:`OpenKeyEx` is provided via :func:`OpenKey`,
269 by the use of default arguments.
Georg Brandl116aa622007-08-15 14:28:22 +0000270
271
272.. function:: QueryInfoKey(key)
273
274 Returns information about a key, as a tuple.
275
Brian Curtin2d067c82010-05-11 20:35:47 +0000276 *key* is an already open key, or one of the predefined
277 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000278
279 The result is a tuple of 3 items:
280
281 +-------+---------------------------------------------+
282 | Index | Meaning |
283 +=======+=============================================+
284 | ``0`` | An integer giving the number of sub keys |
285 | | this key has. |
286 +-------+---------------------------------------------+
287 | ``1`` | An integer giving the number of values this |
288 | | key has. |
289 +-------+---------------------------------------------+
Georg Brandlba956ae2007-11-29 17:24:34 +0000290 | ``2`` | An integer giving when the key was last |
Georg Brandl116aa622007-08-15 14:28:22 +0000291 | | modified (if available) as 100's of |
292 | | nanoseconds since Jan 1, 1600. |
293 +-------+---------------------------------------------+
294
295
296.. function:: QueryValue(key, sub_key)
297
Ezio Melottibc372982010-04-25 17:48:01 +0000298 Retrieves the unnamed value for a key, as a string.
Georg Brandl116aa622007-08-15 14:28:22 +0000299
Brian Curtin2d067c82010-05-11 20:35:47 +0000300 *key* is an already open key, or one of the predefined
301 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000302
Ezio Melottibc372982010-04-25 17:48:01 +0000303 *sub_key* is a string that holds the name of the subkey with which the value is
304 associated. If this parameter is ``None`` or empty, the function retrieves the
305 value set by the :func:`SetValue` method for the key identified by *key*.
Georg Brandl116aa622007-08-15 14:28:22 +0000306
Benjamin Petersond23f8222009-04-05 19:13:16 +0000307 Values in the registry have name, type, and data components. This method
Georg Brandl116aa622007-08-15 14:28:22 +0000308 retrieves the data for a key's first value that has a NULL name. But the
Benjamin Petersond23f8222009-04-05 19:13:16 +0000309 underlying API call doesn't return the type, so always use
310 :func:`QueryValueEx` if possible.
Georg Brandl116aa622007-08-15 14:28:22 +0000311
312
313.. function:: QueryValueEx(key, value_name)
314
Ezio Melottibc372982010-04-25 17:48:01 +0000315 Retrieves the type and data for a specified value name associated with
316 an open registry key.
Georg Brandl116aa622007-08-15 14:28:22 +0000317
Brian Curtin2d067c82010-05-11 20:35:47 +0000318 *key* is an already open key, or one of the predefined
319 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000320
321 *value_name* is a string indicating the value to query.
322
323 The result is a tuple of 2 items:
324
325 +-------+-----------------------------------------+
326 | Index | Meaning |
327 +=======+=========================================+
328 | ``0`` | The value of the registry item. |
329 +-------+-----------------------------------------+
330 | ``1`` | An integer giving the registry type for |
Georg Brandl8173fb32010-05-19 21:03:51 +0000331 | | this value (see table in docs for |
332 | | :meth:`SetValueEx`) |
Georg Brandl116aa622007-08-15 14:28:22 +0000333 +-------+-----------------------------------------+
334
335
336.. function:: SaveKey(key, file_name)
337
338 Saves the specified key, and all its subkeys to the specified file.
339
Brian Curtin2d067c82010-05-11 20:35:47 +0000340 *key* is an already open key, or one of the predefined
341 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000342
Georg Brandl8173fb32010-05-19 21:03:51 +0000343 *file_name* is the name of the file to save registry data to. This file
344 cannot already exist. If this filename includes an extension, it cannot be
345 used on file allocation table (FAT) file systems by the :meth:`LoadKey`
346 method.
Georg Brandl116aa622007-08-15 14:28:22 +0000347
Ezio Melottibc372982010-04-25 17:48:01 +0000348 If *key* represents a key on a remote computer, the path described by
Georg Brandl116aa622007-08-15 14:28:22 +0000349 *file_name* is relative to the remote computer. The caller of this method must
Ezio Melottibc372982010-04-25 17:48:01 +0000350 possess the :const:`SeBackupPrivilege` security privilege. Note that
Brian Curtin2d067c82010-05-11 20:35:47 +0000351 privileges are different than permissions -- see the
352 `Conflicts Between User Rights and Permissions documentation
353 <http://msdn.microsoft.com/en-us/library/ms724878%28v=VS.85%29.aspx>`__
354 for more details.
Georg Brandl116aa622007-08-15 14:28:22 +0000355
356 This function passes NULL for *security_attributes* to the API.
357
358
359.. function:: SetValue(key, sub_key, type, value)
360
361 Associates a value with a specified key.
362
Brian Curtin2d067c82010-05-11 20:35:47 +0000363 *key* is an already open key, or one of the predefined
364 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000365
Ezio Melottibc372982010-04-25 17:48:01 +0000366 *sub_key* is a string that names the subkey with which the value is associated.
Georg Brandl116aa622007-08-15 14:28:22 +0000367
368 *type* is an integer that specifies the type of the data. Currently this must be
369 :const:`REG_SZ`, meaning only strings are supported. Use the :func:`SetValueEx`
370 function for support for other data types.
371
372 *value* is a string that specifies the new value.
373
374 If the key specified by the *sub_key* parameter does not exist, the SetValue
375 function creates it.
376
377 Value lengths are limited by available memory. Long values (more than 2048
378 bytes) should be stored as files with the filenames stored in the configuration
379 registry. This helps the registry perform efficiently.
380
Ezio Melottibc372982010-04-25 17:48:01 +0000381 The key identified by the *key* parameter must have been opened with
Georg Brandl116aa622007-08-15 14:28:22 +0000382 :const:`KEY_SET_VALUE` access.
383
384
385.. function:: SetValueEx(key, value_name, reserved, type, value)
386
387 Stores data in the value field of an open registry key.
388
Brian Curtin2d067c82010-05-11 20:35:47 +0000389 *key* is an already open key, or one of the predefined
390 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000391
Ezio Melottibc372982010-04-25 17:48:01 +0000392 *value_name* is a string that names the subkey with which the value is
Georg Brandl116aa622007-08-15 14:28:22 +0000393 associated.
394
Brian Curtin2d067c82010-05-11 20:35:47 +0000395 *type* is an integer that specifies the type of the data. See
396 :ref:`Value Types <value-types>` for the available types.
Georg Brandl116aa622007-08-15 14:28:22 +0000397
Ezio Melottibc372982010-04-25 17:48:01 +0000398 *reserved* can be anything -- zero is always passed to the API.
Georg Brandl116aa622007-08-15 14:28:22 +0000399
400 *value* is a string that specifies the new value.
401
402 This method can also set additional value and type information for the specified
403 key. The key identified by the key parameter must have been opened with
404 :const:`KEY_SET_VALUE` access.
405
Ezio Melottibc372982010-04-25 17:48:01 +0000406 To open the key, use the :func:`CreateKey` or :func:`OpenKey` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000407
408 Value lengths are limited by available memory. Long values (more than 2048
409 bytes) should be stored as files with the filenames stored in the configuration
410 registry. This helps the registry perform efficiently.
411
412
Brian Curtin3035c392010-04-21 23:56:21 +0000413.. function:: DisableReflectionKey(key)
414
415 Disables registry reflection for 32-bit processes running on a 64-bit
Georg Brandl8173fb32010-05-19 21:03:51 +0000416 operating system.
Brian Curtin3035c392010-04-21 23:56:21 +0000417
Georg Brandl8173fb32010-05-19 21:03:51 +0000418 *key* is an already open key, or one of the predefined :ref:`HKEY_* constants
419 <hkey-constants>`.
Brian Curtin3035c392010-04-21 23:56:21 +0000420
Georg Brandl8173fb32010-05-19 21:03:51 +0000421 Will generally raise :exc:`NotImplemented` if executed on a 32-bit operating
422 system.
Brian Curtin3035c392010-04-21 23:56:21 +0000423
424 If the key is not on the reflection list, the function succeeds but has no
Georg Brandl8173fb32010-05-19 21:03:51 +0000425 effect. Disabling reflection for a key does not affect reflection of any
Brian Curtin3035c392010-04-21 23:56:21 +0000426 subkeys.
427
428
429.. function:: EnableReflectionKey(key)
430
431 Restores registry reflection for the specified disabled key.
432
Georg Brandl8173fb32010-05-19 21:03:51 +0000433 *key* is an already open key, or one of the predefined :ref:`HKEY_* constants
434 <hkey-constants>`.
Brian Curtin3035c392010-04-21 23:56:21 +0000435
Georg Brandl8173fb32010-05-19 21:03:51 +0000436 Will generally raise :exc:`NotImplemented` if executed on a 32-bit operating
437 system.
Brian Curtin3035c392010-04-21 23:56:21 +0000438
439 Restoring reflection for a key does not affect reflection of any subkeys.
440
441
442.. function:: QueryReflectionKey(key)
443
444 Determines the reflection state for the specified key.
445
Brian Curtin2d067c82010-05-11 20:35:47 +0000446 *key* is an already open key, or one of the predefined
447 :ref:`HKEY_* constants <hkey-constants>`.
Brian Curtin3035c392010-04-21 23:56:21 +0000448
449 Returns ``True`` if reflection is disabled.
450
451 Will generally raise :exc:`NotImplemented` if executed on a 32-bit
Georg Brandl8173fb32010-05-19 21:03:51 +0000452 operating system.
Brian Curtin3035c392010-04-21 23:56:21 +0000453
454
Brian Curtin2d067c82010-05-11 20:35:47 +0000455.. _constants:
456
457Constants
458------------------
459
460The following constants are defined for use in many :mod:`_winreg` functions.
461
462.. _hkey-constants:
463
464HKEY_* Constants
465++++++++++++++++
466
467.. data:: HKEY_CLASSES_ROOT
468
469 Registry entries subordinate to this key define types (or classes) of
470 documents and the properties associated with those types. Shell and
471 COM applications use the information stored under this key.
472
473
474.. data:: HKEY_CURRENT_USER
475
476 Registry entries subordinate to this key define the preferences of
477 the current user. These preferences include the settings of
478 environment variables, data about program groups, colors, printers,
479 network connections, and application preferences.
480
481.. data:: HKEY_LOCAL_MACHINE
482
483 Registry entries subordinate to this key define the physical state
484 of the computer, including data about the bus type, system memory,
485 and installed hardware and software.
486
487.. data:: HKEY_USERS
488
489 Registry entries subordinate to this key define the default user
490 configuration for new users on the local computer and the user
491 configuration for the current user.
492
493.. data:: HKEY_PERFORMANCE_DATA
494
495 Registry entries subordinate to this key allow you to access
496 performance data. The data is not actually stored in the registry;
497 the registry functions cause the system to collect the data from
498 its source.
499
500
501.. data:: HKEY_CURRENT_CONFIG
502
503 Contains information about the current hardware profile of the
504 local computer system.
505
506.. data:: HKEY_DYN_DATA
507
508 This key is not used in versions of Windows after 98.
509
510
511.. _access-rights:
512
513Access Rights
514+++++++++++++
515
516For more information, see `Registry Key Security and Access
517<http://msdn.microsoft.com/en-us/library/ms724878%28v=VS.85%29.aspx>`__.
518
519.. data:: KEY_ALL_ACCESS
520
521 Combines the STANDARD_RIGHTS_REQUIRED, :const:`KEY_QUERY_VALUE`,
522 :const:`KEY_SET_VALUE`, :const:`KEY_CREATE_SUB_KEY`,
523 :const:`KEY_ENUMERATE_SUB_KEYS`, :const:`KEY_NOTIFY`,
524 and :const:`KEY_CREATE_LINK` access rights.
525
526.. data:: KEY_WRITE
527
528 Combines the STANDARD_RIGHTS_WRITE, :const:`KEY_SET_VALUE`, and
529 :const:`KEY_CREATE_SUB_KEY` access rights.
530
531.. data:: KEY_READ
532
533 Combines the STANDARD_RIGHTS_READ, :const:`KEY_QUERY_VALUE`,
534 :const:`KEY_ENUMERATE_SUB_KEYS`, and :const:`KEY_NOTIFY` values.
535
536.. data:: KEY_EXECUTE
537
538 Equivalent to :const:`KEY_READ`.
539
540.. data:: KEY_QUERY_VALUE
541
542 Required to query the values of a registry key.
543
544.. data:: KEY_SET_VALUE
545
546 Required to create, delete, or set a registry value.
547
548.. data:: KEY_CREATE_SUB_KEY
549
550 Required to create a subkey of a registry key.
551
552.. data:: KEY_ENUMERATE_SUB_KEYS
553
554 Required to enumerate the subkeys of a registry key.
555
556.. data:: KEY_NOTIFY
557
558 Required to request change notifications for a registry key or for
559 subkeys of a registry key.
560
561.. data:: KEY_CREATE_LINK
562
563 Reserved for system use.
564
565
566.. _64-bit-access-rights:
567
56864-bit Specific
569***************
570
571For more information, see `Accesing an Alternate Registry View
572<http://msdn.microsoft.com/en-us/library/aa384129(v=VS.85).aspx>`__.
573
574.. data:: KEY_WOW64_64KEY
575
576 Indicates that an application on 64-bit Windows should operate on
577 the 64-bit registry view.
578
579.. data:: KEY_WOW64_32KEY
580
581 Indicates that an application on 64-bit Windows should operate on
582 the 32-bit registry view.
583
584
585.. _value-types:
586
587Value Types
588+++++++++++
589
590For more information, see `Registry Value Types
591<http://msdn.microsoft.com/en-us/library/ms724884%28v=VS.85%29.aspx>`__.
592
593.. data:: REG_BINARY
594
595 Binary data in any form.
596
597.. data:: REG_DWORD
598
599 32-bit number.
600
601.. data:: REG_DWORD_LITTLE_ENDIAN
602
603 A 32-bit number in little-endian format.
604
605.. data:: REG_DWORD_BIG_ENDIAN
606
607 A 32-bit number in big-endian format.
608
609.. data:: REG_EXPAND_SZ
610
611 Null-terminated string containing references to environment
612 variables (``%PATH%``).
613
614.. data:: REG_LINK
615
616 A Unicode symbolic link.
617
618.. data:: REG_MULTI_SZ
619
620 A sequence of null-terminated strings, terminated by two null characters.
621 (Python handles this termination automatically.)
622
623.. data:: REG_NONE
624
625 No defined value type.
626
627.. data:: REG_RESOURCE_LIST
628
629 A device-driver resource list.
630
631.. data:: REG_FULL_RESOURCE_DESCRIPTOR
632
633 A hardware setting.
634
635.. data:: REG_RESOURCE_REQUIREMENTS_LIST
636
637 A hardware resource list.
638
639.. data:: REG_SZ
640
641 A null-terminated string.
642
643
Georg Brandl116aa622007-08-15 14:28:22 +0000644.. _handle-object:
645
646Registry Handle Objects
647-----------------------
648
649This object wraps a Windows HKEY object, automatically closing it when the
650object is destroyed. To guarantee cleanup, you can call either the
Georg Brandl8173fb32010-05-19 21:03:51 +0000651:meth:`~PyHKEY.Close` method on the object, or the :func:`CloseKey` function.
Georg Brandl116aa622007-08-15 14:28:22 +0000652
653All registry functions in this module return one of these objects.
654
Ezio Melottibc372982010-04-25 17:48:01 +0000655All registry functions in this module which accept a handle object also accept
656an integer, however, use of the handle object is encouraged.
Georg Brandl116aa622007-08-15 14:28:22 +0000657
Ezio Melottibc372982010-04-25 17:48:01 +0000658Handle objects provide semantics for :meth:`__bool__` -- thus ::
Georg Brandl116aa622007-08-15 14:28:22 +0000659
660 if handle:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000661 print("Yes")
Georg Brandl116aa622007-08-15 14:28:22 +0000662
663will print ``Yes`` if the handle is currently valid (has not been closed or
664detached).
665
666The object also support comparison semantics, so handle objects will compare
667true if they both reference the same underlying Windows handle value.
668
Georg Brandl22b34312009-07-26 14:54:51 +0000669Handle objects can be converted to an integer (e.g., using the built-in
Georg Brandl116aa622007-08-15 14:28:22 +0000670:func:`int` function), in which case the underlying Windows handle value is
Georg Brandl8173fb32010-05-19 21:03:51 +0000671returned. You can also use the :meth:`~PyHKEY.Detach` method to return the
672integer handle, and also disconnect the Windows handle from the handle object.
Georg Brandl116aa622007-08-15 14:28:22 +0000673
674
675.. method:: PyHKEY.Close()
676
677 Closes the underlying Windows handle.
678
679 If the handle is already closed, no error is raised.
680
681
682.. method:: PyHKEY.Detach()
683
684 Detaches the Windows handle from the handle object.
685
Georg Brandl5c106642007-11-29 17:41:05 +0000686 The result is an integer that holds the value of the handle before it is
687 detached. If the handle is already detached or closed, this will return
688 zero.
Georg Brandl116aa622007-08-15 14:28:22 +0000689
690 After calling this function, the handle is effectively invalidated, but the
Ezio Melottibc372982010-04-25 17:48:01 +0000691 handle is not closed. You would call this function when you need the
692 underlying Win32 handle to exist beyond the lifetime of the handle object.
Georg Brandl116aa622007-08-15 14:28:22 +0000693
Christian Heimes2380ac72008-01-09 00:17:24 +0000694.. method:: PyHKEY.__enter__()
695 PyHKEY.__exit__(\*exc_info)
696
Georg Brandl8173fb32010-05-19 21:03:51 +0000697 The HKEY object implements :meth:`~object.__enter__` and
698 :meth:`~object.__exit__` and thus supports the context protocol for the
699 :keyword:`with` statement::
Christian Heimes2380ac72008-01-09 00:17:24 +0000700
701 with OpenKey(HKEY_LOCAL_MACHINE, "foo") as key:
Georg Brandl8173fb32010-05-19 21:03:51 +0000702 ... # work with key
Christian Heimes2380ac72008-01-09 00:17:24 +0000703
704 will automatically close *key* when control leaves the :keyword:`with` block.
705
Christian Heimes2380ac72008-01-09 00:17:24 +0000706