2 :mod:`_winreg` -- Windows registry access
3 =========================================
7 :synopsis: Routines and objects for manipulating the Windows registry.
8 .. sectionauthor:: Mark Hammond <MarkH@ActiveState.com>
11 The :mod:`_winreg` module has been renamed to :mod:`winreg` in Python 3.0.
12 The :term:`2to3` tool will automatically adapt imports when converting your
18 These functions expose the Windows registry API to Python. Instead of using an
19 integer as the registry handle, a handle object is used to ensure that the
20 handles are closed correctly, even if the programmer neglects to explicitly
23 This module exposes a very low-level interface to the Windows registry; it is
24 expected that in the future a new ``winreg`` module will be created offering a
25 higher-level interface to the registry API.
27 This module offers the following functions:
30 .. function:: CloseKey(hkey)
32 Closes a previously opened registry key. The hkey argument specifies a
33 previously opened key.
35 Note that if *hkey* is not closed using this method (or via
36 :meth:`handle.Close`), it is closed when the *hkey* object is destroyed by
40 .. function:: ConnectRegistry(computer_name, key)
42 Establishes a connection to a predefined registry handle on another computer,
43 and returns a :dfn:`handle object`
45 *computer_name* is the name of the remote computer, of the form
46 ``r"\\computername"``. If ``None``, the local computer is used.
48 *key* is the predefined handle to connect to.
50 The return value is the handle of the opened key. If the function fails, an
51 :exc:`EnvironmentError` exception is raised.
54 .. function:: CreateKey(key, sub_key)
56 Creates or opens the specified key, returning a :dfn:`handle object`
58 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
61 *sub_key* is a string that names the key this method opens or creates.
63 If *key* is one of the predefined keys, *sub_key* may be ``None``. In that
64 case, the handle returned is the same key handle passed in to the function.
66 If the key already exists, this function opens the existing key.
68 The return value is the handle of the opened key. If the function fails, an
69 :exc:`EnvironmentError` exception is raised.
72 .. function:: DeleteKey(key, sub_key)
74 Deletes the specified key.
76 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
79 *sub_key* is a string that must be a subkey of the key identified by the *key*
80 parameter. This value must not be ``None``, and the key may not have subkeys.
82 *This method can not delete keys with subkeys.*
84 If the method succeeds, the entire key, including all of its values, is removed.
85 If the method fails, an :exc:`EnvironmentError` exception is raised.
88 .. function:: DeleteValue(key, value)
90 Removes a named value from a registry key.
92 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
95 *value* is a string that identifies the value to remove.
98 .. function:: EnumKey(key, index)
100 Enumerates subkeys of an open registry key, returning a string.
102 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
105 *index* is an integer that identifies the index of the key to retrieve.
107 The function retrieves the name of one subkey each time it is called. It is
108 typically called repeatedly until an :exc:`EnvironmentError` exception is
109 raised, indicating, no more values are available.
112 .. function:: EnumValue(key, index)
114 Enumerates values of an open registry key, returning a tuple.
116 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
119 *index* is an integer that identifies the index of the value to retrieve.
121 The function retrieves the name of one subkey each time it is called. It is
122 typically called repeatedly, until an :exc:`EnvironmentError` exception is
123 raised, indicating no more values.
125 The result is a tuple of 3 items:
127 +-------+--------------------------------------------+
129 +=======+============================================+
130 | ``0`` | A string that identifies the value name |
131 +-------+--------------------------------------------+
132 | ``1`` | An object that holds the value data, and |
133 | | whose type depends on the underlying |
135 +-------+--------------------------------------------+
136 | ``2`` | An integer that identifies the type of the |
138 +-------+--------------------------------------------+
141 .. function:: ExpandEnvironmentStrings(unicode)
143 Expands environment strings %NAME% in unicode string like const:`REG_EXPAND_SZ`::
145 >>> ExpandEnvironmentStrings(u"%windir%")
148 .. versionadded:: 2.6
151 .. function:: FlushKey(key)
153 Writes all the attributes of a key to the registry.
155 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
158 It is not necessary to call :func:`FlushKey` to change a key. Registry changes are
159 flushed to disk by the registry using its lazy flusher. Registry changes are
160 also flushed to disk at system shutdown. Unlike :func:`CloseKey`, the
161 :func:`FlushKey` method returns only when all the data has been written to the
162 registry. An application should only call :func:`FlushKey` if it requires
163 absolute certainty that registry changes are on disk.
167 If you don't know whether a :func:`FlushKey` call is required, it probably
171 .. function:: LoadKey(key, sub_key, file_name)
173 Creates a subkey under the specified key and stores registration information
174 from a specified file into that subkey.
176 *key* is an already open key, or any of the predefined :const:`HKEY_\*`
179 *sub_key* is a string that identifies the sub_key to load.
181 *file_name* is the name of the file to load registry data from. This file must
182 have been created with the :func:`SaveKey` function. Under the file allocation
183 table (FAT) file system, the filename may not have an extension.
185 A call to LoadKey() fails if the calling process does not have the
186 :const:`SE_RESTORE_PRIVILEGE` privilege. Note that privileges are different than
187 permissions - see the Win32 documentation for more details.
189 If *key* is a handle returned by :func:`ConnectRegistry`, then the path
190 specified in *fileName* is relative to the remote computer.
192 The Win32 documentation implies *key* must be in the :const:`HKEY_USER` or
193 :const:`HKEY_LOCAL_MACHINE` tree. This may or may not be true.
196 .. function:: OpenKey(key, sub_key[, res=0][, sam=KEY_READ])
198 Opens the specified key, returning a :dfn:`handle object`
200 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
203 *sub_key* is a string that identifies the sub_key to open.
205 *res* is a reserved integer, and must be zero. The default is zero.
207 *sam* is an integer that specifies an access mask that describes the desired
208 security access for the key. Default is :const:`KEY_READ`
210 The result is a new handle to the specified key.
212 If the function fails, :exc:`EnvironmentError` is raised.
215 .. function:: OpenKeyEx()
217 The functionality of :func:`OpenKeyEx` is provided via :func:`OpenKey`, by the
218 use of default arguments.
221 .. function:: QueryInfoKey(key)
223 Returns information about a key, as a tuple.
225 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
228 The result is a tuple of 3 items:
230 +-------+---------------------------------------------+
232 +=======+=============================================+
233 | ``0`` | An integer giving the number of sub keys |
235 +-------+---------------------------------------------+
236 | ``1`` | An integer giving the number of values this |
238 +-------+---------------------------------------------+
239 | ``2`` | A long integer giving when the key was last |
240 | | modified (if available) as 100's of |
241 | | nanoseconds since Jan 1, 1600. |
242 +-------+---------------------------------------------+
245 .. function:: QueryValue(key, sub_key)
247 Retrieves the unnamed value for a key, as a string
249 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
252 *sub_key* is a string that holds the name of the subkey with which the value is
253 associated. If this parameter is ``None`` or empty, the function retrieves the
254 value set by the :func:`SetValue` method for the key identified by *key*.
256 Values in the registry have name, type, and data components. This method
257 retrieves the data for a key's first value that has a NULL name. But the
258 underlying API call doesn't return the type, Lame Lame Lame, DO NOT USE THIS!!!
261 .. function:: QueryValueEx(key, value_name)
263 Retrieves the type and data for a specified value name associated with an open
266 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
269 *value_name* is a string indicating the value to query.
271 The result is a tuple of 2 items:
273 +-------+-----------------------------------------+
275 +=======+=========================================+
276 | ``0`` | The value of the registry item. |
277 +-------+-----------------------------------------+
278 | ``1`` | An integer giving the registry type for |
280 +-------+-----------------------------------------+
283 .. function:: SaveKey(key, file_name)
285 Saves the specified key, and all its subkeys to the specified file.
287 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
290 *file_name* is the name of the file to save registry data to. This file cannot
291 already exist. If this filename includes an extension, it cannot be used on file
292 allocation table (FAT) file systems by the :meth:`LoadKey`, :meth:`ReplaceKey`
293 or :meth:`RestoreKey` methods.
295 If *key* represents a key on a remote computer, the path described by
296 *file_name* is relative to the remote computer. The caller of this method must
297 possess the :const:`SeBackupPrivilege` security privilege. Note that
298 privileges are different than permissions - see the Win32 documentation for
301 This function passes NULL for *security_attributes* to the API.
304 .. function:: SetValue(key, sub_key, type, value)
306 Associates a value with a specified key.
308 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
311 *sub_key* is a string that names the subkey with which the value is associated.
313 *type* is an integer that specifies the type of the data. Currently this must be
314 :const:`REG_SZ`, meaning only strings are supported. Use the :func:`SetValueEx`
315 function for support for other data types.
317 *value* is a string that specifies the new value.
319 If the key specified by the *sub_key* parameter does not exist, the SetValue
322 Value lengths are limited by available memory. Long values (more than 2048
323 bytes) should be stored as files with the filenames stored in the configuration
324 registry. This helps the registry perform efficiently.
326 The key identified by the *key* parameter must have been opened with
327 :const:`KEY_SET_VALUE` access.
330 .. function:: SetValueEx(key, value_name, reserved, type, value)
332 Stores data in the value field of an open registry key.
334 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
337 *value_name* is a string that names the subkey with which the value is
340 *type* is an integer that specifies the type of the data. This should be one
341 of the following constants defined in this module:
343 +----------------------------------+---------------------------------------------+
344 | Constant | Meaning |
345 +==================================+=============================================+
346 | :const:`REG_BINARY` | Binary data in any form. |
347 +----------------------------------+---------------------------------------------+
348 | :const:`REG_DWORD` | A 32-bit number. |
349 +----------------------------------+---------------------------------------------+
350 | :const:`REG_DWORD_LITTLE_ENDIAN` | A 32-bit number in little-endian format. |
351 +----------------------------------+---------------------------------------------+
352 | :const:`REG_DWORD_BIG_ENDIAN` | A 32-bit number in big-endian format. |
353 +----------------------------------+---------------------------------------------+
354 | :const:`REG_EXPAND_SZ` | Null-terminated string containing |
355 | | references to environment variables |
357 +----------------------------------+---------------------------------------------+
358 | :const:`REG_LINK` | A Unicode symbolic link. |
359 +----------------------------------+---------------------------------------------+
360 | :const:`REG_MULTI_SZ` | A sequence of null-terminated strings, |
361 | | terminated by two null characters. (Python |
362 | | handles this termination automatically.) |
363 +----------------------------------+---------------------------------------------+
364 | :const:`REG_NONE` | No defined value type. |
365 +----------------------------------+---------------------------------------------+
366 | :const:`REG_RESOURCE_LIST` | A device-driver resource list. |
367 +----------------------------------+---------------------------------------------+
368 | :const:`REG_SZ` | A null-terminated string. |
369 +----------------------------------+---------------------------------------------+
371 *reserved* can be anything - zero is always passed to the API.
373 *value* is a string that specifies the new value.
375 This method can also set additional value and type information for the specified
376 key. The key identified by the key parameter must have been opened with
377 :const:`KEY_SET_VALUE` access.
379 To open the key, use the :func:`CreateKeyEx` or :func:`OpenKey` methods.
381 Value lengths are limited by available memory. Long values (more than 2048
382 bytes) should be stored as files with the filenames stored in the configuration
383 registry. This helps the registry perform efficiently.
388 Registry Handle Objects
389 -----------------------
391 This object wraps a Windows HKEY object, automatically closing it when the
392 object is destroyed. To guarantee cleanup, you can call either the
393 :meth:`Close` method on the object, or the :func:`CloseKey` function.
395 All registry functions in this module return one of these objects.
397 All registry functions in this module which accept a handle object also accept
398 an integer, however, use of the handle object is encouraged.
400 Handle objects provide semantics for :meth:`__nonzero__` - thus ::
405 will print ``Yes`` if the handle is currently valid (has not been closed or
408 The object also support comparison semantics, so handle objects will compare
409 true if they both reference the same underlying Windows handle value.
411 Handle objects can be converted to an integer (e.g., using the builtin
412 :func:`int` function), in which case the underlying Windows handle value is
413 returned. You can also use the :meth:`Detach` method to return the integer
414 handle, and also disconnect the Windows handle from the handle object.
417 .. method:: PyHKEY.Close()
419 Closes the underlying Windows handle.
421 If the handle is already closed, no error is raised.
424 .. method:: PyHKEY.Detach()
426 Detaches the Windows handle from the handle object.
428 The result is an integer (or long on 64 bit Windows) that holds the value of the
429 handle before it is detached. If the handle is already detached or closed, this
432 After calling this function, the handle is effectively invalidated, but the
433 handle is not closed. You would call this function when you need the
434 underlying Win32 handle to exist beyond the lifetime of the handle object.
436 .. method:: PyHKEY.__enter__()
437 PyHKEY.__exit__(\*exc_info)
439 The HKEY object implements :meth:`__enter__` and :meth:`__exit__` and thus
440 supports the context protocol for the :keyword:`with` statement::
442 with OpenKey(HKEY_LOCAL_MACHINE, "foo") as key:
443 # ... work with key ...
445 will automatically close *key* when control leaves the :keyword:`with` block.
447 .. versionadded:: 2.6