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