Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions Doc/library/winreg.rst
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,42 @@ This module offers the following functions:
.. audit-event:: winreg.QueryValue key,sub_key,value_name winreg.QueryValueEx


.. function:: GetValue(key, sub_key, value_name, flags=RRF_RT_ANY)

Retrieves the type and data for a specified registry value without requiring
the key to be opened first.

*key* is an already open key, or one of the predefined
:ref:`HKEY_* constants <hkey-constants>`.

*sub_key* is a string that holds the name of the subkey with which the value
is associated. If this parameter is ``None`` or empty, the value will be read
from the key specified by *key*.

*value_name* is a string indicating the value to query, or ``None`` for the
default value of the subkey.

*flags* is an integer that specifies the restrictions on the value type to
be queried (see :ref:`RRF_* constants <rrf-constants>`). Default is
``RRF_RT_ANY``.

The result is a tuple of 2 items:

+-------+-----------------------------------------+
| Index | Meaning |
+=======+=========================================+
| ``0`` | The value of the registry item. |
+-------+-----------------------------------------+
| ``1`` | An integer giving the registry type for |
| | this value (see table in docs for |
| | :meth:`SetValueEx`) |
+-------+-----------------------------------------+

.. audit-event:: winreg.GetValue key,sub_key,value_name,flags winreg.GetValue

.. versionadded:: next


.. function:: SaveKey(key, file_name)

Saves the specified key, and all its subkeys to the specified file.
Expand Down Expand Up @@ -751,6 +787,83 @@ For more information, see `Registry Value Types
A null-terminated string.


.. _rrf-constants:

RRF_* Constants
+++++++++++++++

Registry value retrieval flags used with :func:`GetValue`.

.. data:: RRF_RT_ANY

No type restriction. All registry value types will be returned.


.. data:: RRF_RT_DWORD

Restrict query to 32-bit values.


.. data:: RRF_RT_QWORD

Restrict query to 64-bit values.


.. data:: RRF_RT_REG_BINARY

Restrict query to binary data (REG_BINARY).


.. data:: RRF_RT_REG_DWORD

Restrict query to REG_DWORD values.


.. data:: RRF_RT_REG_QWORD

Restrict query to REG_QWORD values.


.. data:: RRF_RT_REG_EXPAND_SZ

Restrict query to expandable strings (REG_EXPAND_SZ).


.. data:: RRF_RT_REG_MULTI_SZ

Restrict query to multi-strings (REG_MULTI_SZ).


.. data:: RRF_RT_REG_NONE

Restrict query to null values (REG_NONE).


.. data:: RRF_RT_REG_SZ

Restrict query to null-terminated strings (REG_SZ).


.. data:: RRF_NOEXPAND

Do not automatically expand environment variables in REG_EXPAND_SZ values.


.. data:: RRF_ZEROONFAILURE

Set buffer contents to zeroes on failure.


.. data:: RRF_SUBKEY_WOW6464KEY

Query the 64-bit registry view on 64-bit Windows.


.. data:: RRF_SUBKEY_WOW6432KEY

Query the 32-bit registry view on 64-bit Windows.


.. _handle-object:

Registry Handle Objects
Expand Down
35 changes: 35 additions & 0 deletions Lib/test/test_winreg.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,41 @@ def test_delete_tree(self):
with self.assertRaises(OSError):
OpenKey(HKEY_CURRENT_USER, test_key_name)

def test_getvalue(self):
test_subkey = test_key_name + "\\GetValueTest"
key = CreateKey(HKEY_CURRENT_USER, test_subkey)
self.addCleanup(CloseKey, key)
self.addCleanup(DeleteTree, HKEY_CURRENT_USER, test_key_name)

SetValueEx(key, "test_string", 0, REG_SZ, "Hello World")
SetValueEx(key, "test_dword", 0, REG_DWORD, 12345)
SetValueEx(key, "test_binary", 0, REG_BINARY, b"binary_data")
SetValueEx(key, None, 0, REG_SZ, "Default Value")

result, typ = GetValue(HKEY_CURRENT_USER, test_subkey, "test_string")
self.assertEqual(result, "Hello World")
self.assertEqual(typ, REG_SZ)

result, typ = GetValue(HKEY_CURRENT_USER, test_subkey, "test_dword")
self.assertEqual(result, 12345)
self.assertEqual(typ, REG_DWORD)

result, typ = GetValue(HKEY_CURRENT_USER, test_subkey, "test_binary")
self.assertEqual(result, b"binary_data")
self.assertEqual(typ, REG_BINARY)

result, typ = GetValue(HKEY_CURRENT_USER, test_subkey, None)
self.assertEqual(result, "Default Value")
self.assertEqual(typ, REG_SZ)

result, typ = GetValue(HKEY_CURRENT_USER, test_subkey, "")
self.assertEqual(result, "Default Value")
self.assertEqual(typ, REG_SZ)

result, typ = GetValue(HKEY_CURRENT_USER, test_subkey, "test_string", RRF_RT_REG_SZ)
self.assertEqual(result, "Hello World")
self.assertEqual(typ, REG_SZ)


if __name__ == "__main__":
if not REMOTE_NAME:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add :func:`winreg.GetValue` function to retrieve registry values without opening
keys first. Includes RRF_* constants for type restriction flags.
98 changes: 97 additions & 1 deletion PC/clinic/winreg.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading