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