blob: 0ab24105636a407f3c384c1c8fecda2e66d2b6b6 [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
11integer as the registry handle, a handle object is used to ensure that the
12handles are closed correctly, even if the programmer neglects to explicitly
13close them.
14
15This module exposes a very low-level interface to the Windows registry; it is
Ezio Melotti97bb1792010-04-25 17:55:39 +000016expected that in the future a new ``winreg`` module will be created offering a
Georg Brandl116aa622007-08-15 14:28:22 +000017higher-level interface to the registry API.
18
19This module offers the following functions:
20
21
22.. function:: CloseKey(hkey)
23
24 Closes a previously opened registry key. The hkey argument specifies a
25 previously opened key.
26
27 Note that if *hkey* is not closed using this method (or via
28 :meth:`handle.Close`), it is closed when the *hkey* object is destroyed by
29 Python.
30
31
32.. function:: ConnectRegistry(computer_name, key)
33
Ezio Melotti97bb1792010-04-25 17:55:39 +000034 Establishes a connection to a predefined registry handle on another computer,
35 and returns a :ref:`handle object <handle-object>`.
Georg Brandl116aa622007-08-15 14:28:22 +000036
Ezio Melotti97bb1792010-04-25 17:55:39 +000037 *computer_name* is the name of the remote computer, of the form
Georg Brandl116aa622007-08-15 14:28:22 +000038 ``r"\\computername"``. If ``None``, the local computer is used.
39
40 *key* is the predefined handle to connect to.
41
Benjamin Petersond23f8222009-04-05 19:13:16 +000042 The return value is the handle of the opened key. If the function fails, a
Ezio Melotti97bb1792010-04-25 17:55:39 +000043 :exc:`WindowsError` exception is raised.
Georg Brandl116aa622007-08-15 14:28:22 +000044
45
46.. function:: CreateKey(key, sub_key)
47
Ezio Melotti97bb1792010-04-25 17:55:39 +000048 Creates or opens the specified key, returning a
49 :ref:`handle object <handle-object>`.
Georg Brandl116aa622007-08-15 14:28:22 +000050
Ezio Melotti97bb1792010-04-25 17:55:39 +000051 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl116aa622007-08-15 14:28:22 +000052 constants.
53
Ezio Melotti97bb1792010-04-25 17:55:39 +000054 *sub_key* is a string that names the key this method opens or creates.
Georg Brandl116aa622007-08-15 14:28:22 +000055
Ezio Melotti97bb1792010-04-25 17:55:39 +000056 If *key* is one of the predefined keys, *sub_key* may be ``None``. In that
57 case, the handle returned is the same key handle passed in to the function.
Georg Brandl116aa622007-08-15 14:28:22 +000058
59 If the key already exists, this function opens the existing key.
60
Benjamin Petersond23f8222009-04-05 19:13:16 +000061 The return value is the handle of the opened key. If the function fails, a
Ezio Melotti97bb1792010-04-25 17:55:39 +000062 :exc:`WindowsError` exception is raised.
Georg Brandl116aa622007-08-15 14:28:22 +000063
64
65.. function:: DeleteKey(key, sub_key)
66
67 Deletes the specified key.
68
69 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
70 constants.
71
72 *sub_key* is a string that must be a subkey of the key identified by the *key*
73 parameter. This value must not be ``None``, and the key may not have subkeys.
74
75 *This method can not delete keys with subkeys.*
76
77 If the method succeeds, the entire key, including all of its values, is removed.
Ezio Melotti97bb1792010-04-25 17:55:39 +000078 If the method fails, a :exc:`WindowsError` exception is raised.
Georg Brandl116aa622007-08-15 14:28:22 +000079
80
81.. function:: DeleteValue(key, value)
82
83 Removes a named value from a registry key.
84
Ezio Melotti97bb1792010-04-25 17:55:39 +000085 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl116aa622007-08-15 14:28:22 +000086 constants.
87
88 *value* is a string that identifies the value to remove.
89
90
91.. function:: EnumKey(key, index)
92
93 Enumerates subkeys of an open registry key, returning a string.
94
Ezio Melotti97bb1792010-04-25 17:55:39 +000095 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
Georg Brandl116aa622007-08-15 14:28:22 +000096 constants.
97
Ezio Melotti97bb1792010-04-25 17:55:39 +000098 *index* is an integer that identifies the index of the key to retrieve.
Georg Brandl116aa622007-08-15 14:28:22 +000099
Ezio Melotti97bb1792010-04-25 17:55:39 +0000100 The function retrieves the name of one subkey each time it is called. It is
101 typically called repeatedly until a :exc:`WindowsError` exception is
Georg Brandl116aa622007-08-15 14:28:22 +0000102 raised, indicating, no more values are available.
103
104
105.. function:: EnumValue(key, index)
106
107 Enumerates values of an open registry key, returning a tuple.
108
Ezio Melotti97bb1792010-04-25 17:55:39 +0000109 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
Georg Brandl116aa622007-08-15 14:28:22 +0000110 constants.
111
Ezio Melotti97bb1792010-04-25 17:55:39 +0000112 *index* is an integer that identifies the index of the value to retrieve.
Georg Brandl116aa622007-08-15 14:28:22 +0000113
Ezio Melotti97bb1792010-04-25 17:55:39 +0000114 The function retrieves the name of one subkey each time it is called. It is
115 typically called repeatedly, until a :exc:`WindowsError` exception is
116 raised, indicating no more values.
Georg Brandl116aa622007-08-15 14:28:22 +0000117
118 The result is a tuple of 3 items:
119
120 +-------+--------------------------------------------+
121 | Index | Meaning |
122 +=======+============================================+
123 | ``0`` | A string that identifies the value name |
124 +-------+--------------------------------------------+
125 | ``1`` | An object that holds the value data, and |
126 | | whose type depends on the underlying |
127 | | registry type |
128 +-------+--------------------------------------------+
129 | ``2`` | An integer that identifies the type of the |
130 | | value data |
131 +-------+--------------------------------------------+
132
133
Ezio Melotti713e0422009-09-13 08:13:21 +0000134.. function:: ExpandEnvironmentStrings(str)
Christian Heimes2380ac72008-01-09 00:17:24 +0000135
Ezio Melotti713e0422009-09-13 08:13:21 +0000136 Expands environment strings %NAME% in unicode string like :const:`REG_EXPAND_SZ`::
Christian Heimes2380ac72008-01-09 00:17:24 +0000137
Ezio Melotti713e0422009-09-13 08:13:21 +0000138 >>> ExpandEnvironmentStrings('%windir%')
139 'C:\\Windows'
Christian Heimes2380ac72008-01-09 00:17:24 +0000140
Christian Heimes2380ac72008-01-09 00:17:24 +0000141
Georg Brandl116aa622007-08-15 14:28:22 +0000142.. function:: FlushKey(key)
143
144 Writes all the attributes of a key to the registry.
145
Ezio Melotti97bb1792010-04-25 17:55:39 +0000146 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl116aa622007-08-15 14:28:22 +0000147 constants.
148
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000149 It is not necessary to call :func:`FlushKey` to change a key. Registry changes are
Ezio Melotti97bb1792010-04-25 17:55:39 +0000150 flushed to disk by the registry using its lazy flusher. Registry changes are
151 also flushed to disk at system shutdown. Unlike :func:`CloseKey`, the
152 :func:`FlushKey` method returns only when all the data has been written to the
Georg Brandl116aa622007-08-15 14:28:22 +0000153 registry. An application should only call :func:`FlushKey` if it requires
Ezio Melotti97bb1792010-04-25 17:55:39 +0000154 absolute certainty that registry changes are on disk.
Georg Brandl116aa622007-08-15 14:28:22 +0000155
156 .. note::
157
Ezio Melotti97bb1792010-04-25 17:55:39 +0000158 If you don't know whether a :func:`FlushKey` call is required, it probably
Georg Brandl116aa622007-08-15 14:28:22 +0000159 isn't.
160
161
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000162.. function:: LoadKey(key, sub_key, file_name)
Georg Brandl116aa622007-08-15 14:28:22 +0000163
Ezio Melotti97bb1792010-04-25 17:55:39 +0000164 Creates a subkey under the specified key and stores registration information
Georg Brandl116aa622007-08-15 14:28:22 +0000165 from a specified file into that subkey.
166
167 *key* is an already open key, or any of the predefined :const:`HKEY_\*`
168 constants.
169
170 *sub_key* is a string that identifies the sub_key to load.
171
172 *file_name* is the name of the file to load registry data from. This file must
173 have been created with the :func:`SaveKey` function. Under the file allocation
174 table (FAT) file system, the filename may not have an extension.
175
176 A call to LoadKey() fails if the calling process does not have the
177 :const:`SE_RESTORE_PRIVILEGE` privilege. Note that privileges are different than
Ezio Melotti97bb1792010-04-25 17:55:39 +0000178 permissions -- see the Win32 documentation for more details.
Georg Brandl116aa622007-08-15 14:28:22 +0000179
Ezio Melotti97bb1792010-04-25 17:55:39 +0000180 If *key* is a handle returned by :func:`ConnectRegistry`, then the path
181 specified in *fileName* is relative to the remote computer.
Georg Brandl116aa622007-08-15 14:28:22 +0000182
Ezio Melotti97bb1792010-04-25 17:55:39 +0000183 The Win32 documentation implies *key* must be in the :const:`HKEY_USER` or
Georg Brandl116aa622007-08-15 14:28:22 +0000184 :const:`HKEY_LOCAL_MACHINE` tree. This may or may not be true.
185
186
Brian Curtin44bb1f72010-04-24 17:23:03 +0000187.. function:: OpenKey(key, sub_key[, res[, sam]])
Georg Brandl116aa622007-08-15 14:28:22 +0000188
Ezio Melotti97bb1792010-04-25 17:55:39 +0000189 Opens the specified key, returning a :ref:`handle object <handle-object>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000190
191 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
192 constants.
193
194 *sub_key* is a string that identifies the sub_key to open.
195
196 *res* is a reserved integer, and must be zero. The default is zero.
197
Ezio Melotti97bb1792010-04-25 17:55:39 +0000198 *sam* is an integer that specifies an access mask that describes the desired
Georg Brandlb044b2a2009-09-16 16:05:59 +0000199 security access for the key. Default is :const:`KEY_READ`.
Georg Brandl116aa622007-08-15 14:28:22 +0000200
201 The result is a new handle to the specified key.
202
Benjamin Petersond23f8222009-04-05 19:13:16 +0000203 If the function fails, :exc:`WindowsError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000204
205
206.. function:: OpenKeyEx()
207
Ezio Melotti97bb1792010-04-25 17:55:39 +0000208 The functionality of :func:`OpenKeyEx` is provided via :func:`OpenKey`,
209 by the use of default arguments.
Georg Brandl116aa622007-08-15 14:28:22 +0000210
211
212.. function:: QueryInfoKey(key)
213
214 Returns information about a key, as a tuple.
215
Ezio Melotti97bb1792010-04-25 17:55:39 +0000216 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl116aa622007-08-15 14:28:22 +0000217 constants.
218
219 The result is a tuple of 3 items:
220
221 +-------+---------------------------------------------+
222 | Index | Meaning |
223 +=======+=============================================+
224 | ``0`` | An integer giving the number of sub keys |
225 | | this key has. |
226 +-------+---------------------------------------------+
227 | ``1`` | An integer giving the number of values this |
228 | | key has. |
229 +-------+---------------------------------------------+
Georg Brandlba956ae2007-11-29 17:24:34 +0000230 | ``2`` | An integer giving when the key was last |
Georg Brandl116aa622007-08-15 14:28:22 +0000231 | | modified (if available) as 100's of |
232 | | nanoseconds since Jan 1, 1600. |
233 +-------+---------------------------------------------+
234
235
236.. function:: QueryValue(key, sub_key)
237
Ezio Melotti97bb1792010-04-25 17:55:39 +0000238 Retrieves the unnamed value for a key, as a string.
Georg Brandl116aa622007-08-15 14:28:22 +0000239
Ezio Melotti97bb1792010-04-25 17:55:39 +0000240 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl116aa622007-08-15 14:28:22 +0000241 constants.
242
Ezio Melotti97bb1792010-04-25 17:55:39 +0000243 *sub_key* is a string that holds the name of the subkey with which the value is
244 associated. If this parameter is ``None`` or empty, the function retrieves the
245 value set by the :func:`SetValue` method for the key identified by *key*.
Georg Brandl116aa622007-08-15 14:28:22 +0000246
Benjamin Petersond23f8222009-04-05 19:13:16 +0000247 Values in the registry have name, type, and data components. This method
Georg Brandl116aa622007-08-15 14:28:22 +0000248 retrieves the data for a key's first value that has a NULL name. But the
Benjamin Petersond23f8222009-04-05 19:13:16 +0000249 underlying API call doesn't return the type, so always use
250 :func:`QueryValueEx` if possible.
Georg Brandl116aa622007-08-15 14:28:22 +0000251
252
253.. function:: QueryValueEx(key, value_name)
254
Ezio Melotti97bb1792010-04-25 17:55:39 +0000255 Retrieves the type and data for a specified value name associated with
256 an open registry key.
Georg Brandl116aa622007-08-15 14:28:22 +0000257
Ezio Melotti97bb1792010-04-25 17:55:39 +0000258 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl116aa622007-08-15 14:28:22 +0000259 constants.
260
261 *value_name* is a string indicating the value to query.
262
263 The result is a tuple of 2 items:
264
265 +-------+-----------------------------------------+
266 | Index | Meaning |
267 +=======+=========================================+
268 | ``0`` | The value of the registry item. |
269 +-------+-----------------------------------------+
270 | ``1`` | An integer giving the registry type for |
271 | | this value. |
272 +-------+-----------------------------------------+
273
274
275.. function:: SaveKey(key, file_name)
276
277 Saves the specified key, and all its subkeys to the specified file.
278
Ezio Melotti97bb1792010-04-25 17:55:39 +0000279 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl116aa622007-08-15 14:28:22 +0000280 constants.
281
282 *file_name* is the name of the file to save registry data to. This file cannot
283 already exist. If this filename includes an extension, it cannot be used on file
284 allocation table (FAT) file systems by the :meth:`LoadKey`, :meth:`ReplaceKey`
Ezio Melotti97bb1792010-04-25 17:55:39 +0000285 or :meth:`RestoreKey` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000286
Ezio Melotti97bb1792010-04-25 17:55:39 +0000287 If *key* represents a key on a remote computer, the path described by
Georg Brandl116aa622007-08-15 14:28:22 +0000288 *file_name* is relative to the remote computer. The caller of this method must
Ezio Melotti97bb1792010-04-25 17:55:39 +0000289 possess the :const:`SeBackupPrivilege` security privilege. Note that
290 privileges are different than permissions -- see the Win32 documentation for
Georg Brandl116aa622007-08-15 14:28:22 +0000291 more details.
292
293 This function passes NULL for *security_attributes* to the API.
294
295
296.. function:: SetValue(key, sub_key, type, value)
297
298 Associates a value with a specified key.
299
Ezio Melotti97bb1792010-04-25 17:55:39 +0000300 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl116aa622007-08-15 14:28:22 +0000301 constants.
302
Ezio Melotti97bb1792010-04-25 17:55:39 +0000303 *sub_key* is a string that names the subkey with which the value is associated.
Georg Brandl116aa622007-08-15 14:28:22 +0000304
305 *type* is an integer that specifies the type of the data. Currently this must be
306 :const:`REG_SZ`, meaning only strings are supported. Use the :func:`SetValueEx`
307 function for support for other data types.
308
309 *value* is a string that specifies the new value.
310
311 If the key specified by the *sub_key* parameter does not exist, the SetValue
312 function creates it.
313
314 Value lengths are limited by available memory. Long values (more than 2048
315 bytes) should be stored as files with the filenames stored in the configuration
316 registry. This helps the registry perform efficiently.
317
Ezio Melotti97bb1792010-04-25 17:55:39 +0000318 The key identified by the *key* parameter must have been opened with
Georg Brandl116aa622007-08-15 14:28:22 +0000319 :const:`KEY_SET_VALUE` access.
320
321
322.. function:: SetValueEx(key, value_name, reserved, type, value)
323
324 Stores data in the value field of an open registry key.
325
Ezio Melotti97bb1792010-04-25 17:55:39 +0000326 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
Georg Brandl116aa622007-08-15 14:28:22 +0000327 constants.
328
Ezio Melotti97bb1792010-04-25 17:55:39 +0000329 *value_name* is a string that names the subkey with which the value is
Georg Brandl116aa622007-08-15 14:28:22 +0000330 associated.
331
Ezio Melotti97bb1792010-04-25 17:55:39 +0000332 *type* is an integer that specifies the type of the data. This should be one
Georg Brandl116aa622007-08-15 14:28:22 +0000333 of the following constants defined in this module:
334
335 +----------------------------------+---------------------------------------------+
336 | Constant | Meaning |
337 +==================================+=============================================+
338 | :const:`REG_BINARY` | Binary data in any form. |
339 +----------------------------------+---------------------------------------------+
340 | :const:`REG_DWORD` | A 32-bit number. |
341 +----------------------------------+---------------------------------------------+
342 | :const:`REG_DWORD_LITTLE_ENDIAN` | A 32-bit number in little-endian format. |
343 +----------------------------------+---------------------------------------------+
344 | :const:`REG_DWORD_BIG_ENDIAN` | A 32-bit number in big-endian format. |
345 +----------------------------------+---------------------------------------------+
346 | :const:`REG_EXPAND_SZ` | Null-terminated string containing |
347 | | references to environment variables |
348 | | (``%PATH%``). |
349 +----------------------------------+---------------------------------------------+
350 | :const:`REG_LINK` | A Unicode symbolic link. |
351 +----------------------------------+---------------------------------------------+
352 | :const:`REG_MULTI_SZ` | A sequence of null-terminated strings, |
353 | | terminated by two null characters. (Python |
Ezio Melotti97bb1792010-04-25 17:55:39 +0000354 | | handles this termination automatically.) |
Georg Brandl116aa622007-08-15 14:28:22 +0000355 +----------------------------------+---------------------------------------------+
356 | :const:`REG_NONE` | No defined value type. |
357 +----------------------------------+---------------------------------------------+
358 | :const:`REG_RESOURCE_LIST` | A device-driver resource list. |
359 +----------------------------------+---------------------------------------------+
360 | :const:`REG_SZ` | A null-terminated string. |
361 +----------------------------------+---------------------------------------------+
362
Ezio Melotti97bb1792010-04-25 17:55:39 +0000363 *reserved* can be anything -- zero is always passed to the API.
Georg Brandl116aa622007-08-15 14:28:22 +0000364
365 *value* is a string that specifies the new value.
366
367 This method can also set additional value and type information for the specified
368 key. The key identified by the key parameter must have been opened with
369 :const:`KEY_SET_VALUE` access.
370
Ezio Melotti97bb1792010-04-25 17:55:39 +0000371 To open the key, use the :func:`CreateKey` or :func:`OpenKey` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000372
373 Value lengths are limited by available memory. Long values (more than 2048
374 bytes) should be stored as files with the filenames stored in the configuration
375 registry. This helps the registry perform efficiently.
376
377
378.. _handle-object:
379
380Registry Handle Objects
381-----------------------
382
383This object wraps a Windows HKEY object, automatically closing it when the
384object is destroyed. To guarantee cleanup, you can call either the
Ezio Melotti97bb1792010-04-25 17:55:39 +0000385:meth:`Close` method on the object, or the :func:`CloseKey` function.
Georg Brandl116aa622007-08-15 14:28:22 +0000386
387All registry functions in this module return one of these objects.
388
Ezio Melotti97bb1792010-04-25 17:55:39 +0000389All registry functions in this module which accept a handle object also accept
390an integer, however, use of the handle object is encouraged.
Georg Brandl116aa622007-08-15 14:28:22 +0000391
Ezio Melotti97bb1792010-04-25 17:55:39 +0000392Handle objects provide semantics for :meth:`__bool__` -- thus ::
Georg Brandl116aa622007-08-15 14:28:22 +0000393
394 if handle:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000395 print("Yes")
Georg Brandl116aa622007-08-15 14:28:22 +0000396
397will print ``Yes`` if the handle is currently valid (has not been closed or
398detached).
399
400The object also support comparison semantics, so handle objects will compare
401true if they both reference the same underlying Windows handle value.
402
Georg Brandlc5605df2009-08-13 08:26:44 +0000403Handle objects can be converted to an integer (e.g., using the built-in
Georg Brandl116aa622007-08-15 14:28:22 +0000404:func:`int` function), in which case the underlying Windows handle value is
Ezio Melotti97bb1792010-04-25 17:55:39 +0000405returned. You can also use the :meth:`Detach` method to return the integer
Georg Brandl116aa622007-08-15 14:28:22 +0000406handle, and also disconnect the Windows handle from the handle object.
407
408
409.. method:: PyHKEY.Close()
410
411 Closes the underlying Windows handle.
412
413 If the handle is already closed, no error is raised.
414
415
416.. method:: PyHKEY.Detach()
417
418 Detaches the Windows handle from the handle object.
419
Georg Brandl5c106642007-11-29 17:41:05 +0000420 The result is an integer that holds the value of the handle before it is
421 detached. If the handle is already detached or closed, this will return
422 zero.
Georg Brandl116aa622007-08-15 14:28:22 +0000423
424 After calling this function, the handle is effectively invalidated, but the
Ezio Melotti97bb1792010-04-25 17:55:39 +0000425 handle is not closed. You would call this function when you need the
426 underlying Win32 handle to exist beyond the lifetime of the handle object.
Georg Brandl116aa622007-08-15 14:28:22 +0000427
Christian Heimes2380ac72008-01-09 00:17:24 +0000428.. method:: PyHKEY.__enter__()
429 PyHKEY.__exit__(\*exc_info)
430
431 The HKEY object implements :meth:`__enter__` and :meth:`__exit__` and thus
432 supports the context protocol for the :keyword:`with` statement::
433
434 with OpenKey(HKEY_LOCAL_MACHINE, "foo") as key:
435 # ... work with key ...
436
437 will automatically close *key* when control leaves the :keyword:`with` block.
438
Christian Heimes2380ac72008-01-09 00:17:24 +0000439