ยปCore Development>Code coverage>PC/winreg.c

Python code coverage for PC/winreg.c

#countcontent
1n/a/*
2n/a winreg.c
3n/a
4n/a Windows Registry access module for Python.
5n/a
6n/a * Simple registry access written by Mark Hammond in win32api
7n/a module circa 1995.
8n/a * Bill Tutt expanded the support significantly not long after.
9n/a * Numerous other people have submitted patches since then.
10n/a * Ripped from win32api module 03-Feb-2000 by Mark Hammond, and
11n/a basic Unicode support added.
12n/a
13n/a*/
14n/a
15n/a#include "Python.h"
16n/a#include "structmember.h"
17n/a#include "windows.h"
18n/a
19n/astatic BOOL PyHKEY_AsHKEY(PyObject *ob, HKEY *pRes, BOOL bNoneOK);
20n/astatic BOOL clinic_HKEY_converter(PyObject *ob, void *p);
21n/astatic PyObject *PyHKEY_FromHKEY(HKEY h);
22n/astatic BOOL PyHKEY_Close(PyObject *obHandle);
23n/a
24n/astatic char errNotAHandle[] = "Object is not a handle";
25n/a
26n/a/* The win32api module reports the function name that failed,
27n/a but this concept is not in the Python core.
28n/a Hopefully it will one day, and in the meantime I dont
29n/a want to lose this info...
30n/a*/
31n/a#define PyErr_SetFromWindowsErrWithFunction(rc, fnname) \
32n/a PyErr_SetFromWindowsErr(rc)
33n/a
34n/a/* Forward declares */
35n/a
36n/a/* Doc strings */
37n/aPyDoc_STRVAR(module_doc,
38n/a"This module provides access to the Windows registry API.\n"
39n/a"\n"
40n/a"Functions:\n"
41n/a"\n"
42n/a"CloseKey() - Closes a registry key.\n"
43n/a"ConnectRegistry() - Establishes a connection to a predefined registry handle\n"
44n/a" on another computer.\n"
45n/a"CreateKey() - Creates the specified key, or opens it if it already exists.\n"
46n/a"DeleteKey() - Deletes the specified key.\n"
47n/a"DeleteValue() - Removes a named value from the specified registry key.\n"
48n/a"EnumKey() - Enumerates subkeys of the specified open registry key.\n"
49n/a"EnumValue() - Enumerates values of the specified open registry key.\n"
50n/a"ExpandEnvironmentStrings() - Expand the env strings in a REG_EXPAND_SZ\n"
51n/a" string.\n"
52n/a"FlushKey() - Writes all the attributes of the specified key to the registry.\n"
53n/a"LoadKey() - Creates a subkey under HKEY_USER or HKEY_LOCAL_MACHINE and\n"
54n/a" stores registration information from a specified file into that\n"
55n/a" subkey.\n"
56n/a"OpenKey() - Opens the specified key.\n"
57n/a"OpenKeyEx() - Alias of OpenKey().\n"
58n/a"QueryValue() - Retrieves the value associated with the unnamed value for a\n"
59n/a" specified key in the registry.\n"
60n/a"QueryValueEx() - Retrieves the type and data for a specified value name\n"
61n/a" associated with an open registry key.\n"
62n/a"QueryInfoKey() - Returns information about the specified key.\n"
63n/a"SaveKey() - Saves the specified key, and all its subkeys a file.\n"
64n/a"SetValue() - Associates a value with a specified key.\n"
65n/a"SetValueEx() - Stores data in the value field of an open registry key.\n"
66n/a"\n"
67n/a"Special objects:\n"
68n/a"\n"
69n/a"HKEYType -- type object for HKEY objects\n"
70n/a"error -- exception raised for Win32 errors\n"
71n/a"\n"
72n/a"Integer constants:\n"
73n/a"Many constants are defined - see the documentation for each function\n"
74n/a"to see what constants are used, and where.");
75n/a
76n/a
77n/a
78n/a/* PyHKEY docstrings */
79n/aPyDoc_STRVAR(PyHKEY_doc,
80n/a"PyHKEY Object - A Python object, representing a win32 registry key.\n"
81n/a"\n"
82n/a"This object wraps a Windows HKEY object, automatically closing it when\n"
83n/a"the object is destroyed. To guarantee cleanup, you can call either\n"
84n/a"the Close() method on the PyHKEY, or the CloseKey() method.\n"
85n/a"\n"
86n/a"All functions which accept a handle object also accept an integer - \n"
87n/a"however, use of the handle object is encouraged.\n"
88n/a"\n"
89n/a"Functions:\n"
90n/a"Close() - Closes the underlying handle.\n"
91n/a"Detach() - Returns the integer Win32 handle, detaching it from the object\n"
92n/a"\n"
93n/a"Properties:\n"
94n/a"handle - The integer Win32 handle.\n"
95n/a"\n"
96n/a"Operations:\n"
97n/a"__bool__ - Handles with an open object return true, otherwise false.\n"
98n/a"__int__ - Converting a handle to an integer returns the Win32 handle.\n"
99n/a"rich comparison - Handle objects are compared using the handle value.");
100n/a
101n/a
102n/a
103n/a/************************************************************************
104n/a
105n/a The PyHKEY object definition
106n/a
107n/a************************************************************************/
108n/atypedef struct {
109n/a PyObject_VAR_HEAD
110n/a HKEY hkey;
111n/a} PyHKEYObject;
112n/a
113n/a#define PyHKEY_Check(op) ((op)->ob_type == &PyHKEY_Type)
114n/a
115n/astatic char *failMsg = "bad operand type";
116n/a
117n/astatic PyObject *
118n/aPyHKEY_unaryFailureFunc(PyObject *ob)
119n/a{
120n/a PyErr_SetString(PyExc_TypeError, failMsg);
121n/a return NULL;
122n/a}
123n/astatic PyObject *
124n/aPyHKEY_binaryFailureFunc(PyObject *ob1, PyObject *ob2)
125n/a{
126n/a PyErr_SetString(PyExc_TypeError, failMsg);
127n/a return NULL;
128n/a}
129n/astatic PyObject *
130n/aPyHKEY_ternaryFailureFunc(PyObject *ob1, PyObject *ob2, PyObject *ob3)
131n/a{
132n/a PyErr_SetString(PyExc_TypeError, failMsg);
133n/a return NULL;
134n/a}
135n/a
136n/astatic void
137n/aPyHKEY_deallocFunc(PyObject *ob)
138n/a{
139n/a /* Can not call PyHKEY_Close, as the ob->tp_type
140n/a has already been cleared, thus causing the type
141n/a check to fail!
142n/a */
143n/a PyHKEYObject *obkey = (PyHKEYObject *)ob;
144n/a if (obkey->hkey)
145n/a RegCloseKey((HKEY)obkey->hkey);
146n/a PyObject_DEL(ob);
147n/a}
148n/a
149n/astatic int
150n/aPyHKEY_boolFunc(PyObject *ob)
151n/a{
152n/a return ((PyHKEYObject *)ob)->hkey != 0;
153n/a}
154n/a
155n/astatic PyObject *
156n/aPyHKEY_intFunc(PyObject *ob)
157n/a{
158n/a PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
159n/a return PyLong_FromVoidPtr(pyhkey->hkey);
160n/a}
161n/a
162n/astatic PyObject *
163n/aPyHKEY_strFunc(PyObject *ob)
164n/a{
165n/a PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
166n/a return PyUnicode_FromFormat("<PyHKEY:%p>", pyhkey->hkey);
167n/a}
168n/a
169n/astatic int
170n/aPyHKEY_compareFunc(PyObject *ob1, PyObject *ob2)
171n/a{
172n/a PyHKEYObject *pyhkey1 = (PyHKEYObject *)ob1;
173n/a PyHKEYObject *pyhkey2 = (PyHKEYObject *)ob2;
174n/a return pyhkey1 == pyhkey2 ? 0 :
175n/a (pyhkey1 < pyhkey2 ? -1 : 1);
176n/a}
177n/a
178n/astatic Py_hash_t
179n/aPyHKEY_hashFunc(PyObject *ob)
180n/a{
181n/a /* Just use the address.
182n/a XXX - should we use the handle value?
183n/a */
184n/a return _Py_HashPointer(ob);
185n/a}
186n/a
187n/a
188n/astatic PyNumberMethods PyHKEY_NumberMethods =
189n/a{
190n/a PyHKEY_binaryFailureFunc, /* nb_add */
191n/a PyHKEY_binaryFailureFunc, /* nb_subtract */
192n/a PyHKEY_binaryFailureFunc, /* nb_multiply */
193n/a PyHKEY_binaryFailureFunc, /* nb_remainder */
194n/a PyHKEY_binaryFailureFunc, /* nb_divmod */
195n/a PyHKEY_ternaryFailureFunc, /* nb_power */
196n/a PyHKEY_unaryFailureFunc, /* nb_negative */
197n/a PyHKEY_unaryFailureFunc, /* nb_positive */
198n/a PyHKEY_unaryFailureFunc, /* nb_absolute */
199n/a PyHKEY_boolFunc, /* nb_bool */
200n/a PyHKEY_unaryFailureFunc, /* nb_invert */
201n/a PyHKEY_binaryFailureFunc, /* nb_lshift */
202n/a PyHKEY_binaryFailureFunc, /* nb_rshift */
203n/a PyHKEY_binaryFailureFunc, /* nb_and */
204n/a PyHKEY_binaryFailureFunc, /* nb_xor */
205n/a PyHKEY_binaryFailureFunc, /* nb_or */
206n/a PyHKEY_intFunc, /* nb_int */
207n/a 0, /* nb_reserved */
208n/a PyHKEY_unaryFailureFunc, /* nb_float */
209n/a};
210n/a
211n/a/*[clinic input]
212n/amodule winreg
213n/aclass winreg.HKEYType "PyHKEYObject *" "&PyHKEY_Type"
214n/a[clinic start generated code]*/
215n/a/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4c964eba3bf914d6]*/
216n/a
217n/a/*[python input]
218n/aclass REGSAM_converter(CConverter):
219n/a type = 'REGSAM'
220n/a format_unit = 'i'
221n/a
222n/aclass DWORD_converter(CConverter):
223n/a type = 'DWORD'
224n/a format_unit = 'k'
225n/a
226n/aclass HKEY_converter(CConverter):
227n/a type = 'HKEY'
228n/a converter = 'clinic_HKEY_converter'
229n/a
230n/aclass HKEY_return_converter(CReturnConverter):
231n/a type = 'HKEY'
232n/a
233n/a def render(self, function, data):
234n/a self.declare(data)
235n/a self.err_occurred_if_null_pointer("_return_value", data)
236n/a data.return_conversion.append(
237n/a 'return_value = PyHKEY_FromHKEY(_return_value);\n')
238n/a
239n/a# HACK: this only works for PyHKEYObjects, nothing else.
240n/a# Should this be generalized and enshrined in clinic.py,
241n/a# destroy this converter with prejudice.
242n/aclass self_return_converter(CReturnConverter):
243n/a type = 'PyHKEYObject *'
244n/a
245n/a def render(self, function, data):
246n/a self.declare(data)
247n/a data.return_conversion.append(
248n/a 'return_value = (PyObject *)_return_value;\n')
249n/a[python start generated code]*/
250n/a/*[python end generated code: output=da39a3ee5e6b4b0d input=22f7aedc6d68e80e]*/
251n/a
252n/a#include "clinic/winreg.c.h"
253n/a
254n/a/************************************************************************
255n/a
256n/a The PyHKEY object methods
257n/a
258n/a************************************************************************/
259n/a/*[clinic input]
260n/awinreg.HKEYType.Close
261n/a
262n/aCloses the underlying Windows handle.
263n/a
264n/aIf the handle is already closed, no error is raised.
265n/a[clinic start generated code]*/
266n/a
267n/astatic PyObject *
268n/awinreg_HKEYType_Close_impl(PyHKEYObject *self)
269n/a/*[clinic end generated code: output=fced3a624fb0c344 input=6786ac75f6b89de6]*/
270n/a{
271n/a if (!PyHKEY_Close((PyObject *)self))
272n/a return NULL;
273n/a Py_RETURN_NONE;
274n/a}
275n/a
276n/a/*[clinic input]
277n/awinreg.HKEYType.Detach
278n/a
279n/aDetaches the Windows handle from the handle object.
280n/a
281n/aThe result is the value of the handle before it is detached. If the
282n/ahandle is already detached, this will return zero.
283n/a
284n/aAfter calling this function, the handle is effectively invalidated,
285n/abut the handle is not closed. You would call this function when you
286n/aneed the underlying win32 handle to exist beyond the lifetime of the
287n/ahandle object.
288n/a[clinic start generated code]*/
289n/a
290n/astatic PyObject *
291n/awinreg_HKEYType_Detach_impl(PyHKEYObject *self)
292n/a/*[clinic end generated code: output=dda5a9e1a01ae78f input=dd2cc09e6c6ba833]*/
293n/a{
294n/a void* ret;
295n/a ret = (void*)self->hkey;
296n/a self->hkey = 0;
297n/a return PyLong_FromVoidPtr(ret);
298n/a}
299n/a
300n/a/*[clinic input]
301n/awinreg.HKEYType.__enter__ -> self
302n/a[clinic start generated code]*/
303n/a
304n/astatic PyHKEYObject *
305n/awinreg_HKEYType___enter___impl(PyHKEYObject *self)
306n/a/*[clinic end generated code: output=52c34986dab28990 input=c40fab1f0690a8e2]*/
307n/a{
308n/a Py_XINCREF(self);
309n/a return self;
310n/a}
311n/a
312n/a
313n/a/*[clinic input]
314n/awinreg.HKEYType.__exit__
315n/a
316n/a exc_type: object
317n/a exc_value: object
318n/a traceback: object
319n/a[clinic start generated code]*/
320n/a
321n/astatic PyObject *
322n/awinreg_HKEYType___exit___impl(PyHKEYObject *self, PyObject *exc_type,
323n/a PyObject *exc_value, PyObject *traceback)
324n/a/*[clinic end generated code: output=923ebe7389e6a263 input=fb32489ee92403c7]*/
325n/a{
326n/a if (!PyHKEY_Close((PyObject *)self))
327n/a return NULL;
328n/a Py_RETURN_NONE;
329n/a}
330n/a
331n/a/*[clinic input]
332n/a[clinic start generated code]*/
333n/a/*[clinic end generated code: output=da39a3ee5e6b4b0d input=da39a3ee5e6b4b0d]*/
334n/a
335n/astatic struct PyMethodDef PyHKEY_methods[] = {
336n/a WINREG_HKEYTYPE_CLOSE_METHODDEF
337n/a WINREG_HKEYTYPE_DETACH_METHODDEF
338n/a WINREG_HKEYTYPE___ENTER___METHODDEF
339n/a WINREG_HKEYTYPE___EXIT___METHODDEF
340n/a {NULL}
341n/a};
342n/a
343n/a#define OFF(e) offsetof(PyHKEYObject, e)
344n/astatic PyMemberDef PyHKEY_memberlist[] = {
345n/a {"handle", T_INT, OFF(hkey), READONLY},
346n/a {NULL} /* Sentinel */
347n/a};
348n/a
349n/a/* The type itself */
350n/aPyTypeObject PyHKEY_Type =
351n/a{
352n/a PyVarObject_HEAD_INIT(0, 0) /* fill in type at module init */
353n/a "PyHKEY",
354n/a sizeof(PyHKEYObject),
355n/a 0,
356n/a PyHKEY_deallocFunc, /* tp_dealloc */
357n/a 0, /* tp_print */
358n/a 0, /* tp_getattr */
359n/a 0, /* tp_setattr */
360n/a 0, /* tp_reserved */
361n/a 0, /* tp_repr */
362n/a &PyHKEY_NumberMethods, /* tp_as_number */
363n/a 0, /* tp_as_sequence */
364n/a 0, /* tp_as_mapping */
365n/a PyHKEY_hashFunc, /* tp_hash */
366n/a 0, /* tp_call */
367n/a PyHKEY_strFunc, /* tp_str */
368n/a 0, /* tp_getattro */
369n/a 0, /* tp_setattro */
370n/a 0, /* tp_as_buffer */
371n/a 0, /* tp_flags */
372n/a PyHKEY_doc, /* tp_doc */
373n/a 0, /*tp_traverse*/
374n/a 0, /*tp_clear*/
375n/a 0, /*tp_richcompare*/
376n/a 0, /*tp_weaklistoffset*/
377n/a 0, /*tp_iter*/
378n/a 0, /*tp_iternext*/
379n/a PyHKEY_methods, /*tp_methods*/
380n/a PyHKEY_memberlist, /*tp_members*/
381n/a};
382n/a
383n/a/************************************************************************
384n/a The public PyHKEY API (well, not public yet :-)
385n/a************************************************************************/
386n/aPyObject *
387n/aPyHKEY_New(HKEY hInit)
388n/a{
389n/a PyHKEYObject *key = PyObject_NEW(PyHKEYObject, &PyHKEY_Type);
390n/a if (key)
391n/a key->hkey = hInit;
392n/a return (PyObject *)key;
393n/a}
394n/a
395n/aBOOL
396n/aPyHKEY_Close(PyObject *ob_handle)
397n/a{
398n/a LONG rc;
399n/a PyHKEYObject *key;
400n/a
401n/a if (!PyHKEY_Check(ob_handle)) {
402n/a PyErr_SetString(PyExc_TypeError, "bad operand type");
403n/a return FALSE;
404n/a }
405n/a key = (PyHKEYObject *)ob_handle;
406n/a rc = key->hkey ? RegCloseKey((HKEY)key->hkey) : ERROR_SUCCESS;
407n/a key->hkey = 0;
408n/a if (rc != ERROR_SUCCESS)
409n/a PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
410n/a return rc == ERROR_SUCCESS;
411n/a}
412n/a
413n/aBOOL
414n/aPyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK)
415n/a{
416n/a if (ob == Py_None) {
417n/a if (!bNoneOK) {
418n/a PyErr_SetString(
419n/a PyExc_TypeError,
420n/a "None is not a valid HKEY in this context");
421n/a return FALSE;
422n/a }
423n/a *pHANDLE = (HKEY)0;
424n/a }
425n/a else if (PyHKEY_Check(ob)) {
426n/a PyHKEYObject *pH = (PyHKEYObject *)ob;
427n/a *pHANDLE = pH->hkey;
428n/a }
429n/a else if (PyLong_Check(ob)) {
430n/a /* We also support integers */
431n/a PyErr_Clear();
432n/a *pHANDLE = (HKEY)PyLong_AsVoidPtr(ob);
433n/a if (PyErr_Occurred())
434n/a return FALSE;
435n/a }
436n/a else {
437n/a PyErr_SetString(
438n/a PyExc_TypeError,
439n/a "The object is not a PyHKEY object");
440n/a return FALSE;
441n/a }
442n/a return TRUE;
443n/a}
444n/a
445n/aBOOL
446n/aclinic_HKEY_converter(PyObject *ob, void *p)
447n/a{
448n/a if (!PyHKEY_AsHKEY(ob, (HKEY *)p, FALSE))
449n/a return FALSE;
450n/a return TRUE;
451n/a}
452n/a
453n/aPyObject *
454n/aPyHKEY_FromHKEY(HKEY h)
455n/a{
456n/a PyHKEYObject *op;
457n/a
458n/a /* Inline PyObject_New */
459n/a op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject));
460n/a if (op == NULL)
461n/a return PyErr_NoMemory();
462n/a PyObject_INIT(op, &PyHKEY_Type);
463n/a op->hkey = h;
464n/a return (PyObject *)op;
465n/a}
466n/a
467n/a
468n/a/************************************************************************
469n/a The module methods
470n/a************************************************************************/
471n/aBOOL
472n/aPyWinObject_CloseHKEY(PyObject *obHandle)
473n/a{
474n/a BOOL ok;
475n/a if (PyHKEY_Check(obHandle)) {
476n/a ok = PyHKEY_Close(obHandle);
477n/a }
478n/a#if SIZEOF_LONG >= SIZEOF_HKEY
479n/a else if (PyLong_Check(obHandle)) {
480n/a long rc = RegCloseKey((HKEY)PyLong_AsLong(obHandle));
481n/a ok = (rc == ERROR_SUCCESS);
482n/a if (!ok)
483n/a PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
484n/a }
485n/a#else
486n/a else if (PyLong_Check(obHandle)) {
487n/a long rc = RegCloseKey((HKEY)PyLong_AsVoidPtr(obHandle));
488n/a ok = (rc == ERROR_SUCCESS);
489n/a if (!ok)
490n/a PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
491n/a }
492n/a#endif
493n/a else {
494n/a PyErr_SetString(
495n/a PyExc_TypeError,
496n/a "A handle must be a HKEY object or an integer");
497n/a return FALSE;
498n/a }
499n/a return ok;
500n/a}
501n/a
502n/a
503n/a/*
504n/a Private Helper functions for the registry interfaces
505n/a
506n/a** Note that fixupMultiSZ and countString have both had changes
507n/a** made to support "incorrect strings". The registry specification
508n/a** calls for strings to be terminated with 2 null bytes. It seems
509n/a** some commercial packages install strings which dont conform,
510n/a** causing this code to fail - however, "regedit" etc still work
511n/a** with these strings (ie only we dont!).
512n/a*/
513n/astatic void
514n/afixupMultiSZ(wchar_t **str, wchar_t *data, int len)
515n/a{
516n/a wchar_t *P;
517n/a int i;
518n/a wchar_t *Q;
519n/a
520n/a Q = data + len;
521n/a for (P = data, i = 0; P < Q && *P != '\0'; P++, i++) {
522n/a str[i] = P;
523n/a for(; *P != '\0'; P++)
524n/a ;
525n/a }
526n/a}
527n/a
528n/astatic int
529n/acountStrings(wchar_t *data, int len)
530n/a{
531n/a int strings;
532n/a wchar_t *P;
533n/a wchar_t *Q = data + len;
534n/a
535n/a for (P = data, strings = 0; P < Q && *P != '\0'; P++, strings++)
536n/a for (; P < Q && *P != '\0'; P++)
537n/a ;
538n/a return strings;
539n/a}
540n/a
541n/a/* Convert PyObject into Registry data.
542n/a Allocates space as needed. */
543n/astatic BOOL
544n/aPy2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
545n/a{
546n/a Py_ssize_t i,j;
547n/a switch (typ) {
548n/a case REG_DWORD:
549n/a if (value != Py_None && !PyLong_Check(value))
550n/a return FALSE;
551n/a *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1);
552n/a if (*retDataBuf == NULL){
553n/a PyErr_NoMemory();
554n/a return FALSE;
555n/a }
556n/a *retDataSize = sizeof(DWORD);
557n/a if (value == Py_None) {
558n/a DWORD zero = 0;
559n/a memcpy(*retDataBuf, &zero, sizeof(DWORD));
560n/a }
561n/a else {
562n/a DWORD d = PyLong_AsUnsignedLong(value);
563n/a memcpy(*retDataBuf, &d, sizeof(DWORD));
564n/a }
565n/a break;
566n/a case REG_QWORD:
567n/a if (value != Py_None && !PyLong_Check(value))
568n/a return FALSE;
569n/a *retDataBuf = (BYTE *)PyMem_NEW(DWORD64, 1);
570n/a if (*retDataBuf == NULL){
571n/a PyErr_NoMemory();
572n/a return FALSE;
573n/a }
574n/a *retDataSize = sizeof(DWORD64);
575n/a if (value == Py_None) {
576n/a DWORD64 zero = 0;
577n/a memcpy(*retDataBuf, &zero, sizeof(DWORD64));
578n/a }
579n/a else {
580n/a DWORD64 d = PyLong_AsUnsignedLongLong(value);
581n/a memcpy(*retDataBuf, &d, sizeof(DWORD64));
582n/a }
583n/a break;
584n/a case REG_SZ:
585n/a case REG_EXPAND_SZ:
586n/a {
587n/a if (value != Py_None) {
588n/a Py_ssize_t len;
589n/a if (!PyUnicode_Check(value))
590n/a return FALSE;
591n/a *retDataBuf = (BYTE*)PyUnicode_AsWideCharString(value, &len);
592n/a if (*retDataBuf == NULL)
593n/a return FALSE;
594n/a *retDataSize = Py_SAFE_DOWNCAST(
595n/a (len + 1) * sizeof(wchar_t),
596n/a Py_ssize_t, DWORD);
597n/a }
598n/a else {
599n/a *retDataBuf = (BYTE *)PyMem_NEW(wchar_t, 1);
600n/a if (*retDataBuf == NULL) {
601n/a PyErr_NoMemory();
602n/a return FALSE;
603n/a }
604n/a ((wchar_t *)*retDataBuf)[0] = L'\0';
605n/a *retDataSize = 1 * sizeof(wchar_t);
606n/a }
607n/a break;
608n/a }
609n/a case REG_MULTI_SZ:
610n/a {
611n/a DWORD size = 0;
612n/a wchar_t *P;
613n/a
614n/a if (value == Py_None)
615n/a i = 0;
616n/a else {
617n/a if (!PyList_Check(value))
618n/a return FALSE;
619n/a i = PyList_Size(value);
620n/a }
621n/a for (j = 0; j < i; j++)
622n/a {
623n/a PyObject *t;
624n/a wchar_t *wstr;
625n/a Py_ssize_t len;
626n/a
627n/a t = PyList_GET_ITEM(value, j);
628n/a if (!PyUnicode_Check(t))
629n/a return FALSE;
630n/a wstr = PyUnicode_AsUnicodeAndSize(t, &len);
631n/a if (wstr == NULL)
632n/a return FALSE;
633n/a size += Py_SAFE_DOWNCAST((len + 1) * sizeof(wchar_t),
634n/a size_t, DWORD);
635n/a }
636n/a
637n/a *retDataSize = size + 2;
638n/a *retDataBuf = (BYTE *)PyMem_NEW(char,
639n/a *retDataSize);
640n/a if (*retDataBuf == NULL){
641n/a PyErr_NoMemory();
642n/a return FALSE;
643n/a }
644n/a P = (wchar_t *)*retDataBuf;
645n/a
646n/a for (j = 0; j < i; j++)
647n/a {
648n/a PyObject *t;
649n/a wchar_t *wstr;
650n/a Py_ssize_t len;
651n/a
652n/a t = PyList_GET_ITEM(value, j);
653n/a wstr = PyUnicode_AsUnicodeAndSize(t, &len);
654n/a if (wstr == NULL)
655n/a return FALSE;
656n/a wcscpy(P, wstr);
657n/a P += (len + 1);
658n/a }
659n/a /* And doubly-terminate the list... */
660n/a *P = '\0';
661n/a break;
662n/a }
663n/a case REG_BINARY:
664n/a /* ALSO handle ALL unknown data types here. Even if we can't
665n/a support it natively, we should handle the bits. */
666n/a default:
667n/a if (value == Py_None) {
668n/a *retDataSize = 0;
669n/a *retDataBuf = NULL;
670n/a }
671n/a else {
672n/a Py_buffer view;
673n/a
674n/a if (!PyObject_CheckBuffer(value)) {
675n/a PyErr_Format(PyExc_TypeError,
676n/a "Objects of type '%s' can not "
677n/a "be used as binary registry values",
678n/a value->ob_type->tp_name);
679n/a return FALSE;
680n/a }
681n/a
682n/a if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0)
683n/a return FALSE;
684n/a
685n/a *retDataBuf = (BYTE *)PyMem_NEW(char, view.len);
686n/a if (*retDataBuf == NULL){
687n/a PyBuffer_Release(&view);
688n/a PyErr_NoMemory();
689n/a return FALSE;
690n/a }
691n/a *retDataSize = Py_SAFE_DOWNCAST(view.len, Py_ssize_t, DWORD);
692n/a memcpy(*retDataBuf, view.buf, view.len);
693n/a PyBuffer_Release(&view);
694n/a }
695n/a break;
696n/a }
697n/a return TRUE;
698n/a}
699n/a
700n/a/* Convert Registry data into PyObject*/
701n/astatic PyObject *
702n/aReg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
703n/a{
704n/a PyObject *obData;
705n/a
706n/a switch (typ) {
707n/a case REG_DWORD:
708n/a if (retDataSize == 0)
709n/a obData = PyLong_FromUnsignedLong(0);
710n/a else
711n/a obData = PyLong_FromUnsignedLong(*(DWORD *)retDataBuf);
712n/a break;
713n/a case REG_QWORD:
714n/a if (retDataSize == 0)
715n/a obData = PyLong_FromUnsignedLongLong(0);
716n/a else
717n/a obData = PyLong_FromUnsignedLongLong(*(DWORD64 *)retDataBuf);
718n/a break;
719n/a case REG_SZ:
720n/a case REG_EXPAND_SZ:
721n/a {
722n/a /* REG_SZ should be a NUL terminated string, but only by
723n/a * convention. The buffer may have been saved without a NUL
724n/a * or with embedded NULs. To be consistent with reg.exe and
725n/a * regedit.exe, consume only up to the first NUL. */
726n/a wchar_t *data = (wchar_t *)retDataBuf;
727n/a size_t len = wcsnlen(data, retDataSize / sizeof(wchar_t));
728n/a obData = PyUnicode_FromWideChar(data, len);
729n/a break;
730n/a }
731n/a case REG_MULTI_SZ:
732n/a if (retDataSize == 0)
733n/a obData = PyList_New(0);
734n/a else
735n/a {
736n/a int index = 0;
737n/a wchar_t *data = (wchar_t *)retDataBuf;
738n/a int len = retDataSize / 2;
739n/a int s = countStrings(data, len);
740n/a wchar_t **str = PyMem_New(wchar_t *, s);
741n/a if (str == NULL)
742n/a return PyErr_NoMemory();
743n/a
744n/a fixupMultiSZ(str, data, len);
745n/a obData = PyList_New(s);
746n/a if (obData == NULL) {
747n/a PyMem_Free(str);
748n/a return NULL;
749n/a }
750n/a for (index = 0; index < s; index++)
751n/a {
752n/a size_t len = wcslen(str[index]);
753n/a if (len > INT_MAX) {
754n/a PyErr_SetString(PyExc_OverflowError,
755n/a "registry string is too long for a Python string");
756n/a Py_DECREF(obData);
757n/a PyMem_Free(str);
758n/a return NULL;
759n/a }
760n/a PyList_SetItem(obData,
761n/a index,
762n/a PyUnicode_FromWideChar(str[index], len));
763n/a }
764n/a PyMem_Free(str);
765n/a
766n/a break;
767n/a }
768n/a case REG_BINARY:
769n/a /* ALSO handle ALL unknown data types here. Even if we can't
770n/a support it natively, we should handle the bits. */
771n/a default:
772n/a if (retDataSize == 0) {
773n/a Py_INCREF(Py_None);
774n/a obData = Py_None;
775n/a }
776n/a else
777n/a obData = PyBytes_FromStringAndSize(
778n/a (char *)retDataBuf, retDataSize);
779n/a break;
780n/a }
781n/a return obData;
782n/a}
783n/a
784n/a/* The Python methods */
785n/a
786n/a/*[clinic input]
787n/awinreg.CloseKey
788n/a
789n/a hkey: object
790n/a A previously opened key.
791n/a /
792n/a
793n/aCloses a previously opened registry key.
794n/a
795n/aNote that if the key is not closed using this method, it will be
796n/aclosed when the hkey object is destroyed by Python.
797n/a[clinic start generated code]*/
798n/a
799n/astatic PyObject *
800n/awinreg_CloseKey(PyObject *module, PyObject *hkey)
801n/a/*[clinic end generated code: output=a4fa537019a80d15 input=5b1aac65ba5127ad]*/
802n/a{
803n/a if (!PyHKEY_Close(hkey))
804n/a return NULL;
805n/a Py_RETURN_NONE;
806n/a}
807n/a
808n/a/*[clinic input]
809n/awinreg.ConnectRegistry -> HKEY
810n/a
811n/a computer_name: Py_UNICODE(accept={str, NoneType})
812n/a The name of the remote computer, of the form r"\\computername". If
813n/a None, the local computer is used.
814n/a key: HKEY
815n/a The predefined key to connect to.
816n/a /
817n/a
818n/aEstablishes a connection to the registry on another computer.
819n/a
820n/aThe return value is the handle of the opened key.
821n/aIf the function fails, an OSError exception is raised.
822n/a[clinic start generated code]*/
823n/a
824n/astatic HKEY
825n/awinreg_ConnectRegistry_impl(PyObject *module, Py_UNICODE *computer_name,
826n/a HKEY key)
827n/a/*[clinic end generated code: output=5ab79d02aa3167b4 input=5f98a891a347e68e]*/
828n/a{
829n/a HKEY retKey;
830n/a long rc;
831n/a Py_BEGIN_ALLOW_THREADS
832n/a rc = RegConnectRegistryW(computer_name, key, &retKey);
833n/a Py_END_ALLOW_THREADS
834n/a if (rc != ERROR_SUCCESS) {
835n/a PyErr_SetFromWindowsErrWithFunction(rc, "ConnectRegistry");
836n/a return NULL;
837n/a }
838n/a return retKey;
839n/a}
840n/a
841n/a/*[clinic input]
842n/awinreg.CreateKey -> HKEY
843n/a
844n/a key: HKEY
845n/a An already open key, or one of the predefined HKEY_* constants.
846n/a sub_key: Py_UNICODE(accept={str, NoneType})
847n/a The name of the key this method opens or creates.
848n/a /
849n/a
850n/aCreates or opens the specified key.
851n/a
852n/aIf key is one of the predefined keys, sub_key may be None. In that case,
853n/athe handle returned is the same key handle passed in to the function.
854n/a
855n/aIf the key already exists, this function opens the existing key.
856n/a
857n/aThe return value is the handle of the opened key.
858n/aIf the function fails, an OSError exception is raised.
859n/a[clinic start generated code]*/
860n/a
861n/astatic HKEY
862n/awinreg_CreateKey_impl(PyObject *module, HKEY key, Py_UNICODE *sub_key)
863n/a/*[clinic end generated code: output=9c81d4095527c927 input=3cdd1622488acea2]*/
864n/a{
865n/a HKEY retKey;
866n/a long rc;
867n/a
868n/a rc = RegCreateKeyW(key, sub_key, &retKey);
869n/a if (rc != ERROR_SUCCESS) {
870n/a PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey");
871n/a return NULL;
872n/a }
873n/a return retKey;
874n/a}
875n/a
876n/a/*[clinic input]
877n/awinreg.CreateKeyEx -> HKEY
878n/a
879n/a key: HKEY
880n/a An already open key, or one of the predefined HKEY_* constants.
881n/a sub_key: Py_UNICODE(accept={str, NoneType})
882n/a The name of the key this method opens or creates.
883n/a reserved: int = 0
884n/a A reserved integer, and must be zero. Default is zero.
885n/a access: REGSAM(c_default='KEY_WRITE') = winreg.KEY_WRITE
886n/a An integer that specifies an access mask that describes the
887n/a desired security access for the key. Default is KEY_WRITE.
888n/a
889n/aCreates or opens the specified key.
890n/a
891n/aIf key is one of the predefined keys, sub_key may be None. In that case,
892n/athe handle returned is the same key handle passed in to the function.
893n/a
894n/aIf the key already exists, this function opens the existing key
895n/a
896n/aThe return value is the handle of the opened key.
897n/aIf the function fails, an OSError exception is raised.
898n/a[clinic start generated code]*/
899n/a
900n/astatic HKEY
901n/awinreg_CreateKeyEx_impl(PyObject *module, HKEY key, Py_UNICODE *sub_key,
902n/a int reserved, REGSAM access)
903n/a/*[clinic end generated code: output=b9fce6dc5c4e39b1 input=42c2b03f98406b66]*/
904n/a{
905n/a HKEY retKey;
906n/a long rc;
907n/a
908n/a rc = RegCreateKeyExW(key, sub_key, reserved, NULL, (DWORD)NULL,
909n/a access, NULL, &retKey, NULL);
910n/a if (rc != ERROR_SUCCESS) {
911n/a PyErr_SetFromWindowsErrWithFunction(rc, "CreateKeyEx");
912n/a return NULL;
913n/a }
914n/a return retKey;
915n/a}
916n/a
917n/a/*[clinic input]
918n/awinreg.DeleteKey
919n/a key: HKEY
920n/a An already open key, or any one of the predefined HKEY_* constants.
921n/a sub_key: Py_UNICODE
922n/a A string that must be the name of a subkey of the key identified by
923n/a the key parameter. This value must not be None, and the key may not
924n/a have subkeys.
925n/a /
926n/a
927n/aDeletes the specified key.
928n/a
929n/aThis method can not delete keys with subkeys.
930n/a
931n/aIf the function succeeds, the entire key, including all of its values,
932n/ais removed. If the function fails, an OSError exception is raised.
933n/a[clinic start generated code]*/
934n/a
935n/astatic PyObject *
936n/awinreg_DeleteKey_impl(PyObject *module, HKEY key, Py_UNICODE *sub_key)
937n/a/*[clinic end generated code: output=7734b1e431991ae4 input=b31d225b935e4211]*/
938n/a{
939n/a long rc;
940n/a rc = RegDeleteKeyW(key, sub_key );
941n/a if (rc != ERROR_SUCCESS)
942n/a return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey");
943n/a Py_RETURN_NONE;
944n/a}
945n/a
946n/a/*[clinic input]
947n/awinreg.DeleteKeyEx
948n/a
949n/a key: HKEY
950n/a An already open key, or any one of the predefined HKEY_* constants.
951n/a sub_key: Py_UNICODE
952n/a A string that must be the name of a subkey of the key identified by
953n/a the key parameter. This value must not be None, and the key may not
954n/a have subkeys.
955n/a access: REGSAM(c_default='KEY_WOW64_64KEY') = winreg.KEY_WOW64_64KEY
956n/a An integer that specifies an access mask that describes the
957n/a desired security access for the key. Default is KEY_WOW64_64KEY.
958n/a reserved: int = 0
959n/a A reserved integer, and must be zero. Default is zero.
960n/a
961n/aDeletes the specified key (64-bit OS only).
962n/a
963n/aThis method can not delete keys with subkeys.
964n/a
965n/aIf the function succeeds, the entire key, including all of its values,
966n/ais removed. If the function fails, an OSError exception is raised.
967n/aOn unsupported Windows versions, NotImplementedError is raised.
968n/a[clinic start generated code]*/
969n/a
970n/astatic PyObject *
971n/awinreg_DeleteKeyEx_impl(PyObject *module, HKEY key, Py_UNICODE *sub_key,
972n/a REGSAM access, int reserved)
973n/a/*[clinic end generated code: output=01378d86ad3eb936 input=711d9d89e7ecbed7]*/
974n/a{
975n/a HMODULE hMod;
976n/a typedef LONG (WINAPI *RDKEFunc)(HKEY, const wchar_t*, REGSAM, int);
977n/a RDKEFunc pfn = NULL;
978n/a long rc;
979n/a
980n/a /* Only available on 64bit platforms, so we must load it
981n/a dynamically. */
982n/a hMod = GetModuleHandleW(L"advapi32.dll");
983n/a if (hMod)
984n/a pfn = (RDKEFunc)GetProcAddress(hMod,
985n/a "RegDeleteKeyExW");
986n/a if (!pfn) {
987n/a PyErr_SetString(PyExc_NotImplementedError,
988n/a "not implemented on this platform");
989n/a return NULL;
990n/a }
991n/a Py_BEGIN_ALLOW_THREADS
992n/a rc = (*pfn)(key, sub_key, access, reserved);
993n/a Py_END_ALLOW_THREADS
994n/a
995n/a if (rc != ERROR_SUCCESS)
996n/a return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKeyEx");
997n/a Py_RETURN_NONE;
998n/a}
999n/a
1000n/a/*[clinic input]
1001n/awinreg.DeleteValue
1002n/a
1003n/a key: HKEY
1004n/a An already open key, or any one of the predefined HKEY_* constants.
1005n/a value: Py_UNICODE(accept={str, NoneType})
1006n/a A string that identifies the value to remove.
1007n/a /
1008n/a
1009n/aRemoves a named value from a registry key.
1010n/a[clinic start generated code]*/
1011n/a
1012n/astatic PyObject *
1013n/awinreg_DeleteValue_impl(PyObject *module, HKEY key, Py_UNICODE *value)
1014n/a/*[clinic end generated code: output=67e7e9a514f84951 input=a78d3407a4197b21]*/
1015n/a{
1016n/a long rc;
1017n/a Py_BEGIN_ALLOW_THREADS
1018n/a rc = RegDeleteValueW(key, value);
1019n/a Py_END_ALLOW_THREADS
1020n/a if (rc !=ERROR_SUCCESS)
1021n/a return PyErr_SetFromWindowsErrWithFunction(rc,
1022n/a "RegDeleteValue");
1023n/a Py_RETURN_NONE;
1024n/a}
1025n/a
1026n/a/*[clinic input]
1027n/awinreg.EnumKey
1028n/a
1029n/a key: HKEY
1030n/a An already open key, or any one of the predefined HKEY_* constants.
1031n/a index: int
1032n/a An integer that identifies the index of the key to retrieve.
1033n/a /
1034n/a
1035n/aEnumerates subkeys of an open registry key.
1036n/a
1037n/aThe function retrieves the name of one subkey each time it is called.
1038n/aIt is typically called repeatedly until an OSError exception is
1039n/araised, indicating no more values are available.
1040n/a[clinic start generated code]*/
1041n/a
1042n/astatic PyObject *
1043n/awinreg_EnumKey_impl(PyObject *module, HKEY key, int index)
1044n/a/*[clinic end generated code: output=25a6ec52cd147bc4 input=fad9a7c00ab0e04b]*/
1045n/a{
1046n/a long rc;
1047n/a PyObject *retStr;
1048n/a
1049n/a /* The Windows docs claim that the max key name length is 255
1050n/a * characters, plus a terminating nul character. However,
1051n/a * empirical testing demonstrates that it is possible to
1052n/a * create a 256 character key that is missing the terminating
1053n/a * nul. RegEnumKeyEx requires a 257 character buffer to
1054n/a * retrieve such a key name. */
1055n/a wchar_t tmpbuf[257];
1056n/a DWORD len = sizeof(tmpbuf)/sizeof(wchar_t); /* includes NULL terminator */
1057n/a
1058n/a Py_BEGIN_ALLOW_THREADS
1059n/a rc = RegEnumKeyExW(key, index, tmpbuf, &len, NULL, NULL, NULL, NULL);
1060n/a Py_END_ALLOW_THREADS
1061n/a if (rc != ERROR_SUCCESS)
1062n/a return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx");
1063n/a
1064n/a retStr = PyUnicode_FromWideChar(tmpbuf, len);
1065n/a return retStr; /* can be NULL */
1066n/a}
1067n/a
1068n/a/*[clinic input]
1069n/awinreg.EnumValue
1070n/a
1071n/a key: HKEY
1072n/a An already open key, or any one of the predefined HKEY_* constants.
1073n/a index: int
1074n/a An integer that identifies the index of the value to retrieve.
1075n/a /
1076n/a
1077n/aEnumerates values of an open registry key.
1078n/a
1079n/aThe function retrieves the name of one subkey each time it is called.
1080n/aIt is typically called repeatedly, until an OSError exception
1081n/ais raised, indicating no more values.
1082n/a
1083n/aThe result is a tuple of 3 items:
1084n/a value_name
1085n/a A string that identifies the value.
1086n/a value_data
1087n/a An object that holds the value data, and whose type depends
1088n/a on the underlying registry type.
1089n/a data_type
1090n/a An integer that identifies the type of the value data.
1091n/a[clinic start generated code]*/
1092n/a
1093n/astatic PyObject *
1094n/awinreg_EnumValue_impl(PyObject *module, HKEY key, int index)
1095n/a/*[clinic end generated code: output=d363b5a06f8789ac input=4414f47a6fb238b5]*/
1096n/a{
1097n/a long rc;
1098n/a wchar_t *retValueBuf;
1099n/a BYTE *tmpBuf;
1100n/a BYTE *retDataBuf;
1101n/a DWORD retValueSize, bufValueSize;
1102n/a DWORD retDataSize, bufDataSize;
1103n/a DWORD typ;
1104n/a PyObject *obData;
1105n/a PyObject *retVal;
1106n/a
1107n/a if ((rc = RegQueryInfoKeyW(key, NULL, NULL, NULL, NULL, NULL, NULL,
1108n/a NULL,
1109n/a &retValueSize, &retDataSize, NULL, NULL))
1110n/a != ERROR_SUCCESS)
1111n/a return PyErr_SetFromWindowsErrWithFunction(rc,
1112n/a "RegQueryInfoKey");
1113n/a ++retValueSize; /* include null terminators */
1114n/a ++retDataSize;
1115n/a bufDataSize = retDataSize;
1116n/a bufValueSize = retValueSize;
1117n/a retValueBuf = PyMem_New(wchar_t, retValueSize);
1118n/a if (retValueBuf == NULL)
1119n/a return PyErr_NoMemory();
1120n/a retDataBuf = (BYTE *)PyMem_Malloc(retDataSize);
1121n/a if (retDataBuf == NULL) {
1122n/a PyMem_Free(retValueBuf);
1123n/a return PyErr_NoMemory();
1124n/a }
1125n/a
1126n/a while (1) {
1127n/a Py_BEGIN_ALLOW_THREADS
1128n/a rc = RegEnumValueW(key,
1129n/a index,
1130n/a retValueBuf,
1131n/a &retValueSize,
1132n/a NULL,
1133n/a &typ,
1134n/a (BYTE *)retDataBuf,
1135n/a &retDataSize);
1136n/a Py_END_ALLOW_THREADS
1137n/a
1138n/a if (rc != ERROR_MORE_DATA)
1139n/a break;
1140n/a
1141n/a bufDataSize *= 2;
1142n/a tmpBuf = (BYTE *)PyMem_Realloc(retDataBuf, bufDataSize);
1143n/a if (tmpBuf == NULL) {
1144n/a PyErr_NoMemory();
1145n/a retVal = NULL;
1146n/a goto fail;
1147n/a }
1148n/a retDataBuf = tmpBuf;
1149n/a retDataSize = bufDataSize;
1150n/a retValueSize = bufValueSize;
1151n/a }
1152n/a
1153n/a if (rc != ERROR_SUCCESS) {
1154n/a retVal = PyErr_SetFromWindowsErrWithFunction(rc,
1155n/a "PyRegEnumValue");
1156n/a goto fail;
1157n/a }
1158n/a obData = Reg2Py(retDataBuf, retDataSize, typ);
1159n/a if (obData == NULL) {
1160n/a retVal = NULL;
1161n/a goto fail;
1162n/a }
1163n/a retVal = Py_BuildValue("uOi", retValueBuf, obData, typ);
1164n/a Py_DECREF(obData);
1165n/a fail:
1166n/a PyMem_Free(retValueBuf);
1167n/a PyMem_Free(retDataBuf);
1168n/a return retVal;
1169n/a}
1170n/a
1171n/a/*[clinic input]
1172n/awinreg.ExpandEnvironmentStrings
1173n/a
1174n/a string: Py_UNICODE
1175n/a /
1176n/a
1177n/aExpand environment vars.
1178n/a[clinic start generated code]*/
1179n/a
1180n/astatic PyObject *
1181n/awinreg_ExpandEnvironmentStrings_impl(PyObject *module, Py_UNICODE *string)
1182n/a/*[clinic end generated code: output=cba46ac293a8af1a input=b2a9714d2b751aa6]*/
1183n/a{
1184n/a wchar_t *retValue = NULL;
1185n/a DWORD retValueSize;
1186n/a DWORD rc;
1187n/a PyObject *o;
1188n/a
1189n/a retValueSize = ExpandEnvironmentStringsW(string, retValue, 0);
1190n/a if (retValueSize == 0) {
1191n/a return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1192n/a "ExpandEnvironmentStrings");
1193n/a }
1194n/a retValue = PyMem_New(wchar_t, retValueSize);
1195n/a if (retValue == NULL) {
1196n/a return PyErr_NoMemory();
1197n/a }
1198n/a
1199n/a rc = ExpandEnvironmentStringsW(string, retValue, retValueSize);
1200n/a if (rc == 0) {
1201n/a PyMem_Free(retValue);
1202n/a return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1203n/a "ExpandEnvironmentStrings");
1204n/a }
1205n/a o = PyUnicode_FromWideChar(retValue, wcslen(retValue));
1206n/a PyMem_Free(retValue);
1207n/a return o;
1208n/a}
1209n/a
1210n/a/*[clinic input]
1211n/awinreg.FlushKey
1212n/a
1213n/a key: HKEY
1214n/a An already open key, or any one of the predefined HKEY_* constants.
1215n/a /
1216n/a
1217n/aWrites all the attributes of a key to the registry.
1218n/a
1219n/aIt is not necessary to call FlushKey to change a key. Registry changes
1220n/aare flushed to disk by the registry using its lazy flusher. Registry
1221n/achanges are also flushed to disk at system shutdown. Unlike
1222n/aCloseKey(), the FlushKey() method returns only when all the data has
1223n/abeen written to the registry.
1224n/a
1225n/aAn application should only call FlushKey() if it requires absolute
1226n/acertainty that registry changes are on disk. If you don't know whether
1227n/aa FlushKey() call is required, it probably isn't.
1228n/a[clinic start generated code]*/
1229n/a
1230n/astatic PyObject *
1231n/awinreg_FlushKey_impl(PyObject *module, HKEY key)
1232n/a/*[clinic end generated code: output=e6fc230d4c5dc049 input=f57457c12297d82f]*/
1233n/a{
1234n/a long rc;
1235n/a Py_BEGIN_ALLOW_THREADS
1236n/a rc = RegFlushKey(key);
1237n/a Py_END_ALLOW_THREADS
1238n/a if (rc != ERROR_SUCCESS)
1239n/a return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey");
1240n/a Py_RETURN_NONE;
1241n/a}
1242n/a
1243n/a
1244n/a/*[clinic input]
1245n/awinreg.LoadKey
1246n/a
1247n/a key: HKEY
1248n/a An already open key, or any one of the predefined HKEY_* constants.
1249n/a sub_key: Py_UNICODE
1250n/a A string that identifies the sub-key to load.
1251n/a file_name: Py_UNICODE
1252n/a The name of the file to load registry data from. This file must
1253n/a have been created with the SaveKey() function. Under the file
1254n/a allocation table (FAT) file system, the filename may not have an
1255n/a extension.
1256n/a /
1257n/a
1258n/aInsert data into the registry from a file.
1259n/a
1260n/aCreates a subkey under the specified key and stores registration
1261n/ainformation from a specified file into that subkey.
1262n/a
1263n/aA call to LoadKey() fails if the calling process does not have the
1264n/aSE_RESTORE_PRIVILEGE privilege.
1265n/a
1266n/aIf key is a handle returned by ConnectRegistry(), then the path
1267n/aspecified in fileName is relative to the remote computer.
1268n/a
1269n/aThe MSDN docs imply key must be in the HKEY_USER or HKEY_LOCAL_MACHINE
1270n/atree.
1271n/a[clinic start generated code]*/
1272n/a
1273n/astatic PyObject *
1274n/awinreg_LoadKey_impl(PyObject *module, HKEY key, Py_UNICODE *sub_key,
1275n/a Py_UNICODE *file_name)
1276n/a/*[clinic end generated code: output=87344005c5905cde input=e3b5b45ade311582]*/
1277n/a{
1278n/a long rc;
1279n/a
1280n/a Py_BEGIN_ALLOW_THREADS
1281n/a rc = RegLoadKeyW(key, sub_key, file_name );
1282n/a Py_END_ALLOW_THREADS
1283n/a if (rc != ERROR_SUCCESS)
1284n/a return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey");
1285n/a Py_RETURN_NONE;
1286n/a}
1287n/a
1288n/a/*[clinic input]
1289n/awinreg.OpenKey -> HKEY
1290n/a
1291n/a key: HKEY
1292n/a An already open key, or any one of the predefined HKEY_* constants.
1293n/a sub_key: Py_UNICODE(accept={str, NoneType})
1294n/a A string that identifies the sub_key to open.
1295n/a reserved: int = 0
1296n/a A reserved integer that must be zero. Default is zero.
1297n/a access: REGSAM(c_default='KEY_READ') = winreg.KEY_READ
1298n/a An integer that specifies an access mask that describes the desired
1299n/a security access for the key. Default is KEY_READ.
1300n/a
1301n/aOpens the specified key.
1302n/a
1303n/aThe result is a new handle to the specified key.
1304n/aIf the function fails, an OSError exception is raised.
1305n/a[clinic start generated code]*/
1306n/a
1307n/astatic HKEY
1308n/awinreg_OpenKey_impl(PyObject *module, HKEY key, Py_UNICODE *sub_key,
1309n/a int reserved, REGSAM access)
1310n/a/*[clinic end generated code: output=a905f1b947f3ce85 input=098505ac36a9ae28]*/
1311n/a{
1312n/a HKEY retKey;
1313n/a long rc;
1314n/a
1315n/a Py_BEGIN_ALLOW_THREADS
1316n/a rc = RegOpenKeyExW(key, sub_key, reserved, access, &retKey);
1317n/a Py_END_ALLOW_THREADS
1318n/a if (rc != ERROR_SUCCESS) {
1319n/a PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx");
1320n/a return NULL;
1321n/a }
1322n/a return retKey;
1323n/a}
1324n/a
1325n/a/*[clinic input]
1326n/awinreg.OpenKeyEx = winreg.OpenKey
1327n/a
1328n/aOpens the specified key.
1329n/a
1330n/aThe result is a new handle to the specified key.
1331n/aIf the function fails, an OSError exception is raised.
1332n/a[clinic start generated code]*/
1333n/a
1334n/astatic HKEY
1335n/awinreg_OpenKeyEx_impl(PyObject *module, HKEY key, Py_UNICODE *sub_key,
1336n/a int reserved, REGSAM access)
1337n/a/*[clinic end generated code: output=226042593b37e940 input=c6c4972af8622959]*/
1338n/a{
1339n/a return winreg_OpenKey_impl(module, key, sub_key, reserved, access);
1340n/a}
1341n/a
1342n/a/*[clinic input]
1343n/awinreg.QueryInfoKey
1344n/a
1345n/a key: HKEY
1346n/a An already open key, or any one of the predefined HKEY_* constants.
1347n/a /
1348n/a
1349n/aReturns information about a key.
1350n/a
1351n/aThe result is a tuple of 3 items:
1352n/aAn integer that identifies the number of sub keys this key has.
1353n/aAn integer that identifies the number of values this key has.
1354n/aAn integer that identifies when the key was last modified (if available)
1355n/aas 100's of nanoseconds since Jan 1, 1600.
1356n/a[clinic start generated code]*/
1357n/a
1358n/astatic PyObject *
1359n/awinreg_QueryInfoKey_impl(PyObject *module, HKEY key)
1360n/a/*[clinic end generated code: output=dc657b8356a4f438 input=c3593802390cde1f]*/
1361n/a{
1362n/a long rc;
1363n/a DWORD nSubKeys, nValues;
1364n/a FILETIME ft;
1365n/a LARGE_INTEGER li;
1366n/a PyObject *l;
1367n/a PyObject *ret;
1368n/a
1369n/a if ((rc = RegQueryInfoKey(key, NULL, NULL, 0, &nSubKeys, NULL, NULL,
1370n/a &nValues, NULL, NULL, NULL, &ft))
1371n/a != ERROR_SUCCESS)
1372n/a return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey");
1373n/a li.LowPart = ft.dwLowDateTime;
1374n/a li.HighPart = ft.dwHighDateTime;
1375n/a l = PyLong_FromLongLong(li.QuadPart);
1376n/a if (l == NULL)
1377n/a return NULL;
1378n/a ret = Py_BuildValue("iiO", nSubKeys, nValues, l);
1379n/a Py_DECREF(l);
1380n/a return ret;
1381n/a}
1382n/a
1383n/a/*[clinic input]
1384n/awinreg.QueryValue
1385n/a
1386n/a key: HKEY
1387n/a An already open key, or any one of the predefined HKEY_* constants.
1388n/a sub_key: Py_UNICODE(accept={str, NoneType})
1389n/a A string that holds the name of the subkey with which the value
1390n/a is associated. If this parameter is None or empty, the function
1391n/a retrieves the value set by the SetValue() method for the key
1392n/a identified by key.
1393n/a /
1394n/a
1395n/aRetrieves the unnamed value for a key.
1396n/a
1397n/aValues in the registry have name, type, and data components. This method
1398n/aretrieves the data for a key's first value that has a NULL name.
1399n/aBut since the underlying API call doesn't return the type, you'll
1400n/aprobably be happier using QueryValueEx; this function is just here for
1401n/acompleteness.
1402n/a[clinic start generated code]*/
1403n/a
1404n/astatic PyObject *
1405n/awinreg_QueryValue_impl(PyObject *module, HKEY key, Py_UNICODE *sub_key)
1406n/a/*[clinic end generated code: output=2bb8d1e02c10d0b6 input=41cafbbf423b21d6]*/
1407n/a{
1408n/a long rc;
1409n/a PyObject *retStr;
1410n/a wchar_t *retBuf;
1411n/a DWORD bufSize = 0;
1412n/a DWORD retSize = 0;
1413n/a wchar_t *tmp;
1414n/a
1415n/a rc = RegQueryValueW(key, sub_key, NULL, &retSize);
1416n/a if (rc == ERROR_MORE_DATA)
1417n/a retSize = 256;
1418n/a else if (rc != ERROR_SUCCESS)
1419n/a return PyErr_SetFromWindowsErrWithFunction(rc,
1420n/a "RegQueryValue");
1421n/a
1422n/a bufSize = retSize;
1423n/a retBuf = (wchar_t *) PyMem_Malloc(bufSize);
1424n/a if (retBuf == NULL)
1425n/a return PyErr_NoMemory();
1426n/a
1427n/a while (1) {
1428n/a retSize = bufSize;
1429n/a rc = RegQueryValueW(key, sub_key, retBuf, &retSize);
1430n/a if (rc != ERROR_MORE_DATA)
1431n/a break;
1432n/a
1433n/a bufSize *= 2;
1434n/a tmp = (wchar_t *) PyMem_Realloc(retBuf, bufSize);
1435n/a if (tmp == NULL) {
1436n/a PyMem_Free(retBuf);
1437n/a return PyErr_NoMemory();
1438n/a }
1439n/a retBuf = tmp;
1440n/a }
1441n/a
1442n/a if (rc != ERROR_SUCCESS) {
1443n/a PyMem_Free(retBuf);
1444n/a return PyErr_SetFromWindowsErrWithFunction(rc,
1445n/a "RegQueryValue");
1446n/a }
1447n/a
1448n/a retStr = PyUnicode_FromWideChar(retBuf, wcslen(retBuf));
1449n/a PyMem_Free(retBuf);
1450n/a return retStr;
1451n/a}
1452n/a
1453n/a
1454n/a/*[clinic input]
1455n/awinreg.QueryValueEx
1456n/a
1457n/a key: HKEY
1458n/a An already open key, or any one of the predefined HKEY_* constants.
1459n/a name: Py_UNICODE(accept={str, NoneType})
1460n/a A string indicating the value to query.
1461n/a /
1462n/a
1463n/aRetrieves the type and value of a specified sub-key.
1464n/a
1465n/aBehaves mostly like QueryValue(), but also returns the type of the
1466n/aspecified value name associated with the given open registry key.
1467n/a
1468n/aThe return value is a tuple of the value and the type_id.
1469n/a[clinic start generated code]*/
1470n/a
1471n/astatic PyObject *
1472n/awinreg_QueryValueEx_impl(PyObject *module, HKEY key, Py_UNICODE *name)
1473n/a/*[clinic end generated code: output=5b4fa3e33d6d3e8f input=cf366cada4836891]*/
1474n/a{
1475n/a long rc;
1476n/a BYTE *retBuf, *tmp;
1477n/a DWORD bufSize = 0, retSize;
1478n/a DWORD typ;
1479n/a PyObject *obData;
1480n/a PyObject *result;
1481n/a
1482n/a rc = RegQueryValueExW(key, name, NULL, NULL, NULL, &bufSize);
1483n/a if (rc == ERROR_MORE_DATA)
1484n/a bufSize = 256;
1485n/a else if (rc != ERROR_SUCCESS)
1486n/a return PyErr_SetFromWindowsErrWithFunction(rc,
1487n/a "RegQueryValueEx");
1488n/a retBuf = (BYTE *)PyMem_Malloc(bufSize);
1489n/a if (retBuf == NULL)
1490n/a return PyErr_NoMemory();
1491n/a
1492n/a while (1) {
1493n/a retSize = bufSize;
1494n/a rc = RegQueryValueExW(key, name, NULL, &typ,
1495n/a (BYTE *)retBuf, &retSize);
1496n/a if (rc != ERROR_MORE_DATA)
1497n/a break;
1498n/a
1499n/a bufSize *= 2;
1500n/a tmp = (char *) PyMem_Realloc(retBuf, bufSize);
1501n/a if (tmp == NULL) {
1502n/a PyMem_Free(retBuf);
1503n/a return PyErr_NoMemory();
1504n/a }
1505n/a retBuf = tmp;
1506n/a }
1507n/a
1508n/a if (rc != ERROR_SUCCESS) {
1509n/a PyMem_Free(retBuf);
1510n/a return PyErr_SetFromWindowsErrWithFunction(rc,
1511n/a "RegQueryValueEx");
1512n/a }
1513n/a obData = Reg2Py(retBuf, bufSize, typ);
1514n/a PyMem_Free(retBuf);
1515n/a if (obData == NULL)
1516n/a return NULL;
1517n/a result = Py_BuildValue("Oi", obData, typ);
1518n/a Py_DECREF(obData);
1519n/a return result;
1520n/a}
1521n/a
1522n/a/*[clinic input]
1523n/awinreg.SaveKey
1524n/a
1525n/a key: HKEY
1526n/a An already open key, or any one of the predefined HKEY_* constants.
1527n/a file_name: Py_UNICODE
1528n/a The name of the file to save registry data to. This file cannot
1529n/a already exist. If this filename includes an extension, it cannot be
1530n/a used on file allocation table (FAT) file systems by the LoadKey(),
1531n/a ReplaceKey() or RestoreKey() methods.
1532n/a /
1533n/a
1534n/aSaves the specified key, and all its subkeys to the specified file.
1535n/a
1536n/aIf key represents a key on a remote computer, the path described by
1537n/afile_name is relative to the remote computer.
1538n/a
1539n/aThe caller of this method must possess the SeBackupPrivilege
1540n/asecurity privilege. This function passes NULL for security_attributes
1541n/ato the API.
1542n/a[clinic start generated code]*/
1543n/a
1544n/astatic PyObject *
1545n/awinreg_SaveKey_impl(PyObject *module, HKEY key, Py_UNICODE *file_name)
1546n/a/*[clinic end generated code: output=1dda1502bd4c30d8 input=da735241f91ac7a2]*/
1547n/a{
1548n/a LPSECURITY_ATTRIBUTES pSA = NULL;
1549n/a
1550n/a long rc;
1551n/a/* One day we may get security into the core?
1552n/a if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE))
1553n/a return NULL;
1554n/a*/
1555n/a Py_BEGIN_ALLOW_THREADS
1556n/a rc = RegSaveKeyW(key, file_name, pSA );
1557n/a Py_END_ALLOW_THREADS
1558n/a if (rc != ERROR_SUCCESS)
1559n/a return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey");
1560n/a Py_RETURN_NONE;
1561n/a}
1562n/a
1563n/a/*[clinic input]
1564n/awinreg.SetValue
1565n/a
1566n/a key: HKEY
1567n/a An already open key, or any one of the predefined HKEY_* constants.
1568n/a sub_key: Py_UNICODE(accept={str, NoneType})
1569n/a A string that names the subkey with which the value is associated.
1570n/a type: DWORD
1571n/a An integer that specifies the type of the data. Currently this must
1572n/a be REG_SZ, meaning only strings are supported.
1573n/a value: Py_UNICODE(zeroes=True)
1574n/a A string that specifies the new value.
1575n/a /
1576n/a
1577n/aAssociates a value with a specified key.
1578n/a
1579n/aIf the key specified by the sub_key parameter does not exist, the
1580n/aSetValue function creates it.
1581n/a
1582n/aValue lengths are limited by available memory. Long values (more than
1583n/a2048 bytes) should be stored as files with the filenames stored in
1584n/athe configuration registry to help the registry perform efficiently.
1585n/a
1586n/aThe key identified by the key parameter must have been opened with
1587n/aKEY_SET_VALUE access.
1588n/a[clinic start generated code]*/
1589n/a
1590n/astatic PyObject *
1591n/awinreg_SetValue_impl(PyObject *module, HKEY key, Py_UNICODE *sub_key,
1592n/a DWORD type, Py_UNICODE *value,
1593n/a Py_ssize_clean_t value_length)
1594n/a/*[clinic end generated code: output=1e31931174820631 input=2cd2adab79339c53]*/
1595n/a{
1596n/a long rc;
1597n/a
1598n/a if (type != REG_SZ) {
1599n/a PyErr_SetString(PyExc_TypeError,
1600n/a "Type must be winreg.REG_SZ");
1601n/a return NULL;
1602n/a }
1603n/a
1604n/a Py_BEGIN_ALLOW_THREADS
1605n/a rc = RegSetValueW(key, sub_key, REG_SZ, value, value_length+1);
1606n/a Py_END_ALLOW_THREADS
1607n/a if (rc != ERROR_SUCCESS)
1608n/a return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue");
1609n/a Py_RETURN_NONE;
1610n/a}
1611n/a
1612n/a/*[clinic input]
1613n/awinreg.SetValueEx
1614n/a
1615n/a key: HKEY
1616n/a An already open key, or any one of the predefined HKEY_* constants.
1617n/a value_name: Py_UNICODE(accept={str, NoneType})
1618n/a A string containing the name of the value to set, or None.
1619n/a reserved: object
1620n/a Can be anything - zero is always passed to the API.
1621n/a type: DWORD
1622n/a An integer that specifies the type of the data, one of:
1623n/a REG_BINARY -- Binary data in any form.
1624n/a REG_DWORD -- A 32-bit number.
1625n/a REG_DWORD_LITTLE_ENDIAN -- A 32-bit number in little-endian format. Equivalent to REG_DWORD
1626n/a REG_DWORD_BIG_ENDIAN -- A 32-bit number in big-endian format.
1627n/a REG_EXPAND_SZ -- A null-terminated string that contains unexpanded
1628n/a references to environment variables (for example,
1629n/a %PATH%).
1630n/a REG_LINK -- A Unicode symbolic link.
1631n/a REG_MULTI_SZ -- A sequence of null-terminated strings, terminated
1632n/a by two null characters. Note that Python handles
1633n/a this termination automatically.
1634n/a REG_NONE -- No defined value type.
1635n/a REG_QWORD -- A 64-bit number.
1636n/a REG_QWORD_LITTLE_ENDIAN -- A 64-bit number in little-endian format. Equivalent to REG_QWORD.
1637n/a REG_RESOURCE_LIST -- A device-driver resource list.
1638n/a REG_SZ -- A null-terminated string.
1639n/a value: object
1640n/a A string that specifies the new value.
1641n/a /
1642n/a
1643n/aStores data in the value field of an open registry key.
1644n/a
1645n/aThis method can also set additional value and type information for the
1646n/aspecified key. The key identified by the key parameter must have been
1647n/aopened with KEY_SET_VALUE access.
1648n/a
1649n/aTo open the key, use the CreateKeyEx() or OpenKeyEx() methods.
1650n/a
1651n/aValue lengths are limited by available memory. Long values (more than
1652n/a2048 bytes) should be stored as files with the filenames stored in
1653n/athe configuration registry to help the registry perform efficiently.
1654n/a[clinic start generated code]*/
1655n/a
1656n/astatic PyObject *
1657n/awinreg_SetValueEx_impl(PyObject *module, HKEY key, Py_UNICODE *value_name,
1658n/a PyObject *reserved, DWORD type, PyObject *value)
1659n/a/*[clinic end generated code: output=c88c8426b6c00ec7 input=900a9e3990bfb196]*/
1660n/a{
1661n/a BYTE *data;
1662n/a DWORD len;
1663n/a
1664n/a LONG rc;
1665n/a
1666n/a if (!Py2Reg(value, type, &data, &len))
1667n/a {
1668n/a if (!PyErr_Occurred())
1669n/a PyErr_SetString(PyExc_ValueError,
1670n/a "Could not convert the data to the specified type.");
1671n/a return NULL;
1672n/a }
1673n/a Py_BEGIN_ALLOW_THREADS
1674n/a rc = RegSetValueExW(key, value_name, 0, type, data, len);
1675n/a Py_END_ALLOW_THREADS
1676n/a PyMem_DEL(data);
1677n/a if (rc != ERROR_SUCCESS)
1678n/a return PyErr_SetFromWindowsErrWithFunction(rc,
1679n/a "RegSetValueEx");
1680n/a Py_RETURN_NONE;
1681n/a}
1682n/a
1683n/a/*[clinic input]
1684n/awinreg.DisableReflectionKey
1685n/a
1686n/a key: HKEY
1687n/a An already open key, or any one of the predefined HKEY_* constants.
1688n/a /
1689n/a
1690n/aDisables registry reflection for 32bit processes running on a 64bit OS.
1691n/a
1692n/aWill generally raise NotImplemented if executed on a 32bit OS.
1693n/a
1694n/aIf the key is not on the reflection list, the function succeeds but has
1695n/ano effect. Disabling reflection for a key does not affect reflection
1696n/aof any subkeys.
1697n/a[clinic start generated code]*/
1698n/a
1699n/astatic PyObject *
1700n/awinreg_DisableReflectionKey_impl(PyObject *module, HKEY key)
1701n/a/*[clinic end generated code: output=830cce504cc764b4 input=a6c9e5ca5410193c]*/
1702n/a{
1703n/a HMODULE hMod;
1704n/a typedef LONG (WINAPI *RDRKFunc)(HKEY);
1705n/a RDRKFunc pfn = NULL;
1706n/a LONG rc;
1707n/a
1708n/a /* Only available on 64bit platforms, so we must load it
1709n/a dynamically.*/
1710n/a hMod = GetModuleHandleW(L"advapi32.dll");
1711n/a if (hMod)
1712n/a pfn = (RDRKFunc)GetProcAddress(hMod,
1713n/a "RegDisableReflectionKey");
1714n/a if (!pfn) {
1715n/a PyErr_SetString(PyExc_NotImplementedError,
1716n/a "not implemented on this platform");
1717n/a return NULL;
1718n/a }
1719n/a Py_BEGIN_ALLOW_THREADS
1720n/a rc = (*pfn)(key);
1721n/a Py_END_ALLOW_THREADS
1722n/a if (rc != ERROR_SUCCESS)
1723n/a return PyErr_SetFromWindowsErrWithFunction(rc,
1724n/a "RegDisableReflectionKey");
1725n/a Py_RETURN_NONE;
1726n/a}
1727n/a
1728n/a/*[clinic input]
1729n/awinreg.EnableReflectionKey
1730n/a
1731n/a key: HKEY
1732n/a An already open key, or any one of the predefined HKEY_* constants.
1733n/a /
1734n/a
1735n/aRestores registry reflection for the specified disabled key.
1736n/a
1737n/aWill generally raise NotImplemented if executed on a 32bit OS.
1738n/aRestoring reflection for a key does not affect reflection of any
1739n/asubkeys.
1740n/a[clinic start generated code]*/
1741n/a
1742n/astatic PyObject *
1743n/awinreg_EnableReflectionKey_impl(PyObject *module, HKEY key)
1744n/a/*[clinic end generated code: output=86fa1385fdd9ce57 input=7748abbacd1e166a]*/
1745n/a{
1746n/a HMODULE hMod;
1747n/a typedef LONG (WINAPI *RERKFunc)(HKEY);
1748n/a RERKFunc pfn = NULL;
1749n/a LONG rc;
1750n/a
1751n/a /* Only available on 64bit platforms, so we must load it
1752n/a dynamically.*/
1753n/a hMod = GetModuleHandleW(L"advapi32.dll");
1754n/a if (hMod)
1755n/a pfn = (RERKFunc)GetProcAddress(hMod,
1756n/a "RegEnableReflectionKey");
1757n/a if (!pfn) {
1758n/a PyErr_SetString(PyExc_NotImplementedError,
1759n/a "not implemented on this platform");
1760n/a return NULL;
1761n/a }
1762n/a Py_BEGIN_ALLOW_THREADS
1763n/a rc = (*pfn)(key);
1764n/a Py_END_ALLOW_THREADS
1765n/a if (rc != ERROR_SUCCESS)
1766n/a return PyErr_SetFromWindowsErrWithFunction(rc,
1767n/a "RegEnableReflectionKey");
1768n/a Py_RETURN_NONE;
1769n/a}
1770n/a
1771n/a/*[clinic input]
1772n/awinreg.QueryReflectionKey
1773n/a
1774n/a key: HKEY
1775n/a An already open key, or any one of the predefined HKEY_* constants.
1776n/a /
1777n/a
1778n/aReturns the reflection state for the specified key as a bool.
1779n/a
1780n/aWill generally raise NotImplemented if executed on a 32bit OS.
1781n/a[clinic start generated code]*/
1782n/a
1783n/astatic PyObject *
1784n/awinreg_QueryReflectionKey_impl(PyObject *module, HKEY key)
1785n/a/*[clinic end generated code: output=4e774af288c3ebb9 input=9f325eacb5a65d88]*/
1786n/a{
1787n/a HMODULE hMod;
1788n/a typedef LONG (WINAPI *RQRKFunc)(HKEY, BOOL *);
1789n/a RQRKFunc pfn = NULL;
1790n/a BOOL result;
1791n/a LONG rc;
1792n/a
1793n/a /* Only available on 64bit platforms, so we must load it
1794n/a dynamically.*/
1795n/a hMod = GetModuleHandleW(L"advapi32.dll");
1796n/a if (hMod)
1797n/a pfn = (RQRKFunc)GetProcAddress(hMod,
1798n/a "RegQueryReflectionKey");
1799n/a if (!pfn) {
1800n/a PyErr_SetString(PyExc_NotImplementedError,
1801n/a "not implemented on this platform");
1802n/a return NULL;
1803n/a }
1804n/a Py_BEGIN_ALLOW_THREADS
1805n/a rc = (*pfn)(key, &result);
1806n/a Py_END_ALLOW_THREADS
1807n/a if (rc != ERROR_SUCCESS)
1808n/a return PyErr_SetFromWindowsErrWithFunction(rc,
1809n/a "RegQueryReflectionKey");
1810n/a return PyBool_FromLong(result);
1811n/a}
1812n/a
1813n/astatic struct PyMethodDef winreg_methods[] = {
1814n/a WINREG_CLOSEKEY_METHODDEF
1815n/a WINREG_CONNECTREGISTRY_METHODDEF
1816n/a WINREG_CREATEKEY_METHODDEF
1817n/a WINREG_CREATEKEYEX_METHODDEF
1818n/a WINREG_DELETEKEY_METHODDEF
1819n/a WINREG_DELETEKEYEX_METHODDEF
1820n/a WINREG_DELETEVALUE_METHODDEF
1821n/a WINREG_DISABLEREFLECTIONKEY_METHODDEF
1822n/a WINREG_ENABLEREFLECTIONKEY_METHODDEF
1823n/a WINREG_ENUMKEY_METHODDEF
1824n/a WINREG_ENUMVALUE_METHODDEF
1825n/a WINREG_EXPANDENVIRONMENTSTRINGS_METHODDEF
1826n/a WINREG_FLUSHKEY_METHODDEF
1827n/a WINREG_LOADKEY_METHODDEF
1828n/a WINREG_OPENKEY_METHODDEF
1829n/a WINREG_OPENKEYEX_METHODDEF
1830n/a WINREG_QUERYVALUE_METHODDEF
1831n/a WINREG_QUERYVALUEEX_METHODDEF
1832n/a WINREG_QUERYINFOKEY_METHODDEF
1833n/a WINREG_QUERYREFLECTIONKEY_METHODDEF
1834n/a WINREG_SAVEKEY_METHODDEF
1835n/a WINREG_SETVALUE_METHODDEF
1836n/a WINREG_SETVALUEEX_METHODDEF
1837n/a NULL,
1838n/a};
1839n/a
1840n/astatic void
1841n/ainsint(PyObject * d, char * name, long value)
1842n/a{
1843n/a PyObject *v = PyLong_FromLong(value);
1844n/a if (!v || PyDict_SetItemString(d, name, v))
1845n/a PyErr_Clear();
1846n/a Py_XDECREF(v);
1847n/a}
1848n/a
1849n/a#define ADD_INT(val) insint(d, #val, val)
1850n/a
1851n/astatic void
1852n/ainskey(PyObject * d, char * name, HKEY key)
1853n/a{
1854n/a PyObject *v = PyLong_FromVoidPtr(key);
1855n/a if (!v || PyDict_SetItemString(d, name, v))
1856n/a PyErr_Clear();
1857n/a Py_XDECREF(v);
1858n/a}
1859n/a
1860n/a#define ADD_KEY(val) inskey(d, #val, val)
1861n/a
1862n/a
1863n/astatic struct PyModuleDef winregmodule = {
1864n/a PyModuleDef_HEAD_INIT,
1865n/a "winreg",
1866n/a module_doc,
1867n/a -1,
1868n/a winreg_methods,
1869n/a NULL,
1870n/a NULL,
1871n/a NULL,
1872n/a NULL
1873n/a};
1874n/a
1875n/aPyMODINIT_FUNC PyInit_winreg(void)
1876n/a{
1877n/a PyObject *m, *d;
1878n/a m = PyModule_Create(&winregmodule);
1879n/a if (m == NULL)
1880n/a return NULL;
1881n/a d = PyModule_GetDict(m);
1882n/a PyHKEY_Type.tp_doc = PyHKEY_doc;
1883n/a if (PyType_Ready(&PyHKEY_Type) < 0)
1884n/a return NULL;
1885n/a Py_INCREF(&PyHKEY_Type);
1886n/a if (PyDict_SetItemString(d, "HKEYType",
1887n/a (PyObject *)&PyHKEY_Type) != 0)
1888n/a return NULL;
1889n/a Py_INCREF(PyExc_OSError);
1890n/a if (PyDict_SetItemString(d, "error",
1891n/a PyExc_OSError) != 0)
1892n/a return NULL;
1893n/a
1894n/a /* Add the relevant constants */
1895n/a ADD_KEY(HKEY_CLASSES_ROOT);
1896n/a ADD_KEY(HKEY_CURRENT_USER);
1897n/a ADD_KEY(HKEY_LOCAL_MACHINE);
1898n/a ADD_KEY(HKEY_USERS);
1899n/a ADD_KEY(HKEY_PERFORMANCE_DATA);
1900n/a#ifdef HKEY_CURRENT_CONFIG
1901n/a ADD_KEY(HKEY_CURRENT_CONFIG);
1902n/a#endif
1903n/a#ifdef HKEY_DYN_DATA
1904n/a ADD_KEY(HKEY_DYN_DATA);
1905n/a#endif
1906n/a ADD_INT(KEY_QUERY_VALUE);
1907n/a ADD_INT(KEY_SET_VALUE);
1908n/a ADD_INT(KEY_CREATE_SUB_KEY);
1909n/a ADD_INT(KEY_ENUMERATE_SUB_KEYS);
1910n/a ADD_INT(KEY_NOTIFY);
1911n/a ADD_INT(KEY_CREATE_LINK);
1912n/a ADD_INT(KEY_READ);
1913n/a ADD_INT(KEY_WRITE);
1914n/a ADD_INT(KEY_EXECUTE);
1915n/a ADD_INT(KEY_ALL_ACCESS);
1916n/a#ifdef KEY_WOW64_64KEY
1917n/a ADD_INT(KEY_WOW64_64KEY);
1918n/a#endif
1919n/a#ifdef KEY_WOW64_32KEY
1920n/a ADD_INT(KEY_WOW64_32KEY);
1921n/a#endif
1922n/a ADD_INT(REG_OPTION_RESERVED);
1923n/a ADD_INT(REG_OPTION_NON_VOLATILE);
1924n/a ADD_INT(REG_OPTION_VOLATILE);
1925n/a ADD_INT(REG_OPTION_CREATE_LINK);
1926n/a ADD_INT(REG_OPTION_BACKUP_RESTORE);
1927n/a ADD_INT(REG_OPTION_OPEN_LINK);
1928n/a ADD_INT(REG_LEGAL_OPTION);
1929n/a ADD_INT(REG_CREATED_NEW_KEY);
1930n/a ADD_INT(REG_OPENED_EXISTING_KEY);
1931n/a ADD_INT(REG_WHOLE_HIVE_VOLATILE);
1932n/a ADD_INT(REG_REFRESH_HIVE);
1933n/a ADD_INT(REG_NO_LAZY_FLUSH);
1934n/a ADD_INT(REG_NOTIFY_CHANGE_NAME);
1935n/a ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES);
1936n/a ADD_INT(REG_NOTIFY_CHANGE_LAST_SET);
1937n/a ADD_INT(REG_NOTIFY_CHANGE_SECURITY);
1938n/a ADD_INT(REG_LEGAL_CHANGE_FILTER);
1939n/a ADD_INT(REG_NONE);
1940n/a ADD_INT(REG_SZ);
1941n/a ADD_INT(REG_EXPAND_SZ);
1942n/a ADD_INT(REG_BINARY);
1943n/a ADD_INT(REG_DWORD);
1944n/a ADD_INT(REG_DWORD_LITTLE_ENDIAN);
1945n/a ADD_INT(REG_DWORD_BIG_ENDIAN);
1946n/a ADD_INT(REG_QWORD);
1947n/a ADD_INT(REG_QWORD_LITTLE_ENDIAN);
1948n/a ADD_INT(REG_LINK);
1949n/a ADD_INT(REG_MULTI_SZ);
1950n/a ADD_INT(REG_RESOURCE_LIST);
1951n/a ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
1952n/a ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
1953n/a return m;
1954n/a}
1955n/a
1956n/a