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