Skip to content

Commit a4446f8

Browse files
committed
Add winreg.GetValue
1 parent c991e10 commit a4446f8

File tree

2 files changed

+203
-3
lines changed

2 files changed

+203
-3
lines changed

PC/clinic/winreg.c.h

Lines changed: 99 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PC/winreg.c

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1855,7 +1855,7 @@ the configuration registry to help the registry perform efficiently.
18551855
static PyObject *
18561856
winreg_SetValueEx_impl(PyObject *module, HKEY key, const wchar_t *value_name,
18571857
PyObject *reserved, DWORD type, PyObject *value)
1858-
/*[clinic end generated code: output=295db04deb456d9e input=900a9e3990bfb196]*/
1858+
/*[clinic end generated code: output=295db04deb456d9e input=2dd9471b4aff5b84]*/
18591859
{
18601860
LONG rc;
18611861
BYTE *data = NULL;
@@ -2033,6 +2033,97 @@ winreg_DeleteTree_impl(PyObject *module, HKEY key, const wchar_t *sub_key)
20332033
Py_RETURN_NONE;
20342034
}
20352035

2036+
/*[clinic input]
2037+
winreg.GetValue
2038+
2039+
key: HKEY
2040+
An already open key, or any one of the predefined HKEY_* constants.
2041+
sub_key: Py_UNICODE(accept={str, NoneType})
2042+
A string that names the subkey with which the value is associated.
2043+
If this parameter is None or empty, the value will be read from key.
2044+
name: Py_UNICODE(accept={str, NoneType})
2045+
A string indicating the value to query.
2046+
flags: int(c_default='RRF_RT_ANY') = winreg.RRF_RT_ANY
2047+
Restrict the data type of value to be queried.
2048+
/
2049+
2050+
Retrieves the type and data for the specified registry value.
2051+
2052+
Behaves mostly like QueryValueEx(), but you needn't OpenKey() and CloseKey()
2053+
if the key is any one of the predefined HKEY_* constants.
2054+
2055+
The return value is a tuple of the value and the type_id.
2056+
[clinic start generated code]*/
2057+
2058+
static PyObject *
2059+
winreg_GetValue_impl(PyObject *module, HKEY key, const wchar_t *sub_key,
2060+
const wchar_t *name, int flags)
2061+
/*[clinic end generated code: output=31668fd98e5cd5dc input=9f879d56439779e9]*/
2062+
{
2063+
LONG rc;
2064+
BYTE *retBuf, *tmp;
2065+
DWORD bufSize = 0, retSize;
2066+
DWORD typ;
2067+
PyObject *obData;
2068+
PyObject *result;
2069+
2070+
if (PySys_Audit("winreg.GetValue", "nuui",
2071+
(Py_ssize_t)key, sub_key, name, flags) < 0) {
2072+
return NULL;
2073+
}
2074+
2075+
/* First call to get the required buffer size */
2076+
Py_BEGIN_ALLOW_THREADS
2077+
rc = RegGetValueW(key, sub_key, name, flags, &typ, NULL, &bufSize);
2078+
Py_END_ALLOW_THREADS
2079+
2080+
if (rc == ERROR_MORE_DATA) {
2081+
bufSize = 256;
2082+
}
2083+
else if (rc != ERROR_SUCCESS) {
2084+
return PyErr_SetFromWindowsErrWithFunction(rc, "RegGetValue");
2085+
}
2086+
2087+
retBuf = (BYTE *)PyMem_Malloc(bufSize);
2088+
if (retBuf == NULL) {
2089+
return PyErr_NoMemory();
2090+
}
2091+
2092+
/* Second call to get the actual data */
2093+
while (1) {
2094+
retSize = bufSize;
2095+
Py_BEGIN_ALLOW_THREADS
2096+
rc = RegGetValueW(key, sub_key, name, flags, &typ,
2097+
(BYTE *)retBuf, &retSize);
2098+
Py_END_ALLOW_THREADS
2099+
if (rc != ERROR_MORE_DATA) {
2100+
break;
2101+
}
2102+
2103+
bufSize *= 2;
2104+
tmp = (char *) PyMem_Realloc(retBuf, bufSize);
2105+
if (tmp == NULL) {
2106+
PyMem_Free(retBuf);
2107+
return PyErr_NoMemory();
2108+
}
2109+
retBuf = tmp;
2110+
}
2111+
2112+
if (rc != ERROR_SUCCESS) {
2113+
PyMem_Free(retBuf);
2114+
return PyErr_SetFromWindowsErrWithFunction(rc, "RegGetValue");
2115+
}
2116+
2117+
obData = Reg2Py(retBuf, retSize, typ);
2118+
PyMem_Free(retBuf);
2119+
if (obData == NULL) {
2120+
return NULL;
2121+
}
2122+
result = Py_BuildValue("Oi", obData, typ);
2123+
Py_DECREF(obData);
2124+
return result;
2125+
}
2126+
20362127
/*[clinic input]
20372128
winreg.QueryReflectionKey
20382129
@@ -2205,6 +2296,18 @@ exec_module(PyObject *m)
22052296
ADD_INT(REG_RESOURCE_LIST);
22062297
ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
22072298
ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
2299+
ADD_INT(RRF_RT_ANY);
2300+
ADD_INT(RRF_RT_DWORD);
2301+
ADD_INT(RRF_RT_QWORD);
2302+
ADD_INT(RRF_RT_REG_BINARY);
2303+
ADD_INT(RRF_RT_REG_EXPAND_SZ);
2304+
ADD_INT(RRF_RT_REG_MULTI_SZ);
2305+
ADD_INT(RRF_RT_REG_NONE);
2306+
ADD_INT(RRF_RT_REG_SZ);
2307+
ADD_INT(RRF_NOEXPAND);
2308+
ADD_INT(RRF_SUBKEY_WOW6464KEY);
2309+
ADD_INT(RRF_SUBKEY_WOW6432KEY);
2310+
ADD_INT(RRF_WOW64_MASK);
22082311

22092312
#undef ADD_INT
22102313
return 0;

0 commit comments

Comments
 (0)