blob: 825ce1fb2e13e84eb54a9b88b42d0997eebdd6c6 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001:mod:`_winreg` -- Windows registry access
2=========================================
3
4.. module:: _winreg
5 :platform: Windows
6 :synopsis: Routines and objects for manipulating the Windows registry.
7.. sectionauthor:: Mark Hammond <MarkH@ActiveState.com>
8
Georg Brandlecd0ad32008-05-25 07:46:33 +00009.. note::
10 The :mod:`_winreg` module has been renamed to :mod:`winreg` in Python 3.0.
11 The :term:`2to3` tool will automatically adapt imports when converting your
12 sources to 3.0.
13
Georg Brandl8ec7f652007-08-15 14:28:01 +000014
15.. versionadded:: 2.0
16
17These functions expose the Windows registry API to Python. Instead of using an
Georg Brandl9bfb78d2010-04-25 10:54:42 +000018integer as the registry handle, a :ref:`handle object <handle-object>` is used
19to ensure that the handles are closed correctly, even if the programmer neglects
20to explicitly close them.
Georg Brandl8ec7f652007-08-15 14:28:01 +000021
Georg Brandl8ec7f652007-08-15 14:28:01 +000022This module offers the following functions:
23
24
25.. function:: CloseKey(hkey)
26
Georg Brandlbb091e72010-04-25 10:55:58 +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 Curtinb9bf9712010-05-11 19:13:13 +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 Melotti01fa86a2010-04-05 08:02:54 +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 Melotti01fa86a2010-04-05 08:02:54 +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 Brandlb945bbf2009-03-31 16:31:11 +000045 The return value is the handle of the opened key. If the function fails, a
Ezio Melotti01fa86a2010-04-05 08:02:54 +000046 :exc:`WindowsError` exception is raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +000047
48
49.. function:: CreateKey(key, sub_key)
50
Ezio Melotti01fa86a2010-04-05 08:02:54 +000051 Creates or opens the specified key, returning a
52 :ref:`handle object <handle-object>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000053
Brian Curtinb9bf9712010-05-11 19:13:13 +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 Melotti01fa86a2010-04-05 08:02:54 +000057 *sub_key* is a string that names the key this method opens or creates.
Georg Brandl8ec7f652007-08-15 14:28:01 +000058
Ezio Melotti01fa86a2010-04-05 08:02:54 +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 Brandlb945bbf2009-03-31 16:31:11 +000064 The return value is the handle of the opened key. If the function fails, a
Ezio Melotti01fa86a2010-04-05 08:02:54 +000065 :exc:`WindowsError` exception is raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +000066
67
Brian Curtin5fa9fb42010-04-24 17:10:22 +000068.. function:: CreateKeyEx(key, sub_key[, res[, sam]])
Brian Curtine33fa882010-04-02 21:18:14 +000069
Ezio Melotti01fa86a2010-04-05 08:02:54 +000070 Creates or opens the specified key, returning a
71 :ref:`handle object <handle-object>`.
Brian Curtine33fa882010-04-02 21:18:14 +000072
Brian Curtinb9bf9712010-05-11 19:13:13 +000073 *key* is an already open key, or one of the predefined
74 :ref:`HKEY_* constants <hkey-constants>`.
Brian Curtine33fa882010-04-02 21:18:14 +000075
76 *sub_key* is a string that names the key this method opens or creates.
77
78 *res* is a reserved integer, and must be zero. The default is zero.
79
80 *sam* is an integer that specifies an access mask that describes the desired
Brian Curtinb9bf9712010-05-11 19:13:13 +000081 security access for the key. Default is :const:`KEY_ALL_ACCESS`. See
82 :ref:`Access Rights <access-rights>` for other allowed values.
Georg Brandl9bfb78d2010-04-25 10:54:42 +000083
Brian Curtine33fa882010-04-02 21:18:14 +000084
Ezio Melotti01fa86a2010-04-05 08:02:54 +000085 If *key* is one of the predefined keys, *sub_key* may be ``None``. In that
86 case, the handle returned is the same key handle passed in to the function.
Brian Curtine33fa882010-04-02 21:18:14 +000087
88 If the key already exists, this function opens the existing key.
89
90 The return value is the handle of the opened key. If the function fails, a
91 :exc:`WindowsError` exception is raised.
92
93.. versionadded:: 2.7
94
95
Georg Brandl8ec7f652007-08-15 14:28:01 +000096.. function:: DeleteKey(key, sub_key)
97
98 Deletes the specified key.
99
Brian Curtinb9bf9712010-05-11 19:13:13 +0000100 *key* is an already open key, or any one of the predefined
101 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000102
Brian Curtine33fa882010-04-02 21:18:14 +0000103 *sub_key* is a string that must be a subkey of the key identified by the *key*
104 parameter. This value must not be ``None``, and the key may not have subkeys.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000105
106 *This method can not delete keys with subkeys.*
107
108 If the method succeeds, the entire key, including all of its values, is removed.
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000109 If the method fails, a :exc:`WindowsError` exception is raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000110
111
Brian Curtin5fa9fb42010-04-24 17:10:22 +0000112.. function:: DeleteKeyEx(key, sub_key[, sam[, res]])
Brian Curtine33fa882010-04-02 21:18:14 +0000113
114 Deletes the specified key.
115
116 .. note::
117 The :func:`DeleteKeyEx` function is implemented with the RegDeleteKeyEx
118 Windows API function, which is specific to 64-bit versions of Windows.
Brian Curtinb9bf9712010-05-11 19:13:13 +0000119 See the `RegDeleteKeyEx documentation
120 <http://msdn.microsoft.com/en-us/library/ms724847%28VS.85%29.aspx>`__.
Brian Curtine33fa882010-04-02 21:18:14 +0000121
Brian Curtinb9bf9712010-05-11 19:13:13 +0000122 *key* is an already open key, or any one of the predefined
123 :ref:`HKEY_* constants <hkey-constants>`.
Brian Curtine33fa882010-04-02 21:18:14 +0000124
125 *sub_key* is a string that must be a subkey of the key identified by the
126 *key* parameter. This value must not be ``None``, and the key may not have
127 subkeys.
128
129 *res* is a reserved integer, and must be zero. The default is zero.
130
Georg Brandl9bfb78d2010-04-25 10:54:42 +0000131 *sam* is an integer that specifies an access mask that describes the desired
Brian Curtinb9bf9712010-05-11 19:13:13 +0000132 security access for the key. Default is :const:`KEY_WOW64_64KEY`. See
133 :ref:`Access Rights <access-rights>` for other allowed values.
Georg Brandl9bfb78d2010-04-25 10:54:42 +0000134
Brian Curtine33fa882010-04-02 21:18:14 +0000135
136 *This method can not delete keys with subkeys.*
137
138 If the method succeeds, the entire key, including all of its values, is
139 removed. If the method fails, a :exc:`WindowsError` exception is raised.
140
141 On unsupported Windows versions, :exc:`NotImplementedError` is raised.
142
143.. versionadded:: 2.7
144
145
Georg Brandl8ec7f652007-08-15 14:28:01 +0000146.. function:: DeleteValue(key, value)
147
148 Removes a named value from a registry key.
149
Brian Curtinb9bf9712010-05-11 19:13:13 +0000150 *key* is an already open key, or one of the predefined
151 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000152
153 *value* is a string that identifies the value to remove.
154
155
156.. function:: EnumKey(key, index)
157
158 Enumerates subkeys of an open registry key, returning a string.
159
Brian Curtinb9bf9712010-05-11 19:13:13 +0000160 *key* is an already open key, or any one of the predefined
161 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000162
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000163 *index* is an integer that identifies the index of the key to retrieve.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000164
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000165 The function retrieves the name of one subkey each time it is called. It is
166 typically called repeatedly until a :exc:`WindowsError` exception is
Georg Brandl8ec7f652007-08-15 14:28:01 +0000167 raised, indicating, no more values are available.
168
169
170.. function:: EnumValue(key, index)
171
172 Enumerates values of an open registry key, returning a tuple.
173
Brian Curtinb9bf9712010-05-11 19:13:13 +0000174 *key* is an already open key, or any one of the predefined
175 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000176
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000177 *index* is an integer that identifies the index of the value to retrieve.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000178
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000179 The function retrieves the name of one subkey each time it is called. It is
180 typically called repeatedly, until a :exc:`WindowsError` exception is
181 raised, indicating no more values.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000182
183 The result is a tuple of 3 items:
184
185 +-------+--------------------------------------------+
186 | Index | Meaning |
187 +=======+============================================+
188 | ``0`` | A string that identifies the value name |
189 +-------+--------------------------------------------+
190 | ``1`` | An object that holds the value data, and |
191 | | whose type depends on the underlying |
192 | | registry type |
193 +-------+--------------------------------------------+
194 | ``2`` | An integer that identifies the type of the |
Georg Brandl9bfb78d2010-04-25 10:54:42 +0000195 | | value data (see table in docs for |
196 | | :meth:`SetValueEx`) |
Georg Brandl8ec7f652007-08-15 14:28:01 +0000197 +-------+--------------------------------------------+
198
199
Christian Heimesb39a7562008-01-08 15:46:10 +0000200.. function:: ExpandEnvironmentStrings(unicode)
201
Georg Brandl9bfb78d2010-04-25 10:54:42 +0000202 Expands environment variable placeholders ``%NAME%`` in unicode strings like
203 :const:`REG_EXPAND_SZ`::
Georg Brandl502d6312008-01-08 16:18:26 +0000204
205 >>> ExpandEnvironmentStrings(u"%windir%")
206 u"C:\\Windows"
207
208 .. versionadded:: 2.6
Christian Heimesb39a7562008-01-08 15:46:10 +0000209
210
Georg Brandl8ec7f652007-08-15 14:28:01 +0000211.. function:: FlushKey(key)
212
213 Writes all the attributes of a key to the registry.
214
Brian Curtinb9bf9712010-05-11 19:13:13 +0000215 *key* is an already open key, or one of the predefined
216 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000217
Georg Brandl51174092008-05-09 06:10:43 +0000218 It is not necessary to call :func:`FlushKey` to change a key. Registry changes are
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000219 flushed to disk by the registry using its lazy flusher. Registry changes are
220 also flushed to disk at system shutdown. Unlike :func:`CloseKey`, the
221 :func:`FlushKey` method returns only when all the data has been written to the
Georg Brandl8ec7f652007-08-15 14:28:01 +0000222 registry. An application should only call :func:`FlushKey` if it requires
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000223 absolute certainty that registry changes are on disk.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000224
225 .. note::
226
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000227 If you don't know whether a :func:`FlushKey` call is required, it probably
Georg Brandl8ec7f652007-08-15 14:28:01 +0000228 isn't.
229
230
Georg Brandl51174092008-05-09 06:10:43 +0000231.. function:: LoadKey(key, sub_key, file_name)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000232
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000233 Creates a subkey under the specified key and stores registration information
Georg Brandl8ec7f652007-08-15 14:28:01 +0000234 from a specified file into that subkey.
235
Georg Brandl9bfb78d2010-04-25 10:54:42 +0000236 *key* is a handle returned by :func:`ConnectRegistry` or one of the constants
Brian Curtinb9bf9712010-05-11 19:13:13 +0000237 :const:`HKEY_USERS` or :const:`HKEY_LOCAL_MACHINE`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000238
Georg Brandl9bfb78d2010-04-25 10:54:42 +0000239 *sub_key* is a string that identifies the subkey to load.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000240
241 *file_name* is the name of the file to load registry data from. This file must
242 have been created with the :func:`SaveKey` function. Under the file allocation
243 table (FAT) file system, the filename may not have an extension.
244
Georg Brandl9bfb78d2010-04-25 10:54:42 +0000245 A call to :func:`LoadKey` fails if the calling process does not have the
246 :const:`SE_RESTORE_PRIVILEGE` privilege. Note that privileges are different
Brian Curtinb9bf9712010-05-11 19:13:13 +0000247 from permissions -- see the `RegLoadKey documentation
Andrew M. Kuchlinga14f2d12010-05-06 13:03:39 +0000248 <http://msdn.microsoft.com/en-us/library/ms724889%28v=VS.85%29.aspx>`__ for
Georg Brandl9bfb78d2010-04-25 10:54:42 +0000249 more details.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000250
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000251 If *key* is a handle returned by :func:`ConnectRegistry`, then the path
Georg Brandl9bfb78d2010-04-25 10:54:42 +0000252 specified in *file_name* is relative to the remote computer.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000253
254
Brian Curtin5fa9fb42010-04-24 17:10:22 +0000255.. function:: OpenKey(key, sub_key[, res[, sam]])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000256
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000257 Opens the specified key, returning a :ref:`handle object <handle-object>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000258
Brian Curtinb9bf9712010-05-11 19:13:13 +0000259 *key* is an already open key, or any one of the predefined
260 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000261
262 *sub_key* is a string that identifies the sub_key to open.
263
264 *res* is a reserved integer, and must be zero. The default is zero.
265
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000266 *sam* is an integer that specifies an access mask that describes the desired
Brian Curtinb9bf9712010-05-11 19:13:13 +0000267 security access for the key. Default is :const:`KEY_READ`. See
268 :ref:`Access Rights <access-rights>` for other allowed values.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000269
270 The result is a new handle to the specified key.
271
Georg Brandlb945bbf2009-03-31 16:31:11 +0000272 If the function fails, :exc:`WindowsError` is raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000273
274
275.. function:: OpenKeyEx()
276
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000277 The functionality of :func:`OpenKeyEx` is provided via :func:`OpenKey`,
278 by the use of default arguments.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000279
280
281.. function:: QueryInfoKey(key)
282
283 Returns information about a key, as a tuple.
284
Brian Curtinb9bf9712010-05-11 19:13:13 +0000285 *key* is an already open key, or one of the predefined
286 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000287
288 The result is a tuple of 3 items:
289
290 +-------+---------------------------------------------+
291 | Index | Meaning |
292 +=======+=============================================+
293 | ``0`` | An integer giving the number of sub keys |
294 | | this key has. |
295 +-------+---------------------------------------------+
296 | ``1`` | An integer giving the number of values this |
297 | | key has. |
298 +-------+---------------------------------------------+
299 | ``2`` | A long integer giving when the key was last |
300 | | modified (if available) as 100's of |
301 | | nanoseconds since Jan 1, 1600. |
302 +-------+---------------------------------------------+
303
304
305.. function:: QueryValue(key, sub_key)
306
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000307 Retrieves the unnamed value for a key, as a string.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000308
Brian Curtinb9bf9712010-05-11 19:13:13 +0000309 *key* is an already open key, or one of the predefined
310 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000311
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000312 *sub_key* is a string that holds the name of the subkey with which the value is
313 associated. If this parameter is ``None`` or empty, the function retrieves the
314 value set by the :func:`SetValue` method for the key identified by *key*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000315
Georg Brandl75f11072009-04-05 10:32:26 +0000316 Values in the registry have name, type, and data components. This method
Georg Brandl8ec7f652007-08-15 14:28:01 +0000317 retrieves the data for a key's first value that has a NULL name. But the
Georg Brandl75f11072009-04-05 10:32:26 +0000318 underlying API call doesn't return the type, so always use
319 :func:`QueryValueEx` if possible.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000320
321
322.. function:: QueryValueEx(key, value_name)
323
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000324 Retrieves the type and data for a specified value name associated with
325 an open registry key.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000326
Brian Curtinb9bf9712010-05-11 19:13:13 +0000327 *key* is an already open key, or one of the predefined
328 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000329
330 *value_name* is a string indicating the value to query.
331
332 The result is a tuple of 2 items:
333
334 +-------+-----------------------------------------+
335 | Index | Meaning |
336 +=======+=========================================+
337 | ``0`` | The value of the registry item. |
338 +-------+-----------------------------------------+
339 | ``1`` | An integer giving the registry type for |
Georg Brandl9bfb78d2010-04-25 10:54:42 +0000340 | | this value (see table in docs for |
341 | | :meth:`SetValueEx`) |
Georg Brandl8ec7f652007-08-15 14:28:01 +0000342 +-------+-----------------------------------------+
343
344
345.. function:: SaveKey(key, file_name)
346
347 Saves the specified key, and all its subkeys to the specified file.
348
Brian Curtinb9bf9712010-05-11 19:13:13 +0000349 *key* is an already open key, or one of the predefined
350 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000351
Georg Brandl9bfb78d2010-04-25 10:54:42 +0000352 *file_name* is the name of the file to save registry data to. This file
353 cannot already exist. If this filename includes an extension, it cannot be
354 used on file allocation table (FAT) file systems by the :meth:`LoadKey`
355 method.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000356
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000357 If *key* represents a key on a remote computer, the path described by
Georg Brandl8ec7f652007-08-15 14:28:01 +0000358 *file_name* is relative to the remote computer. The caller of this method must
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000359 possess the :const:`SeBackupPrivilege` security privilege. Note that
Brian Curtinb9bf9712010-05-11 19:13:13 +0000360 privileges are different than permissions -- see the
361 `Conflicts Between User Rights and Permissions documentation
362 <http://msdn.microsoft.com/en-us/library/ms724878%28v=VS.85%29.aspx>`__
363 for more details.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000364
365 This function passes NULL for *security_attributes* to the API.
366
367
368.. function:: SetValue(key, sub_key, type, value)
369
370 Associates a value with a specified key.
371
Brian Curtinb9bf9712010-05-11 19:13:13 +0000372 *key* is an already open key, or one of the predefined
373 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000374
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000375 *sub_key* is a string that names the subkey with which the value is associated.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000376
377 *type* is an integer that specifies the type of the data. Currently this must be
378 :const:`REG_SZ`, meaning only strings are supported. Use the :func:`SetValueEx`
379 function for support for other data types.
380
381 *value* is a string that specifies the new value.
382
383 If the key specified by the *sub_key* parameter does not exist, the SetValue
384 function creates it.
385
386 Value lengths are limited by available memory. Long values (more than 2048
387 bytes) should be stored as files with the filenames stored in the configuration
388 registry. This helps the registry perform efficiently.
389
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000390 The key identified by the *key* parameter must have been opened with
Georg Brandl8ec7f652007-08-15 14:28:01 +0000391 :const:`KEY_SET_VALUE` access.
392
393
394.. function:: SetValueEx(key, value_name, reserved, type, value)
395
396 Stores data in the value field of an open registry key.
397
Brian Curtinb9bf9712010-05-11 19:13:13 +0000398 *key* is an already open key, or one of the predefined
399 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000400
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000401 *value_name* is a string that names the subkey with which the value is
Georg Brandl8ec7f652007-08-15 14:28:01 +0000402 associated.
403
Brian Curtinb9bf9712010-05-11 19:13:13 +0000404 *type* is an integer that specifies the type of the data. See
405 :ref:`Value Types <value-types>` for the available types.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000406
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000407 *reserved* can be anything -- zero is always passed to the API.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000408
409 *value* is a string that specifies the new value.
410
411 This method can also set additional value and type information for the specified
412 key. The key identified by the key parameter must have been opened with
413 :const:`KEY_SET_VALUE` access.
414
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000415 To open the key, use the :func:`CreateKey` or :func:`OpenKey` methods.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000416
417 Value lengths are limited by available memory. Long values (more than 2048
418 bytes) should be stored as files with the filenames stored in the configuration
419 registry. This helps the registry perform efficiently.
420
421
Brian Curtine33fa882010-04-02 21:18:14 +0000422.. function:: DisableReflectionKey(key)
423
424 Disables registry reflection for 32-bit processes running on a 64-bit
Georg Brandlbb091e72010-04-25 10:55:58 +0000425 operating system.
Brian Curtine33fa882010-04-02 21:18:14 +0000426
Brian Curtinb9bf9712010-05-11 19:13:13 +0000427 *key* is an already open key, or one of the predefined
428 :ref:`HKEY_* constants <hkey-constants>`.
Brian Curtine33fa882010-04-02 21:18:14 +0000429
430 Will generally raise :exc:`NotImplemented` if executed on a 32-bit
Georg Brandlbb091e72010-04-25 10:55:58 +0000431 operating system.
Brian Curtine33fa882010-04-02 21:18:14 +0000432
433 If the key is not on the reflection list, the function succeeds but has no
434 effect. Disabling reflection for a key does not affect reflection of any
435 subkeys.
436
437
438.. function:: EnableReflectionKey(key)
439
440 Restores registry reflection for the specified disabled key.
441
Brian Curtinb9bf9712010-05-11 19:13:13 +0000442 *key* is an already open key, or one of the predefined
443 :ref:`HKEY_* constants <hkey-constants>`.
Brian Curtine33fa882010-04-02 21:18:14 +0000444
445 Will generally raise :exc:`NotImplemented` if executed on a 32-bit
Georg Brandlbb091e72010-04-25 10:55:58 +0000446 operating system.
Brian Curtine33fa882010-04-02 21:18:14 +0000447
448 Restoring reflection for a key does not affect reflection of any subkeys.
449
450
451.. function:: QueryReflectionKey(key)
452
453 Determines the reflection state for the specified key.
454
Brian Curtinb9bf9712010-05-11 19:13:13 +0000455 *key* is an already open key, or one of the predefined
456 :ref:`HKEY_* constants <hkey-constants>`.
Brian Curtine33fa882010-04-02 21:18:14 +0000457
458 Returns ``True`` if reflection is disabled.
459
460 Will generally raise :exc:`NotImplemented` if executed on a 32-bit
Georg Brandlbb091e72010-04-25 10:55:58 +0000461 operating system.
Brian Curtine33fa882010-04-02 21:18:14 +0000462
463
Brian Curtinb9bf9712010-05-11 19:13:13 +0000464.. _constants:
465
466Constants
467------------------
468
469The following constants are defined for use in many :mod:`_winreg` functions.
470
471.. _hkey-constants:
472
473HKEY_* Constants
474++++++++++++++++
475
476.. data:: HKEY_CLASSES_ROOT
477
478 Registry entries subordinate to this key define types (or classes) of
479 documents and the properties associated with those types. Shell and
480 COM applications use the information stored under this key.
481
482
483.. data:: HKEY_CURRENT_USER
484
485 Registry entries subordinate to this key define the preferences of
486 the current user. These preferences include the settings of
487 environment variables, data about program groups, colors, printers,
488 network connections, and application preferences.
489
490.. data:: HKEY_LOCAL_MACHINE
491
492 Registry entries subordinate to this key define the physical state
493 of the computer, including data about the bus type, system memory,
494 and installed hardware and software.
495
496.. data:: HKEY_USERS
497
498 Registry entries subordinate to this key define the default user
499 configuration for new users on the local computer and the user
500 configuration for the current user.
501
502.. data:: HKEY_PERFORMANCE_DATA
503
504 Registry entries subordinate to this key allow you to access
505 performance data. The data is not actually stored in the registry;
506 the registry functions cause the system to collect the data from
507 its source.
508
509
510.. data:: HKEY_CURRENT_CONFIG
511
512 Contains information about the current hardware profile of the
513 local computer system.
514
515.. data:: HKEY_DYN_DATA
516
517 This key is not used in versions of Windows after 98.
518
519
520.. _access-rights:
521
522Access Rights
523+++++++++++++
524
525For more information, see `Registry Key Security and Access
526<http://msdn.microsoft.com/en-us/library/ms724878%28v=VS.85%29.aspx>`__.
527
528.. data:: KEY_ALL_ACCESS
529
530 Combines the STANDARD_RIGHTS_REQUIRED, :const:`KEY_QUERY_VALUE`,
531 :const:`KEY_SET_VALUE`, :const:`KEY_CREATE_SUB_KEY`,
532 :const:`KEY_ENUMERATE_SUB_KEYS`, :const:`KEY_NOTIFY`,
533 and :const:`KEY_CREATE_LINK` access rights.
534
535.. data:: KEY_WRITE
536
537 Combines the STANDARD_RIGHTS_WRITE, :const:`KEY_SET_VALUE`, and
538 :const:`KEY_CREATE_SUB_KEY` access rights.
539
540.. data:: KEY_READ
541
542 Combines the STANDARD_RIGHTS_READ, :const:`KEY_QUERY_VALUE`,
543 :const:`KEY_ENUMERATE_SUB_KEYS`, and :const:`KEY_NOTIFY` values.
544
545.. data:: KEY_EXECUTE
546
547 Equivalent to :const:`KEY_READ`.
548
549.. data:: KEY_QUERY_VALUE
550
551 Required to query the values of a registry key.
552
553.. data:: KEY_SET_VALUE
554
555 Required to create, delete, or set a registry value.
556
557.. data:: KEY_CREATE_SUB_KEY
558
559 Required to create a subkey of a registry key.
560
561.. data:: KEY_ENUMERATE_SUB_KEYS
562
563 Required to enumerate the subkeys of a registry key.
564
565.. data:: KEY_NOTIFY
566
567 Required to request change notifications for a registry key or for
568 subkeys of a registry key.
569
570.. data:: KEY_CREATE_LINK
571
572 Reserved for system use.
573
574
575.. _64-bit-access-rights:
576
57764-bit Specific
578***************
579
580For more information, see `Accesing an Alternate Registry View
581<http://msdn.microsoft.com/en-us/library/aa384129(v=VS.85).aspx>`__.
582
583.. data:: KEY_WOW64_64KEY
584
585 Indicates that an application on 64-bit Windows should operate on
586 the 64-bit registry view.
587
588.. data:: KEY_WOW64_32KEY
589
590 Indicates that an application on 64-bit Windows should operate on
591 the 32-bit registry view.
592
593
594.. _value-types:
595
596Value Types
597+++++++++++
598
599For more information, see `Registry Value Types
600<http://msdn.microsoft.com/en-us/library/ms724884%28v=VS.85%29.aspx>`__.
601
602.. data:: REG_BINARY
603
604 Binary data in any form.
605
606.. data:: REG_DWORD
607
608 32-bit number.
609
610.. data:: REG_DWORD_LITTLE_ENDIAN
611
612 A 32-bit number in little-endian format.
613
614.. data:: REG_DWORD_BIG_ENDIAN
615
616 A 32-bit number in big-endian format.
617
618.. data:: REG_EXPAND_SZ
619
620 Null-terminated string containing references to environment
621 variables (``%PATH%``).
622
623.. data:: REG_LINK
624
625 A Unicode symbolic link.
626
627.. data:: REG_MULTI_SZ
628
629 A sequence of null-terminated strings, terminated by two null characters.
630 (Python handles this termination automatically.)
631
632.. data:: REG_NONE
633
634 No defined value type.
635
636.. data:: REG_RESOURCE_LIST
637
638 A device-driver resource list.
639
640.. data:: REG_FULL_RESOURCE_DESCRIPTOR
641
642 A hardware setting.
643
644.. data:: REG_RESOURCE_REQUIREMENTS_LIST
645
646 A hardware resource list.
647
648.. data:: REG_SZ
649
650 A null-terminated string.
651
652
Georg Brandl8ec7f652007-08-15 14:28:01 +0000653.. _handle-object:
654
655Registry Handle Objects
656-----------------------
657
658This object wraps a Windows HKEY object, automatically closing it when the
659object is destroyed. To guarantee cleanup, you can call either the
Georg Brandl9bfb78d2010-04-25 10:54:42 +0000660:meth:`~PyHKEY.Close` method on the object, or the :func:`CloseKey` function.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000661
662All registry functions in this module return one of these objects.
663
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000664All registry functions in this module which accept a handle object also accept
665an integer, however, use of the handle object is encouraged.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000666
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000667Handle objects provide semantics for :meth:`__nonzero__` -- thus::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000668
669 if handle:
670 print "Yes"
671
672will print ``Yes`` if the handle is currently valid (has not been closed or
673detached).
674
675The object also support comparison semantics, so handle objects will compare
676true if they both reference the same underlying Windows handle value.
677
Georg Brandld7d4fd72009-07-26 14:37:28 +0000678Handle objects can be converted to an integer (e.g., using the built-in
Georg Brandl8ec7f652007-08-15 14:28:01 +0000679:func:`int` function), in which case the underlying Windows handle value is
Georg Brandl9bfb78d2010-04-25 10:54:42 +0000680returned. You can also use the :meth:`~PyHKEY.Detach` method to return the
681integer handle, and also disconnect the Windows handle from the handle object.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000682
683
684.. method:: PyHKEY.Close()
685
686 Closes the underlying Windows handle.
687
688 If the handle is already closed, no error is raised.
689
690
691.. method:: PyHKEY.Detach()
692
693 Detaches the Windows handle from the handle object.
694
695 The result is an integer (or long on 64 bit Windows) that holds the value of the
696 handle before it is detached. If the handle is already detached or closed, this
697 will return zero.
698
699 After calling this function, the handle is effectively invalidated, but the
Ezio Melotti01fa86a2010-04-05 08:02:54 +0000700 handle is not closed. You would call this function when you need the
701 underlying Win32 handle to exist beyond the lifetime of the handle object.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000702
Christian Heimesb39a7562008-01-08 15:46:10 +0000703.. method:: PyHKEY.__enter__()
Georg Brandl502d6312008-01-08 16:18:26 +0000704 PyHKEY.__exit__(\*exc_info)
705
Georg Brandl9bfb78d2010-04-25 10:54:42 +0000706 The HKEY object implements :meth:`~object.__enter__` and
707 :meth:`~object.__exit__` and thus supports the context protocol for the
708 :keyword:`with` statement::
Georg Brandl502d6312008-01-08 16:18:26 +0000709
710 with OpenKey(HKEY_LOCAL_MACHINE, "foo") as key:
Georg Brandl1e518252010-04-25 10:56:41 +0000711 ... # work with key
Georg Brandl502d6312008-01-08 16:18:26 +0000712
713 will automatically close *key* when control leaves the :keyword:`with` block.
714
715 .. versionadded:: 2.6
Christian Heimesb39a7562008-01-08 15:46:10 +0000716