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 | |
| 15 | This module exposes a very low-level interface to the Windows registry; it is |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 16 | expected that in the future a new ``winreg`` module will be created offering a |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 17 | higher-level interface to the registry API. |
| 18 | |
| 19 | This module offers the following functions: |
| 20 | |
| 21 | |
| 22 | .. function:: CloseKey(hkey) |
| 23 | |
| 24 | Closes a previously opened registry key. The hkey argument specifies a |
| 25 | previously opened key. |
| 26 | |
| 27 | Note that if *hkey* is not closed using this method (or via |
| 28 | :meth:`handle.Close`), it is closed when the *hkey* object is destroyed by |
| 29 | Python. |
| 30 | |
| 31 | |
| 32 | .. function:: ConnectRegistry(computer_name, key) |
| 33 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 34 | Establishes a connection to a predefined registry handle on another computer, |
| 35 | and returns a :ref:`handle object <handle-object>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 36 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 37 | *computer_name* is the name of the remote computer, of the form |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 38 | ``r"\\computername"``. If ``None``, the local computer is used. |
| 39 | |
| 40 | *key* is the predefined handle to connect to. |
| 41 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 42 | The return value is the handle of the opened key. If the function fails, a |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 43 | :exc:`WindowsError` exception is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 44 | |
| 45 | |
| 46 | .. function:: CreateKey(key, sub_key) |
| 47 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 48 | Creates or opens the specified key, returning a |
| 49 | :ref:`handle object <handle-object>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 50 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 51 | *key* is an already open key, or one of the predefined :const:`HKEY_\*` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 52 | constants. |
| 53 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 54 | *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] | 55 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 56 | If *key* is one of the predefined keys, *sub_key* may be ``None``. In that |
| 57 | 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] | 58 | |
| 59 | If the key already exists, this function opens the existing key. |
| 60 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 61 | The return value is the handle of the opened key. If the function fails, a |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 62 | :exc:`WindowsError` exception is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 63 | |
| 64 | |
| 65 | .. function:: DeleteKey(key, sub_key) |
| 66 | |
| 67 | Deletes the specified key. |
| 68 | |
| 69 | *key* is an already open key, or any one of the predefined :const:`HKEY_\*` |
| 70 | constants. |
| 71 | |
| 72 | *sub_key* is a string that must be a subkey of the key identified by the *key* |
| 73 | parameter. This value must not be ``None``, and the key may not have subkeys. |
| 74 | |
| 75 | *This method can not delete keys with subkeys.* |
| 76 | |
| 77 | If the method succeeds, the entire key, including all of its values, is removed. |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 78 | If the method fails, a :exc:`WindowsError` exception is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 79 | |
| 80 | |
| 81 | .. function:: DeleteValue(key, value) |
| 82 | |
| 83 | Removes a named value from a registry key. |
| 84 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 85 | *key* is an already open key, or one of the predefined :const:`HKEY_\*` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 86 | constants. |
| 87 | |
| 88 | *value* is a string that identifies the value to remove. |
| 89 | |
| 90 | |
| 91 | .. function:: EnumKey(key, index) |
| 92 | |
| 93 | Enumerates subkeys of an open registry key, returning a string. |
| 94 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 95 | *key* is an already open key, or any one of the predefined :const:`HKEY_\*` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 96 | constants. |
| 97 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 98 | *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] | 99 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 100 | The function retrieves the name of one subkey each time it is called. It is |
| 101 | typically called repeatedly until a :exc:`WindowsError` exception is |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 102 | raised, indicating, no more values are available. |
| 103 | |
| 104 | |
| 105 | .. function:: EnumValue(key, index) |
| 106 | |
| 107 | Enumerates values of an open registry key, returning a tuple. |
| 108 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 109 | *key* is an already open key, or any one of the predefined :const:`HKEY_\*` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 110 | constants. |
| 111 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 112 | *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] | 113 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 114 | The function retrieves the name of one subkey each time it is called. It is |
| 115 | typically called repeatedly, until a :exc:`WindowsError` exception is |
| 116 | raised, indicating no more values. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 117 | |
| 118 | The result is a tuple of 3 items: |
| 119 | |
| 120 | +-------+--------------------------------------------+ |
| 121 | | Index | Meaning | |
| 122 | +=======+============================================+ |
| 123 | | ``0`` | A string that identifies the value name | |
| 124 | +-------+--------------------------------------------+ |
| 125 | | ``1`` | An object that holds the value data, and | |
| 126 | | | whose type depends on the underlying | |
| 127 | | | registry type | |
| 128 | +-------+--------------------------------------------+ |
| 129 | | ``2`` | An integer that identifies the type of the | |
| 130 | | | value data | |
| 131 | +-------+--------------------------------------------+ |
| 132 | |
| 133 | |
Ezio Melotti | 713e042 | 2009-09-13 08:13:21 +0000 | [diff] [blame] | 134 | .. function:: ExpandEnvironmentStrings(str) |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 135 | |
Ezio Melotti | 713e042 | 2009-09-13 08:13:21 +0000 | [diff] [blame] | 136 | Expands environment strings %NAME% in unicode string like :const:`REG_EXPAND_SZ`:: |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 137 | |
Ezio Melotti | 713e042 | 2009-09-13 08:13:21 +0000 | [diff] [blame] | 138 | >>> ExpandEnvironmentStrings('%windir%') |
| 139 | 'C:\\Windows' |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 140 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 141 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 142 | .. function:: FlushKey(key) |
| 143 | |
| 144 | Writes all the attributes of a key to the registry. |
| 145 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 146 | *key* is an already open key, or one of the predefined :const:`HKEY_\*` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 147 | constants. |
| 148 | |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 149 | It is not necessary to call :func:`FlushKey` to change a key. Registry changes are |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 150 | flushed to disk by the registry using its lazy flusher. Registry changes are |
| 151 | also flushed to disk at system shutdown. Unlike :func:`CloseKey`, the |
| 152 | :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] | 153 | registry. An application should only call :func:`FlushKey` if it requires |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 154 | absolute certainty that registry changes are on disk. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 155 | |
| 156 | .. note:: |
| 157 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 158 | 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] | 159 | isn't. |
| 160 | |
| 161 | |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 162 | .. function:: LoadKey(key, sub_key, file_name) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 163 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 164 | Creates a subkey under the specified key and stores registration information |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 165 | from a specified file into that subkey. |
| 166 | |
| 167 | *key* is an already open key, or any of the predefined :const:`HKEY_\*` |
| 168 | constants. |
| 169 | |
| 170 | *sub_key* is a string that identifies the sub_key to load. |
| 171 | |
| 172 | *file_name* is the name of the file to load registry data from. This file must |
| 173 | have been created with the :func:`SaveKey` function. Under the file allocation |
| 174 | table (FAT) file system, the filename may not have an extension. |
| 175 | |
| 176 | A call to LoadKey() fails if the calling process does not have the |
| 177 | :const:`SE_RESTORE_PRIVILEGE` privilege. Note that privileges are different than |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 178 | permissions -- see the Win32 documentation for more details. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 179 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 180 | If *key* is a handle returned by :func:`ConnectRegistry`, then the path |
| 181 | specified in *fileName* is relative to the remote computer. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 182 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 183 | 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] | 184 | :const:`HKEY_LOCAL_MACHINE` tree. This may or may not be true. |
| 185 | |
| 186 | |
Brian Curtin | 44bb1f7 | 2010-04-24 17:23:03 +0000 | [diff] [blame] | 187 | .. function:: OpenKey(key, sub_key[, res[, sam]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 188 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 189 | Opens the specified key, returning a :ref:`handle object <handle-object>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 190 | |
| 191 | *key* is an already open key, or any one of the predefined :const:`HKEY_\*` |
| 192 | constants. |
| 193 | |
| 194 | *sub_key* is a string that identifies the sub_key to open. |
| 195 | |
| 196 | *res* is a reserved integer, and must be zero. The default is zero. |
| 197 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 198 | *sam* is an integer that specifies an access mask that describes the desired |
Georg Brandl | b044b2a | 2009-09-16 16:05:59 +0000 | [diff] [blame] | 199 | security access for the key. Default is :const:`KEY_READ`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 200 | |
| 201 | The result is a new handle to the specified key. |
| 202 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 203 | If the function fails, :exc:`WindowsError` is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 204 | |
| 205 | |
| 206 | .. function:: OpenKeyEx() |
| 207 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 208 | The functionality of :func:`OpenKeyEx` is provided via :func:`OpenKey`, |
| 209 | by the use of default arguments. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 210 | |
| 211 | |
| 212 | .. function:: QueryInfoKey(key) |
| 213 | |
| 214 | Returns information about a key, as a tuple. |
| 215 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 216 | *key* is an already open key, or one of the predefined :const:`HKEY_\*` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 217 | constants. |
| 218 | |
| 219 | The result is a tuple of 3 items: |
| 220 | |
| 221 | +-------+---------------------------------------------+ |
| 222 | | Index | Meaning | |
| 223 | +=======+=============================================+ |
| 224 | | ``0`` | An integer giving the number of sub keys | |
| 225 | | | this key has. | |
| 226 | +-------+---------------------------------------------+ |
| 227 | | ``1`` | An integer giving the number of values this | |
| 228 | | | key has. | |
| 229 | +-------+---------------------------------------------+ |
Georg Brandl | ba956ae | 2007-11-29 17:24:34 +0000 | [diff] [blame] | 230 | | ``2`` | An integer giving when the key was last | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 231 | | | modified (if available) as 100's of | |
| 232 | | | nanoseconds since Jan 1, 1600. | |
| 233 | +-------+---------------------------------------------+ |
| 234 | |
| 235 | |
| 236 | .. function:: QueryValue(key, sub_key) |
| 237 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 238 | Retrieves the unnamed value for a key, as a string. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 239 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 240 | *key* is an already open key, or one of the predefined :const:`HKEY_\*` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 241 | constants. |
| 242 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 243 | *sub_key* is a string that holds the name of the subkey with which the value is |
| 244 | associated. If this parameter is ``None`` or empty, the function retrieves the |
| 245 | 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] | 246 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 247 | Values in the registry have name, type, and data components. This method |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 248 | 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] | 249 | underlying API call doesn't return the type, so always use |
| 250 | :func:`QueryValueEx` if possible. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 251 | |
| 252 | |
| 253 | .. function:: QueryValueEx(key, value_name) |
| 254 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 255 | Retrieves the type and data for a specified value name associated with |
| 256 | an open registry key. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 257 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 258 | *key* is an already open key, or one of the predefined :const:`HKEY_\*` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 259 | constants. |
| 260 | |
| 261 | *value_name* is a string indicating the value to query. |
| 262 | |
| 263 | The result is a tuple of 2 items: |
| 264 | |
| 265 | +-------+-----------------------------------------+ |
| 266 | | Index | Meaning | |
| 267 | +=======+=========================================+ |
| 268 | | ``0`` | The value of the registry item. | |
| 269 | +-------+-----------------------------------------+ |
| 270 | | ``1`` | An integer giving the registry type for | |
| 271 | | | this value. | |
| 272 | +-------+-----------------------------------------+ |
| 273 | |
| 274 | |
| 275 | .. function:: SaveKey(key, file_name) |
| 276 | |
| 277 | Saves the specified key, and all its subkeys to the specified file. |
| 278 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 279 | *key* is an already open key, or one of the predefined :const:`HKEY_\*` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 280 | constants. |
| 281 | |
| 282 | *file_name* is the name of the file to save registry data to. This file cannot |
| 283 | already exist. If this filename includes an extension, it cannot be used on file |
| 284 | allocation table (FAT) file systems by the :meth:`LoadKey`, :meth:`ReplaceKey` |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 285 | or :meth:`RestoreKey` methods. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 286 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 287 | 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] | 288 | *file_name* is relative to the remote computer. The caller of this method must |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 289 | possess the :const:`SeBackupPrivilege` security privilege. Note that |
| 290 | privileges are different than permissions -- see the Win32 documentation for |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 291 | more details. |
| 292 | |
| 293 | This function passes NULL for *security_attributes* to the API. |
| 294 | |
| 295 | |
| 296 | .. function:: SetValue(key, sub_key, type, value) |
| 297 | |
| 298 | Associates a value with a specified key. |
| 299 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 300 | *key* is an already open key, or one of the predefined :const:`HKEY_\*` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 301 | constants. |
| 302 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 303 | *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] | 304 | |
| 305 | *type* is an integer that specifies the type of the data. Currently this must be |
| 306 | :const:`REG_SZ`, meaning only strings are supported. Use the :func:`SetValueEx` |
| 307 | function for support for other data types. |
| 308 | |
| 309 | *value* is a string that specifies the new value. |
| 310 | |
| 311 | If the key specified by the *sub_key* parameter does not exist, the SetValue |
| 312 | function creates it. |
| 313 | |
| 314 | Value lengths are limited by available memory. Long values (more than 2048 |
| 315 | bytes) should be stored as files with the filenames stored in the configuration |
| 316 | registry. This helps the registry perform efficiently. |
| 317 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 318 | The key identified by the *key* parameter must have been opened with |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 319 | :const:`KEY_SET_VALUE` access. |
| 320 | |
| 321 | |
| 322 | .. function:: SetValueEx(key, value_name, reserved, type, value) |
| 323 | |
| 324 | Stores data in the value field of an open registry key. |
| 325 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 326 | *key* is an already open key, or one of the predefined :const:`HKEY_\*` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 327 | constants. |
| 328 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 329 | *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] | 330 | associated. |
| 331 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 332 | *type* is an integer that specifies the type of the data. This should be one |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 333 | of the following constants defined in this module: |
| 334 | |
| 335 | +----------------------------------+---------------------------------------------+ |
| 336 | | Constant | Meaning | |
| 337 | +==================================+=============================================+ |
| 338 | | :const:`REG_BINARY` | Binary data in any form. | |
| 339 | +----------------------------------+---------------------------------------------+ |
| 340 | | :const:`REG_DWORD` | A 32-bit number. | |
| 341 | +----------------------------------+---------------------------------------------+ |
| 342 | | :const:`REG_DWORD_LITTLE_ENDIAN` | A 32-bit number in little-endian format. | |
| 343 | +----------------------------------+---------------------------------------------+ |
| 344 | | :const:`REG_DWORD_BIG_ENDIAN` | A 32-bit number in big-endian format. | |
| 345 | +----------------------------------+---------------------------------------------+ |
| 346 | | :const:`REG_EXPAND_SZ` | Null-terminated string containing | |
| 347 | | | references to environment variables | |
| 348 | | | (``%PATH%``). | |
| 349 | +----------------------------------+---------------------------------------------+ |
| 350 | | :const:`REG_LINK` | A Unicode symbolic link. | |
| 351 | +----------------------------------+---------------------------------------------+ |
| 352 | | :const:`REG_MULTI_SZ` | A sequence of null-terminated strings, | |
| 353 | | | terminated by two null characters. (Python | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 354 | | | handles this termination automatically.) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 355 | +----------------------------------+---------------------------------------------+ |
| 356 | | :const:`REG_NONE` | No defined value type. | |
| 357 | +----------------------------------+---------------------------------------------+ |
| 358 | | :const:`REG_RESOURCE_LIST` | A device-driver resource list. | |
| 359 | +----------------------------------+---------------------------------------------+ |
| 360 | | :const:`REG_SZ` | A null-terminated string. | |
| 361 | +----------------------------------+---------------------------------------------+ |
| 362 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 363 | *reserved* can be anything -- zero is always passed to the API. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 364 | |
| 365 | *value* is a string that specifies the new value. |
| 366 | |
| 367 | This method can also set additional value and type information for the specified |
| 368 | key. The key identified by the key parameter must have been opened with |
| 369 | :const:`KEY_SET_VALUE` access. |
| 370 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 371 | To open the key, use the :func:`CreateKey` or :func:`OpenKey` methods. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 372 | |
| 373 | Value lengths are limited by available memory. Long values (more than 2048 |
| 374 | bytes) should be stored as files with the filenames stored in the configuration |
| 375 | registry. This helps the registry perform efficiently. |
| 376 | |
| 377 | |
| 378 | .. _handle-object: |
| 379 | |
| 380 | Registry Handle Objects |
| 381 | ----------------------- |
| 382 | |
| 383 | This object wraps a Windows HKEY object, automatically closing it when the |
| 384 | object is destroyed. To guarantee cleanup, you can call either the |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 385 | :meth:`Close` method on the object, or the :func:`CloseKey` function. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 386 | |
| 387 | All registry functions in this module return one of these objects. |
| 388 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 389 | All registry functions in this module which accept a handle object also accept |
| 390 | an integer, however, use of the handle object is encouraged. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 391 | |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 392 | Handle objects provide semantics for :meth:`__bool__` -- thus :: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 393 | |
| 394 | if handle: |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 395 | print("Yes") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 396 | |
| 397 | will print ``Yes`` if the handle is currently valid (has not been closed or |
| 398 | detached). |
| 399 | |
| 400 | The object also support comparison semantics, so handle objects will compare |
| 401 | true if they both reference the same underlying Windows handle value. |
| 402 | |
Georg Brandl | c5605df | 2009-08-13 08:26:44 +0000 | [diff] [blame] | 403 | 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] | 404 | :func:`int` function), in which case the underlying Windows handle value is |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 405 | 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] | 406 | handle, and also disconnect the Windows handle from the handle object. |
| 407 | |
| 408 | |
| 409 | .. method:: PyHKEY.Close() |
| 410 | |
| 411 | Closes the underlying Windows handle. |
| 412 | |
| 413 | If the handle is already closed, no error is raised. |
| 414 | |
| 415 | |
| 416 | .. method:: PyHKEY.Detach() |
| 417 | |
| 418 | Detaches the Windows handle from the handle object. |
| 419 | |
Georg Brandl | 5c10664 | 2007-11-29 17:41:05 +0000 | [diff] [blame] | 420 | The result is an integer that holds the value of the handle before it is |
| 421 | detached. If the handle is already detached or closed, this will return |
| 422 | zero. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 423 | |
| 424 | After calling this function, the handle is effectively invalidated, but the |
Ezio Melotti | 97bb179 | 2010-04-25 17:55:39 +0000 | [diff] [blame] | 425 | handle is not closed. You would call this function when you need the |
| 426 | underlying Win32 handle to exist beyond the lifetime of the handle object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 427 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 428 | .. method:: PyHKEY.__enter__() |
| 429 | PyHKEY.__exit__(\*exc_info) |
| 430 | |
| 431 | The HKEY object implements :meth:`__enter__` and :meth:`__exit__` and thus |
| 432 | supports the context protocol for the :keyword:`with` statement:: |
| 433 | |
| 434 | with OpenKey(HKEY_LOCAL_MACHINE, "foo") as key: |
| 435 | # ... work with key ... |
| 436 | |
| 437 | will automatically close *key* when control leaves the :keyword:`with` block. |
| 438 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 439 | |