blob: 487856a3ac6c60050e7e4e320333d935f3d88522 [file] [log] [blame]
Serhiy Storchaka29b0a262016-12-04 10:20:55 +02001: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.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04007
Georg Brandl116aa622007-08-15 14:28:22 +00008.. sectionauthor:: Mark Hammond <MarkH@ActiveState.com>
9
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040010--------------
Georg Brandl116aa622007-08-15 14:28:22 +000011
Georg Brandl116aa622007-08-15 14:28:22 +000012These functions expose the Windows registry API to Python. Instead of using an
Georg Brandl8173fb32010-05-19 21:03:51 +000013integer as the registry handle, a :ref:`handle object <handle-object>` is used
14to ensure that the handles are closed correctly, even if the programmer neglects
15to explicitly close them.
Georg Brandl116aa622007-08-15 14:28:22 +000016
Andrew Svetlov616f8032012-10-31 19:29:33 +020017.. _exception-changed:
18
19.. versionchanged:: 3.3
20 Several functions in this module used to raise a
21 :exc:`WindowsError`, which is now an alias of :exc:`OSError`.
22
23.. _functions:
24
25Functions
26------------------
27
Georg Brandl116aa622007-08-15 14:28:22 +000028This module offers the following functions:
29
30
31.. function:: CloseKey(hkey)
32
Georg Brandl8173fb32010-05-19 21:03:51 +000033 Closes a previously opened registry key. The *hkey* argument specifies a
Georg Brandl116aa622007-08-15 14:28:22 +000034 previously opened key.
35
Brian Curtin2d067c82010-05-11 20:35:47 +000036 .. note::
Georg Brandl8173fb32010-05-19 21:03:51 +000037
38 If *hkey* is not closed using this method (or via :meth:`hkey.Close()
39 <PyHKEY.Close>`), it is closed when the *hkey* object is destroyed by
40 Python.
Georg Brandl116aa622007-08-15 14:28:22 +000041
42
43.. function:: ConnectRegistry(computer_name, key)
44
Ezio Melottibc372982010-04-25 17:48:01 +000045 Establishes a connection to a predefined registry handle on another computer,
46 and returns a :ref:`handle object <handle-object>`.
Georg Brandl116aa622007-08-15 14:28:22 +000047
Ezio Melottibc372982010-04-25 17:48:01 +000048 *computer_name* is the name of the remote computer, of the form
Georg Brandl116aa622007-08-15 14:28:22 +000049 ``r"\\computername"``. If ``None``, the local computer is used.
50
51 *key* is the predefined handle to connect to.
52
Andrew Svetlov616f8032012-10-31 19:29:33 +020053 The return value is the handle of the opened key. If the function fails, an
Antoine Pitrou442ee032011-10-12 18:53:23 +020054 :exc:`OSError` exception is raised.
55
Steve Doweree17e372019-12-09 11:18:12 -080056 .. audit-event:: winreg.ConnectRegistry computer_name,key winreg.ConnectRegistry
57
Antoine Pitrou442ee032011-10-12 18:53:23 +020058 .. versionchanged:: 3.3
Andrew Svetlov616f8032012-10-31 19:29:33 +020059 See :ref:`above <exception-changed>`.
Georg Brandl116aa622007-08-15 14:28:22 +000060
61
62.. function:: CreateKey(key, sub_key)
63
Ezio Melottibc372982010-04-25 17:48:01 +000064 Creates or opens the specified key, returning a
65 :ref:`handle object <handle-object>`.
Georg Brandl116aa622007-08-15 14:28:22 +000066
Brian Curtin2d067c82010-05-11 20:35:47 +000067 *key* is an already open key, or one of the predefined
68 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +000069
Ezio Melottibc372982010-04-25 17:48:01 +000070 *sub_key* is a string that names the key this method opens or creates.
Georg Brandl116aa622007-08-15 14:28:22 +000071
Ezio Melottibc372982010-04-25 17:48:01 +000072 If *key* is one of the predefined keys, *sub_key* may be ``None``. In that
73 case, the handle returned is the same key handle passed in to the function.
Georg Brandl116aa622007-08-15 14:28:22 +000074
75 If the key already exists, this function opens the existing key.
76
Andrew Svetlov616f8032012-10-31 19:29:33 +020077 The return value is the handle of the opened key. If the function fails, an
Antoine Pitrou442ee032011-10-12 18:53:23 +020078 :exc:`OSError` exception is raised.
79
Steve Doweree17e372019-12-09 11:18:12 -080080 .. audit-event:: winreg.CreateKey key,sub_key,access winreg.CreateKey
81
82 .. audit-event:: winreg.OpenKey/result key winreg.CreateKey
83
Antoine Pitrou442ee032011-10-12 18:53:23 +020084 .. versionchanged:: 3.3
Andrew Svetlov616f8032012-10-31 19:29:33 +020085 See :ref:`above <exception-changed>`.
Georg Brandl116aa622007-08-15 14:28:22 +000086
87
Brian Curtine9aeca72012-10-29 18:16:39 -050088.. function:: CreateKeyEx(key, sub_key, reserved=0, access=KEY_WRITE)
Brian Curtin3035c392010-04-21 23:56:21 +000089
Ezio Melottibc372982010-04-25 17:48:01 +000090 Creates or opens the specified key, returning a
91 :ref:`handle object <handle-object>`.
Brian Curtin3035c392010-04-21 23:56:21 +000092
Brian Curtin2d067c82010-05-11 20:35:47 +000093 *key* is an already open key, or one of the predefined
94 :ref:`HKEY_* constants <hkey-constants>`.
Brian Curtin3035c392010-04-21 23:56:21 +000095
96 *sub_key* is a string that names the key this method opens or creates.
97
Brian Curtine9aeca72012-10-29 18:16:39 -050098 *reserved* is a reserved integer, and must be zero. The default is zero.
Brian Curtin3035c392010-04-21 23:56:21 +000099
Brian Curtine9aeca72012-10-29 18:16:39 -0500100 *access* is an integer that specifies an access mask that describes the desired
101 security access for the key. Default is :const:`KEY_WRITE`. See
Brian Curtin2d067c82010-05-11 20:35:47 +0000102 :ref:`Access Rights <access-rights>` for other allowed values.
Brian Curtin3035c392010-04-21 23:56:21 +0000103
Ezio Melottibc372982010-04-25 17:48:01 +0000104 If *key* is one of the predefined keys, *sub_key* may be ``None``. In that
105 case, the handle returned is the same key handle passed in to the function.
Brian Curtin3035c392010-04-21 23:56:21 +0000106
107 If the key already exists, this function opens the existing key.
108
Andrew Svetlov616f8032012-10-31 19:29:33 +0200109 The return value is the handle of the opened key. If the function fails, an
Antoine Pitrou442ee032011-10-12 18:53:23 +0200110 :exc:`OSError` exception is raised.
Brian Curtin3035c392010-04-21 23:56:21 +0000111
Steve Doweree17e372019-12-09 11:18:12 -0800112 .. audit-event:: winreg.CreateKey key,sub_key,access winreg.CreateKeyEx
113
114 .. audit-event:: winreg.OpenKey/result key winreg.CreateKeyEx
115
Georg Brandl4c25cf32010-04-22 07:00:42 +0000116 .. versionadded:: 3.2
Brian Curtin3035c392010-04-21 23:56:21 +0000117
Antoine Pitrou442ee032011-10-12 18:53:23 +0200118 .. versionchanged:: 3.3
Andrew Svetlov616f8032012-10-31 19:29:33 +0200119 See :ref:`above <exception-changed>`.
Antoine Pitrou442ee032011-10-12 18:53:23 +0200120
Brian Curtin3035c392010-04-21 23:56:21 +0000121
Georg Brandl116aa622007-08-15 14:28:22 +0000122.. function:: DeleteKey(key, sub_key)
123
124 Deletes the specified key.
125
Brian Curtin2d067c82010-05-11 20:35:47 +0000126 *key* is an already open key, or one of the predefined
127 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000128
Brian Curtin3035c392010-04-21 23:56:21 +0000129 *sub_key* is a string that must be a subkey of the key identified by the *key*
130 parameter. This value must not be ``None``, and the key may not have subkeys.
Georg Brandl116aa622007-08-15 14:28:22 +0000131
132 *This method can not delete keys with subkeys.*
133
134 If the method succeeds, the entire key, including all of its values, is removed.
Andrew Svetlov616f8032012-10-31 19:29:33 +0200135 If the method fails, an :exc:`OSError` exception is raised.
Antoine Pitrou442ee032011-10-12 18:53:23 +0200136
Steve Doweree17e372019-12-09 11:18:12 -0800137 .. audit-event:: winreg.DeleteKey key,sub_key,access winreg.DeleteKey
138
Antoine Pitrou442ee032011-10-12 18:53:23 +0200139 .. versionchanged:: 3.3
Andrew Svetlov616f8032012-10-31 19:29:33 +0200140 See :ref:`above <exception-changed>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000141
142
Brian Curtine9aeca72012-10-29 18:16:39 -0500143.. function:: DeleteKeyEx(key, sub_key, access=KEY_WOW64_64KEY, reserved=0)
Brian Curtin3035c392010-04-21 23:56:21 +0000144
145 Deletes the specified key.
146
147 .. note::
148 The :func:`DeleteKeyEx` function is implemented with the RegDeleteKeyEx
149 Windows API function, which is specific to 64-bit versions of Windows.
Brian Curtin2d067c82010-05-11 20:35:47 +0000150 See the `RegDeleteKeyEx documentation
Georg Brandl5d941342016-02-26 19:37:12 +0100151 <https://msdn.microsoft.com/en-us/library/ms724847%28VS.85%29.aspx>`__.
Brian Curtin3035c392010-04-21 23:56:21 +0000152
Brian Curtin2d067c82010-05-11 20:35:47 +0000153 *key* is an already open key, or one of the predefined
154 :ref:`HKEY_* constants <hkey-constants>`.
Brian Curtin3035c392010-04-21 23:56:21 +0000155
156 *sub_key* is a string that must be a subkey of the key identified by the
157 *key* parameter. This value must not be ``None``, and the key may not have
158 subkeys.
159
Brian Curtine9aeca72012-10-29 18:16:39 -0500160 *reserved* is a reserved integer, and must be zero. The default is zero.
Brian Curtin3035c392010-04-21 23:56:21 +0000161
Brian Curtine9aeca72012-10-29 18:16:39 -0500162 *access* is an integer that specifies an access mask that describes the desired
Zachary Waref7d28742014-01-21 13:49:22 -0600163 security access for the key. Default is :const:`KEY_WOW64_64KEY`. See
Brian Curtin2d067c82010-05-11 20:35:47 +0000164 :ref:`Access Rights <access-rights>` for other allowed values.
Brian Curtin3035c392010-04-21 23:56:21 +0000165
166 *This method can not delete keys with subkeys.*
167
168 If the method succeeds, the entire key, including all of its values, is
Andrew Svetlov616f8032012-10-31 19:29:33 +0200169 removed. If the method fails, an :exc:`OSError` exception is raised.
Brian Curtin3035c392010-04-21 23:56:21 +0000170
171 On unsupported Windows versions, :exc:`NotImplementedError` is raised.
172
Steve Doweree17e372019-12-09 11:18:12 -0800173 .. audit-event:: winreg.DeleteKey key,sub_key,access winreg.DeleteKeyEx
174
Georg Brandl4c25cf32010-04-22 07:00:42 +0000175 .. versionadded:: 3.2
Brian Curtin3035c392010-04-21 23:56:21 +0000176
Antoine Pitrou442ee032011-10-12 18:53:23 +0200177 .. versionchanged:: 3.3
Andrew Svetlov616f8032012-10-31 19:29:33 +0200178 See :ref:`above <exception-changed>`.
Antoine Pitrou442ee032011-10-12 18:53:23 +0200179
Brian Curtin3035c392010-04-21 23:56:21 +0000180
Georg Brandl116aa622007-08-15 14:28:22 +0000181.. function:: DeleteValue(key, value)
182
183 Removes a named value from a registry key.
184
Brian Curtin2d067c82010-05-11 20:35:47 +0000185 *key* is an already open key, or one of the predefined
186 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000187
188 *value* is a string that identifies the value to remove.
189
Steve Doweree17e372019-12-09 11:18:12 -0800190 .. audit-event:: winreg.DeleteValue key,value winreg.DeleteValue
191
Georg Brandl116aa622007-08-15 14:28:22 +0000192
193.. function:: EnumKey(key, index)
194
195 Enumerates subkeys of an open registry key, returning a string.
196
Brian Curtin2d067c82010-05-11 20:35:47 +0000197 *key* is an already open key, or one of the predefined
198 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000199
Ezio Melottibc372982010-04-25 17:48:01 +0000200 *index* is an integer that identifies the index of the key to retrieve.
Georg Brandl116aa622007-08-15 14:28:22 +0000201
Ezio Melottibc372982010-04-25 17:48:01 +0000202 The function retrieves the name of one subkey each time it is called. It is
Andrew Svetlov616f8032012-10-31 19:29:33 +0200203 typically called repeatedly until an :exc:`OSError` exception is
Georg Brandl116aa622007-08-15 14:28:22 +0000204 raised, indicating, no more values are available.
205
Steve Doweree17e372019-12-09 11:18:12 -0800206 .. audit-event:: winreg.EnumKey key,index winreg.EnumKey
207
Antoine Pitrou442ee032011-10-12 18:53:23 +0200208 .. versionchanged:: 3.3
Andrew Svetlov616f8032012-10-31 19:29:33 +0200209 See :ref:`above <exception-changed>`.
Antoine Pitrou442ee032011-10-12 18:53:23 +0200210
Georg Brandl116aa622007-08-15 14:28:22 +0000211
212.. function:: EnumValue(key, index)
213
214 Enumerates values of an open registry key, returning a tuple.
215
Brian Curtin2d067c82010-05-11 20:35:47 +0000216 *key* is an already open key, or one of the predefined
217 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000218
Ezio Melottibc372982010-04-25 17:48:01 +0000219 *index* is an integer that identifies the index of the value to retrieve.
Georg Brandl116aa622007-08-15 14:28:22 +0000220
Ezio Melottibc372982010-04-25 17:48:01 +0000221 The function retrieves the name of one subkey each time it is called. It is
Andrew Svetlov616f8032012-10-31 19:29:33 +0200222 typically called repeatedly, until an :exc:`OSError` exception is
Ezio Melottibc372982010-04-25 17:48:01 +0000223 raised, indicating no more values.
Georg Brandl116aa622007-08-15 14:28:22 +0000224
225 The result is a tuple of 3 items:
226
227 +-------+--------------------------------------------+
228 | Index | Meaning |
229 +=======+============================================+
230 | ``0`` | A string that identifies the value name |
231 +-------+--------------------------------------------+
232 | ``1`` | An object that holds the value data, and |
233 | | whose type depends on the underlying |
234 | | registry type |
235 +-------+--------------------------------------------+
236 | ``2`` | An integer that identifies the type of the |
Georg Brandl8173fb32010-05-19 21:03:51 +0000237 | | value data (see table in docs for |
238 | | :meth:`SetValueEx`) |
Georg Brandl116aa622007-08-15 14:28:22 +0000239 +-------+--------------------------------------------+
240
Steve Doweree17e372019-12-09 11:18:12 -0800241 .. audit-event:: winreg.EnumValue key,index winreg.EnumValue
242
Antoine Pitrou442ee032011-10-12 18:53:23 +0200243 .. versionchanged:: 3.3
Andrew Svetlov616f8032012-10-31 19:29:33 +0200244 See :ref:`above <exception-changed>`.
Antoine Pitrou442ee032011-10-12 18:53:23 +0200245
Georg Brandl116aa622007-08-15 14:28:22 +0000246
Serhiy Storchakaddb961d2018-10-26 09:00:49 +0300247.. index::
Serhiy Storchaka913876d2018-10-28 13:41:26 +0200248 single: % (percent); environment variables expansion (Windows)
Serhiy Storchakaddb961d2018-10-26 09:00:49 +0300249
Ezio Melotti985e24d2009-09-13 07:54:02 +0000250.. function:: ExpandEnvironmentStrings(str)
Christian Heimes2380ac72008-01-09 00:17:24 +0000251
Georg Brandl8173fb32010-05-19 21:03:51 +0000252 Expands environment variable placeholders ``%NAME%`` in strings like
253 :const:`REG_EXPAND_SZ`::
Christian Heimes2380ac72008-01-09 00:17:24 +0000254
Ezio Melotti985e24d2009-09-13 07:54:02 +0000255 >>> ExpandEnvironmentStrings('%windir%')
256 'C:\\Windows'
Christian Heimes2380ac72008-01-09 00:17:24 +0000257
Steve Doweree17e372019-12-09 11:18:12 -0800258 .. audit-event:: winreg.ExpandEnvironmentStrings str winreg.ExpandEnvironmentStrings
259
Christian Heimes2380ac72008-01-09 00:17:24 +0000260
Georg Brandl116aa622007-08-15 14:28:22 +0000261.. function:: FlushKey(key)
262
263 Writes all the attributes of a key to the registry.
264
Brian Curtin2d067c82010-05-11 20:35:47 +0000265 *key* is an already open key, or one of the predefined
266 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000267
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000268 It is not necessary to call :func:`FlushKey` to change a key. Registry changes are
Ezio Melottibc372982010-04-25 17:48:01 +0000269 flushed to disk by the registry using its lazy flusher. Registry changes are
270 also flushed to disk at system shutdown. Unlike :func:`CloseKey`, the
271 :func:`FlushKey` method returns only when all the data has been written to the
Georg Brandl116aa622007-08-15 14:28:22 +0000272 registry. An application should only call :func:`FlushKey` if it requires
Ezio Melottibc372982010-04-25 17:48:01 +0000273 absolute certainty that registry changes are on disk.
Georg Brandl116aa622007-08-15 14:28:22 +0000274
275 .. note::
276
Ezio Melottibc372982010-04-25 17:48:01 +0000277 If you don't know whether a :func:`FlushKey` call is required, it probably
Georg Brandl116aa622007-08-15 14:28:22 +0000278 isn't.
279
280
Alexandre Vassalotti6461e102008-05-15 22:09:29 +0000281.. function:: LoadKey(key, sub_key, file_name)
Georg Brandl116aa622007-08-15 14:28:22 +0000282
Ezio Melottibc372982010-04-25 17:48:01 +0000283 Creates a subkey under the specified key and stores registration information
Georg Brandl116aa622007-08-15 14:28:22 +0000284 from a specified file into that subkey.
285
Brian Curtin2d067c82010-05-11 20:35:47 +0000286 *key* is a handle returned by :func:`ConnectRegistry` or one of the constants
287 :const:`HKEY_USERS` or :const:`HKEY_LOCAL_MACHINE`.
Georg Brandl116aa622007-08-15 14:28:22 +0000288
Georg Brandl8173fb32010-05-19 21:03:51 +0000289 *sub_key* is a string that identifies the subkey to load.
Georg Brandl116aa622007-08-15 14:28:22 +0000290
291 *file_name* is the name of the file to load registry data from. This file must
292 have been created with the :func:`SaveKey` function. Under the file allocation
293 table (FAT) file system, the filename may not have an extension.
294
Georg Brandl8173fb32010-05-19 21:03:51 +0000295 A call to :func:`LoadKey` fails if the calling process does not have the
296 :const:`SE_RESTORE_PRIVILEGE` privilege. Note that privileges are different
Brian Curtin2d067c82010-05-11 20:35:47 +0000297 from permissions -- see the `RegLoadKey documentation
Georg Brandl5d941342016-02-26 19:37:12 +0100298 <https://msdn.microsoft.com/en-us/library/ms724889%28v=VS.85%29.aspx>`__ for
Brian Curtin2d067c82010-05-11 20:35:47 +0000299 more details.
Georg Brandl116aa622007-08-15 14:28:22 +0000300
Ezio Melottibc372982010-04-25 17:48:01 +0000301 If *key* is a handle returned by :func:`ConnectRegistry`, then the path
Georg Brandl8173fb32010-05-19 21:03:51 +0000302 specified in *file_name* is relative to the remote computer.
Georg Brandl116aa622007-08-15 14:28:22 +0000303
Steve Doweree17e372019-12-09 11:18:12 -0800304 .. audit-event:: winreg.LoadKey key,sub_key,file_name winreg.LoadKey
305
Georg Brandl116aa622007-08-15 14:28:22 +0000306
Brian Curtin13c70342012-05-29 18:34:45 -0500307.. function:: OpenKey(key, sub_key, reserved=0, access=KEY_READ)
Brian Curtine9aeca72012-10-29 18:16:39 -0500308 OpenKeyEx(key, sub_key, reserved=0, access=KEY_READ)
Georg Brandl116aa622007-08-15 14:28:22 +0000309
Ezio Melottibc372982010-04-25 17:48:01 +0000310 Opens the specified key, returning a :ref:`handle object <handle-object>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000311
Brian Curtin2d067c82010-05-11 20:35:47 +0000312 *key* is an already open key, or one of the predefined
313 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000314
315 *sub_key* is a string that identifies the sub_key to open.
316
Brian Curtin13c70342012-05-29 18:34:45 -0500317 *reserved* is a reserved integer, and must be zero. The default is zero.
Georg Brandl116aa622007-08-15 14:28:22 +0000318
Brian Curtin13c70342012-05-29 18:34:45 -0500319 *access* is an integer that specifies an access mask that describes the desired
Georg Brandl8173fb32010-05-19 21:03:51 +0000320 security access for the key. Default is :const:`KEY_READ`. See :ref:`Access
321 Rights <access-rights>` for other allowed values.
Georg Brandl116aa622007-08-15 14:28:22 +0000322
323 The result is a new handle to the specified key.
324
Antoine Pitrou442ee032011-10-12 18:53:23 +0200325 If the function fails, :exc:`OSError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000326
Steve Doweree17e372019-12-09 11:18:12 -0800327 .. audit-event:: winreg.OpenKey key,sub_key,access winreg.OpenKey
328
329 .. audit-event:: winreg.OpenKey/result key winreg.OpenKey
330
Georg Brandl61063cc2012-06-24 22:48:30 +0200331 .. versionchanged:: 3.2
332 Allow the use of named arguments.
Brian Curtin1771b542010-09-27 17:56:36 +0000333
Antoine Pitrou442ee032011-10-12 18:53:23 +0200334 .. versionchanged:: 3.3
Andrew Svetlov616f8032012-10-31 19:29:33 +0200335 See :ref:`above <exception-changed>`.
Antoine Pitrou442ee032011-10-12 18:53:23 +0200336
Georg Brandl116aa622007-08-15 14:28:22 +0000337
Georg Brandl116aa622007-08-15 14:28:22 +0000338.. function:: QueryInfoKey(key)
339
340 Returns information about a key, as a tuple.
341
Brian Curtin2d067c82010-05-11 20:35:47 +0000342 *key* is an already open key, or one of the predefined
343 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000344
345 The result is a tuple of 3 items:
346
347 +-------+---------------------------------------------+
348 | Index | Meaning |
349 +=======+=============================================+
350 | ``0`` | An integer giving the number of sub keys |
351 | | this key has. |
352 +-------+---------------------------------------------+
353 | ``1`` | An integer giving the number of values this |
354 | | key has. |
355 +-------+---------------------------------------------+
Georg Brandlba956ae2007-11-29 17:24:34 +0000356 | ``2`` | An integer giving when the key was last |
Georg Brandl116aa622007-08-15 14:28:22 +0000357 | | modified (if available) as 100's of |
Zachary Waref9dd2742014-08-11 15:00:48 -0500358 | | nanoseconds since Jan 1, 1601. |
Georg Brandl116aa622007-08-15 14:28:22 +0000359 +-------+---------------------------------------------+
360
Steve Doweree17e372019-12-09 11:18:12 -0800361 .. audit-event:: winreg.QueryInfoKey key winreg.QueryInfoKey
362
Georg Brandl116aa622007-08-15 14:28:22 +0000363
364.. function:: QueryValue(key, sub_key)
365
Ezio Melottibc372982010-04-25 17:48:01 +0000366 Retrieves the unnamed value for a key, as a string.
Georg Brandl116aa622007-08-15 14:28:22 +0000367
Brian Curtin2d067c82010-05-11 20:35:47 +0000368 *key* is an already open key, or one of the predefined
369 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000370
Ezio Melottibc372982010-04-25 17:48:01 +0000371 *sub_key* is a string that holds the name of the subkey with which the value is
372 associated. If this parameter is ``None`` or empty, the function retrieves the
373 value set by the :func:`SetValue` method for the key identified by *key*.
Georg Brandl116aa622007-08-15 14:28:22 +0000374
Benjamin Petersond23f8222009-04-05 19:13:16 +0000375 Values in the registry have name, type, and data components. This method
Serhiy Storchakae835b312019-10-30 21:37:16 +0200376 retrieves the data for a key's first value that has a ``NULL`` name. But the
Benjamin Petersond23f8222009-04-05 19:13:16 +0000377 underlying API call doesn't return the type, so always use
378 :func:`QueryValueEx` if possible.
Georg Brandl116aa622007-08-15 14:28:22 +0000379
Steve Doweree17e372019-12-09 11:18:12 -0800380 .. audit-event:: winreg.QueryValue key,sub_key,value_name winreg.QueryValue
381
Georg Brandl116aa622007-08-15 14:28:22 +0000382
383.. function:: QueryValueEx(key, value_name)
384
Ezio Melottibc372982010-04-25 17:48:01 +0000385 Retrieves the type and data for a specified value name associated with
386 an open registry key.
Georg Brandl116aa622007-08-15 14:28:22 +0000387
Brian Curtin2d067c82010-05-11 20:35:47 +0000388 *key* is an already open key, or one of the predefined
389 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000390
391 *value_name* is a string indicating the value to query.
392
393 The result is a tuple of 2 items:
394
395 +-------+-----------------------------------------+
396 | Index | Meaning |
397 +=======+=========================================+
398 | ``0`` | The value of the registry item. |
399 +-------+-----------------------------------------+
400 | ``1`` | An integer giving the registry type for |
Georg Brandl8173fb32010-05-19 21:03:51 +0000401 | | this value (see table in docs for |
402 | | :meth:`SetValueEx`) |
Georg Brandl116aa622007-08-15 14:28:22 +0000403 +-------+-----------------------------------------+
404
Steve Doweree17e372019-12-09 11:18:12 -0800405 .. audit-event:: winreg.QueryValue key,sub_key,value_name winreg.QueryValueEx
406
Georg Brandl116aa622007-08-15 14:28:22 +0000407
408.. function:: SaveKey(key, file_name)
409
410 Saves the specified key, and all its subkeys to the specified file.
411
Brian Curtin2d067c82010-05-11 20:35:47 +0000412 *key* is an already open key, or one of the predefined
413 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000414
Georg Brandl8173fb32010-05-19 21:03:51 +0000415 *file_name* is the name of the file to save registry data to. This file
416 cannot already exist. If this filename includes an extension, it cannot be
417 used on file allocation table (FAT) file systems by the :meth:`LoadKey`
418 method.
Georg Brandl116aa622007-08-15 14:28:22 +0000419
Ezio Melottibc372982010-04-25 17:48:01 +0000420 If *key* represents a key on a remote computer, the path described by
Georg Brandl116aa622007-08-15 14:28:22 +0000421 *file_name* is relative to the remote computer. The caller of this method must
Ezio Melottibc372982010-04-25 17:48:01 +0000422 possess the :const:`SeBackupPrivilege` security privilege. Note that
Brian Curtin2d067c82010-05-11 20:35:47 +0000423 privileges are different than permissions -- see the
424 `Conflicts Between User Rights and Permissions documentation
Georg Brandl5d941342016-02-26 19:37:12 +0100425 <https://msdn.microsoft.com/en-us/library/ms724878%28v=VS.85%29.aspx>`__
Brian Curtin2d067c82010-05-11 20:35:47 +0000426 for more details.
Georg Brandl116aa622007-08-15 14:28:22 +0000427
Serhiy Storchakae835b312019-10-30 21:37:16 +0200428 This function passes ``NULL`` for *security_attributes* to the API.
Georg Brandl116aa622007-08-15 14:28:22 +0000429
Steve Doweree17e372019-12-09 11:18:12 -0800430 .. audit-event:: winreg.SaveKey key,file_name winreg.SaveKey
431
Georg Brandl116aa622007-08-15 14:28:22 +0000432
433.. function:: SetValue(key, sub_key, type, value)
434
435 Associates a value with a specified key.
436
Brian Curtin2d067c82010-05-11 20:35:47 +0000437 *key* is an already open key, or one of the predefined
438 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000439
Ezio Melottibc372982010-04-25 17:48:01 +0000440 *sub_key* is a string that names the subkey with which the value is associated.
Georg Brandl116aa622007-08-15 14:28:22 +0000441
442 *type* is an integer that specifies the type of the data. Currently this must be
443 :const:`REG_SZ`, meaning only strings are supported. Use the :func:`SetValueEx`
444 function for support for other data types.
445
446 *value* is a string that specifies the new value.
447
448 If the key specified by the *sub_key* parameter does not exist, the SetValue
449 function creates it.
450
451 Value lengths are limited by available memory. Long values (more than 2048
452 bytes) should be stored as files with the filenames stored in the configuration
453 registry. This helps the registry perform efficiently.
454
Ezio Melottibc372982010-04-25 17:48:01 +0000455 The key identified by the *key* parameter must have been opened with
Georg Brandl116aa622007-08-15 14:28:22 +0000456 :const:`KEY_SET_VALUE` access.
457
Steve Doweree17e372019-12-09 11:18:12 -0800458 .. audit-event:: winreg.SetValue key,sub_key,type,value winreg.SetValue
459
Georg Brandl116aa622007-08-15 14:28:22 +0000460
461.. function:: SetValueEx(key, value_name, reserved, type, value)
462
463 Stores data in the value field of an open registry key.
464
Brian Curtin2d067c82010-05-11 20:35:47 +0000465 *key* is an already open key, or one of the predefined
466 :ref:`HKEY_* constants <hkey-constants>`.
Georg Brandl116aa622007-08-15 14:28:22 +0000467
Ezio Melottibc372982010-04-25 17:48:01 +0000468 *value_name* is a string that names the subkey with which the value is
Georg Brandl116aa622007-08-15 14:28:22 +0000469 associated.
470
Brian Curtine9aeca72012-10-29 18:16:39 -0500471 *reserved* can be anything -- zero is always passed to the API.
472
Brian Curtin2d067c82010-05-11 20:35:47 +0000473 *type* is an integer that specifies the type of the data. See
474 :ref:`Value Types <value-types>` for the available types.
Georg Brandl116aa622007-08-15 14:28:22 +0000475
Georg Brandl116aa622007-08-15 14:28:22 +0000476 *value* is a string that specifies the new value.
477
478 This method can also set additional value and type information for the specified
479 key. The key identified by the key parameter must have been opened with
480 :const:`KEY_SET_VALUE` access.
481
Ezio Melottibc372982010-04-25 17:48:01 +0000482 To open the key, use the :func:`CreateKey` or :func:`OpenKey` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000483
484 Value lengths are limited by available memory. Long values (more than 2048
485 bytes) should be stored as files with the filenames stored in the configuration
486 registry. This helps the registry perform efficiently.
487
Steve Doweree17e372019-12-09 11:18:12 -0800488 .. audit-event:: winreg.SetValue key,sub_key,type,value winreg.SetValueEx
489
Georg Brandl116aa622007-08-15 14:28:22 +0000490
Brian Curtin3035c392010-04-21 23:56:21 +0000491.. function:: DisableReflectionKey(key)
492
493 Disables registry reflection for 32-bit processes running on a 64-bit
Georg Brandl8173fb32010-05-19 21:03:51 +0000494 operating system.
Brian Curtin3035c392010-04-21 23:56:21 +0000495
Georg Brandl8173fb32010-05-19 21:03:51 +0000496 *key* is an already open key, or one of the predefined :ref:`HKEY_* constants
497 <hkey-constants>`.
Brian Curtin3035c392010-04-21 23:56:21 +0000498
David Hed5e8e02019-07-31 23:49:55 +0100499 Will generally raise :exc:`NotImplementedError` if executed on a 32-bit operating
Georg Brandl8173fb32010-05-19 21:03:51 +0000500 system.
Brian Curtin3035c392010-04-21 23:56:21 +0000501
502 If the key is not on the reflection list, the function succeeds but has no
Georg Brandl8173fb32010-05-19 21:03:51 +0000503 effect. Disabling reflection for a key does not affect reflection of any
Brian Curtin3035c392010-04-21 23:56:21 +0000504 subkeys.
505
Steve Doweree17e372019-12-09 11:18:12 -0800506 .. audit-event:: winreg.DisableReflectionKey key winreg.DisableReflectionKey
507
Brian Curtin3035c392010-04-21 23:56:21 +0000508
509.. function:: EnableReflectionKey(key)
510
511 Restores registry reflection for the specified disabled key.
512
Georg Brandl8173fb32010-05-19 21:03:51 +0000513 *key* is an already open key, or one of the predefined :ref:`HKEY_* constants
514 <hkey-constants>`.
Brian Curtin3035c392010-04-21 23:56:21 +0000515
David Hed5e8e02019-07-31 23:49:55 +0100516 Will generally raise :exc:`NotImplementedError` if executed on a 32-bit operating
Georg Brandl8173fb32010-05-19 21:03:51 +0000517 system.
Brian Curtin3035c392010-04-21 23:56:21 +0000518
519 Restoring reflection for a key does not affect reflection of any subkeys.
520
Steve Doweree17e372019-12-09 11:18:12 -0800521 .. audit-event:: winreg.EnableReflectionKey key winreg.EnableReflectionKey
522
Brian Curtin3035c392010-04-21 23:56:21 +0000523
524.. function:: QueryReflectionKey(key)
525
526 Determines the reflection state for the specified key.
527
Brian Curtin2d067c82010-05-11 20:35:47 +0000528 *key* is an already open key, or one of the predefined
529 :ref:`HKEY_* constants <hkey-constants>`.
Brian Curtin3035c392010-04-21 23:56:21 +0000530
531 Returns ``True`` if reflection is disabled.
532
David Hed5e8e02019-07-31 23:49:55 +0100533 Will generally raise :exc:`NotImplementedError` if executed on a 32-bit
Georg Brandl8173fb32010-05-19 21:03:51 +0000534 operating system.
Brian Curtin3035c392010-04-21 23:56:21 +0000535
Steve Doweree17e372019-12-09 11:18:12 -0800536 .. audit-event:: winreg.QueryReflectionKey key winreg.QueryReflectionKey
537
Brian Curtin3035c392010-04-21 23:56:21 +0000538
Brian Curtin2d067c82010-05-11 20:35:47 +0000539.. _constants:
540
541Constants
542------------------
543
544The following constants are defined for use in many :mod:`_winreg` functions.
545
546.. _hkey-constants:
547
548HKEY_* Constants
549++++++++++++++++
550
551.. data:: HKEY_CLASSES_ROOT
552
553 Registry entries subordinate to this key define types (or classes) of
554 documents and the properties associated with those types. Shell and
555 COM applications use the information stored under this key.
556
557
558.. data:: HKEY_CURRENT_USER
559
560 Registry entries subordinate to this key define the preferences of
561 the current user. These preferences include the settings of
562 environment variables, data about program groups, colors, printers,
563 network connections, and application preferences.
564
565.. data:: HKEY_LOCAL_MACHINE
566
567 Registry entries subordinate to this key define the physical state
568 of the computer, including data about the bus type, system memory,
569 and installed hardware and software.
570
571.. data:: HKEY_USERS
572
573 Registry entries subordinate to this key define the default user
574 configuration for new users on the local computer and the user
575 configuration for the current user.
576
577.. data:: HKEY_PERFORMANCE_DATA
578
579 Registry entries subordinate to this key allow you to access
580 performance data. The data is not actually stored in the registry;
581 the registry functions cause the system to collect the data from
582 its source.
583
584
585.. data:: HKEY_CURRENT_CONFIG
586
587 Contains information about the current hardware profile of the
588 local computer system.
589
590.. data:: HKEY_DYN_DATA
591
592 This key is not used in versions of Windows after 98.
593
594
595.. _access-rights:
596
597Access Rights
598+++++++++++++
599
600For more information, see `Registry Key Security and Access
Georg Brandl5d941342016-02-26 19:37:12 +0100601<https://msdn.microsoft.com/en-us/library/ms724878%28v=VS.85%29.aspx>`__.
Brian Curtin2d067c82010-05-11 20:35:47 +0000602
603.. data:: KEY_ALL_ACCESS
604
605 Combines the STANDARD_RIGHTS_REQUIRED, :const:`KEY_QUERY_VALUE`,
606 :const:`KEY_SET_VALUE`, :const:`KEY_CREATE_SUB_KEY`,
607 :const:`KEY_ENUMERATE_SUB_KEYS`, :const:`KEY_NOTIFY`,
608 and :const:`KEY_CREATE_LINK` access rights.
609
610.. data:: KEY_WRITE
611
612 Combines the STANDARD_RIGHTS_WRITE, :const:`KEY_SET_VALUE`, and
613 :const:`KEY_CREATE_SUB_KEY` access rights.
614
615.. data:: KEY_READ
616
617 Combines the STANDARD_RIGHTS_READ, :const:`KEY_QUERY_VALUE`,
618 :const:`KEY_ENUMERATE_SUB_KEYS`, and :const:`KEY_NOTIFY` values.
619
620.. data:: KEY_EXECUTE
621
622 Equivalent to :const:`KEY_READ`.
623
624.. data:: KEY_QUERY_VALUE
625
626 Required to query the values of a registry key.
627
628.. data:: KEY_SET_VALUE
629
630 Required to create, delete, or set a registry value.
631
632.. data:: KEY_CREATE_SUB_KEY
633
634 Required to create a subkey of a registry key.
635
636.. data:: KEY_ENUMERATE_SUB_KEYS
637
638 Required to enumerate the subkeys of a registry key.
639
640.. data:: KEY_NOTIFY
641
642 Required to request change notifications for a registry key or for
643 subkeys of a registry key.
644
645.. data:: KEY_CREATE_LINK
646
647 Reserved for system use.
648
649
650.. _64-bit-access-rights:
651
65264-bit Specific
653***************
654
Georg Brandl6faee4e2010-09-21 14:48:28 +0000655For more information, see `Accessing an Alternate Registry View
Georg Brandl5d941342016-02-26 19:37:12 +0100656<https://msdn.microsoft.com/en-us/library/aa384129(v=VS.85).aspx>`__.
Brian Curtin2d067c82010-05-11 20:35:47 +0000657
658.. data:: KEY_WOW64_64KEY
659
660 Indicates that an application on 64-bit Windows should operate on
661 the 64-bit registry view.
662
663.. data:: KEY_WOW64_32KEY
664
665 Indicates that an application on 64-bit Windows should operate on
666 the 32-bit registry view.
667
668
669.. _value-types:
670
671Value Types
672+++++++++++
673
674For more information, see `Registry Value Types
Georg Brandl5d941342016-02-26 19:37:12 +0100675<https://msdn.microsoft.com/en-us/library/ms724884%28v=VS.85%29.aspx>`__.
Brian Curtin2d067c82010-05-11 20:35:47 +0000676
677.. data:: REG_BINARY
678
679 Binary data in any form.
680
681.. data:: REG_DWORD
682
683 32-bit number.
684
685.. data:: REG_DWORD_LITTLE_ENDIAN
686
Steve Dower80ac11d2016-05-24 15:42:04 -0700687 A 32-bit number in little-endian format. Equivalent to :const:`REG_DWORD`.
Brian Curtin2d067c82010-05-11 20:35:47 +0000688
689.. data:: REG_DWORD_BIG_ENDIAN
690
691 A 32-bit number in big-endian format.
692
693.. data:: REG_EXPAND_SZ
694
695 Null-terminated string containing references to environment
696 variables (``%PATH%``).
697
698.. data:: REG_LINK
699
700 A Unicode symbolic link.
701
702.. data:: REG_MULTI_SZ
703
704 A sequence of null-terminated strings, terminated by two null characters.
705 (Python handles this termination automatically.)
706
707.. data:: REG_NONE
708
709 No defined value type.
710
Steve Dower80ac11d2016-05-24 15:42:04 -0700711.. data:: REG_QWORD
712
713 A 64-bit number.
714
Steve Dower4d4bc422016-05-25 11:26:07 -0700715 .. versionadded:: 3.6
716
Steve Dower80ac11d2016-05-24 15:42:04 -0700717.. data:: REG_QWORD_LITTLE_ENDIAN
718
719 A 64-bit number in little-endian format. Equivalent to :const:`REG_QWORD`.
720
Steve Dower4d4bc422016-05-25 11:26:07 -0700721 .. versionadded:: 3.6
722
Brian Curtin2d067c82010-05-11 20:35:47 +0000723.. data:: REG_RESOURCE_LIST
724
725 A device-driver resource list.
726
727.. data:: REG_FULL_RESOURCE_DESCRIPTOR
728
729 A hardware setting.
730
731.. data:: REG_RESOURCE_REQUIREMENTS_LIST
732
733 A hardware resource list.
734
735.. data:: REG_SZ
736
737 A null-terminated string.
738
739
Georg Brandl116aa622007-08-15 14:28:22 +0000740.. _handle-object:
741
742Registry Handle Objects
743-----------------------
744
745This object wraps a Windows HKEY object, automatically closing it when the
746object is destroyed. To guarantee cleanup, you can call either the
Georg Brandl8173fb32010-05-19 21:03:51 +0000747:meth:`~PyHKEY.Close` method on the object, or the :func:`CloseKey` function.
Georg Brandl116aa622007-08-15 14:28:22 +0000748
749All registry functions in this module return one of these objects.
750
Ezio Melottibc372982010-04-25 17:48:01 +0000751All registry functions in this module which accept a handle object also accept
752an integer, however, use of the handle object is encouraged.
Georg Brandl116aa622007-08-15 14:28:22 +0000753
Ezio Melottibc372982010-04-25 17:48:01 +0000754Handle objects provide semantics for :meth:`__bool__` -- thus ::
Georg Brandl116aa622007-08-15 14:28:22 +0000755
756 if handle:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000757 print("Yes")
Georg Brandl116aa622007-08-15 14:28:22 +0000758
759will print ``Yes`` if the handle is currently valid (has not been closed or
760detached).
761
762The object also support comparison semantics, so handle objects will compare
763true if they both reference the same underlying Windows handle value.
764
Georg Brandl22b34312009-07-26 14:54:51 +0000765Handle objects can be converted to an integer (e.g., using the built-in
Georg Brandl116aa622007-08-15 14:28:22 +0000766:func:`int` function), in which case the underlying Windows handle value is
Georg Brandl8173fb32010-05-19 21:03:51 +0000767returned. You can also use the :meth:`~PyHKEY.Detach` method to return the
768integer handle, and also disconnect the Windows handle from the handle object.
Georg Brandl116aa622007-08-15 14:28:22 +0000769
770
771.. method:: PyHKEY.Close()
772
773 Closes the underlying Windows handle.
774
775 If the handle is already closed, no error is raised.
776
777
778.. method:: PyHKEY.Detach()
779
780 Detaches the Windows handle from the handle object.
781
Georg Brandl5c106642007-11-29 17:41:05 +0000782 The result is an integer that holds the value of the handle before it is
783 detached. If the handle is already detached or closed, this will return
784 zero.
Georg Brandl116aa622007-08-15 14:28:22 +0000785
786 After calling this function, the handle is effectively invalidated, but the
Ezio Melottibc372982010-04-25 17:48:01 +0000787 handle is not closed. You would call this function when you need the
788 underlying Win32 handle to exist beyond the lifetime of the handle object.
Georg Brandl116aa622007-08-15 14:28:22 +0000789
Steve Doweree17e372019-12-09 11:18:12 -0800790 .. audit-event:: winreg.PyHKEY.Detach key winreg.PyHKEY.Detach
791
792
Christian Heimes2380ac72008-01-09 00:17:24 +0000793.. method:: PyHKEY.__enter__()
Andre Delfinodcc997c2020-12-16 22:37:28 -0300794 PyHKEY.__exit__(*exc_info)
Christian Heimes2380ac72008-01-09 00:17:24 +0000795
Georg Brandl8173fb32010-05-19 21:03:51 +0000796 The HKEY object implements :meth:`~object.__enter__` and
797 :meth:`~object.__exit__` and thus supports the context protocol for the
798 :keyword:`with` statement::
Christian Heimes2380ac72008-01-09 00:17:24 +0000799
800 with OpenKey(HKEY_LOCAL_MACHINE, "foo") as key:
Georg Brandl8173fb32010-05-19 21:03:51 +0000801 ... # work with key
Christian Heimes2380ac72008-01-09 00:17:24 +0000802
803 will automatically close *key* when control leaves the :keyword:`with` block.
804
Christian Heimes2380ac72008-01-09 00:17:24 +0000805