blob: a9de7d49b88418e17d16fdfc8b7eb5046094b102 [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
18integer as the registry handle, a handle object is used to ensure that the
19handles are closed correctly, even if the programmer neglects to explicitly
20close them.
21
22This module exposes a very low-level interface to the Windows registry; it is
23expected that in the future a new ``winreg`` module will be created offering a
24higher-level interface to the registry API.
25
26This module offers the following functions:
27
28
29.. function:: CloseKey(hkey)
30
31 Closes a previously opened registry key. The hkey argument specifies a
32 previously opened key.
33
34 Note that if *hkey* is not closed using this method (or via
35 :meth:`handle.Close`), it is closed when the *hkey* object is destroyed by
36 Python.
37
38
39.. function:: ConnectRegistry(computer_name, key)
40
41 Establishes a connection to a predefined registry handle on another computer,
42 and returns a :dfn:`handle object`
43
44 *computer_name* is the name of the remote computer, of the form
45 ``r"\\computername"``. If ``None``, the local computer is used.
46
47 *key* is the predefined handle to connect to.
48
Georg Brandlb945bbf2009-03-31 16:31:11 +000049 The return value is the handle of the opened key. If the function fails, a
50 :exc:`WindowsError` exception is raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +000051
52
53.. function:: CreateKey(key, sub_key)
54
55 Creates or opens the specified key, returning a :dfn:`handle object`
56
57 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
58 constants.
59
60 *sub_key* is a string that names the key this method opens or creates.
61
62 If *key* is one of the predefined keys, *sub_key* may be ``None``. In that
63 case, the handle returned is the same key handle passed in to the function.
64
65 If the key already exists, this function opens the existing key.
66
Georg Brandlb945bbf2009-03-31 16:31:11 +000067 The return value is the handle of the opened key. If the function fails, a
68 :exc:`WindowsError` exception is raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +000069
70
Brian Curtine33fa882010-04-02 21:18:14 +000071.. function:: CreateKeyEx(key, sub_key[, res=0[, sam=KEY_ALL_ACCESS]])
72
73 Creates or opens the specified key, returning a :dfn:`handle object`
74
75 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
76 constants.
77
78 *sub_key* is a string that names the key this method opens or creates.
79
80 *res* is a reserved integer, and must be zero. The default is zero.
81
82 *sam* is an integer that specifies an access mask that describes the desired
83 security access for the key. Default is :const:`KEY_ALL_ACCESS`
84
85 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.
87
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 Curtine33fa882010-04-02 21:18:14 +0000100 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000101 constants.
102
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.
Georg Brandlb945bbf2009-03-31 16:31:11 +0000109 If the method fails, a :exc:`WindowsError` exception is raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000110
111
Brian Curtine33fa882010-04-02 21:18:14 +0000112.. function:: DeleteKeyEx(key, sub_key[, sam=KEY_WOW64_64KEY[, res=0]])
113
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.
119 See http://msdn.microsoft.com/en-us/library/ms724847%28VS.85%29.aspx
120
121 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
122 constants.
123
124 *sub_key* is a string that must be a subkey of the key identified by the
125 *key* parameter. This value must not be ``None``, and the key may not have
126 subkeys.
127
128 *res* is a reserved integer, and must be zero. The default is zero.
129
130 *sam* is an integer that specifies an access mask that describes the
131 desired security access for the key. Default is :const:`KEY_WOW64_64KEY`
132
133 *This method can not delete keys with subkeys.*
134
135 If the method succeeds, the entire key, including all of its values, is
136 removed. If the method fails, a :exc:`WindowsError` exception is raised.
137
138 On unsupported Windows versions, :exc:`NotImplementedError` is raised.
139
140.. versionadded:: 2.7
141
142
Georg Brandl8ec7f652007-08-15 14:28:01 +0000143.. function:: DeleteValue(key, value)
144
145 Removes a named value from a registry key.
146
147 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
148 constants.
149
150 *value* is a string that identifies the value to remove.
151
152
153.. function:: EnumKey(key, index)
154
155 Enumerates subkeys of an open registry key, returning a string.
156
157 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
158 constants.
159
160 *index* is an integer that identifies the index of the key to retrieve.
161
162 The function retrieves the name of one subkey each time it is called. It is
Georg Brandlb945bbf2009-03-31 16:31:11 +0000163 typically called repeatedly until a :exc:`WindowsError` exception is
Georg Brandl8ec7f652007-08-15 14:28:01 +0000164 raised, indicating, no more values are available.
165
166
167.. function:: EnumValue(key, index)
168
169 Enumerates values of an open registry key, returning a tuple.
170
171 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
172 constants.
173
174 *index* is an integer that identifies the index of the value to retrieve.
175
176 The function retrieves the name of one subkey each time it is called. It is
Georg Brandlb945bbf2009-03-31 16:31:11 +0000177 typically called repeatedly, until a :exc:`WindowsError` exception is
Georg Brandl8ec7f652007-08-15 14:28:01 +0000178 raised, indicating no more values.
179
180 The result is a tuple of 3 items:
181
182 +-------+--------------------------------------------+
183 | Index | Meaning |
184 +=======+============================================+
185 | ``0`` | A string that identifies the value name |
186 +-------+--------------------------------------------+
187 | ``1`` | An object that holds the value data, and |
188 | | whose type depends on the underlying |
189 | | registry type |
190 +-------+--------------------------------------------+
191 | ``2`` | An integer that identifies the type of the |
192 | | value data |
193 +-------+--------------------------------------------+
194
195
Christian Heimesb39a7562008-01-08 15:46:10 +0000196.. function:: ExpandEnvironmentStrings(unicode)
197
198 Expands environment strings %NAME% in unicode string like const:`REG_EXPAND_SZ`::
Georg Brandl502d6312008-01-08 16:18:26 +0000199
200 >>> ExpandEnvironmentStrings(u"%windir%")
201 u"C:\\Windows"
202
203 .. versionadded:: 2.6
Christian Heimesb39a7562008-01-08 15:46:10 +0000204
205
Georg Brandl8ec7f652007-08-15 14:28:01 +0000206.. function:: FlushKey(key)
207
208 Writes all the attributes of a key to the registry.
209
210 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
211 constants.
212
Georg Brandl51174092008-05-09 06:10:43 +0000213 It is not necessary to call :func:`FlushKey` to change a key. Registry changes are
Georg Brandl8ec7f652007-08-15 14:28:01 +0000214 flushed to disk by the registry using its lazy flusher. Registry changes are
215 also flushed to disk at system shutdown. Unlike :func:`CloseKey`, the
216 :func:`FlushKey` method returns only when all the data has been written to the
217 registry. An application should only call :func:`FlushKey` if it requires
218 absolute certainty that registry changes are on disk.
219
220 .. note::
221
222 If you don't know whether a :func:`FlushKey` call is required, it probably
223 isn't.
224
225
Georg Brandl51174092008-05-09 06:10:43 +0000226.. function:: LoadKey(key, sub_key, file_name)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000227
228 Creates a subkey under the specified key and stores registration information
229 from a specified file into that subkey.
230
231 *key* is an already open key, or any of the predefined :const:`HKEY_\*`
232 constants.
233
234 *sub_key* is a string that identifies the sub_key to load.
235
236 *file_name* is the name of the file to load registry data from. This file must
237 have been created with the :func:`SaveKey` function. Under the file allocation
238 table (FAT) file system, the filename may not have an extension.
239
240 A call to LoadKey() fails if the calling process does not have the
241 :const:`SE_RESTORE_PRIVILEGE` privilege. Note that privileges are different than
242 permissions - see the Win32 documentation for more details.
243
244 If *key* is a handle returned by :func:`ConnectRegistry`, then the path
245 specified in *fileName* is relative to the remote computer.
246
247 The Win32 documentation implies *key* must be in the :const:`HKEY_USER` or
248 :const:`HKEY_LOCAL_MACHINE` tree. This may or may not be true.
249
250
251.. function:: OpenKey(key, sub_key[, res=0][, sam=KEY_READ])
252
253 Opens the specified key, returning a :dfn:`handle object`
254
255 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
256 constants.
257
258 *sub_key* is a string that identifies the sub_key to open.
259
260 *res* is a reserved integer, and must be zero. The default is zero.
261
262 *sam* is an integer that specifies an access mask that describes the desired
263 security access for the key. Default is :const:`KEY_READ`
264
265 The result is a new handle to the specified key.
266
Georg Brandlb945bbf2009-03-31 16:31:11 +0000267 If the function fails, :exc:`WindowsError` is raised.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000268
269
270.. function:: OpenKeyEx()
271
272 The functionality of :func:`OpenKeyEx` is provided via :func:`OpenKey`, by the
273 use of default arguments.
274
275
276.. function:: QueryInfoKey(key)
277
278 Returns information about a key, as a tuple.
279
280 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
281 constants.
282
283 The result is a tuple of 3 items:
284
285 +-------+---------------------------------------------+
286 | Index | Meaning |
287 +=======+=============================================+
288 | ``0`` | An integer giving the number of sub keys |
289 | | this key has. |
290 +-------+---------------------------------------------+
291 | ``1`` | An integer giving the number of values this |
292 | | key has. |
293 +-------+---------------------------------------------+
294 | ``2`` | A long integer giving when the key was last |
295 | | modified (if available) as 100's of |
296 | | nanoseconds since Jan 1, 1600. |
297 +-------+---------------------------------------------+
298
299
300.. function:: QueryValue(key, sub_key)
301
302 Retrieves the unnamed value for a key, as a string
303
304 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
305 constants.
306
307 *sub_key* is a string that holds the name of the subkey with which the value is
308 associated. If this parameter is ``None`` or empty, the function retrieves the
309 value set by the :func:`SetValue` method for the key identified by *key*.
310
Georg Brandl75f11072009-04-05 10:32:26 +0000311 Values in the registry have name, type, and data components. This method
Georg Brandl8ec7f652007-08-15 14:28:01 +0000312 retrieves the data for a key's first value that has a NULL name. But the
Georg Brandl75f11072009-04-05 10:32:26 +0000313 underlying API call doesn't return the type, so always use
314 :func:`QueryValueEx` if possible.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000315
316
317.. function:: QueryValueEx(key, value_name)
318
319 Retrieves the type and data for a specified value name associated with an open
320 registry key.
321
322 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
323 constants.
324
325 *value_name* is a string indicating the value to query.
326
327 The result is a tuple of 2 items:
328
329 +-------+-----------------------------------------+
330 | Index | Meaning |
331 +=======+=========================================+
332 | ``0`` | The value of the registry item. |
333 +-------+-----------------------------------------+
334 | ``1`` | An integer giving the registry type for |
335 | | this value. |
336 +-------+-----------------------------------------+
337
338
339.. function:: SaveKey(key, file_name)
340
341 Saves the specified key, and all its subkeys to the specified file.
342
343 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
344 constants.
345
346 *file_name* is the name of the file to save registry data to. This file cannot
347 already exist. If this filename includes an extension, it cannot be used on file
348 allocation table (FAT) file systems by the :meth:`LoadKey`, :meth:`ReplaceKey`
349 or :meth:`RestoreKey` methods.
350
351 If *key* represents a key on a remote computer, the path described by
352 *file_name* is relative to the remote computer. The caller of this method must
353 possess the :const:`SeBackupPrivilege` security privilege. Note that
354 privileges are different than permissions - see the Win32 documentation for
355 more details.
356
357 This function passes NULL for *security_attributes* to the API.
358
359
360.. function:: SetValue(key, sub_key, type, value)
361
362 Associates a value with a specified key.
363
364 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
365 constants.
366
367 *sub_key* is a string that names the subkey with which the value is associated.
368
369 *type* is an integer that specifies the type of the data. Currently this must be
370 :const:`REG_SZ`, meaning only strings are supported. Use the :func:`SetValueEx`
371 function for support for other data types.
372
373 *value* is a string that specifies the new value.
374
375 If the key specified by the *sub_key* parameter does not exist, the SetValue
376 function creates it.
377
378 Value lengths are limited by available memory. Long values (more than 2048
379 bytes) should be stored as files with the filenames stored in the configuration
380 registry. This helps the registry perform efficiently.
381
382 The key identified by the *key* parameter must have been opened with
383 :const:`KEY_SET_VALUE` access.
384
385
386.. function:: SetValueEx(key, value_name, reserved, type, value)
387
388 Stores data in the value field of an open registry key.
389
390 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
391 constants.
392
393 *value_name* is a string that names the subkey with which the value is
394 associated.
395
396 *type* is an integer that specifies the type of the data. This should be one
397 of the following constants defined in this module:
398
399 +----------------------------------+---------------------------------------------+
400 | Constant | Meaning |
401 +==================================+=============================================+
402 | :const:`REG_BINARY` | Binary data in any form. |
403 +----------------------------------+---------------------------------------------+
404 | :const:`REG_DWORD` | A 32-bit number. |
405 +----------------------------------+---------------------------------------------+
406 | :const:`REG_DWORD_LITTLE_ENDIAN` | A 32-bit number in little-endian format. |
407 +----------------------------------+---------------------------------------------+
408 | :const:`REG_DWORD_BIG_ENDIAN` | A 32-bit number in big-endian format. |
409 +----------------------------------+---------------------------------------------+
410 | :const:`REG_EXPAND_SZ` | Null-terminated string containing |
411 | | references to environment variables |
412 | | (``%PATH%``). |
413 +----------------------------------+---------------------------------------------+
414 | :const:`REG_LINK` | A Unicode symbolic link. |
415 +----------------------------------+---------------------------------------------+
416 | :const:`REG_MULTI_SZ` | A sequence of null-terminated strings, |
417 | | terminated by two null characters. (Python |
418 | | handles this termination automatically.) |
419 +----------------------------------+---------------------------------------------+
420 | :const:`REG_NONE` | No defined value type. |
421 +----------------------------------+---------------------------------------------+
422 | :const:`REG_RESOURCE_LIST` | A device-driver resource list. |
423 +----------------------------------+---------------------------------------------+
424 | :const:`REG_SZ` | A null-terminated string. |
425 +----------------------------------+---------------------------------------------+
426
427 *reserved* can be anything - zero is always passed to the API.
428
429 *value* is a string that specifies the new value.
430
431 This method can also set additional value and type information for the specified
432 key. The key identified by the key parameter must have been opened with
433 :const:`KEY_SET_VALUE` access.
434
Ezio Melotti6164d7a2010-02-11 23:50:57 +0000435 To open the key, use the :func:`CreateKey` or :func:`OpenKey` methods.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000436
437 Value lengths are limited by available memory. Long values (more than 2048
438 bytes) should be stored as files with the filenames stored in the configuration
439 registry. This helps the registry perform efficiently.
440
441
Brian Curtine33fa882010-04-02 21:18:14 +0000442.. function:: DisableReflectionKey(key)
443
444 Disables registry reflection for 32-bit processes running on a 64-bit
445 Operating System.
446
447 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
448 constants.
449
450 Will generally raise :exc:`NotImplemented` if executed on a 32-bit
451 Operating System.
452
453 If the key is not on the reflection list, the function succeeds but has no
454 effect. Disabling reflection for a key does not affect reflection of any
455 subkeys.
456
457
458.. function:: EnableReflectionKey(key)
459
460 Restores registry reflection for the specified disabled key.
461
462 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
463 constants.
464
465 Will generally raise :exc:`NotImplemented` if executed on a 32-bit
466 Operating System.
467
468 Restoring reflection for a key does not affect reflection of any subkeys.
469
470
471.. function:: QueryReflectionKey(key)
472
473 Determines the reflection state for the specified key.
474
475 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
476 constants.
477
478 Returns ``True`` if reflection is disabled.
479
480 Will generally raise :exc:`NotImplemented` if executed on a 32-bit
481 Operating System.
482
483
Georg Brandl8ec7f652007-08-15 14:28:01 +0000484.. _handle-object:
485
486Registry Handle Objects
487-----------------------
488
489This object wraps a Windows HKEY object, automatically closing it when the
490object is destroyed. To guarantee cleanup, you can call either the
491:meth:`Close` method on the object, or the :func:`CloseKey` function.
492
493All registry functions in this module return one of these objects.
494
495All registry functions in this module which accept a handle object also accept
496an integer, however, use of the handle object is encouraged.
497
498Handle objects provide semantics for :meth:`__nonzero__` - thus ::
499
500 if handle:
501 print "Yes"
502
503will print ``Yes`` if the handle is currently valid (has not been closed or
504detached).
505
506The object also support comparison semantics, so handle objects will compare
507true if they both reference the same underlying Windows handle value.
508
Georg Brandld7d4fd72009-07-26 14:37:28 +0000509Handle objects can be converted to an integer (e.g., using the built-in
Georg Brandl8ec7f652007-08-15 14:28:01 +0000510:func:`int` function), in which case the underlying Windows handle value is
511returned. You can also use the :meth:`Detach` method to return the integer
512handle, and also disconnect the Windows handle from the handle object.
513
514
515.. method:: PyHKEY.Close()
516
517 Closes the underlying Windows handle.
518
519 If the handle is already closed, no error is raised.
520
521
522.. method:: PyHKEY.Detach()
523
524 Detaches the Windows handle from the handle object.
525
526 The result is an integer (or long on 64 bit Windows) that holds the value of the
527 handle before it is detached. If the handle is already detached or closed, this
528 will return zero.
529
530 After calling this function, the handle is effectively invalidated, but the
531 handle is not closed. You would call this function when you need the
532 underlying Win32 handle to exist beyond the lifetime of the handle object.
533
Christian Heimesb39a7562008-01-08 15:46:10 +0000534.. method:: PyHKEY.__enter__()
Georg Brandl502d6312008-01-08 16:18:26 +0000535 PyHKEY.__exit__(\*exc_info)
536
537 The HKEY object implements :meth:`__enter__` and :meth:`__exit__` and thus
538 supports the context protocol for the :keyword:`with` statement::
539
540 with OpenKey(HKEY_LOCAL_MACHINE, "foo") as key:
541 # ... work with key ...
542
543 will automatically close *key* when control leaves the :keyword:`with` block.
544
545 .. versionadded:: 2.6
Christian Heimesb39a7562008-01-08 15:46:10 +0000546