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