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