Georg Brandl | 38feaf0 | 2008-05-25 07:45:51 +0000 | [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. |
| 7 | .. sectionauthor:: Mark Hammond <MarkH@ActiveState.com> |
| 8 | |
| 9 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 10 | These functions expose the Windows registry API to Python. Instead of using an |
| 11 | integer as the registry handle, a handle object is used to ensure that the |
| 12 | handles are closed correctly, even if the programmer neglects to explicitly |
| 13 | close them. |
| 14 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 15 | This module offers the following functions: |
| 16 | |
| 17 | |
| 18 | .. function:: CloseKey(hkey) |
| 19 | |
| 20 | Closes a previously opened registry key. The hkey argument specifies a |
| 21 | previously opened key. |
| 22 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame^] | 23 | .. note:: |
| 24 | If *hkey* is not closed using this method (or via :meth:`hkey.Close() <PyHKEY.Close>`), |
| 25 | it is closed when the *hkey* object is destroyed by Python. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 26 | |
| 27 | |
| 28 | .. function:: ConnectRegistry(computer_name, key) |
| 29 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 30 | Establishes a connection to a predefined registry handle on another computer, |
| 31 | and returns a :ref:`handle object <handle-object>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 32 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 33 | *computer_name* is the name of the remote computer, of the form |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 34 | ``r"\\computername"``. If ``None``, the local computer is used. |
| 35 | |
| 36 | *key* is the predefined handle to connect to. |
| 37 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 38 | The return value is the handle of the opened key. If the function fails, a |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 39 | :exc:`WindowsError` exception is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 40 | |
| 41 | |
| 42 | .. function:: CreateKey(key, sub_key) |
| 43 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 44 | Creates or opens the specified key, returning a |
| 45 | :ref:`handle object <handle-object>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 46 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame^] | 47 | *key* is an already open key, or one of the predefined |
| 48 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 49 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 50 | *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] | 51 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 52 | If *key* is one of the predefined keys, *sub_key* may be ``None``. In that |
| 53 | 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] | 54 | |
| 55 | If the key already exists, this function opens the existing key. |
| 56 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 57 | The return value is the handle of the opened key. If the function fails, a |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 58 | :exc:`WindowsError` exception is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 59 | |
| 60 | |
Brian Curtin | d855948 | 2010-04-24 17:21:31 +0000 | [diff] [blame] | 61 | .. function:: CreateKeyEx(key, sub_key[, res[, sam]]) |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 62 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 63 | Creates or opens the specified key, returning a |
| 64 | :ref:`handle object <handle-object>`. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 65 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame^] | 66 | *key* is an already open key, or one of the predefined |
| 67 | :ref:`HKEY_* constants <hkey-constants>`. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 68 | |
| 69 | *sub_key* is a string that names the key this method opens or creates. |
| 70 | |
| 71 | *res* is a reserved integer, and must be zero. The default is zero. |
| 72 | |
| 73 | *sam* is an integer that specifies an access mask that describes the desired |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame^] | 74 | security access for the key. Default is :const:`KEY_ALL_ACCESS`. See |
| 75 | :ref:`Access Rights <access-rights>` for other allowed values. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 76 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 77 | If *key* is one of the predefined keys, *sub_key* may be ``None``. In that |
| 78 | 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] | 79 | |
| 80 | If the key already exists, this function opens the existing key. |
| 81 | |
| 82 | The return value is the handle of the opened key. If the function fails, a |
| 83 | :exc:`WindowsError` exception is raised. |
| 84 | |
Georg Brandl | 4c25cf3 | 2010-04-22 07:00:42 +0000 | [diff] [blame] | 85 | .. versionadded:: 3.2 |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 86 | |
| 87 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 88 | .. function:: DeleteKey(key, sub_key) |
| 89 | |
| 90 | Deletes the specified key. |
| 91 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame^] | 92 | *key* is an already open key, or one of the predefined |
| 93 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 94 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 95 | *sub_key* is a string that must be a subkey of the key identified by the *key* |
| 96 | 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] | 97 | |
| 98 | *This method can not delete keys with subkeys.* |
| 99 | |
| 100 | If the method succeeds, the entire key, including all of its values, is removed. |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 101 | If the method fails, a :exc:`WindowsError` exception is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 102 | |
| 103 | |
Brian Curtin | d855948 | 2010-04-24 17:21:31 +0000 | [diff] [blame] | 104 | .. function:: DeleteKeyEx(key, sub_key[, sam[, res]]) |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 105 | |
| 106 | Deletes the specified key. |
| 107 | |
| 108 | .. note:: |
| 109 | The :func:`DeleteKeyEx` function is implemented with the RegDeleteKeyEx |
| 110 | Windows API function, which is specific to 64-bit versions of Windows. |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame^] | 111 | See the `RegDeleteKeyEx documentation |
| 112 | <http://msdn.microsoft.com/en-us/library/ms724847%28VS.85%29.aspx>`__. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 113 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame^] | 114 | *key* is an already open key, or one of the predefined |
| 115 | :ref:`HKEY_* constants <hkey-constants>`. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 116 | |
| 117 | *sub_key* is a string that must be a subkey of the key identified by the |
| 118 | *key* parameter. This value must not be ``None``, and the key may not have |
| 119 | subkeys. |
| 120 | |
| 121 | *res* is a reserved integer, and must be zero. The default is zero. |
| 122 | |
| 123 | *sam* is an integer that specifies an access mask that describes the |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame^] | 124 | security access for the key. Default is :const:`KEY_ALL_ACCESS`. See |
| 125 | :ref:`Access Rights <access-rights>` for other allowed values. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 126 | |
| 127 | *This method can not delete keys with subkeys.* |
| 128 | |
| 129 | If the method succeeds, the entire key, including all of its values, is |
| 130 | removed. If the method fails, a :exc:`WindowsError` exception is raised. |
| 131 | |
| 132 | On unsupported Windows versions, :exc:`NotImplementedError` is raised. |
| 133 | |
Georg Brandl | 4c25cf3 | 2010-04-22 07:00:42 +0000 | [diff] [blame] | 134 | .. versionadded:: 3.2 |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 135 | |
| 136 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 137 | .. function:: DeleteValue(key, value) |
| 138 | |
| 139 | Removes a named value from a registry key. |
| 140 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame^] | 141 | *key* is an already open key, or one of the predefined |
| 142 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 143 | |
| 144 | *value* is a string that identifies the value to remove. |
| 145 | |
| 146 | |
| 147 | .. function:: EnumKey(key, index) |
| 148 | |
| 149 | Enumerates subkeys of an open registry key, returning a string. |
| 150 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame^] | 151 | *key* is an already open key, or one of the predefined |
| 152 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 153 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 154 | *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] | 155 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 156 | The function retrieves the name of one subkey each time it is called. It is |
| 157 | typically called repeatedly until a :exc:`WindowsError` exception is |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 158 | raised, indicating, no more values are available. |
| 159 | |
| 160 | |
| 161 | .. function:: EnumValue(key, index) |
| 162 | |
| 163 | Enumerates values of an open registry key, returning a tuple. |
| 164 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame^] | 165 | *key* is an already open key, or one of the predefined |
| 166 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 167 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 168 | *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] | 169 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 170 | The function retrieves the name of one subkey each time it is called. It is |
| 171 | typically called repeatedly, until a :exc:`WindowsError` exception is |
| 172 | raised, indicating no more values. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 173 | |
| 174 | The result is a tuple of 3 items: |
| 175 | |
| 176 | +-------+--------------------------------------------+ |
| 177 | | Index | Meaning | |
| 178 | +=======+============================================+ |
| 179 | | ``0`` | A string that identifies the value name | |
| 180 | +-------+--------------------------------------------+ |
| 181 | | ``1`` | An object that holds the value data, and | |
| 182 | | | whose type depends on the underlying | |
| 183 | | | registry type | |
| 184 | +-------+--------------------------------------------+ |
| 185 | | ``2`` | An integer that identifies the type of the | |
| 186 | | | value data | |
| 187 | +-------+--------------------------------------------+ |
| 188 | |
| 189 | |
Ezio Melotti | 985e24d | 2009-09-13 07:54:02 +0000 | [diff] [blame] | 190 | .. function:: ExpandEnvironmentStrings(str) |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 191 | |
Ezio Melotti | 985e24d | 2009-09-13 07:54:02 +0000 | [diff] [blame] | 192 | Expands environment strings %NAME% in unicode string like :const:`REG_EXPAND_SZ`:: |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 193 | |
Ezio Melotti | 985e24d | 2009-09-13 07:54:02 +0000 | [diff] [blame] | 194 | >>> ExpandEnvironmentStrings('%windir%') |
| 195 | 'C:\\Windows' |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 196 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 197 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 198 | .. function:: FlushKey(key) |
| 199 | |
| 200 | Writes all the attributes of a key to the registry. |
| 201 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame^] | 202 | *key* is an already open key, or one of the predefined |
| 203 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 204 | |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 205 | 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] | 206 | flushed to disk by the registry using its lazy flusher. Registry changes are |
| 207 | also flushed to disk at system shutdown. Unlike :func:`CloseKey`, the |
| 208 | :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] | 209 | registry. An application should only call :func:`FlushKey` if it requires |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 210 | absolute certainty that registry changes are on disk. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 211 | |
| 212 | .. note:: |
| 213 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 214 | 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] | 215 | isn't. |
| 216 | |
| 217 | |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 218 | .. function:: LoadKey(key, sub_key, file_name) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 219 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 220 | Creates a subkey under the specified key and stores registration information |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 221 | from a specified file into that subkey. |
| 222 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame^] | 223 | *key* is a handle returned by :func:`ConnectRegistry` or one of the constants |
| 224 | :const:`HKEY_USERS` or :const:`HKEY_LOCAL_MACHINE`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 225 | |
| 226 | *sub_key* is a string that identifies the sub_key to load. |
| 227 | |
| 228 | *file_name* is the name of the file to load registry data from. This file must |
| 229 | have been created with the :func:`SaveKey` function. Under the file allocation |
| 230 | table (FAT) file system, the filename may not have an extension. |
| 231 | |
| 232 | A call to LoadKey() fails if the calling process does not have the |
| 233 | :const:`SE_RESTORE_PRIVILEGE` privilege. Note that privileges are different than |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame^] | 234 | from permissions -- see the `RegLoadKey documentation |
| 235 | <http://msdn.microsoft.com/en-us/library/ms724889%28v=VS.85%29.aspx>`__ for |
| 236 | more details. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 237 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 238 | If *key* is a handle returned by :func:`ConnectRegistry`, then the path |
| 239 | specified in *fileName* is relative to the remote computer. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 240 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 241 | The Win32 documentation implies *key* must be in the :const:`HKEY_USER` or |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 242 | :const:`HKEY_LOCAL_MACHINE` tree. This may or may not be true. |
| 243 | |
| 244 | |
Brian Curtin | d855948 | 2010-04-24 17:21:31 +0000 | [diff] [blame] | 245 | .. function:: OpenKey(key, sub_key[, res[, sam]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 246 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 247 | Opens the specified key, returning a :ref:`handle object <handle-object>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 248 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame^] | 249 | *key* is an already open key, or one of the predefined |
| 250 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 251 | |
| 252 | *sub_key* is a string that identifies the sub_key to open. |
| 253 | |
| 254 | *res* is a reserved integer, and must be zero. The default is zero. |
| 255 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 256 | *sam* is an integer that specifies an access mask that describes the desired |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame^] | 257 | security access for the key. Default is :const:`KEY_READ`. See |
| 258 | :ref:`Access Rights <access-rights>` for other allowed values. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 259 | |
| 260 | The result is a new handle to the specified key. |
| 261 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 262 | If the function fails, :exc:`WindowsError` is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 263 | |
| 264 | |
| 265 | .. function:: OpenKeyEx() |
| 266 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 267 | The functionality of :func:`OpenKeyEx` is provided via :func:`OpenKey`, |
| 268 | by the use of default arguments. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 269 | |
| 270 | |
| 271 | .. function:: QueryInfoKey(key) |
| 272 | |
| 273 | Returns information about a key, as a tuple. |
| 274 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame^] | 275 | *key* is an already open key, or one of the predefined |
| 276 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 277 | |
| 278 | The result is a tuple of 3 items: |
| 279 | |
| 280 | +-------+---------------------------------------------+ |
| 281 | | Index | Meaning | |
| 282 | +=======+=============================================+ |
| 283 | | ``0`` | An integer giving the number of sub keys | |
| 284 | | | this key has. | |
| 285 | +-------+---------------------------------------------+ |
| 286 | | ``1`` | An integer giving the number of values this | |
| 287 | | | key has. | |
| 288 | +-------+---------------------------------------------+ |
Georg Brandl | ba956ae | 2007-11-29 17:24:34 +0000 | [diff] [blame] | 289 | | ``2`` | An integer giving when the key was last | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 290 | | | modified (if available) as 100's of | |
| 291 | | | nanoseconds since Jan 1, 1600. | |
| 292 | +-------+---------------------------------------------+ |
| 293 | |
| 294 | |
| 295 | .. function:: QueryValue(key, sub_key) |
| 296 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 297 | Retrieves the unnamed value for a key, as a string. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 298 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame^] | 299 | *key* is an already open key, or one of the predefined |
| 300 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 301 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 302 | *sub_key* is a string that holds the name of the subkey with which the value is |
| 303 | associated. If this parameter is ``None`` or empty, the function retrieves the |
| 304 | 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] | 305 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 306 | Values in the registry have name, type, and data components. This method |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 307 | 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] | 308 | underlying API call doesn't return the type, so always use |
| 309 | :func:`QueryValueEx` if possible. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 310 | |
| 311 | |
| 312 | .. function:: QueryValueEx(key, value_name) |
| 313 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 314 | Retrieves the type and data for a specified value name associated with |
| 315 | an open registry key. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 316 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame^] | 317 | *key* is an already open key, or one of the predefined |
| 318 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 319 | |
| 320 | *value_name* is a string indicating the value to query. |
| 321 | |
| 322 | The result is a tuple of 2 items: |
| 323 | |
| 324 | +-------+-----------------------------------------+ |
| 325 | | Index | Meaning | |
| 326 | +=======+=========================================+ |
| 327 | | ``0`` | The value of the registry item. | |
| 328 | +-------+-----------------------------------------+ |
| 329 | | ``1`` | An integer giving the registry type for | |
| 330 | | | this value. | |
| 331 | +-------+-----------------------------------------+ |
| 332 | |
| 333 | |
| 334 | .. function:: SaveKey(key, file_name) |
| 335 | |
| 336 | Saves the specified key, and all its subkeys to the specified file. |
| 337 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame^] | 338 | *key* is an already open key, or one of the predefined |
| 339 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 340 | |
| 341 | *file_name* is the name of the file to save registry data to. This file cannot |
| 342 | already exist. If this filename includes an extension, it cannot be used on file |
| 343 | allocation table (FAT) file systems by the :meth:`LoadKey`, :meth:`ReplaceKey` |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 344 | or :meth:`RestoreKey` methods. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 345 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 346 | 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] | 347 | *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] | 348 | possess the :const:`SeBackupPrivilege` security privilege. Note that |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame^] | 349 | privileges are different than permissions -- see the |
| 350 | `Conflicts Between User Rights and Permissions documentation |
| 351 | <http://msdn.microsoft.com/en-us/library/ms724878%28v=VS.85%29.aspx>`__ |
| 352 | for more details. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 353 | |
| 354 | This function passes NULL for *security_attributes* to the API. |
| 355 | |
| 356 | |
| 357 | .. function:: SetValue(key, sub_key, type, value) |
| 358 | |
| 359 | Associates a value with a specified key. |
| 360 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame^] | 361 | *key* is an already open key, or one of the predefined |
| 362 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 363 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 364 | *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] | 365 | |
| 366 | *type* is an integer that specifies the type of the data. Currently this must be |
| 367 | :const:`REG_SZ`, meaning only strings are supported. Use the :func:`SetValueEx` |
| 368 | function for support for other data types. |
| 369 | |
| 370 | *value* is a string that specifies the new value. |
| 371 | |
| 372 | If the key specified by the *sub_key* parameter does not exist, the SetValue |
| 373 | function creates it. |
| 374 | |
| 375 | Value lengths are limited by available memory. Long values (more than 2048 |
| 376 | bytes) should be stored as files with the filenames stored in the configuration |
| 377 | registry. This helps the registry perform efficiently. |
| 378 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 379 | The key identified by the *key* parameter must have been opened with |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 380 | :const:`KEY_SET_VALUE` access. |
| 381 | |
| 382 | |
| 383 | .. function:: SetValueEx(key, value_name, reserved, type, value) |
| 384 | |
| 385 | Stores data in the value field of an open registry key. |
| 386 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame^] | 387 | *key* is an already open key, or one of the predefined |
| 388 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 389 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 390 | *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] | 391 | associated. |
| 392 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame^] | 393 | *type* is an integer that specifies the type of the data. See |
| 394 | :ref:`Value Types <value-types>` for the available types. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 395 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 396 | *reserved* can be anything -- zero is always passed to the API. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 397 | |
| 398 | *value* is a string that specifies the new value. |
| 399 | |
| 400 | This method can also set additional value and type information for the specified |
| 401 | key. The key identified by the key parameter must have been opened with |
| 402 | :const:`KEY_SET_VALUE` access. |
| 403 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 404 | To open the key, use the :func:`CreateKey` or :func:`OpenKey` methods. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 405 | |
| 406 | Value lengths are limited by available memory. Long values (more than 2048 |
| 407 | bytes) should be stored as files with the filenames stored in the configuration |
| 408 | registry. This helps the registry perform efficiently. |
| 409 | |
| 410 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 411 | .. function:: DisableReflectionKey(key) |
| 412 | |
| 413 | Disables registry reflection for 32-bit processes running on a 64-bit |
| 414 | Operating System. |
| 415 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame^] | 416 | *key* is an already open key, or one of the predefined |
| 417 | :ref:`HKEY_* constants <hkey-constants>`. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 418 | |
| 419 | Will generally raise :exc:`NotImplemented` if executed on a 32-bit |
| 420 | Operating System. |
| 421 | |
| 422 | If the key is not on the reflection list, the function succeeds but has no |
| 423 | effect. Disabling reflection for a key does not affect reflection of any |
| 424 | subkeys. |
| 425 | |
| 426 | |
| 427 | .. function:: EnableReflectionKey(key) |
| 428 | |
| 429 | Restores registry reflection for the specified disabled key. |
| 430 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame^] | 431 | *key* is an already open key, or one of the predefined |
| 432 | :ref:`HKEY_* constants <hkey-constants>`. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 433 | |
| 434 | Will generally raise :exc:`NotImplemented` if executed on a 32-bit |
| 435 | Operating System. |
| 436 | |
| 437 | Restoring reflection for a key does not affect reflection of any subkeys. |
| 438 | |
| 439 | |
| 440 | .. function:: QueryReflectionKey(key) |
| 441 | |
| 442 | Determines the reflection state for the specified key. |
| 443 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame^] | 444 | *key* is an already open key, or one of the predefined |
| 445 | :ref:`HKEY_* constants <hkey-constants>`. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 446 | |
| 447 | Returns ``True`` if reflection is disabled. |
| 448 | |
| 449 | Will generally raise :exc:`NotImplemented` if executed on a 32-bit |
| 450 | Operating System. |
| 451 | |
| 452 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame^] | 453 | .. _constants: |
| 454 | |
| 455 | Constants |
| 456 | ------------------ |
| 457 | |
| 458 | The following constants are defined for use in many :mod:`_winreg` functions. |
| 459 | |
| 460 | .. _hkey-constants: |
| 461 | |
| 462 | HKEY_* Constants |
| 463 | ++++++++++++++++ |
| 464 | |
| 465 | .. data:: HKEY_CLASSES_ROOT |
| 466 | |
| 467 | Registry entries subordinate to this key define types (or classes) of |
| 468 | documents and the properties associated with those types. Shell and |
| 469 | COM applications use the information stored under this key. |
| 470 | |
| 471 | |
| 472 | .. data:: HKEY_CURRENT_USER |
| 473 | |
| 474 | Registry entries subordinate to this key define the preferences of |
| 475 | the current user. These preferences include the settings of |
| 476 | environment variables, data about program groups, colors, printers, |
| 477 | network connections, and application preferences. |
| 478 | |
| 479 | .. data:: HKEY_LOCAL_MACHINE |
| 480 | |
| 481 | Registry entries subordinate to this key define the physical state |
| 482 | of the computer, including data about the bus type, system memory, |
| 483 | and installed hardware and software. |
| 484 | |
| 485 | .. data:: HKEY_USERS |
| 486 | |
| 487 | Registry entries subordinate to this key define the default user |
| 488 | configuration for new users on the local computer and the user |
| 489 | configuration for the current user. |
| 490 | |
| 491 | .. data:: HKEY_PERFORMANCE_DATA |
| 492 | |
| 493 | Registry entries subordinate to this key allow you to access |
| 494 | performance data. The data is not actually stored in the registry; |
| 495 | the registry functions cause the system to collect the data from |
| 496 | its source. |
| 497 | |
| 498 | |
| 499 | .. data:: HKEY_CURRENT_CONFIG |
| 500 | |
| 501 | Contains information about the current hardware profile of the |
| 502 | local computer system. |
| 503 | |
| 504 | .. data:: HKEY_DYN_DATA |
| 505 | |
| 506 | This key is not used in versions of Windows after 98. |
| 507 | |
| 508 | |
| 509 | .. _access-rights: |
| 510 | |
| 511 | Access Rights |
| 512 | +++++++++++++ |
| 513 | |
| 514 | For more information, see `Registry Key Security and Access |
| 515 | <http://msdn.microsoft.com/en-us/library/ms724878%28v=VS.85%29.aspx>`__. |
| 516 | |
| 517 | .. data:: KEY_ALL_ACCESS |
| 518 | |
| 519 | Combines the STANDARD_RIGHTS_REQUIRED, :const:`KEY_QUERY_VALUE`, |
| 520 | :const:`KEY_SET_VALUE`, :const:`KEY_CREATE_SUB_KEY`, |
| 521 | :const:`KEY_ENUMERATE_SUB_KEYS`, :const:`KEY_NOTIFY`, |
| 522 | and :const:`KEY_CREATE_LINK` access rights. |
| 523 | |
| 524 | .. data:: KEY_WRITE |
| 525 | |
| 526 | Combines the STANDARD_RIGHTS_WRITE, :const:`KEY_SET_VALUE`, and |
| 527 | :const:`KEY_CREATE_SUB_KEY` access rights. |
| 528 | |
| 529 | .. data:: KEY_READ |
| 530 | |
| 531 | Combines the STANDARD_RIGHTS_READ, :const:`KEY_QUERY_VALUE`, |
| 532 | :const:`KEY_ENUMERATE_SUB_KEYS`, and :const:`KEY_NOTIFY` values. |
| 533 | |
| 534 | .. data:: KEY_EXECUTE |
| 535 | |
| 536 | Equivalent to :const:`KEY_READ`. |
| 537 | |
| 538 | .. data:: KEY_QUERY_VALUE |
| 539 | |
| 540 | Required to query the values of a registry key. |
| 541 | |
| 542 | .. data:: KEY_SET_VALUE |
| 543 | |
| 544 | Required to create, delete, or set a registry value. |
| 545 | |
| 546 | .. data:: KEY_CREATE_SUB_KEY |
| 547 | |
| 548 | Required to create a subkey of a registry key. |
| 549 | |
| 550 | .. data:: KEY_ENUMERATE_SUB_KEYS |
| 551 | |
| 552 | Required to enumerate the subkeys of a registry key. |
| 553 | |
| 554 | .. data:: KEY_NOTIFY |
| 555 | |
| 556 | Required to request change notifications for a registry key or for |
| 557 | subkeys of a registry key. |
| 558 | |
| 559 | .. data:: KEY_CREATE_LINK |
| 560 | |
| 561 | Reserved for system use. |
| 562 | |
| 563 | |
| 564 | .. _64-bit-access-rights: |
| 565 | |
| 566 | 64-bit Specific |
| 567 | *************** |
| 568 | |
| 569 | For more information, see `Accesing an Alternate Registry View |
| 570 | <http://msdn.microsoft.com/en-us/library/aa384129(v=VS.85).aspx>`__. |
| 571 | |
| 572 | .. data:: KEY_WOW64_64KEY |
| 573 | |
| 574 | Indicates that an application on 64-bit Windows should operate on |
| 575 | the 64-bit registry view. |
| 576 | |
| 577 | .. data:: KEY_WOW64_32KEY |
| 578 | |
| 579 | Indicates that an application on 64-bit Windows should operate on |
| 580 | the 32-bit registry view. |
| 581 | |
| 582 | |
| 583 | .. _value-types: |
| 584 | |
| 585 | Value Types |
| 586 | +++++++++++ |
| 587 | |
| 588 | For more information, see `Registry Value Types |
| 589 | <http://msdn.microsoft.com/en-us/library/ms724884%28v=VS.85%29.aspx>`__. |
| 590 | |
| 591 | .. data:: REG_BINARY |
| 592 | |
| 593 | Binary data in any form. |
| 594 | |
| 595 | .. data:: REG_DWORD |
| 596 | |
| 597 | 32-bit number. |
| 598 | |
| 599 | .. data:: REG_DWORD_LITTLE_ENDIAN |
| 600 | |
| 601 | A 32-bit number in little-endian format. |
| 602 | |
| 603 | .. data:: REG_DWORD_BIG_ENDIAN |
| 604 | |
| 605 | A 32-bit number in big-endian format. |
| 606 | |
| 607 | .. data:: REG_EXPAND_SZ |
| 608 | |
| 609 | Null-terminated string containing references to environment |
| 610 | variables (``%PATH%``). |
| 611 | |
| 612 | .. data:: REG_LINK |
| 613 | |
| 614 | A Unicode symbolic link. |
| 615 | |
| 616 | .. data:: REG_MULTI_SZ |
| 617 | |
| 618 | A sequence of null-terminated strings, terminated by two null characters. |
| 619 | (Python handles this termination automatically.) |
| 620 | |
| 621 | .. data:: REG_NONE |
| 622 | |
| 623 | No defined value type. |
| 624 | |
| 625 | .. data:: REG_RESOURCE_LIST |
| 626 | |
| 627 | A device-driver resource list. |
| 628 | |
| 629 | .. data:: REG_FULL_RESOURCE_DESCRIPTOR |
| 630 | |
| 631 | A hardware setting. |
| 632 | |
| 633 | .. data:: REG_RESOURCE_REQUIREMENTS_LIST |
| 634 | |
| 635 | A hardware resource list. |
| 636 | |
| 637 | .. data:: REG_SZ |
| 638 | |
| 639 | A null-terminated string. |
| 640 | |
| 641 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 642 | .. _handle-object: |
| 643 | |
| 644 | Registry Handle Objects |
| 645 | ----------------------- |
| 646 | |
| 647 | This object wraps a Windows HKEY object, automatically closing it when the |
| 648 | object is destroyed. To guarantee cleanup, you can call either the |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 649 | :meth:`Close` method on the object, or the :func:`CloseKey` function. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 650 | |
| 651 | All registry functions in this module return one of these objects. |
| 652 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 653 | All registry functions in this module which accept a handle object also accept |
| 654 | an integer, however, use of the handle object is encouraged. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 655 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 656 | Handle objects provide semantics for :meth:`__bool__` -- thus :: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 657 | |
| 658 | if handle: |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 659 | print("Yes") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 660 | |
| 661 | will print ``Yes`` if the handle is currently valid (has not been closed or |
| 662 | detached). |
| 663 | |
| 664 | The object also support comparison semantics, so handle objects will compare |
| 665 | true if they both reference the same underlying Windows handle value. |
| 666 | |
Georg Brandl | 22b3431 | 2009-07-26 14:54:51 +0000 | [diff] [blame] | 667 | 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] | 668 | :func:`int` function), in which case the underlying Windows handle value is |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 669 | returned. You can also use the :meth:`Detach` method to return the integer |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 670 | handle, and also disconnect the Windows handle from the handle object. |
| 671 | |
| 672 | |
| 673 | .. method:: PyHKEY.Close() |
| 674 | |
| 675 | Closes the underlying Windows handle. |
| 676 | |
| 677 | If the handle is already closed, no error is raised. |
| 678 | |
| 679 | |
| 680 | .. method:: PyHKEY.Detach() |
| 681 | |
| 682 | Detaches the Windows handle from the handle object. |
| 683 | |
Georg Brandl | 5c10664 | 2007-11-29 17:41:05 +0000 | [diff] [blame] | 684 | The result is an integer that holds the value of the handle before it is |
| 685 | detached. If the handle is already detached or closed, this will return |
| 686 | zero. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 687 | |
| 688 | After calling this function, the handle is effectively invalidated, but the |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 689 | handle is not closed. You would call this function when you need the |
| 690 | underlying Win32 handle to exist beyond the lifetime of the handle object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 691 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 692 | .. method:: PyHKEY.__enter__() |
| 693 | PyHKEY.__exit__(\*exc_info) |
| 694 | |
| 695 | The HKEY object implements :meth:`__enter__` and :meth:`__exit__` and thus |
| 696 | supports the context protocol for the :keyword:`with` statement:: |
| 697 | |
| 698 | with OpenKey(HKEY_LOCAL_MACHINE, "foo") as key: |
| 699 | # ... work with key ... |
| 700 | |
| 701 | will automatically close *key* when control leaves the :keyword:`with` block. |
| 702 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 703 | |