blob: 1305b7030fadafba4cbdadf77e86487f6a918d89 [file] [log] [blame]
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001/*
Georg Brandl38feaf02008-05-25 07:45:51 +00002 winreg.c
Guido van Rossum9f3712c2000-03-28 20:37:15 +00003
4 Windows Registry access module for Python.
5
6 * Simple registry access written by Mark Hammond in win32api
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007 module circa 1995.
Guido van Rossum9f3712c2000-03-28 20:37:15 +00008 * Bill Tutt expanded the support significantly not long after.
9 * Numerous other people have submitted patches since then.
10 * Ripped from win32api module 03-Feb-2000 by Mark Hammond, and
11 basic Unicode support added.
12
13*/
14
Inada Naokid5f18a62019-03-20 19:10:17 +090015#define PY_SSIZE_T_CLEAN
Guido van Rossum9f3712c2000-03-28 20:37:15 +000016#include "Python.h"
Victor Stinner4a21e572020-04-15 02:35:41 +020017#include "structmember.h" // PyMemberDef
18#include <windows.h>
Guido van Rossum9f3712c2000-03-28 20:37:15 +000019
20static BOOL PyHKEY_AsHKEY(PyObject *ob, HKEY *pRes, BOOL bNoneOK);
Zachary Warefd2d4822015-05-13 01:21:57 -050021static BOOL clinic_HKEY_converter(PyObject *ob, void *p);
Guido van Rossum9f3712c2000-03-28 20:37:15 +000022static PyObject *PyHKEY_FromHKEY(HKEY h);
23static BOOL PyHKEY_Close(PyObject *obHandle);
24
25static char errNotAHandle[] = "Object is not a handle";
26
27/* The win32api module reports the function name that failed,
28 but this concept is not in the Python core.
luzpaza5293b42017-11-05 07:37:50 -060029 Hopefully it will one day, and in the meantime I don't
Guido van Rossum9f3712c2000-03-28 20:37:15 +000030 want to lose this info...
31*/
32#define PyErr_SetFromWindowsErrWithFunction(rc, fnname) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000033 PyErr_SetFromWindowsErr(rc)
Guido van Rossum9f3712c2000-03-28 20:37:15 +000034
35/* Forward declares */
36
37/* Doc strings */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000038PyDoc_STRVAR(module_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +000039"This module provides access to the Windows registry API.\n"
40"\n"
41"Functions:\n"
42"\n"
43"CloseKey() - Closes a registry key.\n"
44"ConnectRegistry() - Establishes a connection to a predefined registry handle\n"
45" on another computer.\n"
46"CreateKey() - Creates the specified key, or opens it if it already exists.\n"
47"DeleteKey() - Deletes the specified key.\n"
48"DeleteValue() - Removes a named value from the specified registry key.\n"
49"EnumKey() - Enumerates subkeys of the specified open registry key.\n"
50"EnumValue() - Enumerates values of the specified open registry key.\n"
Zachary Warefd2d4822015-05-13 01:21:57 -050051"ExpandEnvironmentStrings() - Expand the env strings in a REG_EXPAND_SZ\n"
52" string.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000053"FlushKey() - Writes all the attributes of the specified key to the registry.\n"
Zachary Warefd2d4822015-05-13 01:21:57 -050054"LoadKey() - Creates a subkey under HKEY_USER or HKEY_LOCAL_MACHINE and\n"
55" stores registration information from a specified file into that\n"
56" subkey.\n"
Brian Curtine9aeca72012-10-29 18:16:39 -050057"OpenKey() - Opens the specified key.\n"
58"OpenKeyEx() - Alias of OpenKey().\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000059"QueryValue() - Retrieves the value associated with the unnamed value for a\n"
60" specified key in the registry.\n"
61"QueryValueEx() - Retrieves the type and data for a specified value name\n"
62" associated with an open registry key.\n"
63"QueryInfoKey() - Returns information about the specified key.\n"
64"SaveKey() - Saves the specified key, and all its subkeys a file.\n"
65"SetValue() - Associates a value with a specified key.\n"
66"SetValueEx() - Stores data in the value field of an open registry key.\n"
67"\n"
68"Special objects:\n"
69"\n"
70"HKEYType -- type object for HKEY objects\n"
71"error -- exception raised for Win32 errors\n"
72"\n"
73"Integer constants:\n"
74"Many constants are defined - see the documentation for each function\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000075"to see what constants are used, and where.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +000076
77
Martin v. Löwisd218dc12008-04-07 03:17:54 +000078
Guido van Rossum9f3712c2000-03-28 20:37:15 +000079/* PyHKEY docstrings */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000080PyDoc_STRVAR(PyHKEY_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +000081"PyHKEY Object - A Python object, representing a win32 registry key.\n"
82"\n"
Mark Hammondb422f952000-06-09 06:01:47 +000083"This object wraps a Windows HKEY object, automatically closing it when\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000084"the object is destroyed. To guarantee cleanup, you can call either\n"
85"the Close() method on the PyHKEY, or the CloseKey() method.\n"
86"\n"
oldkaa0735f2018-02-02 16:52:55 +080087"All functions which accept a handle object also accept an integer --\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000088"however, use of the handle object is encouraged.\n"
89"\n"
90"Functions:\n"
91"Close() - Closes the underlying handle.\n"
92"Detach() - Returns the integer Win32 handle, detaching it from the object\n"
93"\n"
94"Properties:\n"
95"handle - The integer Win32 handle.\n"
96"\n"
97"Operations:\n"
Jack Diederich4dafcc42006-11-28 19:15:13 +000098"__bool__ - Handles with an open object return true, otherwise false.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000099"__int__ - Converting a handle to an integer returns the Win32 handle.\n"
Mark Dickinson211c6252009-02-01 10:28:51 +0000100"rich comparison - Handle objects are compared using the handle value.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000101
102
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000103
104/************************************************************************
105
106 The PyHKEY object definition
107
108************************************************************************/
109typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 PyObject_VAR_HEAD
111 HKEY hkey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000112} PyHKEYObject;
113
114#define PyHKEY_Check(op) ((op)->ob_type == &PyHKEY_Type)
115
116static char *failMsg = "bad operand type";
117
118static PyObject *
119PyHKEY_unaryFailureFunc(PyObject *ob)
120{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 PyErr_SetString(PyExc_TypeError, failMsg);
122 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000123}
124static PyObject *
125PyHKEY_binaryFailureFunc(PyObject *ob1, PyObject *ob2)
126{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 PyErr_SetString(PyExc_TypeError, failMsg);
128 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000129}
130static PyObject *
131PyHKEY_ternaryFailureFunc(PyObject *ob1, PyObject *ob2, PyObject *ob3)
132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 PyErr_SetString(PyExc_TypeError, failMsg);
134 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000135}
136
137static void
138PyHKEY_deallocFunc(PyObject *ob)
139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 /* Can not call PyHKEY_Close, as the ob->tp_type
141 has already been cleared, thus causing the type
142 check to fail!
143 */
144 PyHKEYObject *obkey = (PyHKEYObject *)ob;
145 if (obkey->hkey)
146 RegCloseKey((HKEY)obkey->hkey);
147 PyObject_DEL(ob);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000148}
149
150static int
Jack Diederich4dafcc42006-11-28 19:15:13 +0000151PyHKEY_boolFunc(PyObject *ob)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000152{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 return ((PyHKEYObject *)ob)->hkey != 0;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000154}
155
156static PyObject *
157PyHKEY_intFunc(PyObject *ob)
158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
160 return PyLong_FromVoidPtr(pyhkey->hkey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000161}
162
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000163static PyObject *
164PyHKEY_strFunc(PyObject *ob)
165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
167 return PyUnicode_FromFormat("<PyHKEY:%p>", pyhkey->hkey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000168}
169
170static int
171PyHKEY_compareFunc(PyObject *ob1, PyObject *ob2)
172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 PyHKEYObject *pyhkey1 = (PyHKEYObject *)ob1;
174 PyHKEYObject *pyhkey2 = (PyHKEYObject *)ob2;
175 return pyhkey1 == pyhkey2 ? 0 :
176 (pyhkey1 < pyhkey2 ? -1 : 1);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000177}
178
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000179static Py_hash_t
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000180PyHKEY_hashFunc(PyObject *ob)
181{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000182 /* Just use the address.
183 XXX - should we use the handle value?
184 */
185 return _Py_HashPointer(ob);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000186}
187
188
189static PyNumberMethods PyHKEY_NumberMethods =
190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 PyHKEY_binaryFailureFunc, /* nb_add */
192 PyHKEY_binaryFailureFunc, /* nb_subtract */
193 PyHKEY_binaryFailureFunc, /* nb_multiply */
194 PyHKEY_binaryFailureFunc, /* nb_remainder */
195 PyHKEY_binaryFailureFunc, /* nb_divmod */
196 PyHKEY_ternaryFailureFunc, /* nb_power */
197 PyHKEY_unaryFailureFunc, /* nb_negative */
198 PyHKEY_unaryFailureFunc, /* nb_positive */
199 PyHKEY_unaryFailureFunc, /* nb_absolute */
200 PyHKEY_boolFunc, /* nb_bool */
201 PyHKEY_unaryFailureFunc, /* nb_invert */
202 PyHKEY_binaryFailureFunc, /* nb_lshift */
203 PyHKEY_binaryFailureFunc, /* nb_rshift */
204 PyHKEY_binaryFailureFunc, /* nb_and */
205 PyHKEY_binaryFailureFunc, /* nb_xor */
206 PyHKEY_binaryFailureFunc, /* nb_or */
207 PyHKEY_intFunc, /* nb_int */
208 0, /* nb_reserved */
209 PyHKEY_unaryFailureFunc, /* nb_float */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000210};
211
Zachary Warefd2d4822015-05-13 01:21:57 -0500212/*[clinic input]
213module winreg
214class winreg.HKEYType "PyHKEYObject *" "&PyHKEY_Type"
215[clinic start generated code]*/
216/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4c964eba3bf914d6]*/
217
218/*[python input]
219class REGSAM_converter(CConverter):
220 type = 'REGSAM'
221 format_unit = 'i'
222
223class DWORD_converter(CConverter):
224 type = 'DWORD'
225 format_unit = 'k'
226
227class HKEY_converter(CConverter):
228 type = 'HKEY'
229 converter = 'clinic_HKEY_converter'
230
231class HKEY_return_converter(CReturnConverter):
232 type = 'HKEY'
233
234 def render(self, function, data):
235 self.declare(data)
236 self.err_occurred_if_null_pointer("_return_value", data)
237 data.return_conversion.append(
238 'return_value = PyHKEY_FromHKEY(_return_value);\n')
239
240# HACK: this only works for PyHKEYObjects, nothing else.
241# Should this be generalized and enshrined in clinic.py,
242# destroy this converter with prejudice.
243class self_return_converter(CReturnConverter):
244 type = 'PyHKEYObject *'
245
246 def render(self, function, data):
247 self.declare(data)
248 data.return_conversion.append(
249 'return_value = (PyObject *)_return_value;\n')
250[python start generated code]*/
251/*[python end generated code: output=da39a3ee5e6b4b0d input=22f7aedc6d68e80e]*/
252
253#include "clinic/winreg.c.h"
254
255/************************************************************************
256
257 The PyHKEY object methods
258
259************************************************************************/
260/*[clinic input]
261winreg.HKEYType.Close
262
263Closes the underlying Windows handle.
264
265If the handle is already closed, no error is raised.
266[clinic start generated code]*/
267
268static PyObject *
269winreg_HKEYType_Close_impl(PyHKEYObject *self)
270/*[clinic end generated code: output=fced3a624fb0c344 input=6786ac75f6b89de6]*/
271{
272 if (!PyHKEY_Close((PyObject *)self))
273 return NULL;
274 Py_RETURN_NONE;
275}
276
277/*[clinic input]
278winreg.HKEYType.Detach
279
280Detaches the Windows handle from the handle object.
281
282The result is the value of the handle before it is detached. If the
283handle is already detached, this will return zero.
284
285After calling this function, the handle is effectively invalidated,
286but the handle is not closed. You would call this function when you
287need the underlying win32 handle to exist beyond the lifetime of the
288handle object.
289[clinic start generated code]*/
290
291static PyObject *
292winreg_HKEYType_Detach_impl(PyHKEYObject *self)
293/*[clinic end generated code: output=dda5a9e1a01ae78f input=dd2cc09e6c6ba833]*/
294{
295 void* ret;
Steve Doweree17e372019-12-09 11:18:12 -0800296 if (PySys_Audit("winreg.PyHKEY.Detach", "n", (Py_ssize_t)self->hkey) < 0) {
297 return NULL;
298 }
Zachary Warefd2d4822015-05-13 01:21:57 -0500299 ret = (void*)self->hkey;
300 self->hkey = 0;
301 return PyLong_FromVoidPtr(ret);
302}
303
304/*[clinic input]
305winreg.HKEYType.__enter__ -> self
306[clinic start generated code]*/
307
308static PyHKEYObject *
309winreg_HKEYType___enter___impl(PyHKEYObject *self)
310/*[clinic end generated code: output=52c34986dab28990 input=c40fab1f0690a8e2]*/
311{
312 Py_XINCREF(self);
313 return self;
314}
315
316
317/*[clinic input]
318winreg.HKEYType.__exit__
319
320 exc_type: object
321 exc_value: object
322 traceback: object
323[clinic start generated code]*/
324
325static PyObject *
Zachary Ware77772c02015-05-13 10:58:35 -0500326winreg_HKEYType___exit___impl(PyHKEYObject *self, PyObject *exc_type,
327 PyObject *exc_value, PyObject *traceback)
328/*[clinic end generated code: output=923ebe7389e6a263 input=fb32489ee92403c7]*/
Zachary Warefd2d4822015-05-13 01:21:57 -0500329{
330 if (!PyHKEY_Close((PyObject *)self))
331 return NULL;
332 Py_RETURN_NONE;
333}
334
335/*[clinic input]
336[clinic start generated code]*/
337/*[clinic end generated code: output=da39a3ee5e6b4b0d input=da39a3ee5e6b4b0d]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000338
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000339static struct PyMethodDef PyHKEY_methods[] = {
Zachary Warefd2d4822015-05-13 01:21:57 -0500340 WINREG_HKEYTYPE_CLOSE_METHODDEF
341 WINREG_HKEYTYPE_DETACH_METHODDEF
342 WINREG_HKEYTYPE___ENTER___METHODDEF
343 WINREG_HKEYTYPE___EXIT___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 {NULL}
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000345};
346
347#define OFF(e) offsetof(PyHKEYObject, e)
348static PyMemberDef PyHKEY_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 {"handle", T_INT, OFF(hkey), READONLY},
350 {NULL} /* Sentinel */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000351};
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000352
353/* The type itself */
354PyTypeObject PyHKEY_Type =
355{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 PyVarObject_HEAD_INIT(0, 0) /* fill in type at module init */
357 "PyHKEY",
358 sizeof(PyHKEYObject),
359 0,
360 PyHKEY_deallocFunc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200361 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 0, /* tp_getattr */
363 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200364 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 0, /* tp_repr */
366 &PyHKEY_NumberMethods, /* tp_as_number */
367 0, /* tp_as_sequence */
368 0, /* tp_as_mapping */
369 PyHKEY_hashFunc, /* tp_hash */
370 0, /* tp_call */
371 PyHKEY_strFunc, /* tp_str */
372 0, /* tp_getattro */
373 0, /* tp_setattro */
374 0, /* tp_as_buffer */
375 0, /* tp_flags */
376 PyHKEY_doc, /* tp_doc */
377 0, /*tp_traverse*/
378 0, /*tp_clear*/
379 0, /*tp_richcompare*/
380 0, /*tp_weaklistoffset*/
381 0, /*tp_iter*/
382 0, /*tp_iternext*/
383 PyHKEY_methods, /*tp_methods*/
384 PyHKEY_memberlist, /*tp_members*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000385};
386
387/************************************************************************
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000388 The public PyHKEY API (well, not public yet :-)
389************************************************************************/
390PyObject *
391PyHKEY_New(HKEY hInit)
392{
Victor Stinner92055202020-04-08 00:38:15 +0200393 PyHKEYObject *key = PyObject_New(PyHKEYObject, &PyHKEY_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 if (key)
395 key->hkey = hInit;
396 return (PyObject *)key;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000397}
398
399BOOL
400PyHKEY_Close(PyObject *ob_handle)
401{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 LONG rc;
Steve Doweree17e372019-12-09 11:18:12 -0800403 HKEY key;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000404
Steve Doweree17e372019-12-09 11:18:12 -0800405 if (!PyHKEY_AsHKEY(ob_handle, &key, TRUE)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 return FALSE;
407 }
Steve Doweree17e372019-12-09 11:18:12 -0800408 if (PyHKEY_Check(ob_handle)) {
409 ((PyHKEYObject*)ob_handle)->hkey = 0;
410 }
411 rc = key ? RegCloseKey(key) : ERROR_SUCCESS;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 if (rc != ERROR_SUCCESS)
413 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
414 return rc == ERROR_SUCCESS;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000415}
416
417BOOL
418PyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK)
419{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 if (ob == Py_None) {
421 if (!bNoneOK) {
422 PyErr_SetString(
423 PyExc_TypeError,
424 "None is not a valid HKEY in this context");
425 return FALSE;
426 }
427 *pHANDLE = (HKEY)0;
428 }
429 else if (PyHKEY_Check(ob)) {
430 PyHKEYObject *pH = (PyHKEYObject *)ob;
431 *pHANDLE = pH->hkey;
432 }
433 else if (PyLong_Check(ob)) {
434 /* We also support integers */
435 PyErr_Clear();
436 *pHANDLE = (HKEY)PyLong_AsVoidPtr(ob);
437 if (PyErr_Occurred())
438 return FALSE;
439 }
440 else {
441 PyErr_SetString(
442 PyExc_TypeError,
443 "The object is not a PyHKEY object");
444 return FALSE;
445 }
446 return TRUE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000447}
448
Zachary Warefd2d4822015-05-13 01:21:57 -0500449BOOL
450clinic_HKEY_converter(PyObject *ob, void *p)
451{
452 if (!PyHKEY_AsHKEY(ob, (HKEY *)p, FALSE))
453 return FALSE;
454 return TRUE;
455}
456
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000457PyObject *
458PyHKEY_FromHKEY(HKEY h)
459{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 PyHKEYObject *op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 /* Inline PyObject_New */
463 op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject));
464 if (op == NULL)
465 return PyErr_NoMemory();
Victor Stinnerb509d522018-11-23 14:27:38 +0100466 PyObject_INIT(op, &PyHKEY_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 op->hkey = h;
468 return (PyObject *)op;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000469}
470
471
472/************************************************************************
473 The module methods
474************************************************************************/
475BOOL
476PyWinObject_CloseHKEY(PyObject *obHandle)
477{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 BOOL ok;
479 if (PyHKEY_Check(obHandle)) {
480 ok = PyHKEY_Close(obHandle);
481 }
Fred Drake25e17262000-06-30 17:48:51 +0000482#if SIZEOF_LONG >= SIZEOF_HKEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 else if (PyLong_Check(obHandle)) {
484 long rc = RegCloseKey((HKEY)PyLong_AsLong(obHandle));
485 ok = (rc == ERROR_SUCCESS);
486 if (!ok)
487 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
488 }
Fred Drake25e17262000-06-30 17:48:51 +0000489#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 else if (PyLong_Check(obHandle)) {
491 long rc = RegCloseKey((HKEY)PyLong_AsVoidPtr(obHandle));
492 ok = (rc == ERROR_SUCCESS);
493 if (!ok)
494 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
495 }
Fred Drake25e17262000-06-30 17:48:51 +0000496#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 else {
498 PyErr_SetString(
499 PyExc_TypeError,
500 "A handle must be a HKEY object or an integer");
501 return FALSE;
502 }
503 return ok;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000504}
505
506
507/*
508 Private Helper functions for the registry interfaces
509
510** Note that fixupMultiSZ and countString have both had changes
511** made to support "incorrect strings". The registry specification
512** calls for strings to be terminated with 2 null bytes. It seems
luzpaza5293b42017-11-05 07:37:50 -0600513** some commercial packages install strings which don't conform,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000514** causing this code to fail - however, "regedit" etc still work
luzpaza5293b42017-11-05 07:37:50 -0600515** with these strings (ie only we don't!).
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000516*/
517static void
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000518fixupMultiSZ(wchar_t **str, wchar_t *data, int len)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000519{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 wchar_t *P;
521 int i;
522 wchar_t *Q;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000523
Zackery Spytze223ba12019-09-09 03:26:15 -0600524 if (len > 0 && data[len - 1] == '\0') {
525 Q = data + len - 1;
526 }
527 else {
528 Q = data + len;
529 }
530
531 for (P = data, i = 0; P < Q; P++, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 str[i] = P;
Zackery Spytze223ba12019-09-09 03:26:15 -0600533 for (; P < Q && *P != '\0'; P++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 ;
Zackery Spytze223ba12019-09-09 03:26:15 -0600535 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000537}
538
539static int
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000540countStrings(wchar_t *data, int len)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 int strings;
Zackery Spytze223ba12019-09-09 03:26:15 -0600543 wchar_t *P, *Q;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000544
Zackery Spytze223ba12019-09-09 03:26:15 -0600545 if (len > 0 && data[len - 1] == '\0') {
546 Q = data + len - 1;
547 }
548 else {
549 Q = data + len;
550 }
551
552 for (P = data, strings = 0; P < Q; P++, strings++) {
553 for (; P < Q && *P != '\0'; P++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 ;
Zackery Spytze223ba12019-09-09 03:26:15 -0600555 }
556 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 return strings;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000558}
559
560/* Convert PyObject into Registry data.
561 Allocates space as needed. */
562static BOOL
563Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
564{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 Py_ssize_t i,j;
566 switch (typ) {
567 case REG_DWORD:
568 if (value != Py_None && !PyLong_Check(value))
569 return FALSE;
570 *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1);
Steve Dower4d4bc422016-05-25 11:26:07 -0700571 if (*retDataBuf == NULL){
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 PyErr_NoMemory();
573 return FALSE;
574 }
575 *retDataSize = sizeof(DWORD);
576 if (value == Py_None) {
577 DWORD zero = 0;
578 memcpy(*retDataBuf, &zero, sizeof(DWORD));
579 }
580 else {
Brian Curtin12706f22012-12-27 10:12:45 -0600581 DWORD d = PyLong_AsUnsignedLong(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 memcpy(*retDataBuf, &d, sizeof(DWORD));
583 }
584 break;
Steve Dower80ac11d2016-05-24 15:42:04 -0700585 case REG_QWORD:
586 if (value != Py_None && !PyLong_Check(value))
587 return FALSE;
588 *retDataBuf = (BYTE *)PyMem_NEW(DWORD64, 1);
Steve Dower4d4bc422016-05-25 11:26:07 -0700589 if (*retDataBuf == NULL){
Steve Dower80ac11d2016-05-24 15:42:04 -0700590 PyErr_NoMemory();
591 return FALSE;
592 }
593 *retDataSize = sizeof(DWORD64);
594 if (value == Py_None) {
595 DWORD64 zero = 0;
596 memcpy(*retDataBuf, &zero, sizeof(DWORD64));
597 }
598 else {
599 DWORD64 d = PyLong_AsUnsignedLongLong(value);
600 memcpy(*retDataBuf, &d, sizeof(DWORD64));
601 }
602 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 case REG_SZ:
604 case REG_EXPAND_SZ:
605 {
Victor Stinnerbe492442011-11-21 12:43:50 +0100606 if (value != Py_None) {
607 Py_ssize_t len;
608 if (!PyUnicode_Check(value))
609 return FALSE;
610 *retDataBuf = (BYTE*)PyUnicode_AsWideCharString(value, &len);
611 if (*retDataBuf == NULL)
612 return FALSE;
613 *retDataSize = Py_SAFE_DOWNCAST(
614 (len + 1) * sizeof(wchar_t),
615 Py_ssize_t, DWORD);
616 }
617 else {
618 *retDataBuf = (BYTE *)PyMem_NEW(wchar_t, 1);
619 if (*retDataBuf == NULL) {
620 PyErr_NoMemory();
621 return FALSE;
622 }
623 ((wchar_t *)*retDataBuf)[0] = L'\0';
624 *retDataSize = 1 * sizeof(wchar_t);
625 }
626 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 }
628 case REG_MULTI_SZ:
629 {
630 DWORD size = 0;
631 wchar_t *P;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 if (value == Py_None)
634 i = 0;
635 else {
636 if (!PyList_Check(value))
637 return FALSE;
638 i = PyList_Size(value);
639 }
640 for (j = 0; j < i; j++)
641 {
642 PyObject *t;
Victor Stinnerbe492442011-11-21 12:43:50 +0100643 wchar_t *wstr;
644 Py_ssize_t len;
645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 t = PyList_GET_ITEM(value, j);
647 if (!PyUnicode_Check(t))
648 return FALSE;
Victor Stinnerbe492442011-11-21 12:43:50 +0100649 wstr = PyUnicode_AsUnicodeAndSize(t, &len);
650 if (wstr == NULL)
651 return FALSE;
652 size += Py_SAFE_DOWNCAST((len + 1) * sizeof(wchar_t),
Brian Curtinabb33512010-08-17 20:08:40 +0000653 size_t, DWORD);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 *retDataSize = size + 2;
657 *retDataBuf = (BYTE *)PyMem_NEW(char,
658 *retDataSize);
Steve Dower4d4bc422016-05-25 11:26:07 -0700659 if (*retDataBuf == NULL){
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 PyErr_NoMemory();
661 return FALSE;
662 }
663 P = (wchar_t *)*retDataBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 for (j = 0; j < i; j++)
666 {
667 PyObject *t;
Victor Stinnerbe492442011-11-21 12:43:50 +0100668 wchar_t *wstr;
669 Py_ssize_t len;
670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 t = PyList_GET_ITEM(value, j);
Victor Stinnerbe492442011-11-21 12:43:50 +0100672 wstr = PyUnicode_AsUnicodeAndSize(t, &len);
Zackery Spytz5d953122018-11-08 02:45:00 -0700673 assert(wstr);
Victor Stinnerbe492442011-11-21 12:43:50 +0100674 wcscpy(P, wstr);
675 P += (len + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 }
677 /* And doubly-terminate the list... */
678 *P = '\0';
679 break;
680 }
681 case REG_BINARY:
682 /* ALSO handle ALL unknown data types here. Even if we can't
683 support it natively, we should handle the bits. */
684 default:
Zachary Waread4690f2014-07-03 10:58:06 -0500685 if (value == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 *retDataSize = 0;
Zachary Waread4690f2014-07-03 10:58:06 -0500687 *retDataBuf = NULL;
688 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 else {
690 Py_buffer view;
Neal Norwitz1385b892007-08-26 23:07:13 +0000691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 if (!PyObject_CheckBuffer(value)) {
693 PyErr_Format(PyExc_TypeError,
694 "Objects of type '%s' can not "
695 "be used as binary registry values",
696 value->ob_type->tp_name);
697 return FALSE;
698 }
Neal Norwitz1385b892007-08-26 23:07:13 +0000699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0)
701 return FALSE;
Neal Norwitz1385b892007-08-26 23:07:13 +0000702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 *retDataBuf = (BYTE *)PyMem_NEW(char, view.len);
Steve Dower4d4bc422016-05-25 11:26:07 -0700704 if (*retDataBuf == NULL){
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 PyBuffer_Release(&view);
706 PyErr_NoMemory();
707 return FALSE;
708 }
Brian Curtinabb33512010-08-17 20:08:40 +0000709 *retDataSize = Py_SAFE_DOWNCAST(view.len, Py_ssize_t, DWORD);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 memcpy(*retDataBuf, view.buf, view.len);
711 PyBuffer_Release(&view);
712 }
713 break;
714 }
715 return TRUE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000716}
717
718/* Convert Registry data into PyObject*/
719static PyObject *
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000720Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000721{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 PyObject *obData;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 switch (typ) {
725 case REG_DWORD:
726 if (retDataSize == 0)
Brian Curtin172e4222012-12-27 14:04:42 -0600727 obData = PyLong_FromUnsignedLong(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 else
Steve Dower80ac11d2016-05-24 15:42:04 -0700729 obData = PyLong_FromUnsignedLong(*(DWORD *)retDataBuf);
730 break;
731 case REG_QWORD:
732 if (retDataSize == 0)
733 obData = PyLong_FromUnsignedLongLong(0);
734 else
735 obData = PyLong_FromUnsignedLongLong(*(DWORD64 *)retDataBuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 break;
737 case REG_SZ:
738 case REG_EXPAND_SZ:
739 {
Steve Dower40fa2662016-12-17 13:30:27 -0800740 /* REG_SZ should be a NUL terminated string, but only by
741 * convention. The buffer may have been saved without a NUL
742 * or with embedded NULs. To be consistent with reg.exe and
743 * regedit.exe, consume only up to the first NUL. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 wchar_t *data = (wchar_t *)retDataBuf;
Steve Dower40fa2662016-12-17 13:30:27 -0800745 size_t len = wcsnlen(data, retDataSize / sizeof(wchar_t));
746 obData = PyUnicode_FromWideChar(data, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 break;
748 }
749 case REG_MULTI_SZ:
750 if (retDataSize == 0)
751 obData = PyList_New(0);
752 else
753 {
754 int index = 0;
755 wchar_t *data = (wchar_t *)retDataBuf;
756 int len = retDataSize / 2;
757 int s = countStrings(data, len);
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +0200758 wchar_t **str = PyMem_New(wchar_t *, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 if (str == NULL)
760 return PyErr_NoMemory();
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 fixupMultiSZ(str, data, len);
763 obData = PyList_New(s);
Jesus Cea782c4cf2014-03-13 17:35:32 +0100764 if (obData == NULL) {
Victor Stinner9cb1ec52014-03-13 19:08:10 +0100765 PyMem_Free(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 return NULL;
Jesus Cea782c4cf2014-03-13 17:35:32 +0100767 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 for (index = 0; index < s; index++)
769 {
Zackery Spytze223ba12019-09-09 03:26:15 -0600770 size_t slen = wcsnlen(str[index], len);
771 PyObject *uni = PyUnicode_FromWideChar(str[index], slen);
Zackery Spytz99d56b52018-12-08 07:16:55 -0700772 if (uni == NULL) {
773 Py_DECREF(obData);
774 PyMem_Free(str);
775 return NULL;
776 }
777 PyList_SET_ITEM(obData, index, uni);
Steve Doweref66f312019-09-09 06:24:15 -0700778 len -= Py_SAFE_DOWNCAST(slen + 1, size_t, int);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 }
Victor Stinnerb6404912013-07-07 16:21:41 +0200780 PyMem_Free(str);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 break;
783 }
784 case REG_BINARY:
785 /* ALSO handle ALL unknown data types here. Even if we can't
786 support it natively, we should handle the bits. */
787 default:
788 if (retDataSize == 0) {
789 Py_INCREF(Py_None);
790 obData = Py_None;
791 }
792 else
793 obData = PyBytes_FromStringAndSize(
794 (char *)retDataBuf, retDataSize);
795 break;
796 }
797 return obData;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000798}
799
800/* The Python methods */
801
Zachary Warefd2d4822015-05-13 01:21:57 -0500802/*[clinic input]
803winreg.CloseKey
804
805 hkey: object
806 A previously opened key.
807 /
808
809Closes a previously opened registry key.
810
811Note that if the key is not closed using this method, it will be
812closed when the hkey object is destroyed by Python.
813[clinic start generated code]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000814
815static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300816winreg_CloseKey(PyObject *module, PyObject *hkey)
817/*[clinic end generated code: output=a4fa537019a80d15 input=5b1aac65ba5127ad]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000818{
Zachary Warefd2d4822015-05-13 01:21:57 -0500819 if (!PyHKEY_Close(hkey))
820 return NULL;
821 Py_RETURN_NONE;
822}
823
824/*[clinic input]
825winreg.ConnectRegistry -> HKEY
826
Zachary Ware77772c02015-05-13 10:58:35 -0500827 computer_name: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -0500828 The name of the remote computer, of the form r"\\computername". If
829 None, the local computer is used.
830 key: HKEY
831 The predefined key to connect to.
832 /
833
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +0300834Establishes a connection to the registry on another computer.
Zachary Warefd2d4822015-05-13 01:21:57 -0500835
836The return value is the handle of the opened key.
837If the function fails, an OSError exception is raised.
838[clinic start generated code]*/
839
840static HKEY
Serhiy Storchakaafb3e712018-12-14 11:19:51 +0200841winreg_ConnectRegistry_impl(PyObject *module,
842 const Py_UNICODE *computer_name, HKEY key)
843/*[clinic end generated code: output=cd4f70fb9ec901fb input=5f98a891a347e68e]*/
Zachary Warefd2d4822015-05-13 01:21:57 -0500844{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 HKEY retKey;
846 long rc;
Steve Doweree17e372019-12-09 11:18:12 -0800847 if (PySys_Audit("winreg.ConnectRegistry", "un",
848 computer_name, (Py_ssize_t)key) < 0) {
849 return NULL;
850 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -0500852 rc = RegConnectRegistryW(computer_name, key, &retKey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 Py_END_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -0500854 if (rc != ERROR_SUCCESS) {
855 PyErr_SetFromWindowsErrWithFunction(rc, "ConnectRegistry");
856 return NULL;
857 }
858 return retKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000859}
860
Zachary Warefd2d4822015-05-13 01:21:57 -0500861/*[clinic input]
862winreg.CreateKey -> HKEY
863
864 key: HKEY
865 An already open key, or one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -0500866 sub_key: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -0500867 The name of the key this method opens or creates.
868 /
869
870Creates or opens the specified key.
871
872If key is one of the predefined keys, sub_key may be None. In that case,
873the handle returned is the same key handle passed in to the function.
874
875If the key already exists, this function opens the existing key.
876
877The return value is the handle of the opened key.
878If the function fails, an OSError exception is raised.
879[clinic start generated code]*/
880
881static HKEY
Serhiy Storchakaafb3e712018-12-14 11:19:51 +0200882winreg_CreateKey_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key)
883/*[clinic end generated code: output=2af13910d56eae26 input=3cdd1622488acea2]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000884{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 HKEY retKey;
886 long rc;
Zachary Warefd2d4822015-05-13 01:21:57 -0500887
Steve Doweree17e372019-12-09 11:18:12 -0800888 if (PySys_Audit("winreg.CreateKey", "nun",
889 (Py_ssize_t)key, sub_key,
890 (Py_ssize_t)KEY_WRITE) < 0) {
891 return NULL;
892 }
Zachary Warefd2d4822015-05-13 01:21:57 -0500893 rc = RegCreateKeyW(key, sub_key, &retKey);
894 if (rc != ERROR_SUCCESS) {
895 PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 return NULL;
Zachary Warefd2d4822015-05-13 01:21:57 -0500897 }
Steve Doweree17e372019-12-09 11:18:12 -0800898 if (PySys_Audit("winreg.OpenKey/result", "n",
899 (Py_ssize_t)retKey) < 0) {
900 return NULL;
901 }
Zachary Warefd2d4822015-05-13 01:21:57 -0500902 return retKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000903}
904
Zachary Warefd2d4822015-05-13 01:21:57 -0500905/*[clinic input]
906winreg.CreateKeyEx -> HKEY
907
908 key: HKEY
909 An already open key, or one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -0500910 sub_key: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -0500911 The name of the key this method opens or creates.
912 reserved: int = 0
913 A reserved integer, and must be zero. Default is zero.
914 access: REGSAM(c_default='KEY_WRITE') = winreg.KEY_WRITE
915 An integer that specifies an access mask that describes the
916 desired security access for the key. Default is KEY_WRITE.
917
918Creates or opens the specified key.
919
920If key is one of the predefined keys, sub_key may be None. In that case,
921the handle returned is the same key handle passed in to the function.
922
923If the key already exists, this function opens the existing key
924
925The return value is the handle of the opened key.
926If the function fails, an OSError exception is raised.
927[clinic start generated code]*/
928
929static HKEY
Serhiy Storchakaafb3e712018-12-14 11:19:51 +0200930winreg_CreateKeyEx_impl(PyObject *module, HKEY key,
931 const Py_UNICODE *sub_key, int reserved,
932 REGSAM access)
933/*[clinic end generated code: output=643a70ad6a361a97 input=42c2b03f98406b66]*/
Brian Curtin3035c392010-04-21 23:56:21 +0000934{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 HKEY retKey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 long rc;
Brian Curtin1771b542010-09-27 17:56:36 +0000937
Steve Doweree17e372019-12-09 11:18:12 -0800938 if (PySys_Audit("winreg.CreateKey", "nun",
939 (Py_ssize_t)key, sub_key,
940 (Py_ssize_t)access) < 0) {
941 return NULL;
942 }
Segev Finer679b5662017-07-27 01:17:57 +0300943 rc = RegCreateKeyExW(key, sub_key, reserved, NULL, 0,
Brian Curtin1771b542010-09-27 17:56:36 +0000944 access, NULL, &retKey, NULL);
Zachary Warefd2d4822015-05-13 01:21:57 -0500945 if (rc != ERROR_SUCCESS) {
946 PyErr_SetFromWindowsErrWithFunction(rc, "CreateKeyEx");
947 return NULL;
948 }
Steve Doweree17e372019-12-09 11:18:12 -0800949 if (PySys_Audit("winreg.OpenKey/result", "n",
950 (Py_ssize_t)retKey) < 0) {
951 return NULL;
952 }
Zachary Warefd2d4822015-05-13 01:21:57 -0500953 return retKey;
Brian Curtin3035c392010-04-21 23:56:21 +0000954}
955
Zachary Warefd2d4822015-05-13 01:21:57 -0500956/*[clinic input]
957winreg.DeleteKey
958 key: HKEY
959 An already open key, or any one of the predefined HKEY_* constants.
960 sub_key: Py_UNICODE
961 A string that must be the name of a subkey of the key identified by
962 the key parameter. This value must not be None, and the key may not
963 have subkeys.
964 /
965
966Deletes the specified key.
967
968This method can not delete keys with subkeys.
969
970If the function succeeds, the entire key, including all of its values,
971is removed. If the function fails, an OSError exception is raised.
972[clinic start generated code]*/
973
Brian Curtin3035c392010-04-21 23:56:21 +0000974static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +0200975winreg_DeleteKey_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key)
976/*[clinic end generated code: output=d2652a84f70e0862 input=b31d225b935e4211]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000977{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 long rc;
Steve Doweree17e372019-12-09 11:18:12 -0800979 if (PySys_Audit("winreg.DeleteKey", "nun",
980 (Py_ssize_t)key, sub_key,
981 (Py_ssize_t)0) < 0) {
982 return NULL;
983 }
Zachary Warefd2d4822015-05-13 01:21:57 -0500984 rc = RegDeleteKeyW(key, sub_key );
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 if (rc != ERROR_SUCCESS)
986 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey");
Zachary Warefd2d4822015-05-13 01:21:57 -0500987 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000988}
989
Zachary Warefd2d4822015-05-13 01:21:57 -0500990/*[clinic input]
991winreg.DeleteKeyEx
992
993 key: HKEY
994 An already open key, or any one of the predefined HKEY_* constants.
995 sub_key: Py_UNICODE
996 A string that must be the name of a subkey of the key identified by
997 the key parameter. This value must not be None, and the key may not
998 have subkeys.
999 access: REGSAM(c_default='KEY_WOW64_64KEY') = winreg.KEY_WOW64_64KEY
1000 An integer that specifies an access mask that describes the
1001 desired security access for the key. Default is KEY_WOW64_64KEY.
1002 reserved: int = 0
1003 A reserved integer, and must be zero. Default is zero.
1004
1005Deletes the specified key (64-bit OS only).
1006
1007This method can not delete keys with subkeys.
1008
1009If the function succeeds, the entire key, including all of its values,
1010is removed. If the function fails, an OSError exception is raised.
1011On unsupported Windows versions, NotImplementedError is raised.
1012[clinic start generated code]*/
1013
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001014static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001015winreg_DeleteKeyEx_impl(PyObject *module, HKEY key,
1016 const Py_UNICODE *sub_key, REGSAM access,
1017 int reserved)
1018/*[clinic end generated code: output=52a1c8b374ebc003 input=711d9d89e7ecbed7]*/
Brian Curtin3035c392010-04-21 23:56:21 +00001019{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 HMODULE hMod;
1021 typedef LONG (WINAPI *RDKEFunc)(HKEY, const wchar_t*, REGSAM, int);
1022 RDKEFunc pfn = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 long rc;
Brian Curtin3035c392010-04-21 23:56:21 +00001024
Steve Doweree17e372019-12-09 11:18:12 -08001025 if (PySys_Audit("winreg.DeleteKey", "nun",
1026 (Py_ssize_t)key, sub_key,
1027 (Py_ssize_t)access) < 0) {
1028 return NULL;
1029 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 /* Only available on 64bit platforms, so we must load it
1031 dynamically. */
Tony Roberts4860f012019-02-02 18:16:42 +01001032 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis50590f12012-01-14 17:54:09 +01001033 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 if (hMod)
Steve Doweree17e372019-12-09 11:18:12 -08001035 pfn = (RDKEFunc)GetProcAddress(hMod, "RegDeleteKeyExW");
Tony Roberts4860f012019-02-02 18:16:42 +01001036 Py_END_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 if (!pfn) {
1038 PyErr_SetString(PyExc_NotImplementedError,
1039 "not implemented on this platform");
1040 return NULL;
1041 }
1042 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001043 rc = (*pfn)(key, sub_key, access, reserved);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 Py_END_ALLOW_THREADS
Brian Curtin3035c392010-04-21 23:56:21 +00001045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 if (rc != ERROR_SUCCESS)
1047 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKeyEx");
Zachary Warefd2d4822015-05-13 01:21:57 -05001048 Py_RETURN_NONE;
Brian Curtin3035c392010-04-21 23:56:21 +00001049}
1050
Zachary Warefd2d4822015-05-13 01:21:57 -05001051/*[clinic input]
1052winreg.DeleteValue
1053
1054 key: HKEY
1055 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001056 value: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001057 A string that identifies the value to remove.
1058 /
1059
1060Removes a named value from a registry key.
1061[clinic start generated code]*/
1062
Brian Curtin3035c392010-04-21 23:56:21 +00001063static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001064winreg_DeleteValue_impl(PyObject *module, HKEY key, const Py_UNICODE *value)
1065/*[clinic end generated code: output=56fa9d21f3a54371 input=a78d3407a4197b21]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001066{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 long rc;
Steve Doweree17e372019-12-09 11:18:12 -08001068 if (PySys_Audit("winreg.DeleteValue", "nu",
1069 (Py_ssize_t)key, value) < 0) {
1070 return NULL;
1071 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001073 rc = RegDeleteValueW(key, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 Py_END_ALLOW_THREADS
1075 if (rc !=ERROR_SUCCESS)
1076 return PyErr_SetFromWindowsErrWithFunction(rc,
1077 "RegDeleteValue");
Zachary Warefd2d4822015-05-13 01:21:57 -05001078 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001079}
1080
Zachary Warefd2d4822015-05-13 01:21:57 -05001081/*[clinic input]
1082winreg.EnumKey
1083
1084 key: HKEY
1085 An already open key, or any one of the predefined HKEY_* constants.
1086 index: int
1087 An integer that identifies the index of the key to retrieve.
1088 /
1089
1090Enumerates subkeys of an open registry key.
1091
1092The function retrieves the name of one subkey each time it is called.
1093It is typically called repeatedly until an OSError exception is
1094raised, indicating no more values are available.
1095[clinic start generated code]*/
1096
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001097static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001098winreg_EnumKey_impl(PyObject *module, HKEY key, int index)
1099/*[clinic end generated code: output=25a6ec52cd147bc4 input=fad9a7c00ab0e04b]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 long rc;
1102 PyObject *retStr;
Brian Curtin60853212010-05-26 17:43:50 +00001103
Steve Doweree17e372019-12-09 11:18:12 -08001104 if (PySys_Audit("winreg.EnumKey", "ni",
1105 (Py_ssize_t)key, index) < 0) {
1106 return NULL;
1107 }
Brian Curtin60853212010-05-26 17:43:50 +00001108 /* The Windows docs claim that the max key name length is 255
1109 * characters, plus a terminating nul character. However,
1110 * empirical testing demonstrates that it is possible to
1111 * create a 256 character key that is missing the terminating
1112 * nul. RegEnumKeyEx requires a 257 character buffer to
1113 * retrieve such a key name. */
1114 wchar_t tmpbuf[257];
Kristján Valur Jónsson984dfa72012-04-02 15:23:29 +00001115 DWORD len = sizeof(tmpbuf)/sizeof(wchar_t); /* includes NULL terminator */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001118 rc = RegEnumKeyExW(key, index, tmpbuf, &len, NULL, NULL, NULL, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 Py_END_ALLOW_THREADS
1120 if (rc != ERROR_SUCCESS)
1121 return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx");
Tim Peters313fcd42006-02-19 04:05:39 +00001122
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001123 retStr = PyUnicode_FromWideChar(tmpbuf, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 return retStr; /* can be NULL */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001125}
1126
Zachary Warefd2d4822015-05-13 01:21:57 -05001127/*[clinic input]
1128winreg.EnumValue
1129
1130 key: HKEY
1131 An already open key, or any one of the predefined HKEY_* constants.
1132 index: int
1133 An integer that identifies the index of the value to retrieve.
1134 /
1135
1136Enumerates values of an open registry key.
1137
1138The function retrieves the name of one subkey each time it is called.
1139It is typically called repeatedly, until an OSError exception
1140is raised, indicating no more values.
1141
1142The result is a tuple of 3 items:
1143 value_name
1144 A string that identifies the value.
1145 value_data
1146 An object that holds the value data, and whose type depends
1147 on the underlying registry type.
1148 data_type
1149 An integer that identifies the type of the value data.
1150[clinic start generated code]*/
1151
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001152static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001153winreg_EnumValue_impl(PyObject *module, HKEY key, int index)
1154/*[clinic end generated code: output=d363b5a06f8789ac input=4414f47a6fb238b5]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001155{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 long rc;
1157 wchar_t *retValueBuf;
Amaury Forgeot d'Arcf2b69df2010-08-16 22:11:29 +00001158 BYTE *tmpBuf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 BYTE *retDataBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001160 DWORD retValueSize, bufValueSize;
1161 DWORD retDataSize, bufDataSize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 DWORD typ;
1163 PyObject *obData;
1164 PyObject *retVal;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001165
Steve Doweree17e372019-12-09 11:18:12 -08001166 if (PySys_Audit("winreg.EnumValue", "ni",
1167 (Py_ssize_t)key, index) < 0) {
1168 return NULL;
1169 }
Zachary Warefd2d4822015-05-13 01:21:57 -05001170 if ((rc = RegQueryInfoKeyW(key, NULL, NULL, NULL, NULL, NULL, NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 NULL,
1172 &retValueSize, &retDataSize, NULL, NULL))
1173 != ERROR_SUCCESS)
1174 return PyErr_SetFromWindowsErrWithFunction(rc,
1175 "RegQueryInfoKey");
1176 ++retValueSize; /* include null terminators */
1177 ++retDataSize;
Brian Curtin60853212010-05-26 17:43:50 +00001178 bufDataSize = retDataSize;
1179 bufValueSize = retValueSize;
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02001180 retValueBuf = PyMem_New(wchar_t, retValueSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 if (retValueBuf == NULL)
1182 return PyErr_NoMemory();
1183 retDataBuf = (BYTE *)PyMem_Malloc(retDataSize);
1184 if (retDataBuf == NULL) {
1185 PyMem_Free(retValueBuf);
1186 return PyErr_NoMemory();
1187 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001188
Brian Curtin60853212010-05-26 17:43:50 +00001189 while (1) {
Brian Curtin60853212010-05-26 17:43:50 +00001190 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001191 rc = RegEnumValueW(key,
Brian Curtin60853212010-05-26 17:43:50 +00001192 index,
1193 retValueBuf,
1194 &retValueSize,
1195 NULL,
1196 &typ,
1197 (BYTE *)retDataBuf,
1198 &retDataSize);
1199 Py_END_ALLOW_THREADS
1200
1201 if (rc != ERROR_MORE_DATA)
1202 break;
1203
1204 bufDataSize *= 2;
Amaury Forgeot d'Arcf2b69df2010-08-16 22:11:29 +00001205 tmpBuf = (BYTE *)PyMem_Realloc(retDataBuf, bufDataSize);
Brian Curtin9b7e2d12010-06-08 20:57:52 +00001206 if (tmpBuf == NULL) {
Brian Curtin60853212010-05-26 17:43:50 +00001207 PyErr_NoMemory();
1208 retVal = NULL;
1209 goto fail;
1210 }
Brian Curtin9b7e2d12010-06-08 20:57:52 +00001211 retDataBuf = tmpBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001212 retDataSize = bufDataSize;
1213 retValueSize = bufValueSize;
1214 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 if (rc != ERROR_SUCCESS) {
1217 retVal = PyErr_SetFromWindowsErrWithFunction(rc,
1218 "PyRegEnumValue");
1219 goto fail;
1220 }
1221 obData = Reg2Py(retDataBuf, retDataSize, typ);
1222 if (obData == NULL) {
1223 retVal = NULL;
1224 goto fail;
1225 }
1226 retVal = Py_BuildValue("uOi", retValueBuf, obData, typ);
1227 Py_DECREF(obData);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001228 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 PyMem_Free(retValueBuf);
1230 PyMem_Free(retDataBuf);
1231 return retVal;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001232}
1233
Zachary Warefd2d4822015-05-13 01:21:57 -05001234/*[clinic input]
1235winreg.ExpandEnvironmentStrings
1236
1237 string: Py_UNICODE
1238 /
1239
1240Expand environment vars.
1241[clinic start generated code]*/
1242
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001243static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001244winreg_ExpandEnvironmentStrings_impl(PyObject *module,
1245 const Py_UNICODE *string)
1246/*[clinic end generated code: output=8fa4e959747a7312 input=b2a9714d2b751aa6]*/
Christian Heimes2380ac72008-01-09 00:17:24 +00001247{
Victor Stinner9d3b93b2011-11-22 02:27:30 +01001248 wchar_t *retValue = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 DWORD retValueSize;
1250 DWORD rc;
1251 PyObject *o;
Christian Heimes2380ac72008-01-09 00:17:24 +00001252
Steve Doweree17e372019-12-09 11:18:12 -08001253 if (PySys_Audit("winreg.ExpandEnvironmentStrings", "u",
1254 string) < 0) {
1255 return NULL;
1256 }
1257
Zachary Warefd2d4822015-05-13 01:21:57 -05001258 retValueSize = ExpandEnvironmentStringsW(string, retValue, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 if (retValueSize == 0) {
1260 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1261 "ExpandEnvironmentStrings");
1262 }
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02001263 retValue = PyMem_New(wchar_t, retValueSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 if (retValue == NULL) {
1265 return PyErr_NoMemory();
1266 }
Christian Heimes2380ac72008-01-09 00:17:24 +00001267
Zachary Warefd2d4822015-05-13 01:21:57 -05001268 rc = ExpandEnvironmentStringsW(string, retValue, retValueSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 if (rc == 0) {
1270 PyMem_Free(retValue);
1271 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1272 "ExpandEnvironmentStrings");
1273 }
Victor Stinner9d3b93b2011-11-22 02:27:30 +01001274 o = PyUnicode_FromWideChar(retValue, wcslen(retValue));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 PyMem_Free(retValue);
1276 return o;
Christian Heimes2380ac72008-01-09 00:17:24 +00001277}
1278
Zachary Warefd2d4822015-05-13 01:21:57 -05001279/*[clinic input]
1280winreg.FlushKey
1281
1282 key: HKEY
1283 An already open key, or any one of the predefined HKEY_* constants.
1284 /
1285
1286Writes all the attributes of a key to the registry.
1287
1288It is not necessary to call FlushKey to change a key. Registry changes
1289are flushed to disk by the registry using its lazy flusher. Registry
1290changes are also flushed to disk at system shutdown. Unlike
1291CloseKey(), the FlushKey() method returns only when all the data has
1292been written to the registry.
1293
1294An application should only call FlushKey() if it requires absolute
1295certainty that registry changes are on disk. If you don't know whether
1296a FlushKey() call is required, it probably isn't.
1297[clinic start generated code]*/
1298
Christian Heimes2380ac72008-01-09 00:17:24 +00001299static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001300winreg_FlushKey_impl(PyObject *module, HKEY key)
1301/*[clinic end generated code: output=e6fc230d4c5dc049 input=f57457c12297d82f]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001302{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 long rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001305 rc = RegFlushKey(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 Py_END_ALLOW_THREADS
1307 if (rc != ERROR_SUCCESS)
1308 return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001309 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001310}
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001311
Zachary Warefd2d4822015-05-13 01:21:57 -05001312
1313/*[clinic input]
1314winreg.LoadKey
1315
1316 key: HKEY
1317 An already open key, or any one of the predefined HKEY_* constants.
1318 sub_key: Py_UNICODE
1319 A string that identifies the sub-key to load.
1320 file_name: Py_UNICODE
1321 The name of the file to load registry data from. This file must
1322 have been created with the SaveKey() function. Under the file
1323 allocation table (FAT) file system, the filename may not have an
1324 extension.
1325 /
1326
1327Insert data into the registry from a file.
1328
1329Creates a subkey under the specified key and stores registration
1330information from a specified file into that subkey.
1331
1332A call to LoadKey() fails if the calling process does not have the
1333SE_RESTORE_PRIVILEGE privilege.
1334
1335If key is a handle returned by ConnectRegistry(), then the path
1336specified in fileName is relative to the remote computer.
1337
1338The MSDN docs imply key must be in the HKEY_USER or HKEY_LOCAL_MACHINE
1339tree.
1340[clinic start generated code]*/
1341
1342static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001343winreg_LoadKey_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key,
1344 const Py_UNICODE *file_name)
1345/*[clinic end generated code: output=65f89f2548cb27c7 input=e3b5b45ade311582]*/
Zachary Warefd2d4822015-05-13 01:21:57 -05001346{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 long rc;
Zachary Warefd2d4822015-05-13 01:21:57 -05001348
Steve Doweree17e372019-12-09 11:18:12 -08001349 if (PySys_Audit("winreg.LoadKey", "nuu",
1350 (Py_ssize_t)key, sub_key, file_name) < 0) {
1351 return NULL;
1352 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001354 rc = RegLoadKeyW(key, sub_key, file_name );
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 Py_END_ALLOW_THREADS
1356 if (rc != ERROR_SUCCESS)
1357 return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001358 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001359}
1360
Zachary Warefd2d4822015-05-13 01:21:57 -05001361/*[clinic input]
1362winreg.OpenKey -> HKEY
1363
1364 key: HKEY
1365 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001366 sub_key: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001367 A string that identifies the sub_key to open.
1368 reserved: int = 0
1369 A reserved integer that must be zero. Default is zero.
1370 access: REGSAM(c_default='KEY_READ') = winreg.KEY_READ
1371 An integer that specifies an access mask that describes the desired
1372 security access for the key. Default is KEY_READ.
1373
1374Opens the specified key.
1375
1376The result is a new handle to the specified key.
1377If the function fails, an OSError exception is raised.
1378[clinic start generated code]*/
1379
1380static HKEY
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001381winreg_OpenKey_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key,
Zachary Ware77772c02015-05-13 10:58:35 -05001382 int reserved, REGSAM access)
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001383/*[clinic end generated code: output=8849bff2c30104ad input=098505ac36a9ae28]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001384{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 HKEY retKey;
1386 long rc;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001387
Steve Doweree17e372019-12-09 11:18:12 -08001388 if (PySys_Audit("winreg.OpenKey", "nun",
1389 (Py_ssize_t)key, sub_key,
1390 (Py_ssize_t)access) < 0) {
1391 return NULL;
1392 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001394 rc = RegOpenKeyExW(key, sub_key, reserved, access, &retKey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 Py_END_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001396 if (rc != ERROR_SUCCESS) {
1397 PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx");
1398 return NULL;
1399 }
Steve Doweree17e372019-12-09 11:18:12 -08001400 if (PySys_Audit("winreg.OpenKey/result", "n",
1401 (Py_ssize_t)retKey) < 0) {
1402 return NULL;
1403 }
Zachary Warefd2d4822015-05-13 01:21:57 -05001404 return retKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001405}
1406
Zachary Warefd2d4822015-05-13 01:21:57 -05001407/*[clinic input]
1408winreg.OpenKeyEx = winreg.OpenKey
1409
1410Opens the specified key.
1411
1412The result is a new handle to the specified key.
1413If the function fails, an OSError exception is raised.
1414[clinic start generated code]*/
1415
1416static HKEY
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001417winreg_OpenKeyEx_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key,
Zachary Ware77772c02015-05-13 10:58:35 -05001418 int reserved, REGSAM access)
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001419/*[clinic end generated code: output=81bc2bd684bc77ae input=c6c4972af8622959]*/
Zachary Warefd2d4822015-05-13 01:21:57 -05001420{
1421 return winreg_OpenKey_impl(module, key, sub_key, reserved, access);
1422}
1423
1424/*[clinic input]
1425winreg.QueryInfoKey
1426
1427 key: HKEY
1428 An already open key, or any one of the predefined HKEY_* constants.
1429 /
1430
1431Returns information about a key.
1432
1433The result is a tuple of 3 items:
1434An integer that identifies the number of sub keys this key has.
1435An integer that identifies the number of values this key has.
1436An integer that identifies when the key was last modified (if available)
1437as 100's of nanoseconds since Jan 1, 1600.
1438[clinic start generated code]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001439
1440static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001441winreg_QueryInfoKey_impl(PyObject *module, HKEY key)
1442/*[clinic end generated code: output=dc657b8356a4f438 input=c3593802390cde1f]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001443{
Steve Doweree17e372019-12-09 11:18:12 -08001444 long rc;
1445 DWORD nSubKeys, nValues;
1446 FILETIME ft;
1447 LARGE_INTEGER li;
1448 PyObject *l;
1449 PyObject *ret;
Zachary Warefd2d4822015-05-13 01:21:57 -05001450
Steve Doweree17e372019-12-09 11:18:12 -08001451 if (PySys_Audit("winreg.QueryInfoKey", "n", (Py_ssize_t)key) < 0) {
1452 return NULL;
1453 }
Minmin Gong98e42d12020-05-18 09:50:03 -07001454 if ((rc = RegQueryInfoKeyW(key, NULL, NULL, 0, &nSubKeys, NULL, NULL,
1455 &nValues, NULL, NULL, NULL, &ft))
1456 != ERROR_SUCCESS) {
Steve Doweree17e372019-12-09 11:18:12 -08001457 return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey");
1458 }
1459 li.LowPart = ft.dwLowDateTime;
1460 li.HighPart = ft.dwHighDateTime;
1461 l = PyLong_FromLongLong(li.QuadPart);
1462 if (l == NULL) {
1463 return NULL;
1464 }
1465 ret = Py_BuildValue("iiO", nSubKeys, nValues, l);
1466 Py_DECREF(l);
1467 return ret;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001468}
1469
Zachary Warefd2d4822015-05-13 01:21:57 -05001470/*[clinic input]
1471winreg.QueryValue
1472
1473 key: HKEY
1474 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001475 sub_key: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001476 A string that holds the name of the subkey with which the value
1477 is associated. If this parameter is None or empty, the function
1478 retrieves the value set by the SetValue() method for the key
1479 identified by key.
1480 /
1481
1482Retrieves the unnamed value for a key.
1483
1484Values in the registry have name, type, and data components. This method
1485retrieves the data for a key's first value that has a NULL name.
1486But since the underlying API call doesn't return the type, you'll
1487probably be happier using QueryValueEx; this function is just here for
1488completeness.
1489[clinic start generated code]*/
1490
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001491static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001492winreg_QueryValue_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key)
1493/*[clinic end generated code: output=c655810ae50c63a9 input=41cafbbf423b21d6]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 long rc;
1496 PyObject *retStr;
1497 wchar_t *retBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001498 DWORD bufSize = 0;
1499 DWORD retSize = 0;
1500 wchar_t *tmp;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001501
Steve Doweree17e372019-12-09 11:18:12 -08001502 if (PySys_Audit("winreg.QueryValue", "nuu",
1503 (Py_ssize_t)key, sub_key, NULL) < 0) {
1504 return NULL;
1505 }
Zachary Warefd2d4822015-05-13 01:21:57 -05001506 rc = RegQueryValueW(key, sub_key, NULL, &retSize);
Brian Curtin60853212010-05-26 17:43:50 +00001507 if (rc == ERROR_MORE_DATA)
1508 retSize = 256;
1509 else if (rc != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 return PyErr_SetFromWindowsErrWithFunction(rc,
1511 "RegQueryValue");
Brian Curtin60853212010-05-26 17:43:50 +00001512
1513 bufSize = retSize;
1514 retBuf = (wchar_t *) PyMem_Malloc(bufSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 if (retBuf == NULL)
1516 return PyErr_NoMemory();
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001517
Brian Curtin60853212010-05-26 17:43:50 +00001518 while (1) {
1519 retSize = bufSize;
Zachary Warefd2d4822015-05-13 01:21:57 -05001520 rc = RegQueryValueW(key, sub_key, retBuf, &retSize);
Brian Curtin60853212010-05-26 17:43:50 +00001521 if (rc != ERROR_MORE_DATA)
1522 break;
1523
1524 bufSize *= 2;
1525 tmp = (wchar_t *) PyMem_Realloc(retBuf, bufSize);
1526 if (tmp == NULL) {
1527 PyMem_Free(retBuf);
1528 return PyErr_NoMemory();
1529 }
1530 retBuf = tmp;
1531 }
1532
1533 if (rc != ERROR_SUCCESS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 PyMem_Free(retBuf);
1535 return PyErr_SetFromWindowsErrWithFunction(rc,
1536 "RegQueryValue");
1537 }
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001538
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001539 retStr = PyUnicode_FromWideChar(retBuf, wcslen(retBuf));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 PyMem_Free(retBuf);
1541 return retStr;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001542}
1543
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001544
Zachary Warefd2d4822015-05-13 01:21:57 -05001545/*[clinic input]
1546winreg.QueryValueEx
1547
1548 key: HKEY
1549 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001550 name: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001551 A string indicating the value to query.
1552 /
1553
1554Retrieves the type and value of a specified sub-key.
1555
1556Behaves mostly like QueryValue(), but also returns the type of the
1557specified value name associated with the given open registry key.
1558
1559The return value is a tuple of the value and the type_id.
1560[clinic start generated code]*/
1561
1562static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001563winreg_QueryValueEx_impl(PyObject *module, HKEY key, const Py_UNICODE *name)
1564/*[clinic end generated code: output=f1b85b1c3d887ec7 input=cf366cada4836891]*/
Zachary Warefd2d4822015-05-13 01:21:57 -05001565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 long rc;
Brian Curtin60853212010-05-26 17:43:50 +00001567 BYTE *retBuf, *tmp;
1568 DWORD bufSize = 0, retSize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 DWORD typ;
1570 PyObject *obData;
1571 PyObject *result;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001572
Steve Doweree17e372019-12-09 11:18:12 -08001573 if (PySys_Audit("winreg.QueryValue", "nuu",
1574 (Py_ssize_t)key, NULL, name) < 0) {
1575 return NULL;
1576 }
Zachary Warefd2d4822015-05-13 01:21:57 -05001577 rc = RegQueryValueExW(key, name, NULL, NULL, NULL, &bufSize);
Brian Curtin60853212010-05-26 17:43:50 +00001578 if (rc == ERROR_MORE_DATA)
1579 bufSize = 256;
1580 else if (rc != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 return PyErr_SetFromWindowsErrWithFunction(rc,
1582 "RegQueryValueEx");
1583 retBuf = (BYTE *)PyMem_Malloc(bufSize);
1584 if (retBuf == NULL)
1585 return PyErr_NoMemory();
Brian Curtin60853212010-05-26 17:43:50 +00001586
1587 while (1) {
1588 retSize = bufSize;
Zachary Warefd2d4822015-05-13 01:21:57 -05001589 rc = RegQueryValueExW(key, name, NULL, &typ,
Brian Curtin60853212010-05-26 17:43:50 +00001590 (BYTE *)retBuf, &retSize);
1591 if (rc != ERROR_MORE_DATA)
1592 break;
1593
1594 bufSize *= 2;
1595 tmp = (char *) PyMem_Realloc(retBuf, bufSize);
1596 if (tmp == NULL) {
1597 PyMem_Free(retBuf);
1598 return PyErr_NoMemory();
1599 }
1600 retBuf = tmp;
1601 }
1602
1603 if (rc != ERROR_SUCCESS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 PyMem_Free(retBuf);
1605 return PyErr_SetFromWindowsErrWithFunction(rc,
1606 "RegQueryValueEx");
1607 }
1608 obData = Reg2Py(retBuf, bufSize, typ);
1609 PyMem_Free(retBuf);
1610 if (obData == NULL)
1611 return NULL;
1612 result = Py_BuildValue("Oi", obData, typ);
1613 Py_DECREF(obData);
1614 return result;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001615}
1616
Zachary Warefd2d4822015-05-13 01:21:57 -05001617/*[clinic input]
1618winreg.SaveKey
1619
1620 key: HKEY
1621 An already open key, or any one of the predefined HKEY_* constants.
1622 file_name: Py_UNICODE
1623 The name of the file to save registry data to. This file cannot
1624 already exist. If this filename includes an extension, it cannot be
1625 used on file allocation table (FAT) file systems by the LoadKey(),
1626 ReplaceKey() or RestoreKey() methods.
1627 /
1628
1629Saves the specified key, and all its subkeys to the specified file.
1630
1631If key represents a key on a remote computer, the path described by
1632file_name is relative to the remote computer.
1633
1634The caller of this method must possess the SeBackupPrivilege
1635security privilege. This function passes NULL for security_attributes
1636to the API.
1637[clinic start generated code]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001638
1639static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001640winreg_SaveKey_impl(PyObject *module, HKEY key, const Py_UNICODE *file_name)
1641/*[clinic end generated code: output=ca94b835c88f112b input=da735241f91ac7a2]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001642{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 LPSECURITY_ATTRIBUTES pSA = NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 long rc;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001646/* One day we may get security into the core?
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE))
1648 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001649*/
Steve Doweree17e372019-12-09 11:18:12 -08001650 if (PySys_Audit("winreg.SaveKey", "nu",
1651 (Py_ssize_t)key, file_name) < 0) {
1652 return NULL;
1653 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001655 rc = RegSaveKeyW(key, file_name, pSA );
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 Py_END_ALLOW_THREADS
1657 if (rc != ERROR_SUCCESS)
1658 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001659 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001660}
1661
Zachary Warefd2d4822015-05-13 01:21:57 -05001662/*[clinic input]
1663winreg.SetValue
1664
1665 key: HKEY
1666 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001667 sub_key: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001668 A string that names the subkey with which the value is associated.
1669 type: DWORD
1670 An integer that specifies the type of the data. Currently this must
1671 be REG_SZ, meaning only strings are supported.
Zachary Ware77772c02015-05-13 10:58:35 -05001672 value: Py_UNICODE(zeroes=True)
Zachary Warefd2d4822015-05-13 01:21:57 -05001673 A string that specifies the new value.
1674 /
1675
1676Associates a value with a specified key.
1677
1678If the key specified by the sub_key parameter does not exist, the
1679SetValue function creates it.
1680
1681Value lengths are limited by available memory. Long values (more than
16822048 bytes) should be stored as files with the filenames stored in
1683the configuration registry to help the registry perform efficiently.
1684
1685The key identified by the key parameter must have been opened with
1686KEY_SET_VALUE access.
1687[clinic start generated code]*/
1688
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001689static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001690winreg_SetValue_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key,
1691 DWORD type, const Py_UNICODE *value,
Zachary Ware77772c02015-05-13 10:58:35 -05001692 Py_ssize_clean_t value_length)
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001693/*[clinic end generated code: output=686bedb1cbb4367b input=2cd2adab79339c53]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001694{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 long rc;
Zachary Warefd2d4822015-05-13 01:21:57 -05001696
1697 if (type != REG_SZ) {
Inada Naokicc60cdd92019-03-20 20:53:08 +09001698 PyErr_SetString(PyExc_TypeError, "type must be winreg.REG_SZ");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 return NULL;
1700 }
Inada Naokicc60cdd92019-03-20 20:53:08 +09001701 if ((size_t)value_length >= PY_DWORD_MAX) {
1702 PyErr_SetString(PyExc_OverflowError, "value is too long");
Inada Naokid5f18a62019-03-20 19:10:17 +09001703 return NULL;
1704 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001705
Steve Doweree17e372019-12-09 11:18:12 -08001706 if (PySys_Audit("winreg.SetValue", "nunu#",
1707 (Py_ssize_t)key, sub_key, (Py_ssize_t)type,
1708 value, value_length) < 0) {
1709 return NULL;
1710 }
1711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 Py_BEGIN_ALLOW_THREADS
Zackery Spytz34366b72019-04-22 11:08:05 -06001713 rc = RegSetValueW(key, sub_key, REG_SZ, value, (DWORD)(value_length + 1));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 Py_END_ALLOW_THREADS
1715 if (rc != ERROR_SUCCESS)
1716 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue");
Zachary Warefd2d4822015-05-13 01:21:57 -05001717 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001718}
1719
Zachary Warefd2d4822015-05-13 01:21:57 -05001720/*[clinic input]
1721winreg.SetValueEx
1722
1723 key: HKEY
1724 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001725 value_name: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001726 A string containing the name of the value to set, or None.
1727 reserved: object
1728 Can be anything - zero is always passed to the API.
1729 type: DWORD
1730 An integer that specifies the type of the data, one of:
1731 REG_BINARY -- Binary data in any form.
1732 REG_DWORD -- A 32-bit number.
Steve Dower80ac11d2016-05-24 15:42:04 -07001733 REG_DWORD_LITTLE_ENDIAN -- A 32-bit number in little-endian format. Equivalent to REG_DWORD
Zachary Warefd2d4822015-05-13 01:21:57 -05001734 REG_DWORD_BIG_ENDIAN -- A 32-bit number in big-endian format.
1735 REG_EXPAND_SZ -- A null-terminated string that contains unexpanded
1736 references to environment variables (for example,
1737 %PATH%).
1738 REG_LINK -- A Unicode symbolic link.
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03001739 REG_MULTI_SZ -- A sequence of null-terminated strings, terminated
Zachary Warefd2d4822015-05-13 01:21:57 -05001740 by two null characters. Note that Python handles
1741 this termination automatically.
1742 REG_NONE -- No defined value type.
Steve Dower80ac11d2016-05-24 15:42:04 -07001743 REG_QWORD -- A 64-bit number.
1744 REG_QWORD_LITTLE_ENDIAN -- A 64-bit number in little-endian format. Equivalent to REG_QWORD.
Zachary Warefd2d4822015-05-13 01:21:57 -05001745 REG_RESOURCE_LIST -- A device-driver resource list.
1746 REG_SZ -- A null-terminated string.
1747 value: object
1748 A string that specifies the new value.
1749 /
1750
1751Stores data in the value field of an open registry key.
1752
1753This method can also set additional value and type information for the
1754specified key. The key identified by the key parameter must have been
1755opened with KEY_SET_VALUE access.
1756
1757To open the key, use the CreateKeyEx() or OpenKeyEx() methods.
1758
1759Value lengths are limited by available memory. Long values (more than
17602048 bytes) should be stored as files with the filenames stored in
1761the configuration registry to help the registry perform efficiently.
1762[clinic start generated code]*/
1763
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001764static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001765winreg_SetValueEx_impl(PyObject *module, HKEY key,
1766 const Py_UNICODE *value_name, PyObject *reserved,
1767 DWORD type, PyObject *value)
1768/*[clinic end generated code: output=811b769a66ae11b7 input=900a9e3990bfb196]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 BYTE *data;
1771 DWORD len;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 LONG rc;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001774
Zachary Warefd2d4822015-05-13 01:21:57 -05001775 if (!Py2Reg(value, type, &data, &len))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 {
1777 if (!PyErr_Occurred())
1778 PyErr_SetString(PyExc_ValueError,
1779 "Could not convert the data to the specified type.");
1780 return NULL;
1781 }
Steve Doweree17e372019-12-09 11:18:12 -08001782 if (PySys_Audit("winreg.SetValue", "nunO",
1783 (Py_ssize_t)key, value_name, (Py_ssize_t)type,
1784 value) < 0) {
1785 return NULL;
1786 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001788 rc = RegSetValueExW(key, value_name, 0, type, data, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 Py_END_ALLOW_THREADS
1790 PyMem_DEL(data);
1791 if (rc != ERROR_SUCCESS)
1792 return PyErr_SetFromWindowsErrWithFunction(rc,
1793 "RegSetValueEx");
Zachary Warefd2d4822015-05-13 01:21:57 -05001794 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001795}
1796
Zachary Warefd2d4822015-05-13 01:21:57 -05001797/*[clinic input]
1798winreg.DisableReflectionKey
1799
1800 key: HKEY
1801 An already open key, or any one of the predefined HKEY_* constants.
1802 /
1803
1804Disables registry reflection for 32bit processes running on a 64bit OS.
1805
David Hed5e8e02019-07-31 23:49:55 +01001806Will generally raise NotImplementedError if executed on a 32bit OS.
Zachary Warefd2d4822015-05-13 01:21:57 -05001807
1808If the key is not on the reflection list, the function succeeds but has
1809no effect. Disabling reflection for a key does not affect reflection
1810of any subkeys.
1811[clinic start generated code]*/
1812
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001813static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001814winreg_DisableReflectionKey_impl(PyObject *module, HKEY key)
David Hed5e8e02019-07-31 23:49:55 +01001815/*[clinic end generated code: output=830cce504cc764b4 input=70bece2dee02e073]*/
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001816{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 HMODULE hMod;
1818 typedef LONG (WINAPI *RDRKFunc)(HKEY);
1819 RDRKFunc pfn = NULL;
1820 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001821
Steve Doweree17e372019-12-09 11:18:12 -08001822 if (PySys_Audit("winreg.DisableReflectionKey", "n", (Py_ssize_t)key) < 0) {
1823 return NULL;
1824 }
1825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 /* Only available on 64bit platforms, so we must load it
1827 dynamically.*/
Tony Roberts4860f012019-02-02 18:16:42 +01001828 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis50590f12012-01-14 17:54:09 +01001829 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 if (hMod)
1831 pfn = (RDRKFunc)GetProcAddress(hMod,
1832 "RegDisableReflectionKey");
Tony Roberts4860f012019-02-02 18:16:42 +01001833 Py_END_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 if (!pfn) {
1835 PyErr_SetString(PyExc_NotImplementedError,
1836 "not implemented on this platform");
1837 return NULL;
1838 }
1839 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001840 rc = (*pfn)(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841 Py_END_ALLOW_THREADS
1842 if (rc != ERROR_SUCCESS)
1843 return PyErr_SetFromWindowsErrWithFunction(rc,
1844 "RegDisableReflectionKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001845 Py_RETURN_NONE;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001846}
1847
Zachary Warefd2d4822015-05-13 01:21:57 -05001848/*[clinic input]
1849winreg.EnableReflectionKey
1850
1851 key: HKEY
1852 An already open key, or any one of the predefined HKEY_* constants.
1853 /
1854
1855Restores registry reflection for the specified disabled key.
1856
David Hed5e8e02019-07-31 23:49:55 +01001857Will generally raise NotImplementedError if executed on a 32bit OS.
Zachary Warefd2d4822015-05-13 01:21:57 -05001858Restoring reflection for a key does not affect reflection of any
1859subkeys.
1860[clinic start generated code]*/
1861
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001862static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001863winreg_EnableReflectionKey_impl(PyObject *module, HKEY key)
David Hed5e8e02019-07-31 23:49:55 +01001864/*[clinic end generated code: output=86fa1385fdd9ce57 input=eeae770c6eb9f559]*/
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001865{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 HMODULE hMod;
1867 typedef LONG (WINAPI *RERKFunc)(HKEY);
1868 RERKFunc pfn = NULL;
1869 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001870
Steve Doweree17e372019-12-09 11:18:12 -08001871 if (PySys_Audit("winreg.EnableReflectionKey", "n", (Py_ssize_t)key) < 0) {
1872 return NULL;
1873 }
1874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 /* Only available on 64bit platforms, so we must load it
1876 dynamically.*/
Tony Roberts4860f012019-02-02 18:16:42 +01001877 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis50590f12012-01-14 17:54:09 +01001878 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 if (hMod)
1880 pfn = (RERKFunc)GetProcAddress(hMod,
1881 "RegEnableReflectionKey");
Tony Roberts4860f012019-02-02 18:16:42 +01001882 Py_END_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 if (!pfn) {
1884 PyErr_SetString(PyExc_NotImplementedError,
1885 "not implemented on this platform");
1886 return NULL;
1887 }
1888 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001889 rc = (*pfn)(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 Py_END_ALLOW_THREADS
1891 if (rc != ERROR_SUCCESS)
1892 return PyErr_SetFromWindowsErrWithFunction(rc,
1893 "RegEnableReflectionKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001894 Py_RETURN_NONE;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001895}
1896
Zachary Warefd2d4822015-05-13 01:21:57 -05001897/*[clinic input]
1898winreg.QueryReflectionKey
1899
1900 key: HKEY
1901 An already open key, or any one of the predefined HKEY_* constants.
1902 /
1903
1904Returns the reflection state for the specified key as a bool.
1905
David Hed5e8e02019-07-31 23:49:55 +01001906Will generally raise NotImplementedError if executed on a 32bit OS.
Zachary Warefd2d4822015-05-13 01:21:57 -05001907[clinic start generated code]*/
1908
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001909static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001910winreg_QueryReflectionKey_impl(PyObject *module, HKEY key)
David Hed5e8e02019-07-31 23:49:55 +01001911/*[clinic end generated code: output=4e774af288c3ebb9 input=a98fa51d55ade186]*/
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001912{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 HMODULE hMod;
1914 typedef LONG (WINAPI *RQRKFunc)(HKEY, BOOL *);
1915 RQRKFunc pfn = NULL;
1916 BOOL result;
1917 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001918
Steve Doweree17e372019-12-09 11:18:12 -08001919 if (PySys_Audit("winreg.QueryReflectionKey", "n", (Py_ssize_t)key) < 0) {
1920 return NULL;
1921 }
1922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 /* Only available on 64bit platforms, so we must load it
1924 dynamically.*/
Tony Roberts4860f012019-02-02 18:16:42 +01001925 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis50590f12012-01-14 17:54:09 +01001926 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927 if (hMod)
1928 pfn = (RQRKFunc)GetProcAddress(hMod,
1929 "RegQueryReflectionKey");
Tony Roberts4860f012019-02-02 18:16:42 +01001930 Py_END_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 if (!pfn) {
1932 PyErr_SetString(PyExc_NotImplementedError,
1933 "not implemented on this platform");
1934 return NULL;
1935 }
1936 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001937 rc = (*pfn)(key, &result);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 Py_END_ALLOW_THREADS
1939 if (rc != ERROR_SUCCESS)
1940 return PyErr_SetFromWindowsErrWithFunction(rc,
1941 "RegQueryReflectionKey");
1942 return PyBool_FromLong(result);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001943}
1944
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001945static struct PyMethodDef winreg_methods[] = {
Zachary Warefd2d4822015-05-13 01:21:57 -05001946 WINREG_CLOSEKEY_METHODDEF
1947 WINREG_CONNECTREGISTRY_METHODDEF
1948 WINREG_CREATEKEY_METHODDEF
1949 WINREG_CREATEKEYEX_METHODDEF
1950 WINREG_DELETEKEY_METHODDEF
1951 WINREG_DELETEKEYEX_METHODDEF
1952 WINREG_DELETEVALUE_METHODDEF
1953 WINREG_DISABLEREFLECTIONKEY_METHODDEF
1954 WINREG_ENABLEREFLECTIONKEY_METHODDEF
1955 WINREG_ENUMKEY_METHODDEF
1956 WINREG_ENUMVALUE_METHODDEF
1957 WINREG_EXPANDENVIRONMENTSTRINGS_METHODDEF
1958 WINREG_FLUSHKEY_METHODDEF
1959 WINREG_LOADKEY_METHODDEF
1960 WINREG_OPENKEY_METHODDEF
1961 WINREG_OPENKEYEX_METHODDEF
1962 WINREG_QUERYVALUE_METHODDEF
1963 WINREG_QUERYVALUEEX_METHODDEF
1964 WINREG_QUERYINFOKEY_METHODDEF
1965 WINREG_QUERYREFLECTIONKEY_METHODDEF
1966 WINREG_SAVEKEY_METHODDEF
1967 WINREG_SETVALUE_METHODDEF
1968 WINREG_SETVALUEEX_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 NULL,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001970};
1971
1972static void
1973insint(PyObject * d, char * name, long value)
1974{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 PyObject *v = PyLong_FromLong(value);
1976 if (!v || PyDict_SetItemString(d, name, v))
1977 PyErr_Clear();
1978 Py_XDECREF(v);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001979}
1980
1981#define ADD_INT(val) insint(d, #val, val)
1982
1983static void
1984inskey(PyObject * d, char * name, HKEY key)
1985{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001986 PyObject *v = PyLong_FromVoidPtr(key);
1987 if (!v || PyDict_SetItemString(d, name, v))
1988 PyErr_Clear();
1989 Py_XDECREF(v);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001990}
1991
1992#define ADD_KEY(val) inskey(d, #val, val)
1993
Martin v. Löwis1a214512008-06-11 05:26:20 +00001994
1995static struct PyModuleDef winregmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 PyModuleDef_HEAD_INIT,
1997 "winreg",
1998 module_doc,
1999 -1,
2000 winreg_methods,
2001 NULL,
2002 NULL,
2003 NULL,
2004 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002005};
2006
2007PyMODINIT_FUNC PyInit_winreg(void)
Guido van Rossum9f3712c2000-03-28 20:37:15 +00002008{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 PyObject *m, *d;
2010 m = PyModule_Create(&winregmodule);
2011 if (m == NULL)
2012 return NULL;
2013 d = PyModule_GetDict(m);
2014 PyHKEY_Type.tp_doc = PyHKEY_doc;
2015 if (PyType_Ready(&PyHKEY_Type) < 0)
2016 return NULL;
2017 Py_INCREF(&PyHKEY_Type);
2018 if (PyDict_SetItemString(d, "HKEYType",
2019 (PyObject *)&PyHKEY_Type) != 0)
2020 return NULL;
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02002021 Py_INCREF(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002022 if (PyDict_SetItemString(d, "error",
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02002023 PyExc_OSError) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00002025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 /* Add the relevant constants */
2027 ADD_KEY(HKEY_CLASSES_ROOT);
2028 ADD_KEY(HKEY_CURRENT_USER);
2029 ADD_KEY(HKEY_LOCAL_MACHINE);
2030 ADD_KEY(HKEY_USERS);
2031 ADD_KEY(HKEY_PERFORMANCE_DATA);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00002032#ifdef HKEY_CURRENT_CONFIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002033 ADD_KEY(HKEY_CURRENT_CONFIG);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00002034#endif
2035#ifdef HKEY_DYN_DATA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002036 ADD_KEY(HKEY_DYN_DATA);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00002037#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 ADD_INT(KEY_QUERY_VALUE);
2039 ADD_INT(KEY_SET_VALUE);
2040 ADD_INT(KEY_CREATE_SUB_KEY);
2041 ADD_INT(KEY_ENUMERATE_SUB_KEYS);
2042 ADD_INT(KEY_NOTIFY);
2043 ADD_INT(KEY_CREATE_LINK);
2044 ADD_INT(KEY_READ);
2045 ADD_INT(KEY_WRITE);
2046 ADD_INT(KEY_EXECUTE);
2047 ADD_INT(KEY_ALL_ACCESS);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00002048#ifdef KEY_WOW64_64KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002049 ADD_INT(KEY_WOW64_64KEY);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00002050#endif
2051#ifdef KEY_WOW64_32KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002052 ADD_INT(KEY_WOW64_32KEY);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00002053#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002054 ADD_INT(REG_OPTION_RESERVED);
2055 ADD_INT(REG_OPTION_NON_VOLATILE);
2056 ADD_INT(REG_OPTION_VOLATILE);
2057 ADD_INT(REG_OPTION_CREATE_LINK);
2058 ADD_INT(REG_OPTION_BACKUP_RESTORE);
2059 ADD_INT(REG_OPTION_OPEN_LINK);
2060 ADD_INT(REG_LEGAL_OPTION);
2061 ADD_INT(REG_CREATED_NEW_KEY);
2062 ADD_INT(REG_OPENED_EXISTING_KEY);
2063 ADD_INT(REG_WHOLE_HIVE_VOLATILE);
2064 ADD_INT(REG_REFRESH_HIVE);
2065 ADD_INT(REG_NO_LAZY_FLUSH);
2066 ADD_INT(REG_NOTIFY_CHANGE_NAME);
2067 ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES);
2068 ADD_INT(REG_NOTIFY_CHANGE_LAST_SET);
2069 ADD_INT(REG_NOTIFY_CHANGE_SECURITY);
2070 ADD_INT(REG_LEGAL_CHANGE_FILTER);
2071 ADD_INT(REG_NONE);
2072 ADD_INT(REG_SZ);
2073 ADD_INT(REG_EXPAND_SZ);
2074 ADD_INT(REG_BINARY);
2075 ADD_INT(REG_DWORD);
2076 ADD_INT(REG_DWORD_LITTLE_ENDIAN);
2077 ADD_INT(REG_DWORD_BIG_ENDIAN);
Steve Dower80ac11d2016-05-24 15:42:04 -07002078 ADD_INT(REG_QWORD);
2079 ADD_INT(REG_QWORD_LITTLE_ENDIAN);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002080 ADD_INT(REG_LINK);
2081 ADD_INT(REG_MULTI_SZ);
2082 ADD_INT(REG_RESOURCE_LIST);
2083 ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
2084 ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
2085 return m;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00002086}
2087
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00002088