blob: d62a7be28d3fabea651bc86c1d5e5af2842adbe9 [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 Stinner04fc4f22020-06-16 01:28:07 +020017#include "pycore_object.h" // _PyObject_Init()
Victor Stinner4a21e572020-04-15 02:35:41 +020018#include "structmember.h" // PyMemberDef
19#include <windows.h>
Guido van Rossum9f3712c2000-03-28 20:37:15 +000020
21static BOOL PyHKEY_AsHKEY(PyObject *ob, HKEY *pRes, BOOL bNoneOK);
Zachary Warefd2d4822015-05-13 01:21:57 -050022static BOOL clinic_HKEY_converter(PyObject *ob, void *p);
Guido van Rossum9f3712c2000-03-28 20:37:15 +000023static PyObject *PyHKEY_FromHKEY(HKEY h);
24static BOOL PyHKEY_Close(PyObject *obHandle);
25
26static char errNotAHandle[] = "Object is not a handle";
27
28/* The win32api module reports the function name that failed,
29 but this concept is not in the Python core.
luzpaza5293b42017-11-05 07:37:50 -060030 Hopefully it will one day, and in the meantime I don't
Guido van Rossum9f3712c2000-03-28 20:37:15 +000031 want to lose this info...
32*/
33#define PyErr_SetFromWindowsErrWithFunction(rc, fnname) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000034 PyErr_SetFromWindowsErr(rc)
Guido van Rossum9f3712c2000-03-28 20:37:15 +000035
36/* Forward declares */
37
38/* Doc strings */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000039PyDoc_STRVAR(module_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +000040"This module provides access to the Windows registry API.\n"
41"\n"
42"Functions:\n"
43"\n"
44"CloseKey() - Closes a registry key.\n"
45"ConnectRegistry() - Establishes a connection to a predefined registry handle\n"
46" on another computer.\n"
47"CreateKey() - Creates the specified key, or opens it if it already exists.\n"
48"DeleteKey() - Deletes the specified key.\n"
49"DeleteValue() - Removes a named value from the specified registry key.\n"
50"EnumKey() - Enumerates subkeys of the specified open registry key.\n"
51"EnumValue() - Enumerates values of the specified open registry key.\n"
Zachary Warefd2d4822015-05-13 01:21:57 -050052"ExpandEnvironmentStrings() - Expand the env strings in a REG_EXPAND_SZ\n"
53" string.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000054"FlushKey() - Writes all the attributes of the specified key to the registry.\n"
Zachary Warefd2d4822015-05-13 01:21:57 -050055"LoadKey() - Creates a subkey under HKEY_USER or HKEY_LOCAL_MACHINE and\n"
56" stores registration information from a specified file into that\n"
57" subkey.\n"
Brian Curtine9aeca72012-10-29 18:16:39 -050058"OpenKey() - Opens the specified key.\n"
59"OpenKeyEx() - Alias of OpenKey().\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000060"QueryValue() - Retrieves the value associated with the unnamed value for a\n"
61" specified key in the registry.\n"
62"QueryValueEx() - Retrieves the type and data for a specified value name\n"
63" associated with an open registry key.\n"
64"QueryInfoKey() - Returns information about the specified key.\n"
65"SaveKey() - Saves the specified key, and all its subkeys a file.\n"
66"SetValue() - Associates a value with a specified key.\n"
67"SetValueEx() - Stores data in the value field of an open registry key.\n"
68"\n"
69"Special objects:\n"
70"\n"
71"HKEYType -- type object for HKEY objects\n"
72"error -- exception raised for Win32 errors\n"
73"\n"
74"Integer constants:\n"
75"Many constants are defined - see the documentation for each function\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000076"to see what constants are used, and where.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +000077
78
Martin v. Löwisd218dc12008-04-07 03:17:54 +000079
Guido van Rossum9f3712c2000-03-28 20:37:15 +000080/* PyHKEY docstrings */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000081PyDoc_STRVAR(PyHKEY_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +000082"PyHKEY Object - A Python object, representing a win32 registry key.\n"
83"\n"
Mark Hammondb422f952000-06-09 06:01:47 +000084"This object wraps a Windows HKEY object, automatically closing it when\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000085"the object is destroyed. To guarantee cleanup, you can call either\n"
86"the Close() method on the PyHKEY, or the CloseKey() method.\n"
87"\n"
oldkaa0735f2018-02-02 16:52:55 +080088"All functions which accept a handle object also accept an integer --\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000089"however, use of the handle object is encouraged.\n"
90"\n"
91"Functions:\n"
92"Close() - Closes the underlying handle.\n"
93"Detach() - Returns the integer Win32 handle, detaching it from the object\n"
94"\n"
95"Properties:\n"
96"handle - The integer Win32 handle.\n"
97"\n"
98"Operations:\n"
Jack Diederich4dafcc42006-11-28 19:15:13 +000099"__bool__ - Handles with an open object return true, otherwise false.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000100"__int__ - Converting a handle to an integer returns the Win32 handle.\n"
Mark Dickinson211c6252009-02-01 10:28:51 +0000101"rich comparison - Handle objects are compared using the handle value.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000102
103
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000104
105/************************************************************************
106
107 The PyHKEY object definition
108
109************************************************************************/
110typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 PyObject_VAR_HEAD
112 HKEY hkey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000113} PyHKEYObject;
114
Victor Stinner8182cc22020-07-10 12:40:38 +0200115#define PyHKEY_Check(op) Py_IS_TYPE(op, &PyHKEY_Type)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000116
117static char *failMsg = "bad operand type";
118
119static PyObject *
120PyHKEY_unaryFailureFunc(PyObject *ob)
121{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 PyErr_SetString(PyExc_TypeError, failMsg);
123 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000124}
125static PyObject *
126PyHKEY_binaryFailureFunc(PyObject *ob1, PyObject *ob2)
127{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 PyErr_SetString(PyExc_TypeError, failMsg);
129 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000130}
131static PyObject *
132PyHKEY_ternaryFailureFunc(PyObject *ob1, PyObject *ob2, PyObject *ob3)
133{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 PyErr_SetString(PyExc_TypeError, failMsg);
135 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000136}
137
138static void
139PyHKEY_deallocFunc(PyObject *ob)
140{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 /* Can not call PyHKEY_Close, as the ob->tp_type
142 has already been cleared, thus causing the type
143 check to fail!
144 */
145 PyHKEYObject *obkey = (PyHKEYObject *)ob;
146 if (obkey->hkey)
147 RegCloseKey((HKEY)obkey->hkey);
Victor Stinner32bd68c2020-12-01 10:37:39 +0100148 PyObject_Free(ob);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000149}
150
151static int
Jack Diederich4dafcc42006-11-28 19:15:13 +0000152PyHKEY_boolFunc(PyObject *ob)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000153{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 return ((PyHKEYObject *)ob)->hkey != 0;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000155}
156
157static PyObject *
158PyHKEY_intFunc(PyObject *ob)
159{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
161 return PyLong_FromVoidPtr(pyhkey->hkey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000162}
163
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000164static PyObject *
165PyHKEY_strFunc(PyObject *ob)
166{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
168 return PyUnicode_FromFormat("<PyHKEY:%p>", pyhkey->hkey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000169}
170
171static int
172PyHKEY_compareFunc(PyObject *ob1, PyObject *ob2)
173{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 PyHKEYObject *pyhkey1 = (PyHKEYObject *)ob1;
175 PyHKEYObject *pyhkey2 = (PyHKEYObject *)ob2;
176 return pyhkey1 == pyhkey2 ? 0 :
177 (pyhkey1 < pyhkey2 ? -1 : 1);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000178}
179
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000180static Py_hash_t
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000181PyHKEY_hashFunc(PyObject *ob)
182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 /* Just use the address.
184 XXX - should we use the handle value?
185 */
186 return _Py_HashPointer(ob);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000187}
188
189
190static PyNumberMethods PyHKEY_NumberMethods =
191{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 PyHKEY_binaryFailureFunc, /* nb_add */
193 PyHKEY_binaryFailureFunc, /* nb_subtract */
194 PyHKEY_binaryFailureFunc, /* nb_multiply */
195 PyHKEY_binaryFailureFunc, /* nb_remainder */
196 PyHKEY_binaryFailureFunc, /* nb_divmod */
197 PyHKEY_ternaryFailureFunc, /* nb_power */
198 PyHKEY_unaryFailureFunc, /* nb_negative */
199 PyHKEY_unaryFailureFunc, /* nb_positive */
200 PyHKEY_unaryFailureFunc, /* nb_absolute */
201 PyHKEY_boolFunc, /* nb_bool */
202 PyHKEY_unaryFailureFunc, /* nb_invert */
203 PyHKEY_binaryFailureFunc, /* nb_lshift */
204 PyHKEY_binaryFailureFunc, /* nb_rshift */
205 PyHKEY_binaryFailureFunc, /* nb_and */
206 PyHKEY_binaryFailureFunc, /* nb_xor */
207 PyHKEY_binaryFailureFunc, /* nb_or */
208 PyHKEY_intFunc, /* nb_int */
209 0, /* nb_reserved */
210 PyHKEY_unaryFailureFunc, /* nb_float */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000211};
212
Zachary Warefd2d4822015-05-13 01:21:57 -0500213/*[clinic input]
214module winreg
215class winreg.HKEYType "PyHKEYObject *" "&PyHKEY_Type"
216[clinic start generated code]*/
217/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4c964eba3bf914d6]*/
218
219/*[python input]
220class REGSAM_converter(CConverter):
221 type = 'REGSAM'
222 format_unit = 'i'
223
224class DWORD_converter(CConverter):
225 type = 'DWORD'
226 format_unit = 'k'
227
228class HKEY_converter(CConverter):
229 type = 'HKEY'
230 converter = 'clinic_HKEY_converter'
231
232class HKEY_return_converter(CReturnConverter):
233 type = 'HKEY'
234
235 def render(self, function, data):
236 self.declare(data)
237 self.err_occurred_if_null_pointer("_return_value", data)
238 data.return_conversion.append(
239 'return_value = PyHKEY_FromHKEY(_return_value);\n')
240
241# HACK: this only works for PyHKEYObjects, nothing else.
242# Should this be generalized and enshrined in clinic.py,
243# destroy this converter with prejudice.
244class self_return_converter(CReturnConverter):
245 type = 'PyHKEYObject *'
246
247 def render(self, function, data):
248 self.declare(data)
249 data.return_conversion.append(
250 'return_value = (PyObject *)_return_value;\n')
251[python start generated code]*/
252/*[python end generated code: output=da39a3ee5e6b4b0d input=22f7aedc6d68e80e]*/
253
254#include "clinic/winreg.c.h"
255
256/************************************************************************
257
258 The PyHKEY object methods
259
260************************************************************************/
261/*[clinic input]
262winreg.HKEYType.Close
263
264Closes the underlying Windows handle.
265
266If the handle is already closed, no error is raised.
267[clinic start generated code]*/
268
269static PyObject *
270winreg_HKEYType_Close_impl(PyHKEYObject *self)
271/*[clinic end generated code: output=fced3a624fb0c344 input=6786ac75f6b89de6]*/
272{
273 if (!PyHKEY_Close((PyObject *)self))
274 return NULL;
275 Py_RETURN_NONE;
276}
277
278/*[clinic input]
279winreg.HKEYType.Detach
280
281Detaches the Windows handle from the handle object.
282
283The result is the value of the handle before it is detached. If the
284handle is already detached, this will return zero.
285
286After calling this function, the handle is effectively invalidated,
287but the handle is not closed. You would call this function when you
288need the underlying win32 handle to exist beyond the lifetime of the
289handle object.
290[clinic start generated code]*/
291
292static PyObject *
293winreg_HKEYType_Detach_impl(PyHKEYObject *self)
294/*[clinic end generated code: output=dda5a9e1a01ae78f input=dd2cc09e6c6ba833]*/
295{
296 void* ret;
Steve Doweree17e372019-12-09 11:18:12 -0800297 if (PySys_Audit("winreg.PyHKEY.Detach", "n", (Py_ssize_t)self->hkey) < 0) {
298 return NULL;
299 }
Zachary Warefd2d4822015-05-13 01:21:57 -0500300 ret = (void*)self->hkey;
301 self->hkey = 0;
302 return PyLong_FromVoidPtr(ret);
303}
304
305/*[clinic input]
306winreg.HKEYType.__enter__ -> self
307[clinic start generated code]*/
308
309static PyHKEYObject *
310winreg_HKEYType___enter___impl(PyHKEYObject *self)
311/*[clinic end generated code: output=52c34986dab28990 input=c40fab1f0690a8e2]*/
312{
313 Py_XINCREF(self);
314 return self;
315}
316
317
318/*[clinic input]
319winreg.HKEYType.__exit__
320
321 exc_type: object
322 exc_value: object
323 traceback: object
324[clinic start generated code]*/
325
326static PyObject *
Zachary Ware77772c02015-05-13 10:58:35 -0500327winreg_HKEYType___exit___impl(PyHKEYObject *self, PyObject *exc_type,
328 PyObject *exc_value, PyObject *traceback)
329/*[clinic end generated code: output=923ebe7389e6a263 input=fb32489ee92403c7]*/
Zachary Warefd2d4822015-05-13 01:21:57 -0500330{
331 if (!PyHKEY_Close((PyObject *)self))
332 return NULL;
333 Py_RETURN_NONE;
334}
335
336/*[clinic input]
337[clinic start generated code]*/
338/*[clinic end generated code: output=da39a3ee5e6b4b0d input=da39a3ee5e6b4b0d]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000339
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000340static struct PyMethodDef PyHKEY_methods[] = {
Zachary Warefd2d4822015-05-13 01:21:57 -0500341 WINREG_HKEYTYPE_CLOSE_METHODDEF
342 WINREG_HKEYTYPE_DETACH_METHODDEF
343 WINREG_HKEYTYPE___ENTER___METHODDEF
344 WINREG_HKEYTYPE___EXIT___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 {NULL}
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000346};
347
348#define OFF(e) offsetof(PyHKEYObject, e)
349static PyMemberDef PyHKEY_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 {"handle", T_INT, OFF(hkey), READONLY},
351 {NULL} /* Sentinel */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000352};
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000353
354/* The type itself */
355PyTypeObject PyHKEY_Type =
356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 PyVarObject_HEAD_INIT(0, 0) /* fill in type at module init */
358 "PyHKEY",
359 sizeof(PyHKEYObject),
360 0,
361 PyHKEY_deallocFunc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200362 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 0, /* tp_getattr */
364 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200365 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 0, /* tp_repr */
367 &PyHKEY_NumberMethods, /* tp_as_number */
368 0, /* tp_as_sequence */
369 0, /* tp_as_mapping */
370 PyHKEY_hashFunc, /* tp_hash */
371 0, /* tp_call */
372 PyHKEY_strFunc, /* tp_str */
373 0, /* tp_getattro */
374 0, /* tp_setattro */
375 0, /* tp_as_buffer */
376 0, /* tp_flags */
377 PyHKEY_doc, /* tp_doc */
378 0, /*tp_traverse*/
379 0, /*tp_clear*/
380 0, /*tp_richcompare*/
381 0, /*tp_weaklistoffset*/
382 0, /*tp_iter*/
383 0, /*tp_iternext*/
384 PyHKEY_methods, /*tp_methods*/
385 PyHKEY_memberlist, /*tp_members*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000386};
387
388/************************************************************************
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000389 The public PyHKEY API (well, not public yet :-)
390************************************************************************/
391PyObject *
392PyHKEY_New(HKEY hInit)
393{
Victor Stinner92055202020-04-08 00:38:15 +0200394 PyHKEYObject *key = PyObject_New(PyHKEYObject, &PyHKEY_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 if (key)
396 key->hkey = hInit;
397 return (PyObject *)key;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000398}
399
400BOOL
401PyHKEY_Close(PyObject *ob_handle)
402{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 LONG rc;
Steve Doweree17e372019-12-09 11:18:12 -0800404 HKEY key;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000405
Steve Doweree17e372019-12-09 11:18:12 -0800406 if (!PyHKEY_AsHKEY(ob_handle, &key, TRUE)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 return FALSE;
408 }
Steve Doweree17e372019-12-09 11:18:12 -0800409 if (PyHKEY_Check(ob_handle)) {
410 ((PyHKEYObject*)ob_handle)->hkey = 0;
411 }
412 rc = key ? RegCloseKey(key) : ERROR_SUCCESS;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 if (rc != ERROR_SUCCESS)
414 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
415 return rc == ERROR_SUCCESS;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000416}
417
418BOOL
419PyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK)
420{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 if (ob == Py_None) {
422 if (!bNoneOK) {
423 PyErr_SetString(
424 PyExc_TypeError,
425 "None is not a valid HKEY in this context");
426 return FALSE;
427 }
428 *pHANDLE = (HKEY)0;
429 }
430 else if (PyHKEY_Check(ob)) {
431 PyHKEYObject *pH = (PyHKEYObject *)ob;
432 *pHANDLE = pH->hkey;
433 }
434 else if (PyLong_Check(ob)) {
435 /* We also support integers */
436 PyErr_Clear();
437 *pHANDLE = (HKEY)PyLong_AsVoidPtr(ob);
438 if (PyErr_Occurred())
439 return FALSE;
440 }
441 else {
442 PyErr_SetString(
443 PyExc_TypeError,
444 "The object is not a PyHKEY object");
445 return FALSE;
446 }
447 return TRUE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000448}
449
Zachary Warefd2d4822015-05-13 01:21:57 -0500450BOOL
451clinic_HKEY_converter(PyObject *ob, void *p)
452{
453 if (!PyHKEY_AsHKEY(ob, (HKEY *)p, FALSE))
454 return FALSE;
455 return TRUE;
456}
457
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000458PyObject *
459PyHKEY_FromHKEY(HKEY h)
460{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 /* Inline PyObject_New */
Victor Stinner32bd68c2020-12-01 10:37:39 +0100462 PyHKEYObject *op = (PyHKEYObject *) PyObject_Malloc(sizeof(PyHKEYObject));
Victor Stinner04fc4f22020-06-16 01:28:07 +0200463 if (op == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 return PyErr_NoMemory();
Victor Stinner04fc4f22020-06-16 01:28:07 +0200465 }
Victor Stinner15edaec2020-08-05 16:23:10 +0200466 _PyObject_Init((PyObject*)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 Py_ssize_t len;
644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 t = PyList_GET_ITEM(value, j);
646 if (!PyUnicode_Check(t))
647 return FALSE;
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +0300648#if USE_UNICODE_WCHAR_CACHE
649_Py_COMP_DIAG_PUSH
650_Py_COMP_DIAG_IGNORE_DEPR_DECLS
651 len = PyUnicode_GetSize(t);
652 if (len < 0)
Victor Stinnerbe492442011-11-21 12:43:50 +0100653 return FALSE;
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +0300654 len++;
655_Py_COMP_DIAG_POP
656#else /* USE_UNICODE_WCHAR_CACHE */
657 len = PyUnicode_AsWideChar(t, NULL, 0);
658 if (len < 0)
659 return FALSE;
660#endif /* USE_UNICODE_WCHAR_CACHE */
661 size += Py_SAFE_DOWNCAST(len * sizeof(wchar_t),
Brian Curtinabb33512010-08-17 20:08:40 +0000662 size_t, DWORD);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 *retDataSize = size + 2;
666 *retDataBuf = (BYTE *)PyMem_NEW(char,
667 *retDataSize);
Steve Dower4d4bc422016-05-25 11:26:07 -0700668 if (*retDataBuf == NULL){
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 PyErr_NoMemory();
670 return FALSE;
671 }
672 P = (wchar_t *)*retDataBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 for (j = 0; j < i; j++)
675 {
676 PyObject *t;
Victor Stinnerbe492442011-11-21 12:43:50 +0100677 Py_ssize_t len;
678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 t = PyList_GET_ITEM(value, j);
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +0300680 assert(size > 0);
681 len = PyUnicode_AsWideChar(t, P, size);
682 assert(len >= 0);
683 assert(len < size);
684 size -= (DWORD)len + 1;
685 P += len + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 }
687 /* And doubly-terminate the list... */
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +0300688 *P = L'\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 break;
690 }
691 case REG_BINARY:
692 /* ALSO handle ALL unknown data types here. Even if we can't
693 support it natively, we should handle the bits. */
694 default:
Zachary Waread4690f2014-07-03 10:58:06 -0500695 if (value == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 *retDataSize = 0;
Zachary Waread4690f2014-07-03 10:58:06 -0500697 *retDataBuf = NULL;
698 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 else {
700 Py_buffer view;
Neal Norwitz1385b892007-08-26 23:07:13 +0000701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 if (!PyObject_CheckBuffer(value)) {
703 PyErr_Format(PyExc_TypeError,
704 "Objects of type '%s' can not "
705 "be used as binary registry values",
Victor Stinner8182cc22020-07-10 12:40:38 +0200706 Py_TYPE(value)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 return FALSE;
708 }
Neal Norwitz1385b892007-08-26 23:07:13 +0000709
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0)
711 return FALSE;
Neal Norwitz1385b892007-08-26 23:07:13 +0000712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 *retDataBuf = (BYTE *)PyMem_NEW(char, view.len);
Steve Dower4d4bc422016-05-25 11:26:07 -0700714 if (*retDataBuf == NULL){
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 PyBuffer_Release(&view);
716 PyErr_NoMemory();
717 return FALSE;
718 }
Brian Curtinabb33512010-08-17 20:08:40 +0000719 *retDataSize = Py_SAFE_DOWNCAST(view.len, Py_ssize_t, DWORD);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 memcpy(*retDataBuf, view.buf, view.len);
721 PyBuffer_Release(&view);
722 }
723 break;
724 }
725 return TRUE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000726}
727
728/* Convert Registry data into PyObject*/
729static PyObject *
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000730Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000731{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 PyObject *obData;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 switch (typ) {
735 case REG_DWORD:
736 if (retDataSize == 0)
Brian Curtin172e4222012-12-27 14:04:42 -0600737 obData = PyLong_FromUnsignedLong(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 else
Steve Dower80ac11d2016-05-24 15:42:04 -0700739 obData = PyLong_FromUnsignedLong(*(DWORD *)retDataBuf);
740 break;
741 case REG_QWORD:
742 if (retDataSize == 0)
743 obData = PyLong_FromUnsignedLongLong(0);
744 else
745 obData = PyLong_FromUnsignedLongLong(*(DWORD64 *)retDataBuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 break;
747 case REG_SZ:
748 case REG_EXPAND_SZ:
749 {
Steve Dower40fa2662016-12-17 13:30:27 -0800750 /* REG_SZ should be a NUL terminated string, but only by
751 * convention. The buffer may have been saved without a NUL
752 * or with embedded NULs. To be consistent with reg.exe and
753 * regedit.exe, consume only up to the first NUL. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 wchar_t *data = (wchar_t *)retDataBuf;
Steve Dower40fa2662016-12-17 13:30:27 -0800755 size_t len = wcsnlen(data, retDataSize / sizeof(wchar_t));
756 obData = PyUnicode_FromWideChar(data, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 break;
758 }
759 case REG_MULTI_SZ:
760 if (retDataSize == 0)
761 obData = PyList_New(0);
762 else
763 {
764 int index = 0;
765 wchar_t *data = (wchar_t *)retDataBuf;
766 int len = retDataSize / 2;
767 int s = countStrings(data, len);
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +0200768 wchar_t **str = PyMem_New(wchar_t *, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 if (str == NULL)
770 return PyErr_NoMemory();
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 fixupMultiSZ(str, data, len);
773 obData = PyList_New(s);
Jesus Cea782c4cf2014-03-13 17:35:32 +0100774 if (obData == NULL) {
Victor Stinner9cb1ec52014-03-13 19:08:10 +0100775 PyMem_Free(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 return NULL;
Jesus Cea782c4cf2014-03-13 17:35:32 +0100777 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 for (index = 0; index < s; index++)
779 {
Zackery Spytze223ba12019-09-09 03:26:15 -0600780 size_t slen = wcsnlen(str[index], len);
781 PyObject *uni = PyUnicode_FromWideChar(str[index], slen);
Zackery Spytz99d56b52018-12-08 07:16:55 -0700782 if (uni == NULL) {
783 Py_DECREF(obData);
784 PyMem_Free(str);
785 return NULL;
786 }
787 PyList_SET_ITEM(obData, index, uni);
Steve Doweref66f312019-09-09 06:24:15 -0700788 len -= Py_SAFE_DOWNCAST(slen + 1, size_t, int);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 }
Victor Stinnerb6404912013-07-07 16:21:41 +0200790 PyMem_Free(str);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 break;
793 }
794 case REG_BINARY:
795 /* ALSO handle ALL unknown data types here. Even if we can't
796 support it natively, we should handle the bits. */
797 default:
798 if (retDataSize == 0) {
799 Py_INCREF(Py_None);
800 obData = Py_None;
801 }
802 else
803 obData = PyBytes_FromStringAndSize(
804 (char *)retDataBuf, retDataSize);
805 break;
806 }
807 return obData;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000808}
809
810/* The Python methods */
811
Zachary Warefd2d4822015-05-13 01:21:57 -0500812/*[clinic input]
813winreg.CloseKey
814
815 hkey: object
816 A previously opened key.
817 /
818
819Closes a previously opened registry key.
820
821Note that if the key is not closed using this method, it will be
822closed when the hkey object is destroyed by Python.
823[clinic start generated code]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000824
825static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300826winreg_CloseKey(PyObject *module, PyObject *hkey)
827/*[clinic end generated code: output=a4fa537019a80d15 input=5b1aac65ba5127ad]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000828{
Zachary Warefd2d4822015-05-13 01:21:57 -0500829 if (!PyHKEY_Close(hkey))
830 return NULL;
831 Py_RETURN_NONE;
832}
833
834/*[clinic input]
835winreg.ConnectRegistry -> HKEY
836
Zachary Ware77772c02015-05-13 10:58:35 -0500837 computer_name: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -0500838 The name of the remote computer, of the form r"\\computername". If
839 None, the local computer is used.
840 key: HKEY
841 The predefined key to connect to.
842 /
843
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +0300844Establishes a connection to the registry on another computer.
Zachary Warefd2d4822015-05-13 01:21:57 -0500845
846The return value is the handle of the opened key.
847If the function fails, an OSError exception is raised.
848[clinic start generated code]*/
849
850static HKEY
Serhiy Storchakaafb3e712018-12-14 11:19:51 +0200851winreg_ConnectRegistry_impl(PyObject *module,
852 const Py_UNICODE *computer_name, HKEY key)
853/*[clinic end generated code: output=cd4f70fb9ec901fb input=5f98a891a347e68e]*/
Zachary Warefd2d4822015-05-13 01:21:57 -0500854{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 HKEY retKey;
856 long rc;
Steve Doweree17e372019-12-09 11:18:12 -0800857 if (PySys_Audit("winreg.ConnectRegistry", "un",
858 computer_name, (Py_ssize_t)key) < 0) {
859 return NULL;
860 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -0500862 rc = RegConnectRegistryW(computer_name, key, &retKey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 Py_END_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -0500864 if (rc != ERROR_SUCCESS) {
865 PyErr_SetFromWindowsErrWithFunction(rc, "ConnectRegistry");
866 return NULL;
867 }
868 return retKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000869}
870
Zachary Warefd2d4822015-05-13 01:21:57 -0500871/*[clinic input]
872winreg.CreateKey -> HKEY
873
874 key: HKEY
875 An already open key, or one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -0500876 sub_key: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -0500877 The name of the key this method opens or creates.
878 /
879
880Creates or opens the specified key.
881
882If key is one of the predefined keys, sub_key may be None. In that case,
883the handle returned is the same key handle passed in to the function.
884
885If the key already exists, this function opens the existing key.
886
887The return value is the handle of the opened key.
888If the function fails, an OSError exception is raised.
889[clinic start generated code]*/
890
891static HKEY
Serhiy Storchakaafb3e712018-12-14 11:19:51 +0200892winreg_CreateKey_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key)
893/*[clinic end generated code: output=2af13910d56eae26 input=3cdd1622488acea2]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000894{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 HKEY retKey;
896 long rc;
Zachary Warefd2d4822015-05-13 01:21:57 -0500897
Steve Doweree17e372019-12-09 11:18:12 -0800898 if (PySys_Audit("winreg.CreateKey", "nun",
899 (Py_ssize_t)key, sub_key,
900 (Py_ssize_t)KEY_WRITE) < 0) {
901 return NULL;
902 }
Zachary Warefd2d4822015-05-13 01:21:57 -0500903 rc = RegCreateKeyW(key, sub_key, &retKey);
904 if (rc != ERROR_SUCCESS) {
905 PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 return NULL;
Zachary Warefd2d4822015-05-13 01:21:57 -0500907 }
Steve Doweree17e372019-12-09 11:18:12 -0800908 if (PySys_Audit("winreg.OpenKey/result", "n",
909 (Py_ssize_t)retKey) < 0) {
910 return NULL;
911 }
Zachary Warefd2d4822015-05-13 01:21:57 -0500912 return retKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000913}
914
Zachary Warefd2d4822015-05-13 01:21:57 -0500915/*[clinic input]
916winreg.CreateKeyEx -> HKEY
917
918 key: HKEY
919 An already open key, or one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -0500920 sub_key: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -0500921 The name of the key this method opens or creates.
922 reserved: int = 0
923 A reserved integer, and must be zero. Default is zero.
924 access: REGSAM(c_default='KEY_WRITE') = winreg.KEY_WRITE
925 An integer that specifies an access mask that describes the
926 desired security access for the key. Default is KEY_WRITE.
927
928Creates or opens the specified key.
929
930If key is one of the predefined keys, sub_key may be None. In that case,
931the handle returned is the same key handle passed in to the function.
932
933If the key already exists, this function opens the existing key
934
935The return value is the handle of the opened key.
936If the function fails, an OSError exception is raised.
937[clinic start generated code]*/
938
939static HKEY
Serhiy Storchakaafb3e712018-12-14 11:19:51 +0200940winreg_CreateKeyEx_impl(PyObject *module, HKEY key,
941 const Py_UNICODE *sub_key, int reserved,
942 REGSAM access)
943/*[clinic end generated code: output=643a70ad6a361a97 input=42c2b03f98406b66]*/
Brian Curtin3035c392010-04-21 23:56:21 +0000944{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 HKEY retKey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 long rc;
Brian Curtin1771b542010-09-27 17:56:36 +0000947
Steve Doweree17e372019-12-09 11:18:12 -0800948 if (PySys_Audit("winreg.CreateKey", "nun",
949 (Py_ssize_t)key, sub_key,
950 (Py_ssize_t)access) < 0) {
951 return NULL;
952 }
Segev Finer679b5662017-07-27 01:17:57 +0300953 rc = RegCreateKeyExW(key, sub_key, reserved, NULL, 0,
Brian Curtin1771b542010-09-27 17:56:36 +0000954 access, NULL, &retKey, NULL);
Zachary Warefd2d4822015-05-13 01:21:57 -0500955 if (rc != ERROR_SUCCESS) {
956 PyErr_SetFromWindowsErrWithFunction(rc, "CreateKeyEx");
957 return NULL;
958 }
Steve Doweree17e372019-12-09 11:18:12 -0800959 if (PySys_Audit("winreg.OpenKey/result", "n",
960 (Py_ssize_t)retKey) < 0) {
961 return NULL;
962 }
Zachary Warefd2d4822015-05-13 01:21:57 -0500963 return retKey;
Brian Curtin3035c392010-04-21 23:56:21 +0000964}
965
Zachary Warefd2d4822015-05-13 01:21:57 -0500966/*[clinic input]
967winreg.DeleteKey
968 key: HKEY
969 An already open key, or any one of the predefined HKEY_* constants.
970 sub_key: Py_UNICODE
971 A string that must be the name of a subkey of the key identified by
972 the key parameter. This value must not be None, and the key may not
973 have subkeys.
974 /
975
976Deletes the specified key.
977
978This method can not delete keys with subkeys.
979
980If the function succeeds, the entire key, including all of its values,
981is removed. If the function fails, an OSError exception is raised.
982[clinic start generated code]*/
983
Brian Curtin3035c392010-04-21 23:56:21 +0000984static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +0200985winreg_DeleteKey_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key)
986/*[clinic end generated code: output=d2652a84f70e0862 input=b31d225b935e4211]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000987{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 long rc;
Steve Doweree17e372019-12-09 11:18:12 -0800989 if (PySys_Audit("winreg.DeleteKey", "nun",
990 (Py_ssize_t)key, sub_key,
991 (Py_ssize_t)0) < 0) {
992 return NULL;
993 }
Zachary Warefd2d4822015-05-13 01:21:57 -0500994 rc = RegDeleteKeyW(key, sub_key );
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 if (rc != ERROR_SUCCESS)
996 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey");
Zachary Warefd2d4822015-05-13 01:21:57 -0500997 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000998}
999
Zachary Warefd2d4822015-05-13 01:21:57 -05001000/*[clinic input]
1001winreg.DeleteKeyEx
1002
1003 key: HKEY
1004 An already open key, or any one of the predefined HKEY_* constants.
1005 sub_key: Py_UNICODE
1006 A string that must be the name of a subkey of the key identified by
1007 the key parameter. This value must not be None, and the key may not
1008 have subkeys.
1009 access: REGSAM(c_default='KEY_WOW64_64KEY') = winreg.KEY_WOW64_64KEY
1010 An integer that specifies an access mask that describes the
1011 desired security access for the key. Default is KEY_WOW64_64KEY.
1012 reserved: int = 0
1013 A reserved integer, and must be zero. Default is zero.
1014
1015Deletes the specified key (64-bit OS only).
1016
1017This method can not delete keys with subkeys.
1018
1019If the function succeeds, the entire key, including all of its values,
1020is removed. If the function fails, an OSError exception is raised.
1021On unsupported Windows versions, NotImplementedError is raised.
1022[clinic start generated code]*/
1023
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001024static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001025winreg_DeleteKeyEx_impl(PyObject *module, HKEY key,
1026 const Py_UNICODE *sub_key, REGSAM access,
1027 int reserved)
1028/*[clinic end generated code: output=52a1c8b374ebc003 input=711d9d89e7ecbed7]*/
Brian Curtin3035c392010-04-21 23:56:21 +00001029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 HMODULE hMod;
1031 typedef LONG (WINAPI *RDKEFunc)(HKEY, const wchar_t*, REGSAM, int);
1032 RDKEFunc pfn = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 long rc;
Brian Curtin3035c392010-04-21 23:56:21 +00001034
Steve Doweree17e372019-12-09 11:18:12 -08001035 if (PySys_Audit("winreg.DeleteKey", "nun",
1036 (Py_ssize_t)key, sub_key,
1037 (Py_ssize_t)access) < 0) {
1038 return NULL;
1039 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 /* Only available on 64bit platforms, so we must load it
1041 dynamically. */
Tony Roberts4860f012019-02-02 18:16:42 +01001042 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis50590f12012-01-14 17:54:09 +01001043 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 if (hMod)
Steve Doweree17e372019-12-09 11:18:12 -08001045 pfn = (RDKEFunc)GetProcAddress(hMod, "RegDeleteKeyExW");
Tony Roberts4860f012019-02-02 18:16:42 +01001046 Py_END_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 if (!pfn) {
1048 PyErr_SetString(PyExc_NotImplementedError,
1049 "not implemented on this platform");
1050 return NULL;
1051 }
1052 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001053 rc = (*pfn)(key, sub_key, access, reserved);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 Py_END_ALLOW_THREADS
Brian Curtin3035c392010-04-21 23:56:21 +00001055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 if (rc != ERROR_SUCCESS)
1057 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKeyEx");
Zachary Warefd2d4822015-05-13 01:21:57 -05001058 Py_RETURN_NONE;
Brian Curtin3035c392010-04-21 23:56:21 +00001059}
1060
Zachary Warefd2d4822015-05-13 01:21:57 -05001061/*[clinic input]
1062winreg.DeleteValue
1063
1064 key: HKEY
1065 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001066 value: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001067 A string that identifies the value to remove.
1068 /
1069
1070Removes a named value from a registry key.
1071[clinic start generated code]*/
1072
Brian Curtin3035c392010-04-21 23:56:21 +00001073static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001074winreg_DeleteValue_impl(PyObject *module, HKEY key, const Py_UNICODE *value)
1075/*[clinic end generated code: output=56fa9d21f3a54371 input=a78d3407a4197b21]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001076{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 long rc;
Steve Doweree17e372019-12-09 11:18:12 -08001078 if (PySys_Audit("winreg.DeleteValue", "nu",
1079 (Py_ssize_t)key, value) < 0) {
1080 return NULL;
1081 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001083 rc = RegDeleteValueW(key, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 Py_END_ALLOW_THREADS
1085 if (rc !=ERROR_SUCCESS)
1086 return PyErr_SetFromWindowsErrWithFunction(rc,
1087 "RegDeleteValue");
Zachary Warefd2d4822015-05-13 01:21:57 -05001088 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001089}
1090
Zachary Warefd2d4822015-05-13 01:21:57 -05001091/*[clinic input]
1092winreg.EnumKey
1093
1094 key: HKEY
1095 An already open key, or any one of the predefined HKEY_* constants.
1096 index: int
1097 An integer that identifies the index of the key to retrieve.
1098 /
1099
1100Enumerates subkeys of an open registry key.
1101
1102The function retrieves the name of one subkey each time it is called.
1103It is typically called repeatedly until an OSError exception is
1104raised, indicating no more values are available.
1105[clinic start generated code]*/
1106
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001107static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001108winreg_EnumKey_impl(PyObject *module, HKEY key, int index)
1109/*[clinic end generated code: output=25a6ec52cd147bc4 input=fad9a7c00ab0e04b]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001110{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 long rc;
1112 PyObject *retStr;
Brian Curtin60853212010-05-26 17:43:50 +00001113
Steve Doweree17e372019-12-09 11:18:12 -08001114 if (PySys_Audit("winreg.EnumKey", "ni",
1115 (Py_ssize_t)key, index) < 0) {
1116 return NULL;
1117 }
Brian Curtin60853212010-05-26 17:43:50 +00001118 /* The Windows docs claim that the max key name length is 255
1119 * characters, plus a terminating nul character. However,
1120 * empirical testing demonstrates that it is possible to
1121 * create a 256 character key that is missing the terminating
1122 * nul. RegEnumKeyEx requires a 257 character buffer to
1123 * retrieve such a key name. */
1124 wchar_t tmpbuf[257];
Kristján Valur Jónsson984dfa72012-04-02 15:23:29 +00001125 DWORD len = sizeof(tmpbuf)/sizeof(wchar_t); /* includes NULL terminator */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001128 rc = RegEnumKeyExW(key, index, tmpbuf, &len, NULL, NULL, NULL, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 Py_END_ALLOW_THREADS
1130 if (rc != ERROR_SUCCESS)
1131 return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx");
Tim Peters313fcd42006-02-19 04:05:39 +00001132
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001133 retStr = PyUnicode_FromWideChar(tmpbuf, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 return retStr; /* can be NULL */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001135}
1136
Zachary Warefd2d4822015-05-13 01:21:57 -05001137/*[clinic input]
1138winreg.EnumValue
1139
1140 key: HKEY
1141 An already open key, or any one of the predefined HKEY_* constants.
1142 index: int
1143 An integer that identifies the index of the value to retrieve.
1144 /
1145
1146Enumerates values of an open registry key.
1147
1148The function retrieves the name of one subkey each time it is called.
1149It is typically called repeatedly, until an OSError exception
1150is raised, indicating no more values.
1151
1152The result is a tuple of 3 items:
1153 value_name
1154 A string that identifies the value.
1155 value_data
1156 An object that holds the value data, and whose type depends
1157 on the underlying registry type.
1158 data_type
1159 An integer that identifies the type of the value data.
1160[clinic start generated code]*/
1161
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001162static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001163winreg_EnumValue_impl(PyObject *module, HKEY key, int index)
1164/*[clinic end generated code: output=d363b5a06f8789ac input=4414f47a6fb238b5]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 long rc;
1167 wchar_t *retValueBuf;
Amaury Forgeot d'Arcf2b69df2010-08-16 22:11:29 +00001168 BYTE *tmpBuf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 BYTE *retDataBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001170 DWORD retValueSize, bufValueSize;
1171 DWORD retDataSize, bufDataSize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 DWORD typ;
1173 PyObject *obData;
1174 PyObject *retVal;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001175
Steve Doweree17e372019-12-09 11:18:12 -08001176 if (PySys_Audit("winreg.EnumValue", "ni",
1177 (Py_ssize_t)key, index) < 0) {
1178 return NULL;
1179 }
Zachary Warefd2d4822015-05-13 01:21:57 -05001180 if ((rc = RegQueryInfoKeyW(key, NULL, NULL, NULL, NULL, NULL, NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 NULL,
1182 &retValueSize, &retDataSize, NULL, NULL))
1183 != ERROR_SUCCESS)
1184 return PyErr_SetFromWindowsErrWithFunction(rc,
1185 "RegQueryInfoKey");
1186 ++retValueSize; /* include null terminators */
1187 ++retDataSize;
Brian Curtin60853212010-05-26 17:43:50 +00001188 bufDataSize = retDataSize;
1189 bufValueSize = retValueSize;
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02001190 retValueBuf = PyMem_New(wchar_t, retValueSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 if (retValueBuf == NULL)
1192 return PyErr_NoMemory();
1193 retDataBuf = (BYTE *)PyMem_Malloc(retDataSize);
1194 if (retDataBuf == NULL) {
1195 PyMem_Free(retValueBuf);
1196 return PyErr_NoMemory();
1197 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001198
Brian Curtin60853212010-05-26 17:43:50 +00001199 while (1) {
Brian Curtin60853212010-05-26 17:43:50 +00001200 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001201 rc = RegEnumValueW(key,
Brian Curtin60853212010-05-26 17:43:50 +00001202 index,
1203 retValueBuf,
1204 &retValueSize,
1205 NULL,
1206 &typ,
1207 (BYTE *)retDataBuf,
1208 &retDataSize);
1209 Py_END_ALLOW_THREADS
1210
1211 if (rc != ERROR_MORE_DATA)
1212 break;
1213
1214 bufDataSize *= 2;
Amaury Forgeot d'Arcf2b69df2010-08-16 22:11:29 +00001215 tmpBuf = (BYTE *)PyMem_Realloc(retDataBuf, bufDataSize);
Brian Curtin9b7e2d12010-06-08 20:57:52 +00001216 if (tmpBuf == NULL) {
Brian Curtin60853212010-05-26 17:43:50 +00001217 PyErr_NoMemory();
1218 retVal = NULL;
1219 goto fail;
1220 }
Brian Curtin9b7e2d12010-06-08 20:57:52 +00001221 retDataBuf = tmpBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001222 retDataSize = bufDataSize;
1223 retValueSize = bufValueSize;
1224 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 if (rc != ERROR_SUCCESS) {
1227 retVal = PyErr_SetFromWindowsErrWithFunction(rc,
1228 "PyRegEnumValue");
1229 goto fail;
1230 }
1231 obData = Reg2Py(retDataBuf, retDataSize, typ);
1232 if (obData == NULL) {
1233 retVal = NULL;
1234 goto fail;
1235 }
1236 retVal = Py_BuildValue("uOi", retValueBuf, obData, typ);
1237 Py_DECREF(obData);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001238 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 PyMem_Free(retValueBuf);
1240 PyMem_Free(retDataBuf);
1241 return retVal;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001242}
1243
Zachary Warefd2d4822015-05-13 01:21:57 -05001244/*[clinic input]
1245winreg.ExpandEnvironmentStrings
1246
1247 string: Py_UNICODE
1248 /
1249
1250Expand environment vars.
1251[clinic start generated code]*/
1252
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001253static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001254winreg_ExpandEnvironmentStrings_impl(PyObject *module,
1255 const Py_UNICODE *string)
1256/*[clinic end generated code: output=8fa4e959747a7312 input=b2a9714d2b751aa6]*/
Christian Heimes2380ac72008-01-09 00:17:24 +00001257{
Victor Stinner9d3b93b2011-11-22 02:27:30 +01001258 wchar_t *retValue = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 DWORD retValueSize;
1260 DWORD rc;
1261 PyObject *o;
Christian Heimes2380ac72008-01-09 00:17:24 +00001262
Steve Doweree17e372019-12-09 11:18:12 -08001263 if (PySys_Audit("winreg.ExpandEnvironmentStrings", "u",
1264 string) < 0) {
1265 return NULL;
1266 }
1267
Zachary Warefd2d4822015-05-13 01:21:57 -05001268 retValueSize = ExpandEnvironmentStringsW(string, retValue, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 if (retValueSize == 0) {
1270 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1271 "ExpandEnvironmentStrings");
1272 }
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02001273 retValue = PyMem_New(wchar_t, retValueSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 if (retValue == NULL) {
1275 return PyErr_NoMemory();
1276 }
Christian Heimes2380ac72008-01-09 00:17:24 +00001277
Zachary Warefd2d4822015-05-13 01:21:57 -05001278 rc = ExpandEnvironmentStringsW(string, retValue, retValueSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 if (rc == 0) {
1280 PyMem_Free(retValue);
1281 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1282 "ExpandEnvironmentStrings");
1283 }
Victor Stinner9d3b93b2011-11-22 02:27:30 +01001284 o = PyUnicode_FromWideChar(retValue, wcslen(retValue));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 PyMem_Free(retValue);
1286 return o;
Christian Heimes2380ac72008-01-09 00:17:24 +00001287}
1288
Zachary Warefd2d4822015-05-13 01:21:57 -05001289/*[clinic input]
1290winreg.FlushKey
1291
1292 key: HKEY
1293 An already open key, or any one of the predefined HKEY_* constants.
1294 /
1295
1296Writes all the attributes of a key to the registry.
1297
1298It is not necessary to call FlushKey to change a key. Registry changes
1299are flushed to disk by the registry using its lazy flusher. Registry
1300changes are also flushed to disk at system shutdown. Unlike
1301CloseKey(), the FlushKey() method returns only when all the data has
1302been written to the registry.
1303
1304An application should only call FlushKey() if it requires absolute
1305certainty that registry changes are on disk. If you don't know whether
1306a FlushKey() call is required, it probably isn't.
1307[clinic start generated code]*/
1308
Christian Heimes2380ac72008-01-09 00:17:24 +00001309static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001310winreg_FlushKey_impl(PyObject *module, HKEY key)
1311/*[clinic end generated code: output=e6fc230d4c5dc049 input=f57457c12297d82f]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 long rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001315 rc = RegFlushKey(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 Py_END_ALLOW_THREADS
1317 if (rc != ERROR_SUCCESS)
1318 return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001319 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001320}
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001321
Zachary Warefd2d4822015-05-13 01:21:57 -05001322
1323/*[clinic input]
1324winreg.LoadKey
1325
1326 key: HKEY
1327 An already open key, or any one of the predefined HKEY_* constants.
1328 sub_key: Py_UNICODE
1329 A string that identifies the sub-key to load.
1330 file_name: Py_UNICODE
1331 The name of the file to load registry data from. This file must
1332 have been created with the SaveKey() function. Under the file
1333 allocation table (FAT) file system, the filename may not have an
1334 extension.
1335 /
1336
1337Insert data into the registry from a file.
1338
1339Creates a subkey under the specified key and stores registration
1340information from a specified file into that subkey.
1341
1342A call to LoadKey() fails if the calling process does not have the
1343SE_RESTORE_PRIVILEGE privilege.
1344
1345If key is a handle returned by ConnectRegistry(), then the path
1346specified in fileName is relative to the remote computer.
1347
1348The MSDN docs imply key must be in the HKEY_USER or HKEY_LOCAL_MACHINE
1349tree.
1350[clinic start generated code]*/
1351
1352static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001353winreg_LoadKey_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key,
1354 const Py_UNICODE *file_name)
1355/*[clinic end generated code: output=65f89f2548cb27c7 input=e3b5b45ade311582]*/
Zachary Warefd2d4822015-05-13 01:21:57 -05001356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 long rc;
Zachary Warefd2d4822015-05-13 01:21:57 -05001358
Steve Doweree17e372019-12-09 11:18:12 -08001359 if (PySys_Audit("winreg.LoadKey", "nuu",
1360 (Py_ssize_t)key, sub_key, file_name) < 0) {
1361 return NULL;
1362 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001364 rc = RegLoadKeyW(key, sub_key, file_name );
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 Py_END_ALLOW_THREADS
1366 if (rc != ERROR_SUCCESS)
1367 return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001368 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001369}
1370
Zachary Warefd2d4822015-05-13 01:21:57 -05001371/*[clinic input]
1372winreg.OpenKey -> HKEY
1373
1374 key: HKEY
1375 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001376 sub_key: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001377 A string that identifies the sub_key to open.
1378 reserved: int = 0
1379 A reserved integer that must be zero. Default is zero.
1380 access: REGSAM(c_default='KEY_READ') = winreg.KEY_READ
1381 An integer that specifies an access mask that describes the desired
1382 security access for the key. Default is KEY_READ.
1383
1384Opens the specified key.
1385
1386The result is a new handle to the specified key.
1387If the function fails, an OSError exception is raised.
1388[clinic start generated code]*/
1389
1390static HKEY
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001391winreg_OpenKey_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key,
Zachary Ware77772c02015-05-13 10:58:35 -05001392 int reserved, REGSAM access)
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001393/*[clinic end generated code: output=8849bff2c30104ad input=098505ac36a9ae28]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 HKEY retKey;
1396 long rc;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001397
Steve Doweree17e372019-12-09 11:18:12 -08001398 if (PySys_Audit("winreg.OpenKey", "nun",
1399 (Py_ssize_t)key, sub_key,
1400 (Py_ssize_t)access) < 0) {
1401 return NULL;
1402 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001404 rc = RegOpenKeyExW(key, sub_key, reserved, access, &retKey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 Py_END_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001406 if (rc != ERROR_SUCCESS) {
1407 PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx");
1408 return NULL;
1409 }
Steve Doweree17e372019-12-09 11:18:12 -08001410 if (PySys_Audit("winreg.OpenKey/result", "n",
1411 (Py_ssize_t)retKey) < 0) {
1412 return NULL;
1413 }
Zachary Warefd2d4822015-05-13 01:21:57 -05001414 return retKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001415}
1416
Zachary Warefd2d4822015-05-13 01:21:57 -05001417/*[clinic input]
1418winreg.OpenKeyEx = winreg.OpenKey
1419
1420Opens the specified key.
1421
1422The result is a new handle to the specified key.
1423If the function fails, an OSError exception is raised.
1424[clinic start generated code]*/
1425
1426static HKEY
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001427winreg_OpenKeyEx_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key,
Zachary Ware77772c02015-05-13 10:58:35 -05001428 int reserved, REGSAM access)
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001429/*[clinic end generated code: output=81bc2bd684bc77ae input=c6c4972af8622959]*/
Zachary Warefd2d4822015-05-13 01:21:57 -05001430{
1431 return winreg_OpenKey_impl(module, key, sub_key, reserved, access);
1432}
1433
1434/*[clinic input]
1435winreg.QueryInfoKey
1436
1437 key: HKEY
1438 An already open key, or any one of the predefined HKEY_* constants.
1439 /
1440
1441Returns information about a key.
1442
1443The result is a tuple of 3 items:
1444An integer that identifies the number of sub keys this key has.
1445An integer that identifies the number of values this key has.
1446An integer that identifies when the key was last modified (if available)
1447as 100's of nanoseconds since Jan 1, 1600.
1448[clinic start generated code]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001449
1450static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001451winreg_QueryInfoKey_impl(PyObject *module, HKEY key)
1452/*[clinic end generated code: output=dc657b8356a4f438 input=c3593802390cde1f]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001453{
Steve Doweree17e372019-12-09 11:18:12 -08001454 long rc;
1455 DWORD nSubKeys, nValues;
1456 FILETIME ft;
1457 LARGE_INTEGER li;
1458 PyObject *l;
1459 PyObject *ret;
Zachary Warefd2d4822015-05-13 01:21:57 -05001460
Steve Doweree17e372019-12-09 11:18:12 -08001461 if (PySys_Audit("winreg.QueryInfoKey", "n", (Py_ssize_t)key) < 0) {
1462 return NULL;
1463 }
Minmin Gong98e42d12020-05-18 09:50:03 -07001464 if ((rc = RegQueryInfoKeyW(key, NULL, NULL, 0, &nSubKeys, NULL, NULL,
1465 &nValues, NULL, NULL, NULL, &ft))
1466 != ERROR_SUCCESS) {
Steve Doweree17e372019-12-09 11:18:12 -08001467 return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey");
1468 }
1469 li.LowPart = ft.dwLowDateTime;
1470 li.HighPart = ft.dwHighDateTime;
1471 l = PyLong_FromLongLong(li.QuadPart);
1472 if (l == NULL) {
1473 return NULL;
1474 }
1475 ret = Py_BuildValue("iiO", nSubKeys, nValues, l);
1476 Py_DECREF(l);
1477 return ret;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001478}
1479
Zachary Warefd2d4822015-05-13 01:21:57 -05001480/*[clinic input]
1481winreg.QueryValue
1482
1483 key: HKEY
1484 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001485 sub_key: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001486 A string that holds the name of the subkey with which the value
1487 is associated. If this parameter is None or empty, the function
1488 retrieves the value set by the SetValue() method for the key
1489 identified by key.
1490 /
1491
1492Retrieves the unnamed value for a key.
1493
1494Values in the registry have name, type, and data components. This method
1495retrieves the data for a key's first value that has a NULL name.
1496But since the underlying API call doesn't return the type, you'll
1497probably be happier using QueryValueEx; this function is just here for
1498completeness.
1499[clinic start generated code]*/
1500
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001501static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001502winreg_QueryValue_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key)
1503/*[clinic end generated code: output=c655810ae50c63a9 input=41cafbbf423b21d6]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001504{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 long rc;
1506 PyObject *retStr;
1507 wchar_t *retBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001508 DWORD bufSize = 0;
1509 DWORD retSize = 0;
1510 wchar_t *tmp;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001511
Steve Doweree17e372019-12-09 11:18:12 -08001512 if (PySys_Audit("winreg.QueryValue", "nuu",
1513 (Py_ssize_t)key, sub_key, NULL) < 0) {
1514 return NULL;
1515 }
Zachary Warefd2d4822015-05-13 01:21:57 -05001516 rc = RegQueryValueW(key, sub_key, NULL, &retSize);
Brian Curtin60853212010-05-26 17:43:50 +00001517 if (rc == ERROR_MORE_DATA)
1518 retSize = 256;
1519 else if (rc != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 return PyErr_SetFromWindowsErrWithFunction(rc,
1521 "RegQueryValue");
Brian Curtin60853212010-05-26 17:43:50 +00001522
1523 bufSize = retSize;
1524 retBuf = (wchar_t *) PyMem_Malloc(bufSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 if (retBuf == NULL)
1526 return PyErr_NoMemory();
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001527
Brian Curtin60853212010-05-26 17:43:50 +00001528 while (1) {
1529 retSize = bufSize;
Zachary Warefd2d4822015-05-13 01:21:57 -05001530 rc = RegQueryValueW(key, sub_key, retBuf, &retSize);
Brian Curtin60853212010-05-26 17:43:50 +00001531 if (rc != ERROR_MORE_DATA)
1532 break;
1533
1534 bufSize *= 2;
1535 tmp = (wchar_t *) PyMem_Realloc(retBuf, bufSize);
1536 if (tmp == NULL) {
1537 PyMem_Free(retBuf);
1538 return PyErr_NoMemory();
1539 }
1540 retBuf = tmp;
1541 }
1542
1543 if (rc != ERROR_SUCCESS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 PyMem_Free(retBuf);
1545 return PyErr_SetFromWindowsErrWithFunction(rc,
1546 "RegQueryValue");
1547 }
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001548
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001549 retStr = PyUnicode_FromWideChar(retBuf, wcslen(retBuf));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 PyMem_Free(retBuf);
1551 return retStr;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001552}
1553
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001554
Zachary Warefd2d4822015-05-13 01:21:57 -05001555/*[clinic input]
1556winreg.QueryValueEx
1557
1558 key: HKEY
1559 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001560 name: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001561 A string indicating the value to query.
1562 /
1563
1564Retrieves the type and value of a specified sub-key.
1565
1566Behaves mostly like QueryValue(), but also returns the type of the
1567specified value name associated with the given open registry key.
1568
1569The return value is a tuple of the value and the type_id.
1570[clinic start generated code]*/
1571
1572static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001573winreg_QueryValueEx_impl(PyObject *module, HKEY key, const Py_UNICODE *name)
1574/*[clinic end generated code: output=f1b85b1c3d887ec7 input=cf366cada4836891]*/
Zachary Warefd2d4822015-05-13 01:21:57 -05001575{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 long rc;
Brian Curtin60853212010-05-26 17:43:50 +00001577 BYTE *retBuf, *tmp;
1578 DWORD bufSize = 0, retSize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 DWORD typ;
1580 PyObject *obData;
1581 PyObject *result;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001582
Steve Doweree17e372019-12-09 11:18:12 -08001583 if (PySys_Audit("winreg.QueryValue", "nuu",
1584 (Py_ssize_t)key, NULL, name) < 0) {
1585 return NULL;
1586 }
Zachary Warefd2d4822015-05-13 01:21:57 -05001587 rc = RegQueryValueExW(key, name, NULL, NULL, NULL, &bufSize);
Brian Curtin60853212010-05-26 17:43:50 +00001588 if (rc == ERROR_MORE_DATA)
1589 bufSize = 256;
1590 else if (rc != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 return PyErr_SetFromWindowsErrWithFunction(rc,
1592 "RegQueryValueEx");
1593 retBuf = (BYTE *)PyMem_Malloc(bufSize);
1594 if (retBuf == NULL)
1595 return PyErr_NoMemory();
Brian Curtin60853212010-05-26 17:43:50 +00001596
1597 while (1) {
1598 retSize = bufSize;
Zachary Warefd2d4822015-05-13 01:21:57 -05001599 rc = RegQueryValueExW(key, name, NULL, &typ,
Brian Curtin60853212010-05-26 17:43:50 +00001600 (BYTE *)retBuf, &retSize);
1601 if (rc != ERROR_MORE_DATA)
1602 break;
1603
1604 bufSize *= 2;
1605 tmp = (char *) PyMem_Realloc(retBuf, bufSize);
1606 if (tmp == NULL) {
1607 PyMem_Free(retBuf);
1608 return PyErr_NoMemory();
1609 }
1610 retBuf = tmp;
1611 }
1612
1613 if (rc != ERROR_SUCCESS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 PyMem_Free(retBuf);
1615 return PyErr_SetFromWindowsErrWithFunction(rc,
1616 "RegQueryValueEx");
1617 }
1618 obData = Reg2Py(retBuf, bufSize, typ);
1619 PyMem_Free(retBuf);
1620 if (obData == NULL)
1621 return NULL;
1622 result = Py_BuildValue("Oi", obData, typ);
1623 Py_DECREF(obData);
1624 return result;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001625}
1626
Zachary Warefd2d4822015-05-13 01:21:57 -05001627/*[clinic input]
1628winreg.SaveKey
1629
1630 key: HKEY
1631 An already open key, or any one of the predefined HKEY_* constants.
1632 file_name: Py_UNICODE
1633 The name of the file to save registry data to. This file cannot
1634 already exist. If this filename includes an extension, it cannot be
1635 used on file allocation table (FAT) file systems by the LoadKey(),
1636 ReplaceKey() or RestoreKey() methods.
1637 /
1638
1639Saves the specified key, and all its subkeys to the specified file.
1640
1641If key represents a key on a remote computer, the path described by
1642file_name is relative to the remote computer.
1643
1644The caller of this method must possess the SeBackupPrivilege
1645security privilege. This function passes NULL for security_attributes
1646to the API.
1647[clinic start generated code]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001648
1649static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001650winreg_SaveKey_impl(PyObject *module, HKEY key, const Py_UNICODE *file_name)
1651/*[clinic end generated code: output=ca94b835c88f112b input=da735241f91ac7a2]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 LPSECURITY_ATTRIBUTES pSA = NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 long rc;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001656/* One day we may get security into the core?
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE))
1658 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001659*/
Steve Doweree17e372019-12-09 11:18:12 -08001660 if (PySys_Audit("winreg.SaveKey", "nu",
1661 (Py_ssize_t)key, file_name) < 0) {
1662 return NULL;
1663 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001665 rc = RegSaveKeyW(key, file_name, pSA );
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 Py_END_ALLOW_THREADS
1667 if (rc != ERROR_SUCCESS)
1668 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001669 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001670}
1671
Zachary Warefd2d4822015-05-13 01:21:57 -05001672/*[clinic input]
1673winreg.SetValue
1674
1675 key: HKEY
1676 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001677 sub_key: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001678 A string that names the subkey with which the value is associated.
1679 type: DWORD
1680 An integer that specifies the type of the data. Currently this must
1681 be REG_SZ, meaning only strings are supported.
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +03001682 value as value_obj: unicode
Zachary Warefd2d4822015-05-13 01:21:57 -05001683 A string that specifies the new value.
1684 /
1685
1686Associates a value with a specified key.
1687
1688If the key specified by the sub_key parameter does not exist, the
1689SetValue function creates it.
1690
1691Value lengths are limited by available memory. Long values (more than
16922048 bytes) should be stored as files with the filenames stored in
1693the configuration registry to help the registry perform efficiently.
1694
1695The key identified by the key parameter must have been opened with
1696KEY_SET_VALUE access.
1697[clinic start generated code]*/
1698
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001699static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001700winreg_SetValue_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key,
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +03001701 DWORD type, PyObject *value_obj)
1702/*[clinic end generated code: output=d4773dc9c372311a input=bf088494ae2d24fd]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001703{
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +03001704 Py_ssize_t value_length;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 long rc;
Zachary Warefd2d4822015-05-13 01:21:57 -05001706
1707 if (type != REG_SZ) {
Inada Naokicc60cdd92019-03-20 20:53:08 +09001708 PyErr_SetString(PyExc_TypeError, "type must be winreg.REG_SZ");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 return NULL;
1710 }
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +03001711
1712#if USE_UNICODE_WCHAR_CACHE
1713_Py_COMP_DIAG_PUSH
1714_Py_COMP_DIAG_IGNORE_DEPR_DECLS
1715 const wchar_t *value = PyUnicode_AsUnicodeAndSize(value_obj, &value_length);
1716_Py_COMP_DIAG_POP
1717#else /* USE_UNICODE_WCHAR_CACHE */
1718 wchar_t *value = PyUnicode_AsWideCharString(value_obj, &value_length);
1719#endif /* USE_UNICODE_WCHAR_CACHE */
1720 if (value == NULL) {
1721 return NULL;
1722 }
1723 if ((Py_ssize_t)(DWORD)value_length != value_length) {
Inada Naokicc60cdd92019-03-20 20:53:08 +09001724 PyErr_SetString(PyExc_OverflowError, "value is too long");
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +03001725#if !USE_UNICODE_WCHAR_CACHE
1726 PyMem_Free(value);
1727#endif /* USE_UNICODE_WCHAR_CACHE */
Inada Naokid5f18a62019-03-20 19:10:17 +09001728 return NULL;
1729 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001730
Steve Doweree17e372019-12-09 11:18:12 -08001731 if (PySys_Audit("winreg.SetValue", "nunu#",
1732 (Py_ssize_t)key, sub_key, (Py_ssize_t)type,
1733 value, value_length) < 0) {
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +03001734#if !USE_UNICODE_WCHAR_CACHE
1735 PyMem_Free(value);
1736#endif /* USE_UNICODE_WCHAR_CACHE */
Steve Doweree17e372019-12-09 11:18:12 -08001737 return NULL;
1738 }
1739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 Py_BEGIN_ALLOW_THREADS
Zackery Spytz34366b72019-04-22 11:08:05 -06001741 rc = RegSetValueW(key, sub_key, REG_SZ, value, (DWORD)(value_length + 1));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 Py_END_ALLOW_THREADS
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +03001743#if !USE_UNICODE_WCHAR_CACHE
1744 PyMem_Free(value);
1745#endif /* USE_UNICODE_WCHAR_CACHE */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 if (rc != ERROR_SUCCESS)
1747 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue");
Zachary Warefd2d4822015-05-13 01:21:57 -05001748 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001749}
1750
Zachary Warefd2d4822015-05-13 01:21:57 -05001751/*[clinic input]
1752winreg.SetValueEx
1753
1754 key: HKEY
1755 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001756 value_name: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001757 A string containing the name of the value to set, or None.
1758 reserved: object
1759 Can be anything - zero is always passed to the API.
1760 type: DWORD
1761 An integer that specifies the type of the data, one of:
1762 REG_BINARY -- Binary data in any form.
1763 REG_DWORD -- A 32-bit number.
Steve Dower80ac11d2016-05-24 15:42:04 -07001764 REG_DWORD_LITTLE_ENDIAN -- A 32-bit number in little-endian format. Equivalent to REG_DWORD
Zachary Warefd2d4822015-05-13 01:21:57 -05001765 REG_DWORD_BIG_ENDIAN -- A 32-bit number in big-endian format.
1766 REG_EXPAND_SZ -- A null-terminated string that contains unexpanded
1767 references to environment variables (for example,
1768 %PATH%).
1769 REG_LINK -- A Unicode symbolic link.
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03001770 REG_MULTI_SZ -- A sequence of null-terminated strings, terminated
Zachary Warefd2d4822015-05-13 01:21:57 -05001771 by two null characters. Note that Python handles
1772 this termination automatically.
1773 REG_NONE -- No defined value type.
Steve Dower80ac11d2016-05-24 15:42:04 -07001774 REG_QWORD -- A 64-bit number.
1775 REG_QWORD_LITTLE_ENDIAN -- A 64-bit number in little-endian format. Equivalent to REG_QWORD.
Zachary Warefd2d4822015-05-13 01:21:57 -05001776 REG_RESOURCE_LIST -- A device-driver resource list.
1777 REG_SZ -- A null-terminated string.
1778 value: object
1779 A string that specifies the new value.
1780 /
1781
1782Stores data in the value field of an open registry key.
1783
1784This method can also set additional value and type information for the
1785specified key. The key identified by the key parameter must have been
1786opened with KEY_SET_VALUE access.
1787
1788To open the key, use the CreateKeyEx() or OpenKeyEx() methods.
1789
1790Value lengths are limited by available memory. Long values (more than
17912048 bytes) should be stored as files with the filenames stored in
1792the configuration registry to help the registry perform efficiently.
1793[clinic start generated code]*/
1794
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001795static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001796winreg_SetValueEx_impl(PyObject *module, HKEY key,
1797 const Py_UNICODE *value_name, PyObject *reserved,
1798 DWORD type, PyObject *value)
1799/*[clinic end generated code: output=811b769a66ae11b7 input=900a9e3990bfb196]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001800{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 BYTE *data;
1802 DWORD len;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001804 LONG rc;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001805
Zachary Warefd2d4822015-05-13 01:21:57 -05001806 if (!Py2Reg(value, type, &data, &len))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 {
1808 if (!PyErr_Occurred())
1809 PyErr_SetString(PyExc_ValueError,
1810 "Could not convert the data to the specified type.");
1811 return NULL;
1812 }
Steve Doweree17e372019-12-09 11:18:12 -08001813 if (PySys_Audit("winreg.SetValue", "nunO",
1814 (Py_ssize_t)key, value_name, (Py_ssize_t)type,
1815 value) < 0) {
1816 return NULL;
1817 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001819 rc = RegSetValueExW(key, value_name, 0, type, data, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001820 Py_END_ALLOW_THREADS
Victor Stinner00d7abd2020-12-01 09:56:42 +01001821 PyMem_Free(data);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 if (rc != ERROR_SUCCESS)
1823 return PyErr_SetFromWindowsErrWithFunction(rc,
1824 "RegSetValueEx");
Zachary Warefd2d4822015-05-13 01:21:57 -05001825 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001826}
1827
Zachary Warefd2d4822015-05-13 01:21:57 -05001828/*[clinic input]
1829winreg.DisableReflectionKey
1830
1831 key: HKEY
1832 An already open key, or any one of the predefined HKEY_* constants.
1833 /
1834
1835Disables registry reflection for 32bit processes running on a 64bit OS.
1836
David Hed5e8e02019-07-31 23:49:55 +01001837Will generally raise NotImplementedError if executed on a 32bit OS.
Zachary Warefd2d4822015-05-13 01:21:57 -05001838
1839If the key is not on the reflection list, the function succeeds but has
1840no effect. Disabling reflection for a key does not affect reflection
1841of any subkeys.
1842[clinic start generated code]*/
1843
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001844static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001845winreg_DisableReflectionKey_impl(PyObject *module, HKEY key)
David Hed5e8e02019-07-31 23:49:55 +01001846/*[clinic end generated code: output=830cce504cc764b4 input=70bece2dee02e073]*/
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001847{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848 HMODULE hMod;
1849 typedef LONG (WINAPI *RDRKFunc)(HKEY);
1850 RDRKFunc pfn = NULL;
1851 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001852
Steve Doweree17e372019-12-09 11:18:12 -08001853 if (PySys_Audit("winreg.DisableReflectionKey", "n", (Py_ssize_t)key) < 0) {
1854 return NULL;
1855 }
1856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 /* Only available on 64bit platforms, so we must load it
1858 dynamically.*/
Tony Roberts4860f012019-02-02 18:16:42 +01001859 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis50590f12012-01-14 17:54:09 +01001860 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001861 if (hMod)
1862 pfn = (RDRKFunc)GetProcAddress(hMod,
1863 "RegDisableReflectionKey");
Tony Roberts4860f012019-02-02 18:16:42 +01001864 Py_END_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 if (!pfn) {
1866 PyErr_SetString(PyExc_NotImplementedError,
1867 "not implemented on this platform");
1868 return NULL;
1869 }
1870 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001871 rc = (*pfn)(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 Py_END_ALLOW_THREADS
1873 if (rc != ERROR_SUCCESS)
1874 return PyErr_SetFromWindowsErrWithFunction(rc,
1875 "RegDisableReflectionKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001876 Py_RETURN_NONE;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001877}
1878
Zachary Warefd2d4822015-05-13 01:21:57 -05001879/*[clinic input]
1880winreg.EnableReflectionKey
1881
1882 key: HKEY
1883 An already open key, or any one of the predefined HKEY_* constants.
1884 /
1885
1886Restores registry reflection for the specified disabled key.
1887
David Hed5e8e02019-07-31 23:49:55 +01001888Will generally raise NotImplementedError if executed on a 32bit OS.
Zachary Warefd2d4822015-05-13 01:21:57 -05001889Restoring reflection for a key does not affect reflection of any
1890subkeys.
1891[clinic start generated code]*/
1892
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001893static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001894winreg_EnableReflectionKey_impl(PyObject *module, HKEY key)
David Hed5e8e02019-07-31 23:49:55 +01001895/*[clinic end generated code: output=86fa1385fdd9ce57 input=eeae770c6eb9f559]*/
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001896{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897 HMODULE hMod;
1898 typedef LONG (WINAPI *RERKFunc)(HKEY);
1899 RERKFunc pfn = NULL;
1900 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001901
Steve Doweree17e372019-12-09 11:18:12 -08001902 if (PySys_Audit("winreg.EnableReflectionKey", "n", (Py_ssize_t)key) < 0) {
1903 return NULL;
1904 }
1905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 /* Only available on 64bit platforms, so we must load it
1907 dynamically.*/
Tony Roberts4860f012019-02-02 18:16:42 +01001908 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis50590f12012-01-14 17:54:09 +01001909 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 if (hMod)
1911 pfn = (RERKFunc)GetProcAddress(hMod,
1912 "RegEnableReflectionKey");
Tony Roberts4860f012019-02-02 18:16:42 +01001913 Py_END_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 if (!pfn) {
1915 PyErr_SetString(PyExc_NotImplementedError,
1916 "not implemented on this platform");
1917 return NULL;
1918 }
1919 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001920 rc = (*pfn)(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 Py_END_ALLOW_THREADS
1922 if (rc != ERROR_SUCCESS)
1923 return PyErr_SetFromWindowsErrWithFunction(rc,
1924 "RegEnableReflectionKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001925 Py_RETURN_NONE;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001926}
1927
Zachary Warefd2d4822015-05-13 01:21:57 -05001928/*[clinic input]
1929winreg.QueryReflectionKey
1930
1931 key: HKEY
1932 An already open key, or any one of the predefined HKEY_* constants.
1933 /
1934
1935Returns the reflection state for the specified key as a bool.
1936
David Hed5e8e02019-07-31 23:49:55 +01001937Will generally raise NotImplementedError if executed on a 32bit OS.
Zachary Warefd2d4822015-05-13 01:21:57 -05001938[clinic start generated code]*/
1939
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001940static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001941winreg_QueryReflectionKey_impl(PyObject *module, HKEY key)
David Hed5e8e02019-07-31 23:49:55 +01001942/*[clinic end generated code: output=4e774af288c3ebb9 input=a98fa51d55ade186]*/
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001943{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 HMODULE hMod;
1945 typedef LONG (WINAPI *RQRKFunc)(HKEY, BOOL *);
1946 RQRKFunc pfn = NULL;
1947 BOOL result;
1948 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001949
Steve Doweree17e372019-12-09 11:18:12 -08001950 if (PySys_Audit("winreg.QueryReflectionKey", "n", (Py_ssize_t)key) < 0) {
1951 return NULL;
1952 }
1953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 /* Only available on 64bit platforms, so we must load it
1955 dynamically.*/
Tony Roberts4860f012019-02-02 18:16:42 +01001956 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis50590f12012-01-14 17:54:09 +01001957 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 if (hMod)
1959 pfn = (RQRKFunc)GetProcAddress(hMod,
1960 "RegQueryReflectionKey");
Tony Roberts4860f012019-02-02 18:16:42 +01001961 Py_END_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 if (!pfn) {
1963 PyErr_SetString(PyExc_NotImplementedError,
1964 "not implemented on this platform");
1965 return NULL;
1966 }
1967 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001968 rc = (*pfn)(key, &result);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 Py_END_ALLOW_THREADS
1970 if (rc != ERROR_SUCCESS)
1971 return PyErr_SetFromWindowsErrWithFunction(rc,
1972 "RegQueryReflectionKey");
1973 return PyBool_FromLong(result);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001974}
1975
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001976static struct PyMethodDef winreg_methods[] = {
Zachary Warefd2d4822015-05-13 01:21:57 -05001977 WINREG_CLOSEKEY_METHODDEF
1978 WINREG_CONNECTREGISTRY_METHODDEF
1979 WINREG_CREATEKEY_METHODDEF
1980 WINREG_CREATEKEYEX_METHODDEF
1981 WINREG_DELETEKEY_METHODDEF
1982 WINREG_DELETEKEYEX_METHODDEF
1983 WINREG_DELETEVALUE_METHODDEF
1984 WINREG_DISABLEREFLECTIONKEY_METHODDEF
1985 WINREG_ENABLEREFLECTIONKEY_METHODDEF
1986 WINREG_ENUMKEY_METHODDEF
1987 WINREG_ENUMVALUE_METHODDEF
1988 WINREG_EXPANDENVIRONMENTSTRINGS_METHODDEF
1989 WINREG_FLUSHKEY_METHODDEF
1990 WINREG_LOADKEY_METHODDEF
1991 WINREG_OPENKEY_METHODDEF
1992 WINREG_OPENKEYEX_METHODDEF
1993 WINREG_QUERYVALUE_METHODDEF
1994 WINREG_QUERYVALUEEX_METHODDEF
1995 WINREG_QUERYINFOKEY_METHODDEF
1996 WINREG_QUERYREFLECTIONKEY_METHODDEF
1997 WINREG_SAVEKEY_METHODDEF
1998 WINREG_SETVALUE_METHODDEF
1999 WINREG_SETVALUEEX_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 NULL,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00002001};
2002
2003static void
2004insint(PyObject * d, char * name, long value)
2005{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 PyObject *v = PyLong_FromLong(value);
2007 if (!v || PyDict_SetItemString(d, name, v))
2008 PyErr_Clear();
2009 Py_XDECREF(v);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00002010}
2011
2012#define ADD_INT(val) insint(d, #val, val)
2013
2014static void
2015inskey(PyObject * d, char * name, HKEY key)
2016{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 PyObject *v = PyLong_FromVoidPtr(key);
2018 if (!v || PyDict_SetItemString(d, name, v))
2019 PyErr_Clear();
2020 Py_XDECREF(v);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00002021}
2022
2023#define ADD_KEY(val) inskey(d, #val, val)
2024
Martin v. Löwis1a214512008-06-11 05:26:20 +00002025
2026static struct PyModuleDef winregmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 PyModuleDef_HEAD_INIT,
2028 "winreg",
2029 module_doc,
2030 -1,
2031 winreg_methods,
2032 NULL,
2033 NULL,
2034 NULL,
2035 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002036};
2037
2038PyMODINIT_FUNC PyInit_winreg(void)
Guido van Rossum9f3712c2000-03-28 20:37:15 +00002039{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002040 PyObject *m, *d;
2041 m = PyModule_Create(&winregmodule);
2042 if (m == NULL)
2043 return NULL;
2044 d = PyModule_GetDict(m);
2045 PyHKEY_Type.tp_doc = PyHKEY_doc;
2046 if (PyType_Ready(&PyHKEY_Type) < 0)
2047 return NULL;
2048 Py_INCREF(&PyHKEY_Type);
2049 if (PyDict_SetItemString(d, "HKEYType",
2050 (PyObject *)&PyHKEY_Type) != 0)
2051 return NULL;
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02002052 Py_INCREF(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 if (PyDict_SetItemString(d, "error",
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02002054 PyExc_OSError) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00002056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 /* Add the relevant constants */
2058 ADD_KEY(HKEY_CLASSES_ROOT);
2059 ADD_KEY(HKEY_CURRENT_USER);
2060 ADD_KEY(HKEY_LOCAL_MACHINE);
2061 ADD_KEY(HKEY_USERS);
2062 ADD_KEY(HKEY_PERFORMANCE_DATA);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00002063#ifdef HKEY_CURRENT_CONFIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002064 ADD_KEY(HKEY_CURRENT_CONFIG);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00002065#endif
2066#ifdef HKEY_DYN_DATA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 ADD_KEY(HKEY_DYN_DATA);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00002068#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 ADD_INT(KEY_QUERY_VALUE);
2070 ADD_INT(KEY_SET_VALUE);
2071 ADD_INT(KEY_CREATE_SUB_KEY);
2072 ADD_INT(KEY_ENUMERATE_SUB_KEYS);
2073 ADD_INT(KEY_NOTIFY);
2074 ADD_INT(KEY_CREATE_LINK);
2075 ADD_INT(KEY_READ);
2076 ADD_INT(KEY_WRITE);
2077 ADD_INT(KEY_EXECUTE);
2078 ADD_INT(KEY_ALL_ACCESS);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00002079#ifdef KEY_WOW64_64KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002080 ADD_INT(KEY_WOW64_64KEY);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00002081#endif
2082#ifdef KEY_WOW64_32KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083 ADD_INT(KEY_WOW64_32KEY);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00002084#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 ADD_INT(REG_OPTION_RESERVED);
2086 ADD_INT(REG_OPTION_NON_VOLATILE);
2087 ADD_INT(REG_OPTION_VOLATILE);
2088 ADD_INT(REG_OPTION_CREATE_LINK);
2089 ADD_INT(REG_OPTION_BACKUP_RESTORE);
2090 ADD_INT(REG_OPTION_OPEN_LINK);
2091 ADD_INT(REG_LEGAL_OPTION);
2092 ADD_INT(REG_CREATED_NEW_KEY);
2093 ADD_INT(REG_OPENED_EXISTING_KEY);
2094 ADD_INT(REG_WHOLE_HIVE_VOLATILE);
2095 ADD_INT(REG_REFRESH_HIVE);
2096 ADD_INT(REG_NO_LAZY_FLUSH);
2097 ADD_INT(REG_NOTIFY_CHANGE_NAME);
2098 ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES);
2099 ADD_INT(REG_NOTIFY_CHANGE_LAST_SET);
2100 ADD_INT(REG_NOTIFY_CHANGE_SECURITY);
2101 ADD_INT(REG_LEGAL_CHANGE_FILTER);
2102 ADD_INT(REG_NONE);
2103 ADD_INT(REG_SZ);
2104 ADD_INT(REG_EXPAND_SZ);
2105 ADD_INT(REG_BINARY);
2106 ADD_INT(REG_DWORD);
2107 ADD_INT(REG_DWORD_LITTLE_ENDIAN);
2108 ADD_INT(REG_DWORD_BIG_ENDIAN);
Steve Dower80ac11d2016-05-24 15:42:04 -07002109 ADD_INT(REG_QWORD);
2110 ADD_INT(REG_QWORD_LITTLE_ENDIAN);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002111 ADD_INT(REG_LINK);
2112 ADD_INT(REG_MULTI_SZ);
2113 ADD_INT(REG_RESOURCE_LIST);
2114 ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
2115 ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
2116 return m;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00002117}
2118
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00002119