Serhiy Storchaka | 29b0a26 | 2016-12-04 10:20:55 +0200 | [diff] [blame] | 1 | :mod:`winreg` --- Windows registry access |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2 | ========================================= |
| 3 | |
Georg Brandl | 38feaf0 | 2008-05-25 07:45:51 +0000 | [diff] [blame] | 4 | .. module:: winreg |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 5 | :platform: Windows |
| 6 | :synopsis: Routines and objects for manipulating the Windows registry. |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 7 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 8 | .. sectionauthor:: Mark Hammond <MarkH@ActiveState.com> |
| 9 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 10 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 11 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 12 | These functions expose the Windows registry API to Python. Instead of using an |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 13 | integer as the registry handle, a :ref:`handle object <handle-object>` is used |
| 14 | to ensure that the handles are closed correctly, even if the programmer neglects |
| 15 | to explicitly close them. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 16 | |
Andrew Svetlov | 616f803 | 2012-10-31 19:29:33 +0200 | [diff] [blame] | 17 | .. _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 | |
| 25 | Functions |
| 26 | ------------------ |
| 27 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 28 | This module offers the following functions: |
| 29 | |
| 30 | |
| 31 | .. function:: CloseKey(hkey) |
| 32 | |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 33 | Closes a previously opened registry key. The *hkey* argument specifies a |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 34 | previously opened key. |
| 35 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 36 | .. note:: |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 37 | |
| 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 41 | |
| 42 | |
| 43 | .. function:: ConnectRegistry(computer_name, key) |
| 44 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 45 | Establishes a connection to a predefined registry handle on another computer, |
| 46 | and returns a :ref:`handle object <handle-object>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 47 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 48 | *computer_name* is the name of the remote computer, of the form |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 49 | ``r"\\computername"``. If ``None``, the local computer is used. |
| 50 | |
| 51 | *key* is the predefined handle to connect to. |
| 52 | |
Andrew Svetlov | 616f803 | 2012-10-31 19:29:33 +0200 | [diff] [blame] | 53 | The return value is the handle of the opened key. If the function fails, an |
Antoine Pitrou | 442ee03 | 2011-10-12 18:53:23 +0200 | [diff] [blame] | 54 | :exc:`OSError` exception is raised. |
| 55 | |
Steve Dower | ee17e37 | 2019-12-09 11:18:12 -0800 | [diff] [blame] | 56 | .. audit-event:: winreg.ConnectRegistry computer_name,key winreg.ConnectRegistry |
| 57 | |
Antoine Pitrou | 442ee03 | 2011-10-12 18:53:23 +0200 | [diff] [blame] | 58 | .. versionchanged:: 3.3 |
Andrew Svetlov | 616f803 | 2012-10-31 19:29:33 +0200 | [diff] [blame] | 59 | See :ref:`above <exception-changed>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 60 | |
| 61 | |
| 62 | .. function:: CreateKey(key, sub_key) |
| 63 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 64 | Creates or opens the specified key, returning a |
| 65 | :ref:`handle object <handle-object>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 66 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 67 | *key* is an already open key, or one of the predefined |
| 68 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 69 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 70 | *sub_key* is a string that names the key this method opens or creates. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 71 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 72 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 74 | |
| 75 | If the key already exists, this function opens the existing key. |
| 76 | |
Andrew Svetlov | 616f803 | 2012-10-31 19:29:33 +0200 | [diff] [blame] | 77 | The return value is the handle of the opened key. If the function fails, an |
Antoine Pitrou | 442ee03 | 2011-10-12 18:53:23 +0200 | [diff] [blame] | 78 | :exc:`OSError` exception is raised. |
| 79 | |
Steve Dower | ee17e37 | 2019-12-09 11:18:12 -0800 | [diff] [blame] | 80 | .. audit-event:: winreg.CreateKey key,sub_key,access winreg.CreateKey |
| 81 | |
| 82 | .. audit-event:: winreg.OpenKey/result key winreg.CreateKey |
| 83 | |
Antoine Pitrou | 442ee03 | 2011-10-12 18:53:23 +0200 | [diff] [blame] | 84 | .. versionchanged:: 3.3 |
Andrew Svetlov | 616f803 | 2012-10-31 19:29:33 +0200 | [diff] [blame] | 85 | See :ref:`above <exception-changed>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 86 | |
| 87 | |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 88 | .. function:: CreateKeyEx(key, sub_key, reserved=0, access=KEY_WRITE) |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 89 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 90 | Creates or opens the specified key, returning a |
| 91 | :ref:`handle object <handle-object>`. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 92 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 93 | *key* is an already open key, or one of the predefined |
| 94 | :ref:`HKEY_* constants <hkey-constants>`. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 95 | |
| 96 | *sub_key* is a string that names the key this method opens or creates. |
| 97 | |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 98 | *reserved* is a reserved integer, and must be zero. The default is zero. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 99 | |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 100 | *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 Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 102 | :ref:`Access Rights <access-rights>` for other allowed values. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 103 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 104 | 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 Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 106 | |
| 107 | If the key already exists, this function opens the existing key. |
| 108 | |
Andrew Svetlov | 616f803 | 2012-10-31 19:29:33 +0200 | [diff] [blame] | 109 | The return value is the handle of the opened key. If the function fails, an |
Antoine Pitrou | 442ee03 | 2011-10-12 18:53:23 +0200 | [diff] [blame] | 110 | :exc:`OSError` exception is raised. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 111 | |
Steve Dower | ee17e37 | 2019-12-09 11:18:12 -0800 | [diff] [blame] | 112 | .. audit-event:: winreg.CreateKey key,sub_key,access winreg.CreateKeyEx |
| 113 | |
| 114 | .. audit-event:: winreg.OpenKey/result key winreg.CreateKeyEx |
| 115 | |
Georg Brandl | 4c25cf3 | 2010-04-22 07:00:42 +0000 | [diff] [blame] | 116 | .. versionadded:: 3.2 |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 117 | |
Antoine Pitrou | 442ee03 | 2011-10-12 18:53:23 +0200 | [diff] [blame] | 118 | .. versionchanged:: 3.3 |
Andrew Svetlov | 616f803 | 2012-10-31 19:29:33 +0200 | [diff] [blame] | 119 | See :ref:`above <exception-changed>`. |
Antoine Pitrou | 442ee03 | 2011-10-12 18:53:23 +0200 | [diff] [blame] | 120 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 121 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 122 | .. function:: DeleteKey(key, sub_key) |
| 123 | |
| 124 | Deletes the specified key. |
| 125 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 126 | *key* is an already open key, or one of the predefined |
| 127 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 128 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 129 | *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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 131 | |
| 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 Svetlov | 616f803 | 2012-10-31 19:29:33 +0200 | [diff] [blame] | 135 | If the method fails, an :exc:`OSError` exception is raised. |
Antoine Pitrou | 442ee03 | 2011-10-12 18:53:23 +0200 | [diff] [blame] | 136 | |
Steve Dower | ee17e37 | 2019-12-09 11:18:12 -0800 | [diff] [blame] | 137 | .. audit-event:: winreg.DeleteKey key,sub_key,access winreg.DeleteKey |
| 138 | |
Antoine Pitrou | 442ee03 | 2011-10-12 18:53:23 +0200 | [diff] [blame] | 139 | .. versionchanged:: 3.3 |
Andrew Svetlov | 616f803 | 2012-10-31 19:29:33 +0200 | [diff] [blame] | 140 | See :ref:`above <exception-changed>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 141 | |
| 142 | |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 143 | .. function:: DeleteKeyEx(key, sub_key, access=KEY_WOW64_64KEY, reserved=0) |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 144 | |
| 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 Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 150 | See the `RegDeleteKeyEx documentation |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 151 | <https://msdn.microsoft.com/en-us/library/ms724847%28VS.85%29.aspx>`__. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 152 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 153 | *key* is an already open key, or one of the predefined |
| 154 | :ref:`HKEY_* constants <hkey-constants>`. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 155 | |
| 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 Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 160 | *reserved* is a reserved integer, and must be zero. The default is zero. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 161 | |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 162 | *access* is an integer that specifies an access mask that describes the desired |
Zachary Ware | f7d2874 | 2014-01-21 13:49:22 -0600 | [diff] [blame] | 163 | security access for the key. Default is :const:`KEY_WOW64_64KEY`. See |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 164 | :ref:`Access Rights <access-rights>` for other allowed values. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 165 | |
| 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 Svetlov | 616f803 | 2012-10-31 19:29:33 +0200 | [diff] [blame] | 169 | removed. If the method fails, an :exc:`OSError` exception is raised. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 170 | |
| 171 | On unsupported Windows versions, :exc:`NotImplementedError` is raised. |
| 172 | |
Steve Dower | ee17e37 | 2019-12-09 11:18:12 -0800 | [diff] [blame] | 173 | .. audit-event:: winreg.DeleteKey key,sub_key,access winreg.DeleteKeyEx |
| 174 | |
Georg Brandl | 4c25cf3 | 2010-04-22 07:00:42 +0000 | [diff] [blame] | 175 | .. versionadded:: 3.2 |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 176 | |
Antoine Pitrou | 442ee03 | 2011-10-12 18:53:23 +0200 | [diff] [blame] | 177 | .. versionchanged:: 3.3 |
Andrew Svetlov | 616f803 | 2012-10-31 19:29:33 +0200 | [diff] [blame] | 178 | See :ref:`above <exception-changed>`. |
Antoine Pitrou | 442ee03 | 2011-10-12 18:53:23 +0200 | [diff] [blame] | 179 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 180 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 181 | .. function:: DeleteValue(key, value) |
| 182 | |
| 183 | Removes a named value from a registry key. |
| 184 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 185 | *key* is an already open key, or one of the predefined |
| 186 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 187 | |
| 188 | *value* is a string that identifies the value to remove. |
| 189 | |
Steve Dower | ee17e37 | 2019-12-09 11:18:12 -0800 | [diff] [blame] | 190 | .. audit-event:: winreg.DeleteValue key,value winreg.DeleteValue |
| 191 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 192 | |
| 193 | .. function:: EnumKey(key, index) |
| 194 | |
| 195 | Enumerates subkeys of an open registry key, returning a string. |
| 196 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 197 | *key* is an already open key, or one of the predefined |
| 198 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 199 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 200 | *index* is an integer that identifies the index of the key to retrieve. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 201 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 202 | The function retrieves the name of one subkey each time it is called. It is |
Andrew Svetlov | 616f803 | 2012-10-31 19:29:33 +0200 | [diff] [blame] | 203 | typically called repeatedly until an :exc:`OSError` exception is |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 204 | raised, indicating, no more values are available. |
| 205 | |
Steve Dower | ee17e37 | 2019-12-09 11:18:12 -0800 | [diff] [blame] | 206 | .. audit-event:: winreg.EnumKey key,index winreg.EnumKey |
| 207 | |
Antoine Pitrou | 442ee03 | 2011-10-12 18:53:23 +0200 | [diff] [blame] | 208 | .. versionchanged:: 3.3 |
Andrew Svetlov | 616f803 | 2012-10-31 19:29:33 +0200 | [diff] [blame] | 209 | See :ref:`above <exception-changed>`. |
Antoine Pitrou | 442ee03 | 2011-10-12 18:53:23 +0200 | [diff] [blame] | 210 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 211 | |
| 212 | .. function:: EnumValue(key, index) |
| 213 | |
| 214 | Enumerates values of an open registry key, returning a tuple. |
| 215 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 216 | *key* is an already open key, or one of the predefined |
| 217 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 218 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 219 | *index* is an integer that identifies the index of the value to retrieve. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 220 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 221 | The function retrieves the name of one subkey each time it is called. It is |
Andrew Svetlov | 616f803 | 2012-10-31 19:29:33 +0200 | [diff] [blame] | 222 | typically called repeatedly, until an :exc:`OSError` exception is |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 223 | raised, indicating no more values. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 224 | |
| 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 Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 237 | | | value data (see table in docs for | |
| 238 | | | :meth:`SetValueEx`) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 239 | +-------+--------------------------------------------+ |
| 240 | |
Steve Dower | ee17e37 | 2019-12-09 11:18:12 -0800 | [diff] [blame] | 241 | .. audit-event:: winreg.EnumValue key,index winreg.EnumValue |
| 242 | |
Antoine Pitrou | 442ee03 | 2011-10-12 18:53:23 +0200 | [diff] [blame] | 243 | .. versionchanged:: 3.3 |
Andrew Svetlov | 616f803 | 2012-10-31 19:29:33 +0200 | [diff] [blame] | 244 | See :ref:`above <exception-changed>`. |
Antoine Pitrou | 442ee03 | 2011-10-12 18:53:23 +0200 | [diff] [blame] | 245 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 246 | |
Serhiy Storchaka | ddb961d | 2018-10-26 09:00:49 +0300 | [diff] [blame] | 247 | .. index:: |
Serhiy Storchaka | 913876d | 2018-10-28 13:41:26 +0200 | [diff] [blame] | 248 | single: % (percent); environment variables expansion (Windows) |
Serhiy Storchaka | ddb961d | 2018-10-26 09:00:49 +0300 | [diff] [blame] | 249 | |
Ezio Melotti | 985e24d | 2009-09-13 07:54:02 +0000 | [diff] [blame] | 250 | .. function:: ExpandEnvironmentStrings(str) |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 251 | |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 252 | Expands environment variable placeholders ``%NAME%`` in strings like |
| 253 | :const:`REG_EXPAND_SZ`:: |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 254 | |
Ezio Melotti | 985e24d | 2009-09-13 07:54:02 +0000 | [diff] [blame] | 255 | >>> ExpandEnvironmentStrings('%windir%') |
| 256 | 'C:\\Windows' |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 257 | |
Steve Dower | ee17e37 | 2019-12-09 11:18:12 -0800 | [diff] [blame] | 258 | .. audit-event:: winreg.ExpandEnvironmentStrings str winreg.ExpandEnvironmentStrings |
| 259 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 260 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 261 | .. function:: FlushKey(key) |
| 262 | |
| 263 | Writes all the attributes of a key to the registry. |
| 264 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 265 | *key* is an already open key, or one of the predefined |
| 266 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 267 | |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 268 | It is not necessary to call :func:`FlushKey` to change a key. Registry changes are |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 269 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 272 | registry. An application should only call :func:`FlushKey` if it requires |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 273 | absolute certainty that registry changes are on disk. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 274 | |
| 275 | .. note:: |
| 276 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 277 | If you don't know whether a :func:`FlushKey` call is required, it probably |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 278 | isn't. |
| 279 | |
| 280 | |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 281 | .. function:: LoadKey(key, sub_key, file_name) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 282 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 283 | Creates a subkey under the specified key and stores registration information |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 284 | from a specified file into that subkey. |
| 285 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 286 | *key* is a handle returned by :func:`ConnectRegistry` or one of the constants |
| 287 | :const:`HKEY_USERS` or :const:`HKEY_LOCAL_MACHINE`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 288 | |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 289 | *sub_key* is a string that identifies the subkey to load. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 290 | |
| 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 Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 295 | 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 Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 297 | from permissions -- see the `RegLoadKey documentation |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 298 | <https://msdn.microsoft.com/en-us/library/ms724889%28v=VS.85%29.aspx>`__ for |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 299 | more details. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 300 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 301 | If *key* is a handle returned by :func:`ConnectRegistry`, then the path |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 302 | specified in *file_name* is relative to the remote computer. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 303 | |
Steve Dower | ee17e37 | 2019-12-09 11:18:12 -0800 | [diff] [blame] | 304 | .. audit-event:: winreg.LoadKey key,sub_key,file_name winreg.LoadKey |
| 305 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 306 | |
Brian Curtin | 13c7034 | 2012-05-29 18:34:45 -0500 | [diff] [blame] | 307 | .. function:: OpenKey(key, sub_key, reserved=0, access=KEY_READ) |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 308 | OpenKeyEx(key, sub_key, reserved=0, access=KEY_READ) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 309 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 310 | Opens the specified key, returning a :ref:`handle object <handle-object>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 311 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 312 | *key* is an already open key, or one of the predefined |
| 313 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 314 | |
| 315 | *sub_key* is a string that identifies the sub_key to open. |
| 316 | |
Brian Curtin | 13c7034 | 2012-05-29 18:34:45 -0500 | [diff] [blame] | 317 | *reserved* is a reserved integer, and must be zero. The default is zero. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 318 | |
Brian Curtin | 13c7034 | 2012-05-29 18:34:45 -0500 | [diff] [blame] | 319 | *access* is an integer that specifies an access mask that describes the desired |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 320 | security access for the key. Default is :const:`KEY_READ`. See :ref:`Access |
| 321 | Rights <access-rights>` for other allowed values. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 322 | |
| 323 | The result is a new handle to the specified key. |
| 324 | |
Antoine Pitrou | 442ee03 | 2011-10-12 18:53:23 +0200 | [diff] [blame] | 325 | If the function fails, :exc:`OSError` is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 326 | |
Steve Dower | ee17e37 | 2019-12-09 11:18:12 -0800 | [diff] [blame] | 327 | .. audit-event:: winreg.OpenKey key,sub_key,access winreg.OpenKey |
| 328 | |
| 329 | .. audit-event:: winreg.OpenKey/result key winreg.OpenKey |
| 330 | |
Georg Brandl | 61063cc | 2012-06-24 22:48:30 +0200 | [diff] [blame] | 331 | .. versionchanged:: 3.2 |
| 332 | Allow the use of named arguments. |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 333 | |
Antoine Pitrou | 442ee03 | 2011-10-12 18:53:23 +0200 | [diff] [blame] | 334 | .. versionchanged:: 3.3 |
Andrew Svetlov | 616f803 | 2012-10-31 19:29:33 +0200 | [diff] [blame] | 335 | See :ref:`above <exception-changed>`. |
Antoine Pitrou | 442ee03 | 2011-10-12 18:53:23 +0200 | [diff] [blame] | 336 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 337 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 338 | .. function:: QueryInfoKey(key) |
| 339 | |
| 340 | Returns information about a key, as a tuple. |
| 341 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 342 | *key* is an already open key, or one of the predefined |
| 343 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 344 | |
| 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 Brandl | ba956ae | 2007-11-29 17:24:34 +0000 | [diff] [blame] | 356 | | ``2`` | An integer giving when the key was last | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 357 | | | modified (if available) as 100's of | |
Zachary Ware | f9dd274 | 2014-08-11 15:00:48 -0500 | [diff] [blame] | 358 | | | nanoseconds since Jan 1, 1601. | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 359 | +-------+---------------------------------------------+ |
| 360 | |
Steve Dower | ee17e37 | 2019-12-09 11:18:12 -0800 | [diff] [blame] | 361 | .. audit-event:: winreg.QueryInfoKey key winreg.QueryInfoKey |
| 362 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 363 | |
| 364 | .. function:: QueryValue(key, sub_key) |
| 365 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 366 | Retrieves the unnamed value for a key, as a string. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 367 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 368 | *key* is an already open key, or one of the predefined |
| 369 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 370 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 371 | *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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 374 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 375 | Values in the registry have name, type, and data components. This method |
Serhiy Storchaka | e835b31 | 2019-10-30 21:37:16 +0200 | [diff] [blame] | 376 | retrieves the data for a key's first value that has a ``NULL`` name. But the |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 377 | underlying API call doesn't return the type, so always use |
| 378 | :func:`QueryValueEx` if possible. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 379 | |
Steve Dower | ee17e37 | 2019-12-09 11:18:12 -0800 | [diff] [blame] | 380 | .. audit-event:: winreg.QueryValue key,sub_key,value_name winreg.QueryValue |
| 381 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 382 | |
| 383 | .. function:: QueryValueEx(key, value_name) |
| 384 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 385 | Retrieves the type and data for a specified value name associated with |
| 386 | an open registry key. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 387 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 388 | *key* is an already open key, or one of the predefined |
| 389 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 390 | |
| 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 Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 401 | | | this value (see table in docs for | |
| 402 | | | :meth:`SetValueEx`) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 403 | +-------+-----------------------------------------+ |
| 404 | |
Steve Dower | ee17e37 | 2019-12-09 11:18:12 -0800 | [diff] [blame] | 405 | .. audit-event:: winreg.QueryValue key,sub_key,value_name winreg.QueryValueEx |
| 406 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 407 | |
| 408 | .. function:: SaveKey(key, file_name) |
| 409 | |
| 410 | Saves the specified key, and all its subkeys to the specified file. |
| 411 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 412 | *key* is an already open key, or one of the predefined |
| 413 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 414 | |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 415 | *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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 419 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 420 | If *key* represents a key on a remote computer, the path described by |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 421 | *file_name* is relative to the remote computer. The caller of this method must |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 422 | possess the :const:`SeBackupPrivilege` security privilege. Note that |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 423 | privileges are different than permissions -- see the |
| 424 | `Conflicts Between User Rights and Permissions documentation |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 425 | <https://msdn.microsoft.com/en-us/library/ms724878%28v=VS.85%29.aspx>`__ |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 426 | for more details. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 427 | |
Serhiy Storchaka | e835b31 | 2019-10-30 21:37:16 +0200 | [diff] [blame] | 428 | This function passes ``NULL`` for *security_attributes* to the API. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 429 | |
Steve Dower | ee17e37 | 2019-12-09 11:18:12 -0800 | [diff] [blame] | 430 | .. audit-event:: winreg.SaveKey key,file_name winreg.SaveKey |
| 431 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 432 | |
| 433 | .. function:: SetValue(key, sub_key, type, value) |
| 434 | |
| 435 | Associates a value with a specified key. |
| 436 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 437 | *key* is an already open key, or one of the predefined |
| 438 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 439 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 440 | *sub_key* is a string that names the subkey with which the value is associated. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 441 | |
| 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 Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 455 | The key identified by the *key* parameter must have been opened with |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 456 | :const:`KEY_SET_VALUE` access. |
| 457 | |
Steve Dower | ee17e37 | 2019-12-09 11:18:12 -0800 | [diff] [blame] | 458 | .. audit-event:: winreg.SetValue key,sub_key,type,value winreg.SetValue |
| 459 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 460 | |
| 461 | .. function:: SetValueEx(key, value_name, reserved, type, value) |
| 462 | |
| 463 | Stores data in the value field of an open registry key. |
| 464 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 465 | *key* is an already open key, or one of the predefined |
| 466 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 467 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 468 | *value_name* is a string that names the subkey with which the value is |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 469 | associated. |
| 470 | |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 471 | *reserved* can be anything -- zero is always passed to the API. |
| 472 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 473 | *type* is an integer that specifies the type of the data. See |
| 474 | :ref:`Value Types <value-types>` for the available types. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 475 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 476 | *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 Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 482 | To open the key, use the :func:`CreateKey` or :func:`OpenKey` methods. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 483 | |
| 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 Dower | ee17e37 | 2019-12-09 11:18:12 -0800 | [diff] [blame] | 488 | .. audit-event:: winreg.SetValue key,sub_key,type,value winreg.SetValueEx |
| 489 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 490 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 491 | .. function:: DisableReflectionKey(key) |
| 492 | |
| 493 | Disables registry reflection for 32-bit processes running on a 64-bit |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 494 | operating system. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 495 | |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 496 | *key* is an already open key, or one of the predefined :ref:`HKEY_* constants |
| 497 | <hkey-constants>`. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 498 | |
David H | ed5e8e0 | 2019-07-31 23:49:55 +0100 | [diff] [blame] | 499 | Will generally raise :exc:`NotImplementedError` if executed on a 32-bit operating |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 500 | system. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 501 | |
| 502 | If the key is not on the reflection list, the function succeeds but has no |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 503 | effect. Disabling reflection for a key does not affect reflection of any |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 504 | subkeys. |
| 505 | |
Steve Dower | ee17e37 | 2019-12-09 11:18:12 -0800 | [diff] [blame] | 506 | .. audit-event:: winreg.DisableReflectionKey key winreg.DisableReflectionKey |
| 507 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 508 | |
| 509 | .. function:: EnableReflectionKey(key) |
| 510 | |
| 511 | Restores registry reflection for the specified disabled key. |
| 512 | |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 513 | *key* is an already open key, or one of the predefined :ref:`HKEY_* constants |
| 514 | <hkey-constants>`. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 515 | |
David H | ed5e8e0 | 2019-07-31 23:49:55 +0100 | [diff] [blame] | 516 | Will generally raise :exc:`NotImplementedError` if executed on a 32-bit operating |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 517 | system. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 518 | |
| 519 | Restoring reflection for a key does not affect reflection of any subkeys. |
| 520 | |
Steve Dower | ee17e37 | 2019-12-09 11:18:12 -0800 | [diff] [blame] | 521 | .. audit-event:: winreg.EnableReflectionKey key winreg.EnableReflectionKey |
| 522 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 523 | |
| 524 | .. function:: QueryReflectionKey(key) |
| 525 | |
| 526 | Determines the reflection state for the specified key. |
| 527 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 528 | *key* is an already open key, or one of the predefined |
| 529 | :ref:`HKEY_* constants <hkey-constants>`. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 530 | |
| 531 | Returns ``True`` if reflection is disabled. |
| 532 | |
David H | ed5e8e0 | 2019-07-31 23:49:55 +0100 | [diff] [blame] | 533 | Will generally raise :exc:`NotImplementedError` if executed on a 32-bit |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 534 | operating system. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 535 | |
Steve Dower | ee17e37 | 2019-12-09 11:18:12 -0800 | [diff] [blame] | 536 | .. audit-event:: winreg.QueryReflectionKey key winreg.QueryReflectionKey |
| 537 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 538 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 539 | .. _constants: |
| 540 | |
| 541 | Constants |
| 542 | ------------------ |
| 543 | |
| 544 | The following constants are defined for use in many :mod:`_winreg` functions. |
| 545 | |
| 546 | .. _hkey-constants: |
| 547 | |
| 548 | HKEY_* 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 | |
| 597 | Access Rights |
| 598 | +++++++++++++ |
| 599 | |
| 600 | For more information, see `Registry Key Security and Access |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 601 | <https://msdn.microsoft.com/en-us/library/ms724878%28v=VS.85%29.aspx>`__. |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 602 | |
| 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 | |
| 652 | 64-bit Specific |
| 653 | *************** |
| 654 | |
Georg Brandl | 6faee4e | 2010-09-21 14:48:28 +0000 | [diff] [blame] | 655 | For more information, see `Accessing an Alternate Registry View |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 656 | <https://msdn.microsoft.com/en-us/library/aa384129(v=VS.85).aspx>`__. |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 657 | |
| 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 | |
| 671 | Value Types |
| 672 | +++++++++++ |
| 673 | |
| 674 | For more information, see `Registry Value Types |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 675 | <https://msdn.microsoft.com/en-us/library/ms724884%28v=VS.85%29.aspx>`__. |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 676 | |
| 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 Dower | 80ac11d | 2016-05-24 15:42:04 -0700 | [diff] [blame] | 687 | A 32-bit number in little-endian format. Equivalent to :const:`REG_DWORD`. |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 688 | |
| 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 Dower | 80ac11d | 2016-05-24 15:42:04 -0700 | [diff] [blame] | 711 | .. data:: REG_QWORD |
| 712 | |
| 713 | A 64-bit number. |
| 714 | |
Steve Dower | 4d4bc42 | 2016-05-25 11:26:07 -0700 | [diff] [blame] | 715 | .. versionadded:: 3.6 |
| 716 | |
Steve Dower | 80ac11d | 2016-05-24 15:42:04 -0700 | [diff] [blame] | 717 | .. data:: REG_QWORD_LITTLE_ENDIAN |
| 718 | |
| 719 | A 64-bit number in little-endian format. Equivalent to :const:`REG_QWORD`. |
| 720 | |
Steve Dower | 4d4bc42 | 2016-05-25 11:26:07 -0700 | [diff] [blame] | 721 | .. versionadded:: 3.6 |
| 722 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 723 | .. 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 740 | .. _handle-object: |
| 741 | |
| 742 | Registry Handle Objects |
| 743 | ----------------------- |
| 744 | |
| 745 | This object wraps a Windows HKEY object, automatically closing it when the |
| 746 | object is destroyed. To guarantee cleanup, you can call either the |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 747 | :meth:`~PyHKEY.Close` method on the object, or the :func:`CloseKey` function. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 748 | |
| 749 | All registry functions in this module return one of these objects. |
| 750 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 751 | All registry functions in this module which accept a handle object also accept |
| 752 | an integer, however, use of the handle object is encouraged. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 753 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 754 | Handle objects provide semantics for :meth:`__bool__` -- thus :: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 755 | |
| 756 | if handle: |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 757 | print("Yes") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 758 | |
| 759 | will print ``Yes`` if the handle is currently valid (has not been closed or |
| 760 | detached). |
| 761 | |
| 762 | The object also support comparison semantics, so handle objects will compare |
| 763 | true if they both reference the same underlying Windows handle value. |
| 764 | |
Georg Brandl | 22b3431 | 2009-07-26 14:54:51 +0000 | [diff] [blame] | 765 | Handle objects can be converted to an integer (e.g., using the built-in |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 766 | :func:`int` function), in which case the underlying Windows handle value is |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 767 | returned. You can also use the :meth:`~PyHKEY.Detach` method to return the |
| 768 | integer handle, and also disconnect the Windows handle from the handle object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 769 | |
| 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 Brandl | 5c10664 | 2007-11-29 17:41:05 +0000 | [diff] [blame] | 782 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 785 | |
| 786 | After calling this function, the handle is effectively invalidated, but the |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 787 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 789 | |
Steve Dower | ee17e37 | 2019-12-09 11:18:12 -0800 | [diff] [blame] | 790 | .. audit-event:: winreg.PyHKEY.Detach key winreg.PyHKEY.Detach |
| 791 | |
| 792 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 793 | .. method:: PyHKEY.__enter__() |
Andre Delfino | dcc997c | 2020-12-16 22:37:28 -0300 | [diff] [blame] | 794 | PyHKEY.__exit__(*exc_info) |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 795 | |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 796 | 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 Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 799 | |
| 800 | with OpenKey(HKEY_LOCAL_MACHINE, "foo") as key: |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 801 | ... # work with key |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 802 | |
| 803 | will automatically close *key* when control leaves the :keyword:`with` block. |
| 804 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 805 | |