blob: 72a7c380beefe1eecbf2a93f80610f7093188dbf [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"
17#include "structmember.h"
Guido van Rossume7ba4952007-06-06 23:52:48 +000018#include "windows.h"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000019
20static BOOL PyHKEY_AsHKEY(PyObject *ob, HKEY *pRes, BOOL bNoneOK);
Zachary Warefd2d4822015-05-13 01:21:57 -050021static BOOL clinic_HKEY_converter(PyObject *ob, void *p);
Guido van Rossum9f3712c2000-03-28 20:37:15 +000022static PyObject *PyHKEY_FromHKEY(HKEY h);
23static BOOL PyHKEY_Close(PyObject *obHandle);
24
25static char errNotAHandle[] = "Object is not a handle";
26
27/* The win32api module reports the function name that failed,
28 but this concept is not in the Python core.
luzpaza5293b42017-11-05 07:37:50 -060029 Hopefully it will one day, and in the meantime I don't
Guido van Rossum9f3712c2000-03-28 20:37:15 +000030 want to lose this info...
31*/
32#define PyErr_SetFromWindowsErrWithFunction(rc, fnname) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000033 PyErr_SetFromWindowsErr(rc)
Guido van Rossum9f3712c2000-03-28 20:37:15 +000034
35/* Forward declares */
36
37/* Doc strings */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000038PyDoc_STRVAR(module_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +000039"This module provides access to the Windows registry API.\n"
40"\n"
41"Functions:\n"
42"\n"
43"CloseKey() - Closes a registry key.\n"
44"ConnectRegistry() - Establishes a connection to a predefined registry handle\n"
45" on another computer.\n"
46"CreateKey() - Creates the specified key, or opens it if it already exists.\n"
47"DeleteKey() - Deletes the specified key.\n"
48"DeleteValue() - Removes a named value from the specified registry key.\n"
49"EnumKey() - Enumerates subkeys of the specified open registry key.\n"
50"EnumValue() - Enumerates values of the specified open registry key.\n"
Zachary Warefd2d4822015-05-13 01:21:57 -050051"ExpandEnvironmentStrings() - Expand the env strings in a REG_EXPAND_SZ\n"
52" string.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000053"FlushKey() - Writes all the attributes of the specified key to the registry.\n"
Zachary Warefd2d4822015-05-13 01:21:57 -050054"LoadKey() - Creates a subkey under HKEY_USER or HKEY_LOCAL_MACHINE and\n"
55" stores registration information from a specified file into that\n"
56" subkey.\n"
Brian Curtine9aeca72012-10-29 18:16:39 -050057"OpenKey() - Opens the specified key.\n"
58"OpenKeyEx() - Alias of OpenKey().\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000059"QueryValue() - Retrieves the value associated with the unnamed value for a\n"
60" specified key in the registry.\n"
61"QueryValueEx() - Retrieves the type and data for a specified value name\n"
62" associated with an open registry key.\n"
63"QueryInfoKey() - Returns information about the specified key.\n"
64"SaveKey() - Saves the specified key, and all its subkeys a file.\n"
65"SetValue() - Associates a value with a specified key.\n"
66"SetValueEx() - Stores data in the value field of an open registry key.\n"
67"\n"
68"Special objects:\n"
69"\n"
70"HKEYType -- type object for HKEY objects\n"
71"error -- exception raised for Win32 errors\n"
72"\n"
73"Integer constants:\n"
74"Many constants are defined - see the documentation for each function\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000075"to see what constants are used, and where.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +000076
77
Martin v. Löwisd218dc12008-04-07 03:17:54 +000078
Guido van Rossum9f3712c2000-03-28 20:37:15 +000079/* PyHKEY docstrings */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000080PyDoc_STRVAR(PyHKEY_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +000081"PyHKEY Object - A Python object, representing a win32 registry key.\n"
82"\n"
Mark Hammondb422f952000-06-09 06:01:47 +000083"This object wraps a Windows HKEY object, automatically closing it when\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000084"the object is destroyed. To guarantee cleanup, you can call either\n"
85"the Close() method on the PyHKEY, or the CloseKey() method.\n"
86"\n"
oldkaa0735f2018-02-02 16:52:55 +080087"All functions which accept a handle object also accept an integer --\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000088"however, use of the handle object is encouraged.\n"
89"\n"
90"Functions:\n"
91"Close() - Closes the underlying handle.\n"
92"Detach() - Returns the integer Win32 handle, detaching it from the object\n"
93"\n"
94"Properties:\n"
95"handle - The integer Win32 handle.\n"
96"\n"
97"Operations:\n"
Jack Diederich4dafcc42006-11-28 19:15:13 +000098"__bool__ - Handles with an open object return true, otherwise false.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000099"__int__ - Converting a handle to an integer returns the Win32 handle.\n"
Mark Dickinson211c6252009-02-01 10:28:51 +0000100"rich comparison - Handle objects are compared using the handle value.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000101
102
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000103
104/************************************************************************
105
106 The PyHKEY object definition
107
108************************************************************************/
109typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 PyObject_VAR_HEAD
111 HKEY hkey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000112} PyHKEYObject;
113
114#define PyHKEY_Check(op) ((op)->ob_type == &PyHKEY_Type)
115
116static char *failMsg = "bad operand type";
117
118static PyObject *
119PyHKEY_unaryFailureFunc(PyObject *ob)
120{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 PyErr_SetString(PyExc_TypeError, failMsg);
122 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000123}
124static PyObject *
125PyHKEY_binaryFailureFunc(PyObject *ob1, PyObject *ob2)
126{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 PyErr_SetString(PyExc_TypeError, failMsg);
128 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000129}
130static PyObject *
131PyHKEY_ternaryFailureFunc(PyObject *ob1, PyObject *ob2, PyObject *ob3)
132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 PyErr_SetString(PyExc_TypeError, failMsg);
134 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000135}
136
137static void
138PyHKEY_deallocFunc(PyObject *ob)
139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 /* Can not call PyHKEY_Close, as the ob->tp_type
141 has already been cleared, thus causing the type
142 check to fail!
143 */
144 PyHKEYObject *obkey = (PyHKEYObject *)ob;
145 if (obkey->hkey)
146 RegCloseKey((HKEY)obkey->hkey);
147 PyObject_DEL(ob);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000148}
149
150static int
Jack Diederich4dafcc42006-11-28 19:15:13 +0000151PyHKEY_boolFunc(PyObject *ob)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000152{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 return ((PyHKEYObject *)ob)->hkey != 0;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000154}
155
156static PyObject *
157PyHKEY_intFunc(PyObject *ob)
158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
160 return PyLong_FromVoidPtr(pyhkey->hkey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000161}
162
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000163static PyObject *
164PyHKEY_strFunc(PyObject *ob)
165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
167 return PyUnicode_FromFormat("<PyHKEY:%p>", pyhkey->hkey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000168}
169
170static int
171PyHKEY_compareFunc(PyObject *ob1, PyObject *ob2)
172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 PyHKEYObject *pyhkey1 = (PyHKEYObject *)ob1;
174 PyHKEYObject *pyhkey2 = (PyHKEYObject *)ob2;
175 return pyhkey1 == pyhkey2 ? 0 :
176 (pyhkey1 < pyhkey2 ? -1 : 1);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000177}
178
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000179static Py_hash_t
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000180PyHKEY_hashFunc(PyObject *ob)
181{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000182 /* Just use the address.
183 XXX - should we use the handle value?
184 */
185 return _Py_HashPointer(ob);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000186}
187
188
189static PyNumberMethods PyHKEY_NumberMethods =
190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 PyHKEY_binaryFailureFunc, /* nb_add */
192 PyHKEY_binaryFailureFunc, /* nb_subtract */
193 PyHKEY_binaryFailureFunc, /* nb_multiply */
194 PyHKEY_binaryFailureFunc, /* nb_remainder */
195 PyHKEY_binaryFailureFunc, /* nb_divmod */
196 PyHKEY_ternaryFailureFunc, /* nb_power */
197 PyHKEY_unaryFailureFunc, /* nb_negative */
198 PyHKEY_unaryFailureFunc, /* nb_positive */
199 PyHKEY_unaryFailureFunc, /* nb_absolute */
200 PyHKEY_boolFunc, /* nb_bool */
201 PyHKEY_unaryFailureFunc, /* nb_invert */
202 PyHKEY_binaryFailureFunc, /* nb_lshift */
203 PyHKEY_binaryFailureFunc, /* nb_rshift */
204 PyHKEY_binaryFailureFunc, /* nb_and */
205 PyHKEY_binaryFailureFunc, /* nb_xor */
206 PyHKEY_binaryFailureFunc, /* nb_or */
207 PyHKEY_intFunc, /* nb_int */
208 0, /* nb_reserved */
209 PyHKEY_unaryFailureFunc, /* nb_float */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000210};
211
Zachary Warefd2d4822015-05-13 01:21:57 -0500212/*[clinic input]
213module winreg
214class winreg.HKEYType "PyHKEYObject *" "&PyHKEY_Type"
215[clinic start generated code]*/
216/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4c964eba3bf914d6]*/
217
218/*[python input]
219class REGSAM_converter(CConverter):
220 type = 'REGSAM'
221 format_unit = 'i'
222
223class DWORD_converter(CConverter):
224 type = 'DWORD'
225 format_unit = 'k'
226
227class HKEY_converter(CConverter):
228 type = 'HKEY'
229 converter = 'clinic_HKEY_converter'
230
231class HKEY_return_converter(CReturnConverter):
232 type = 'HKEY'
233
234 def render(self, function, data):
235 self.declare(data)
236 self.err_occurred_if_null_pointer("_return_value", data)
237 data.return_conversion.append(
238 'return_value = PyHKEY_FromHKEY(_return_value);\n')
239
240# HACK: this only works for PyHKEYObjects, nothing else.
241# Should this be generalized and enshrined in clinic.py,
242# destroy this converter with prejudice.
243class self_return_converter(CReturnConverter):
244 type = 'PyHKEYObject *'
245
246 def render(self, function, data):
247 self.declare(data)
248 data.return_conversion.append(
249 'return_value = (PyObject *)_return_value;\n')
250[python start generated code]*/
251/*[python end generated code: output=da39a3ee5e6b4b0d input=22f7aedc6d68e80e]*/
252
253#include "clinic/winreg.c.h"
254
255/************************************************************************
256
257 The PyHKEY object methods
258
259************************************************************************/
260/*[clinic input]
261winreg.HKEYType.Close
262
263Closes the underlying Windows handle.
264
265If the handle is already closed, no error is raised.
266[clinic start generated code]*/
267
268static PyObject *
269winreg_HKEYType_Close_impl(PyHKEYObject *self)
270/*[clinic end generated code: output=fced3a624fb0c344 input=6786ac75f6b89de6]*/
271{
272 if (!PyHKEY_Close((PyObject *)self))
273 return NULL;
274 Py_RETURN_NONE;
275}
276
277/*[clinic input]
278winreg.HKEYType.Detach
279
280Detaches the Windows handle from the handle object.
281
282The result is the value of the handle before it is detached. If the
283handle is already detached, this will return zero.
284
285After calling this function, the handle is effectively invalidated,
286but the handle is not closed. You would call this function when you
287need the underlying win32 handle to exist beyond the lifetime of the
288handle object.
289[clinic start generated code]*/
290
291static PyObject *
292winreg_HKEYType_Detach_impl(PyHKEYObject *self)
293/*[clinic end generated code: output=dda5a9e1a01ae78f input=dd2cc09e6c6ba833]*/
294{
295 void* ret;
296 ret = (void*)self->hkey;
297 self->hkey = 0;
298 return PyLong_FromVoidPtr(ret);
299}
300
301/*[clinic input]
302winreg.HKEYType.__enter__ -> self
303[clinic start generated code]*/
304
305static PyHKEYObject *
306winreg_HKEYType___enter___impl(PyHKEYObject *self)
307/*[clinic end generated code: output=52c34986dab28990 input=c40fab1f0690a8e2]*/
308{
309 Py_XINCREF(self);
310 return self;
311}
312
313
314/*[clinic input]
315winreg.HKEYType.__exit__
316
317 exc_type: object
318 exc_value: object
319 traceback: object
320[clinic start generated code]*/
321
322static PyObject *
Zachary Ware77772c02015-05-13 10:58:35 -0500323winreg_HKEYType___exit___impl(PyHKEYObject *self, PyObject *exc_type,
324 PyObject *exc_value, PyObject *traceback)
325/*[clinic end generated code: output=923ebe7389e6a263 input=fb32489ee92403c7]*/
Zachary Warefd2d4822015-05-13 01:21:57 -0500326{
327 if (!PyHKEY_Close((PyObject *)self))
328 return NULL;
329 Py_RETURN_NONE;
330}
331
332/*[clinic input]
333[clinic start generated code]*/
334/*[clinic end generated code: output=da39a3ee5e6b4b0d input=da39a3ee5e6b4b0d]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000335
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000336static struct PyMethodDef PyHKEY_methods[] = {
Zachary Warefd2d4822015-05-13 01:21:57 -0500337 WINREG_HKEYTYPE_CLOSE_METHODDEF
338 WINREG_HKEYTYPE_DETACH_METHODDEF
339 WINREG_HKEYTYPE___ENTER___METHODDEF
340 WINREG_HKEYTYPE___EXIT___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 {NULL}
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000342};
343
344#define OFF(e) offsetof(PyHKEYObject, e)
345static PyMemberDef PyHKEY_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 {"handle", T_INT, OFF(hkey), READONLY},
347 {NULL} /* Sentinel */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000348};
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000349
350/* The type itself */
351PyTypeObject PyHKEY_Type =
352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 PyVarObject_HEAD_INIT(0, 0) /* fill in type at module init */
354 "PyHKEY",
355 sizeof(PyHKEYObject),
356 0,
357 PyHKEY_deallocFunc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200358 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 0, /* tp_getattr */
360 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200361 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 0, /* tp_repr */
363 &PyHKEY_NumberMethods, /* tp_as_number */
364 0, /* tp_as_sequence */
365 0, /* tp_as_mapping */
366 PyHKEY_hashFunc, /* tp_hash */
367 0, /* tp_call */
368 PyHKEY_strFunc, /* tp_str */
369 0, /* tp_getattro */
370 0, /* tp_setattro */
371 0, /* tp_as_buffer */
372 0, /* tp_flags */
373 PyHKEY_doc, /* tp_doc */
374 0, /*tp_traverse*/
375 0, /*tp_clear*/
376 0, /*tp_richcompare*/
377 0, /*tp_weaklistoffset*/
378 0, /*tp_iter*/
379 0, /*tp_iternext*/
380 PyHKEY_methods, /*tp_methods*/
381 PyHKEY_memberlist, /*tp_members*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000382};
383
384/************************************************************************
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000385 The public PyHKEY API (well, not public yet :-)
386************************************************************************/
387PyObject *
388PyHKEY_New(HKEY hInit)
389{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 PyHKEYObject *key = PyObject_NEW(PyHKEYObject, &PyHKEY_Type);
391 if (key)
392 key->hkey = hInit;
393 return (PyObject *)key;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000394}
395
396BOOL
397PyHKEY_Close(PyObject *ob_handle)
398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 LONG rc;
400 PyHKEYObject *key;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 if (!PyHKEY_Check(ob_handle)) {
403 PyErr_SetString(PyExc_TypeError, "bad operand type");
404 return FALSE;
405 }
406 key = (PyHKEYObject *)ob_handle;
407 rc = key->hkey ? RegCloseKey((HKEY)key->hkey) : ERROR_SUCCESS;
408 key->hkey = 0;
409 if (rc != ERROR_SUCCESS)
410 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
411 return rc == ERROR_SUCCESS;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000412}
413
414BOOL
415PyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK)
416{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 if (ob == Py_None) {
418 if (!bNoneOK) {
419 PyErr_SetString(
420 PyExc_TypeError,
421 "None is not a valid HKEY in this context");
422 return FALSE;
423 }
424 *pHANDLE = (HKEY)0;
425 }
426 else if (PyHKEY_Check(ob)) {
427 PyHKEYObject *pH = (PyHKEYObject *)ob;
428 *pHANDLE = pH->hkey;
429 }
430 else if (PyLong_Check(ob)) {
431 /* We also support integers */
432 PyErr_Clear();
433 *pHANDLE = (HKEY)PyLong_AsVoidPtr(ob);
434 if (PyErr_Occurred())
435 return FALSE;
436 }
437 else {
438 PyErr_SetString(
439 PyExc_TypeError,
440 "The object is not a PyHKEY object");
441 return FALSE;
442 }
443 return TRUE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000444}
445
Zachary Warefd2d4822015-05-13 01:21:57 -0500446BOOL
447clinic_HKEY_converter(PyObject *ob, void *p)
448{
449 if (!PyHKEY_AsHKEY(ob, (HKEY *)p, FALSE))
450 return FALSE;
451 return TRUE;
452}
453
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000454PyObject *
455PyHKEY_FromHKEY(HKEY h)
456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 PyHKEYObject *op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 /* Inline PyObject_New */
460 op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject));
461 if (op == NULL)
462 return PyErr_NoMemory();
Victor Stinnerb509d522018-11-23 14:27:38 +0100463 PyObject_INIT(op, &PyHKEY_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 op->hkey = h;
465 return (PyObject *)op;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000466}
467
468
469/************************************************************************
470 The module methods
471************************************************************************/
472BOOL
473PyWinObject_CloseHKEY(PyObject *obHandle)
474{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 BOOL ok;
476 if (PyHKEY_Check(obHandle)) {
477 ok = PyHKEY_Close(obHandle);
478 }
Fred Drake25e17262000-06-30 17:48:51 +0000479#if SIZEOF_LONG >= SIZEOF_HKEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 else if (PyLong_Check(obHandle)) {
481 long rc = RegCloseKey((HKEY)PyLong_AsLong(obHandle));
482 ok = (rc == ERROR_SUCCESS);
483 if (!ok)
484 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
485 }
Fred Drake25e17262000-06-30 17:48:51 +0000486#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 else if (PyLong_Check(obHandle)) {
488 long rc = RegCloseKey((HKEY)PyLong_AsVoidPtr(obHandle));
489 ok = (rc == ERROR_SUCCESS);
490 if (!ok)
491 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
492 }
Fred Drake25e17262000-06-30 17:48:51 +0000493#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 else {
495 PyErr_SetString(
496 PyExc_TypeError,
497 "A handle must be a HKEY object or an integer");
498 return FALSE;
499 }
500 return ok;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000501}
502
503
504/*
505 Private Helper functions for the registry interfaces
506
507** Note that fixupMultiSZ and countString have both had changes
508** made to support "incorrect strings". The registry specification
509** calls for strings to be terminated with 2 null bytes. It seems
luzpaza5293b42017-11-05 07:37:50 -0600510** some commercial packages install strings which don't conform,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000511** causing this code to fail - however, "regedit" etc still work
luzpaza5293b42017-11-05 07:37:50 -0600512** with these strings (ie only we don't!).
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000513*/
514static void
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000515fixupMultiSZ(wchar_t **str, wchar_t *data, int len)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000516{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 wchar_t *P;
518 int i;
519 wchar_t *Q;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000520
Miss Islington (bot)ebca7eb2019-09-09 03:11:00 -0700521 if (len > 0 && data[len - 1] == '\0') {
522 Q = data + len - 1;
523 }
524 else {
525 Q = data + len;
526 }
527
528 for (P = data, i = 0; P < Q; P++, i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 str[i] = P;
Miss Islington (bot)ebca7eb2019-09-09 03:11:00 -0700530 for (; P < Q && *P != '\0'; P++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 ;
Miss Islington (bot)ebca7eb2019-09-09 03:11:00 -0700532 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000534}
535
536static int
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000537countStrings(wchar_t *data, int len)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000538{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 int strings;
Miss Islington (bot)ebca7eb2019-09-09 03:11:00 -0700540 wchar_t *P, *Q;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000541
Miss Islington (bot)ebca7eb2019-09-09 03:11:00 -0700542 if (len > 0 && data[len - 1] == '\0') {
543 Q = data + len - 1;
544 }
545 else {
546 Q = data + len;
547 }
548
549 for (P = data, strings = 0; P < Q; P++, strings++) {
550 for (; P < Q && *P != '\0'; P++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 ;
Miss Islington (bot)ebca7eb2019-09-09 03:11:00 -0700552 }
553 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 return strings;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000555}
556
557/* Convert PyObject into Registry data.
558 Allocates space as needed. */
559static BOOL
560Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
561{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 Py_ssize_t i,j;
563 switch (typ) {
564 case REG_DWORD:
565 if (value != Py_None && !PyLong_Check(value))
566 return FALSE;
567 *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1);
Steve Dower4d4bc422016-05-25 11:26:07 -0700568 if (*retDataBuf == NULL){
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 PyErr_NoMemory();
570 return FALSE;
571 }
572 *retDataSize = sizeof(DWORD);
573 if (value == Py_None) {
574 DWORD zero = 0;
575 memcpy(*retDataBuf, &zero, sizeof(DWORD));
576 }
577 else {
Brian Curtin12706f22012-12-27 10:12:45 -0600578 DWORD d = PyLong_AsUnsignedLong(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 memcpy(*retDataBuf, &d, sizeof(DWORD));
580 }
581 break;
Steve Dower80ac11d2016-05-24 15:42:04 -0700582 case REG_QWORD:
583 if (value != Py_None && !PyLong_Check(value))
584 return FALSE;
585 *retDataBuf = (BYTE *)PyMem_NEW(DWORD64, 1);
Steve Dower4d4bc422016-05-25 11:26:07 -0700586 if (*retDataBuf == NULL){
Steve Dower80ac11d2016-05-24 15:42:04 -0700587 PyErr_NoMemory();
588 return FALSE;
589 }
590 *retDataSize = sizeof(DWORD64);
591 if (value == Py_None) {
592 DWORD64 zero = 0;
593 memcpy(*retDataBuf, &zero, sizeof(DWORD64));
594 }
595 else {
596 DWORD64 d = PyLong_AsUnsignedLongLong(value);
597 memcpy(*retDataBuf, &d, sizeof(DWORD64));
598 }
599 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 case REG_SZ:
601 case REG_EXPAND_SZ:
602 {
Victor Stinnerbe492442011-11-21 12:43:50 +0100603 if (value != Py_None) {
604 Py_ssize_t len;
605 if (!PyUnicode_Check(value))
606 return FALSE;
607 *retDataBuf = (BYTE*)PyUnicode_AsWideCharString(value, &len);
608 if (*retDataBuf == NULL)
609 return FALSE;
610 *retDataSize = Py_SAFE_DOWNCAST(
611 (len + 1) * sizeof(wchar_t),
612 Py_ssize_t, DWORD);
613 }
614 else {
615 *retDataBuf = (BYTE *)PyMem_NEW(wchar_t, 1);
616 if (*retDataBuf == NULL) {
617 PyErr_NoMemory();
618 return FALSE;
619 }
620 ((wchar_t *)*retDataBuf)[0] = L'\0';
621 *retDataSize = 1 * sizeof(wchar_t);
622 }
623 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 }
625 case REG_MULTI_SZ:
626 {
627 DWORD size = 0;
628 wchar_t *P;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 if (value == Py_None)
631 i = 0;
632 else {
633 if (!PyList_Check(value))
634 return FALSE;
635 i = PyList_Size(value);
636 }
637 for (j = 0; j < i; j++)
638 {
639 PyObject *t;
Victor Stinnerbe492442011-11-21 12:43:50 +0100640 wchar_t *wstr;
641 Py_ssize_t len;
642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 t = PyList_GET_ITEM(value, j);
644 if (!PyUnicode_Check(t))
645 return FALSE;
Victor Stinnerbe492442011-11-21 12:43:50 +0100646 wstr = PyUnicode_AsUnicodeAndSize(t, &len);
647 if (wstr == NULL)
648 return FALSE;
649 size += Py_SAFE_DOWNCAST((len + 1) * sizeof(wchar_t),
Brian Curtinabb33512010-08-17 20:08:40 +0000650 size_t, DWORD);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 *retDataSize = size + 2;
654 *retDataBuf = (BYTE *)PyMem_NEW(char,
655 *retDataSize);
Steve Dower4d4bc422016-05-25 11:26:07 -0700656 if (*retDataBuf == NULL){
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 PyErr_NoMemory();
658 return FALSE;
659 }
660 P = (wchar_t *)*retDataBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 for (j = 0; j < i; j++)
663 {
664 PyObject *t;
Victor Stinnerbe492442011-11-21 12:43:50 +0100665 wchar_t *wstr;
666 Py_ssize_t len;
667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 t = PyList_GET_ITEM(value, j);
Victor Stinnerbe492442011-11-21 12:43:50 +0100669 wstr = PyUnicode_AsUnicodeAndSize(t, &len);
Zackery Spytz5d953122018-11-08 02:45:00 -0700670 assert(wstr);
Victor Stinnerbe492442011-11-21 12:43:50 +0100671 wcscpy(P, wstr);
672 P += (len + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 }
674 /* And doubly-terminate the list... */
675 *P = '\0';
676 break;
677 }
678 case REG_BINARY:
679 /* ALSO handle ALL unknown data types here. Even if we can't
680 support it natively, we should handle the bits. */
681 default:
Zachary Waread4690f2014-07-03 10:58:06 -0500682 if (value == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 *retDataSize = 0;
Zachary Waread4690f2014-07-03 10:58:06 -0500684 *retDataBuf = NULL;
685 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 else {
687 Py_buffer view;
Neal Norwitz1385b892007-08-26 23:07:13 +0000688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 if (!PyObject_CheckBuffer(value)) {
690 PyErr_Format(PyExc_TypeError,
691 "Objects of type '%s' can not "
692 "be used as binary registry values",
693 value->ob_type->tp_name);
694 return FALSE;
695 }
Neal Norwitz1385b892007-08-26 23:07:13 +0000696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0)
698 return FALSE;
Neal Norwitz1385b892007-08-26 23:07:13 +0000699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 *retDataBuf = (BYTE *)PyMem_NEW(char, view.len);
Steve Dower4d4bc422016-05-25 11:26:07 -0700701 if (*retDataBuf == NULL){
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 PyBuffer_Release(&view);
703 PyErr_NoMemory();
704 return FALSE;
705 }
Brian Curtinabb33512010-08-17 20:08:40 +0000706 *retDataSize = Py_SAFE_DOWNCAST(view.len, Py_ssize_t, DWORD);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 memcpy(*retDataBuf, view.buf, view.len);
708 PyBuffer_Release(&view);
709 }
710 break;
711 }
712 return TRUE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000713}
714
715/* Convert Registry data into PyObject*/
716static PyObject *
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000717Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000718{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 PyObject *obData;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 switch (typ) {
722 case REG_DWORD:
723 if (retDataSize == 0)
Brian Curtin172e4222012-12-27 14:04:42 -0600724 obData = PyLong_FromUnsignedLong(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 else
Steve Dower80ac11d2016-05-24 15:42:04 -0700726 obData = PyLong_FromUnsignedLong(*(DWORD *)retDataBuf);
727 break;
728 case REG_QWORD:
729 if (retDataSize == 0)
730 obData = PyLong_FromUnsignedLongLong(0);
731 else
732 obData = PyLong_FromUnsignedLongLong(*(DWORD64 *)retDataBuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 break;
734 case REG_SZ:
735 case REG_EXPAND_SZ:
736 {
Steve Dower40fa2662016-12-17 13:30:27 -0800737 /* REG_SZ should be a NUL terminated string, but only by
738 * convention. The buffer may have been saved without a NUL
739 * or with embedded NULs. To be consistent with reg.exe and
740 * regedit.exe, consume only up to the first NUL. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 wchar_t *data = (wchar_t *)retDataBuf;
Steve Dower40fa2662016-12-17 13:30:27 -0800742 size_t len = wcsnlen(data, retDataSize / sizeof(wchar_t));
743 obData = PyUnicode_FromWideChar(data, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 break;
745 }
746 case REG_MULTI_SZ:
747 if (retDataSize == 0)
748 obData = PyList_New(0);
749 else
750 {
751 int index = 0;
752 wchar_t *data = (wchar_t *)retDataBuf;
753 int len = retDataSize / 2;
754 int s = countStrings(data, len);
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +0200755 wchar_t **str = PyMem_New(wchar_t *, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 if (str == NULL)
757 return PyErr_NoMemory();
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 fixupMultiSZ(str, data, len);
760 obData = PyList_New(s);
Jesus Cea782c4cf2014-03-13 17:35:32 +0100761 if (obData == NULL) {
Victor Stinner9cb1ec52014-03-13 19:08:10 +0100762 PyMem_Free(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 return NULL;
Jesus Cea782c4cf2014-03-13 17:35:32 +0100764 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 for (index = 0; index < s; index++)
766 {
Miss Islington (bot)ebca7eb2019-09-09 03:11:00 -0700767 size_t slen = wcsnlen(str[index], len);
768 PyObject *uni = PyUnicode_FromWideChar(str[index], slen);
Zackery Spytz99d56b52018-12-08 07:16:55 -0700769 if (uni == NULL) {
770 Py_DECREF(obData);
771 PyMem_Free(str);
772 return NULL;
773 }
774 PyList_SET_ITEM(obData, index, uni);
Miss Islington (bot)44729c92019-09-09 07:23:37 -0700775 len -= Py_SAFE_DOWNCAST(slen + 1, size_t, int);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 }
Victor Stinnerb6404912013-07-07 16:21:41 +0200777 PyMem_Free(str);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 break;
780 }
781 case REG_BINARY:
782 /* ALSO handle ALL unknown data types here. Even if we can't
783 support it natively, we should handle the bits. */
784 default:
785 if (retDataSize == 0) {
786 Py_INCREF(Py_None);
787 obData = Py_None;
788 }
789 else
790 obData = PyBytes_FromStringAndSize(
791 (char *)retDataBuf, retDataSize);
792 break;
793 }
794 return obData;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000795}
796
797/* The Python methods */
798
Zachary Warefd2d4822015-05-13 01:21:57 -0500799/*[clinic input]
800winreg.CloseKey
801
802 hkey: object
803 A previously opened key.
804 /
805
806Closes a previously opened registry key.
807
808Note that if the key is not closed using this method, it will be
809closed when the hkey object is destroyed by Python.
810[clinic start generated code]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000811
812static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300813winreg_CloseKey(PyObject *module, PyObject *hkey)
814/*[clinic end generated code: output=a4fa537019a80d15 input=5b1aac65ba5127ad]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000815{
Zachary Warefd2d4822015-05-13 01:21:57 -0500816 if (!PyHKEY_Close(hkey))
817 return NULL;
818 Py_RETURN_NONE;
819}
820
821/*[clinic input]
822winreg.ConnectRegistry -> HKEY
823
Zachary Ware77772c02015-05-13 10:58:35 -0500824 computer_name: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -0500825 The name of the remote computer, of the form r"\\computername". If
826 None, the local computer is used.
827 key: HKEY
828 The predefined key to connect to.
829 /
830
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +0300831Establishes a connection to the registry on another computer.
Zachary Warefd2d4822015-05-13 01:21:57 -0500832
833The return value is the handle of the opened key.
834If the function fails, an OSError exception is raised.
835[clinic start generated code]*/
836
837static HKEY
Serhiy Storchakaafb3e712018-12-14 11:19:51 +0200838winreg_ConnectRegistry_impl(PyObject *module,
839 const Py_UNICODE *computer_name, HKEY key)
840/*[clinic end generated code: output=cd4f70fb9ec901fb input=5f98a891a347e68e]*/
Zachary Warefd2d4822015-05-13 01:21:57 -0500841{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 HKEY retKey;
843 long rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -0500845 rc = RegConnectRegistryW(computer_name, key, &retKey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 Py_END_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -0500847 if (rc != ERROR_SUCCESS) {
848 PyErr_SetFromWindowsErrWithFunction(rc, "ConnectRegistry");
849 return NULL;
850 }
851 return retKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000852}
853
Zachary Warefd2d4822015-05-13 01:21:57 -0500854/*[clinic input]
855winreg.CreateKey -> HKEY
856
857 key: HKEY
858 An already open key, or one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -0500859 sub_key: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -0500860 The name of the key this method opens or creates.
861 /
862
863Creates or opens the specified key.
864
865If key is one of the predefined keys, sub_key may be None. In that case,
866the handle returned is the same key handle passed in to the function.
867
868If the key already exists, this function opens the existing key.
869
870The return value is the handle of the opened key.
871If the function fails, an OSError exception is raised.
872[clinic start generated code]*/
873
874static HKEY
Serhiy Storchakaafb3e712018-12-14 11:19:51 +0200875winreg_CreateKey_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key)
876/*[clinic end generated code: output=2af13910d56eae26 input=3cdd1622488acea2]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000877{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 HKEY retKey;
879 long rc;
Zachary Warefd2d4822015-05-13 01:21:57 -0500880
881 rc = RegCreateKeyW(key, sub_key, &retKey);
882 if (rc != ERROR_SUCCESS) {
883 PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 return NULL;
Zachary Warefd2d4822015-05-13 01:21:57 -0500885 }
886 return retKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000887}
888
Zachary Warefd2d4822015-05-13 01:21:57 -0500889/*[clinic input]
890winreg.CreateKeyEx -> HKEY
891
892 key: HKEY
893 An already open key, or one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -0500894 sub_key: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -0500895 The name of the key this method opens or creates.
896 reserved: int = 0
897 A reserved integer, and must be zero. Default is zero.
898 access: REGSAM(c_default='KEY_WRITE') = winreg.KEY_WRITE
899 An integer that specifies an access mask that describes the
900 desired security access for the key. Default is KEY_WRITE.
901
902Creates or opens the specified key.
903
904If key is one of the predefined keys, sub_key may be None. In that case,
905the handle returned is the same key handle passed in to the function.
906
907If the key already exists, this function opens the existing key
908
909The return value is the handle of the opened key.
910If the function fails, an OSError exception is raised.
911[clinic start generated code]*/
912
913static HKEY
Serhiy Storchakaafb3e712018-12-14 11:19:51 +0200914winreg_CreateKeyEx_impl(PyObject *module, HKEY key,
915 const Py_UNICODE *sub_key, int reserved,
916 REGSAM access)
917/*[clinic end generated code: output=643a70ad6a361a97 input=42c2b03f98406b66]*/
Brian Curtin3035c392010-04-21 23:56:21 +0000918{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 HKEY retKey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 long rc;
Brian Curtin1771b542010-09-27 17:56:36 +0000921
Segev Finer679b5662017-07-27 01:17:57 +0300922 rc = RegCreateKeyExW(key, sub_key, reserved, NULL, 0,
Brian Curtin1771b542010-09-27 17:56:36 +0000923 access, NULL, &retKey, NULL);
Zachary Warefd2d4822015-05-13 01:21:57 -0500924 if (rc != ERROR_SUCCESS) {
925 PyErr_SetFromWindowsErrWithFunction(rc, "CreateKeyEx");
926 return NULL;
927 }
928 return retKey;
Brian Curtin3035c392010-04-21 23:56:21 +0000929}
930
Zachary Warefd2d4822015-05-13 01:21:57 -0500931/*[clinic input]
932winreg.DeleteKey
933 key: HKEY
934 An already open key, or any one of the predefined HKEY_* constants.
935 sub_key: Py_UNICODE
936 A string that must be the name of a subkey of the key identified by
937 the key parameter. This value must not be None, and the key may not
938 have subkeys.
939 /
940
941Deletes the specified key.
942
943This method can not delete keys with subkeys.
944
945If the function succeeds, the entire key, including all of its values,
946is removed. If the function fails, an OSError exception is raised.
947[clinic start generated code]*/
948
Brian Curtin3035c392010-04-21 23:56:21 +0000949static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +0200950winreg_DeleteKey_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key)
951/*[clinic end generated code: output=d2652a84f70e0862 input=b31d225b935e4211]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000952{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 long rc;
Zachary Warefd2d4822015-05-13 01:21:57 -0500954 rc = RegDeleteKeyW(key, sub_key );
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 if (rc != ERROR_SUCCESS)
956 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey");
Zachary Warefd2d4822015-05-13 01:21:57 -0500957 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000958}
959
Zachary Warefd2d4822015-05-13 01:21:57 -0500960/*[clinic input]
961winreg.DeleteKeyEx
962
963 key: HKEY
964 An already open key, or any one of the predefined HKEY_* constants.
965 sub_key: Py_UNICODE
966 A string that must be the name of a subkey of the key identified by
967 the key parameter. This value must not be None, and the key may not
968 have subkeys.
969 access: REGSAM(c_default='KEY_WOW64_64KEY') = winreg.KEY_WOW64_64KEY
970 An integer that specifies an access mask that describes the
971 desired security access for the key. Default is KEY_WOW64_64KEY.
972 reserved: int = 0
973 A reserved integer, and must be zero. Default is zero.
974
975Deletes the specified key (64-bit OS only).
976
977This method can not delete keys with subkeys.
978
979If the function succeeds, the entire key, including all of its values,
980is removed. If the function fails, an OSError exception is raised.
981On unsupported Windows versions, NotImplementedError is raised.
982[clinic start generated code]*/
983
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000984static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +0200985winreg_DeleteKeyEx_impl(PyObject *module, HKEY key,
986 const Py_UNICODE *sub_key, REGSAM access,
987 int reserved)
988/*[clinic end generated code: output=52a1c8b374ebc003 input=711d9d89e7ecbed7]*/
Brian Curtin3035c392010-04-21 23:56:21 +0000989{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 HMODULE hMod;
991 typedef LONG (WINAPI *RDKEFunc)(HKEY, const wchar_t*, REGSAM, int);
992 RDKEFunc pfn = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 long rc;
Brian Curtin3035c392010-04-21 23:56:21 +0000994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 /* Only available on 64bit platforms, so we must load it
996 dynamically. */
Tony Roberts4860f012019-02-02 18:16:42 +0100997 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis50590f12012-01-14 17:54:09 +0100998 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 if (hMod)
1000 pfn = (RDKEFunc)GetProcAddress(hMod,
1001 "RegDeleteKeyExW");
Tony Roberts4860f012019-02-02 18:16:42 +01001002 Py_END_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 if (!pfn) {
1004 PyErr_SetString(PyExc_NotImplementedError,
1005 "not implemented on this platform");
1006 return NULL;
1007 }
1008 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001009 rc = (*pfn)(key, sub_key, access, reserved);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 Py_END_ALLOW_THREADS
Brian Curtin3035c392010-04-21 23:56:21 +00001011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 if (rc != ERROR_SUCCESS)
1013 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKeyEx");
Zachary Warefd2d4822015-05-13 01:21:57 -05001014 Py_RETURN_NONE;
Brian Curtin3035c392010-04-21 23:56:21 +00001015}
1016
Zachary Warefd2d4822015-05-13 01:21:57 -05001017/*[clinic input]
1018winreg.DeleteValue
1019
1020 key: HKEY
1021 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001022 value: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001023 A string that identifies the value to remove.
1024 /
1025
1026Removes a named value from a registry key.
1027[clinic start generated code]*/
1028
Brian Curtin3035c392010-04-21 23:56:21 +00001029static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001030winreg_DeleteValue_impl(PyObject *module, HKEY key, const Py_UNICODE *value)
1031/*[clinic end generated code: output=56fa9d21f3a54371 input=a78d3407a4197b21]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001032{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 long rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001035 rc = RegDeleteValueW(key, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 Py_END_ALLOW_THREADS
1037 if (rc !=ERROR_SUCCESS)
1038 return PyErr_SetFromWindowsErrWithFunction(rc,
1039 "RegDeleteValue");
Zachary Warefd2d4822015-05-13 01:21:57 -05001040 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001041}
1042
Zachary Warefd2d4822015-05-13 01:21:57 -05001043/*[clinic input]
1044winreg.EnumKey
1045
1046 key: HKEY
1047 An already open key, or any one of the predefined HKEY_* constants.
1048 index: int
1049 An integer that identifies the index of the key to retrieve.
1050 /
1051
1052Enumerates subkeys of an open registry key.
1053
1054The function retrieves the name of one subkey each time it is called.
1055It is typically called repeatedly until an OSError exception is
1056raised, indicating no more values are available.
1057[clinic start generated code]*/
1058
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001059static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001060winreg_EnumKey_impl(PyObject *module, HKEY key, int index)
1061/*[clinic end generated code: output=25a6ec52cd147bc4 input=fad9a7c00ab0e04b]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 long rc;
1064 PyObject *retStr;
Brian Curtin60853212010-05-26 17:43:50 +00001065
1066 /* The Windows docs claim that the max key name length is 255
1067 * characters, plus a terminating nul character. However,
1068 * empirical testing demonstrates that it is possible to
1069 * create a 256 character key that is missing the terminating
1070 * nul. RegEnumKeyEx requires a 257 character buffer to
1071 * retrieve such a key name. */
1072 wchar_t tmpbuf[257];
Kristján Valur Jónsson984dfa72012-04-02 15:23:29 +00001073 DWORD len = sizeof(tmpbuf)/sizeof(wchar_t); /* includes NULL terminator */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001076 rc = RegEnumKeyExW(key, index, tmpbuf, &len, NULL, NULL, NULL, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 Py_END_ALLOW_THREADS
1078 if (rc != ERROR_SUCCESS)
1079 return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx");
Tim Peters313fcd42006-02-19 04:05:39 +00001080
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001081 retStr = PyUnicode_FromWideChar(tmpbuf, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 return retStr; /* can be NULL */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001083}
1084
Zachary Warefd2d4822015-05-13 01:21:57 -05001085/*[clinic input]
1086winreg.EnumValue
1087
1088 key: HKEY
1089 An already open key, or any one of the predefined HKEY_* constants.
1090 index: int
1091 An integer that identifies the index of the value to retrieve.
1092 /
1093
1094Enumerates values of an open registry key.
1095
1096The function retrieves the name of one subkey each time it is called.
1097It is typically called repeatedly, until an OSError exception
1098is raised, indicating no more values.
1099
1100The result is a tuple of 3 items:
1101 value_name
1102 A string that identifies the value.
1103 value_data
1104 An object that holds the value data, and whose type depends
1105 on the underlying registry type.
1106 data_type
1107 An integer that identifies the type of the value data.
1108[clinic start generated code]*/
1109
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001110static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001111winreg_EnumValue_impl(PyObject *module, HKEY key, int index)
1112/*[clinic end generated code: output=d363b5a06f8789ac input=4414f47a6fb238b5]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 long rc;
1115 wchar_t *retValueBuf;
Amaury Forgeot d'Arcf2b69df2010-08-16 22:11:29 +00001116 BYTE *tmpBuf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 BYTE *retDataBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001118 DWORD retValueSize, bufValueSize;
1119 DWORD retDataSize, bufDataSize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 DWORD typ;
1121 PyObject *obData;
1122 PyObject *retVal;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001123
Zachary Warefd2d4822015-05-13 01:21:57 -05001124 if ((rc = RegQueryInfoKeyW(key, NULL, NULL, NULL, NULL, NULL, NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 NULL,
1126 &retValueSize, &retDataSize, NULL, NULL))
1127 != ERROR_SUCCESS)
1128 return PyErr_SetFromWindowsErrWithFunction(rc,
1129 "RegQueryInfoKey");
1130 ++retValueSize; /* include null terminators */
1131 ++retDataSize;
Brian Curtin60853212010-05-26 17:43:50 +00001132 bufDataSize = retDataSize;
1133 bufValueSize = retValueSize;
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02001134 retValueBuf = PyMem_New(wchar_t, retValueSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 if (retValueBuf == NULL)
1136 return PyErr_NoMemory();
1137 retDataBuf = (BYTE *)PyMem_Malloc(retDataSize);
1138 if (retDataBuf == NULL) {
1139 PyMem_Free(retValueBuf);
1140 return PyErr_NoMemory();
1141 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001142
Brian Curtin60853212010-05-26 17:43:50 +00001143 while (1) {
Brian Curtin60853212010-05-26 17:43:50 +00001144 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001145 rc = RegEnumValueW(key,
Brian Curtin60853212010-05-26 17:43:50 +00001146 index,
1147 retValueBuf,
1148 &retValueSize,
1149 NULL,
1150 &typ,
1151 (BYTE *)retDataBuf,
1152 &retDataSize);
1153 Py_END_ALLOW_THREADS
1154
1155 if (rc != ERROR_MORE_DATA)
1156 break;
1157
1158 bufDataSize *= 2;
Amaury Forgeot d'Arcf2b69df2010-08-16 22:11:29 +00001159 tmpBuf = (BYTE *)PyMem_Realloc(retDataBuf, bufDataSize);
Brian Curtin9b7e2d12010-06-08 20:57:52 +00001160 if (tmpBuf == NULL) {
Brian Curtin60853212010-05-26 17:43:50 +00001161 PyErr_NoMemory();
1162 retVal = NULL;
1163 goto fail;
1164 }
Brian Curtin9b7e2d12010-06-08 20:57:52 +00001165 retDataBuf = tmpBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001166 retDataSize = bufDataSize;
1167 retValueSize = bufValueSize;
1168 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 if (rc != ERROR_SUCCESS) {
1171 retVal = PyErr_SetFromWindowsErrWithFunction(rc,
1172 "PyRegEnumValue");
1173 goto fail;
1174 }
1175 obData = Reg2Py(retDataBuf, retDataSize, typ);
1176 if (obData == NULL) {
1177 retVal = NULL;
1178 goto fail;
1179 }
1180 retVal = Py_BuildValue("uOi", retValueBuf, obData, typ);
1181 Py_DECREF(obData);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001182 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 PyMem_Free(retValueBuf);
1184 PyMem_Free(retDataBuf);
1185 return retVal;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001186}
1187
Zachary Warefd2d4822015-05-13 01:21:57 -05001188/*[clinic input]
1189winreg.ExpandEnvironmentStrings
1190
1191 string: Py_UNICODE
1192 /
1193
1194Expand environment vars.
1195[clinic start generated code]*/
1196
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001197static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001198winreg_ExpandEnvironmentStrings_impl(PyObject *module,
1199 const Py_UNICODE *string)
1200/*[clinic end generated code: output=8fa4e959747a7312 input=b2a9714d2b751aa6]*/
Christian Heimes2380ac72008-01-09 00:17:24 +00001201{
Victor Stinner9d3b93b2011-11-22 02:27:30 +01001202 wchar_t *retValue = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 DWORD retValueSize;
1204 DWORD rc;
1205 PyObject *o;
Christian Heimes2380ac72008-01-09 00:17:24 +00001206
Zachary Warefd2d4822015-05-13 01:21:57 -05001207 retValueSize = ExpandEnvironmentStringsW(string, retValue, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 if (retValueSize == 0) {
1209 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1210 "ExpandEnvironmentStrings");
1211 }
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02001212 retValue = PyMem_New(wchar_t, retValueSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 if (retValue == NULL) {
1214 return PyErr_NoMemory();
1215 }
Christian Heimes2380ac72008-01-09 00:17:24 +00001216
Zachary Warefd2d4822015-05-13 01:21:57 -05001217 rc = ExpandEnvironmentStringsW(string, retValue, retValueSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 if (rc == 0) {
1219 PyMem_Free(retValue);
1220 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1221 "ExpandEnvironmentStrings");
1222 }
Victor Stinner9d3b93b2011-11-22 02:27:30 +01001223 o = PyUnicode_FromWideChar(retValue, wcslen(retValue));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 PyMem_Free(retValue);
1225 return o;
Christian Heimes2380ac72008-01-09 00:17:24 +00001226}
1227
Zachary Warefd2d4822015-05-13 01:21:57 -05001228/*[clinic input]
1229winreg.FlushKey
1230
1231 key: HKEY
1232 An already open key, or any one of the predefined HKEY_* constants.
1233 /
1234
1235Writes all the attributes of a key to the registry.
1236
1237It is not necessary to call FlushKey to change a key. Registry changes
1238are flushed to disk by the registry using its lazy flusher. Registry
1239changes are also flushed to disk at system shutdown. Unlike
1240CloseKey(), the FlushKey() method returns only when all the data has
1241been written to the registry.
1242
1243An application should only call FlushKey() if it requires absolute
1244certainty that registry changes are on disk. If you don't know whether
1245a FlushKey() call is required, it probably isn't.
1246[clinic start generated code]*/
1247
Christian Heimes2380ac72008-01-09 00:17:24 +00001248static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001249winreg_FlushKey_impl(PyObject *module, HKEY key)
1250/*[clinic end generated code: output=e6fc230d4c5dc049 input=f57457c12297d82f]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001251{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 long rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001254 rc = RegFlushKey(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 Py_END_ALLOW_THREADS
1256 if (rc != ERROR_SUCCESS)
1257 return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001258 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001259}
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001260
Zachary Warefd2d4822015-05-13 01:21:57 -05001261
1262/*[clinic input]
1263winreg.LoadKey
1264
1265 key: HKEY
1266 An already open key, or any one of the predefined HKEY_* constants.
1267 sub_key: Py_UNICODE
1268 A string that identifies the sub-key to load.
1269 file_name: Py_UNICODE
1270 The name of the file to load registry data from. This file must
1271 have been created with the SaveKey() function. Under the file
1272 allocation table (FAT) file system, the filename may not have an
1273 extension.
1274 /
1275
1276Insert data into the registry from a file.
1277
1278Creates a subkey under the specified key and stores registration
1279information from a specified file into that subkey.
1280
1281A call to LoadKey() fails if the calling process does not have the
1282SE_RESTORE_PRIVILEGE privilege.
1283
1284If key is a handle returned by ConnectRegistry(), then the path
1285specified in fileName is relative to the remote computer.
1286
1287The MSDN docs imply key must be in the HKEY_USER or HKEY_LOCAL_MACHINE
1288tree.
1289[clinic start generated code]*/
1290
1291static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001292winreg_LoadKey_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key,
1293 const Py_UNICODE *file_name)
1294/*[clinic end generated code: output=65f89f2548cb27c7 input=e3b5b45ade311582]*/
Zachary Warefd2d4822015-05-13 01:21:57 -05001295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 long rc;
Zachary Warefd2d4822015-05-13 01:21:57 -05001297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001299 rc = RegLoadKeyW(key, sub_key, file_name );
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 Py_END_ALLOW_THREADS
1301 if (rc != ERROR_SUCCESS)
1302 return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001303 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001304}
1305
Zachary Warefd2d4822015-05-13 01:21:57 -05001306/*[clinic input]
1307winreg.OpenKey -> HKEY
1308
1309 key: HKEY
1310 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001311 sub_key: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001312 A string that identifies the sub_key to open.
1313 reserved: int = 0
1314 A reserved integer that must be zero. Default is zero.
1315 access: REGSAM(c_default='KEY_READ') = winreg.KEY_READ
1316 An integer that specifies an access mask that describes the desired
1317 security access for the key. Default is KEY_READ.
1318
1319Opens the specified key.
1320
1321The result is a new handle to the specified key.
1322If the function fails, an OSError exception is raised.
1323[clinic start generated code]*/
1324
1325static HKEY
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001326winreg_OpenKey_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key,
Zachary Ware77772c02015-05-13 10:58:35 -05001327 int reserved, REGSAM access)
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001328/*[clinic end generated code: output=8849bff2c30104ad input=098505ac36a9ae28]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 HKEY retKey;
1331 long rc;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001334 rc = RegOpenKeyExW(key, sub_key, reserved, access, &retKey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 Py_END_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001336 if (rc != ERROR_SUCCESS) {
1337 PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx");
1338 return NULL;
1339 }
1340 return retKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001341}
1342
Zachary Warefd2d4822015-05-13 01:21:57 -05001343/*[clinic input]
1344winreg.OpenKeyEx = winreg.OpenKey
1345
1346Opens the specified key.
1347
1348The result is a new handle to the specified key.
1349If the function fails, an OSError exception is raised.
1350[clinic start generated code]*/
1351
1352static HKEY
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001353winreg_OpenKeyEx_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key,
Zachary Ware77772c02015-05-13 10:58:35 -05001354 int reserved, REGSAM access)
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001355/*[clinic end generated code: output=81bc2bd684bc77ae input=c6c4972af8622959]*/
Zachary Warefd2d4822015-05-13 01:21:57 -05001356{
1357 return winreg_OpenKey_impl(module, key, sub_key, reserved, access);
1358}
1359
1360/*[clinic input]
1361winreg.QueryInfoKey
1362
1363 key: HKEY
1364 An already open key, or any one of the predefined HKEY_* constants.
1365 /
1366
1367Returns information about a key.
1368
1369The result is a tuple of 3 items:
1370An integer that identifies the number of sub keys this key has.
1371An integer that identifies the number of values this key has.
1372An integer that identifies when the key was last modified (if available)
1373as 100's of nanoseconds since Jan 1, 1600.
1374[clinic start generated code]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001375
1376static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001377winreg_QueryInfoKey_impl(PyObject *module, HKEY key)
1378/*[clinic end generated code: output=dc657b8356a4f438 input=c3593802390cde1f]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001379{
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001380 long rc;
1381 DWORD nSubKeys, nValues;
1382 FILETIME ft;
1383 LARGE_INTEGER li;
1384 PyObject *l;
1385 PyObject *ret;
Zachary Warefd2d4822015-05-13 01:21:57 -05001386
1387 if ((rc = RegQueryInfoKey(key, NULL, NULL, 0, &nSubKeys, NULL, NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 &nValues, NULL, NULL, NULL, &ft))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001389 != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey");
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001391 li.LowPart = ft.dwLowDateTime;
1392 li.HighPart = ft.dwHighDateTime;
1393 l = PyLong_FromLongLong(li.QuadPart);
1394 if (l == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001396 ret = Py_BuildValue("iiO", nSubKeys, nValues, l);
1397 Py_DECREF(l);
1398 return ret;
1399}
1400
Zachary Warefd2d4822015-05-13 01:21:57 -05001401/*[clinic input]
1402winreg.QueryValue
1403
1404 key: HKEY
1405 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001406 sub_key: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001407 A string that holds the name of the subkey with which the value
1408 is associated. If this parameter is None or empty, the function
1409 retrieves the value set by the SetValue() method for the key
1410 identified by key.
1411 /
1412
1413Retrieves the unnamed value for a key.
1414
1415Values in the registry have name, type, and data components. This method
1416retrieves the data for a key's first value that has a NULL name.
1417But since the underlying API call doesn't return the type, you'll
1418probably be happier using QueryValueEx; this function is just here for
1419completeness.
1420[clinic start generated code]*/
1421
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001422static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001423winreg_QueryValue_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key)
1424/*[clinic end generated code: output=c655810ae50c63a9 input=41cafbbf423b21d6]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001425{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 long rc;
1427 PyObject *retStr;
1428 wchar_t *retBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001429 DWORD bufSize = 0;
1430 DWORD retSize = 0;
1431 wchar_t *tmp;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001432
Zachary Warefd2d4822015-05-13 01:21:57 -05001433 rc = RegQueryValueW(key, sub_key, NULL, &retSize);
Brian Curtin60853212010-05-26 17:43:50 +00001434 if (rc == ERROR_MORE_DATA)
1435 retSize = 256;
1436 else if (rc != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 return PyErr_SetFromWindowsErrWithFunction(rc,
1438 "RegQueryValue");
Brian Curtin60853212010-05-26 17:43:50 +00001439
1440 bufSize = retSize;
1441 retBuf = (wchar_t *) PyMem_Malloc(bufSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 if (retBuf == NULL)
1443 return PyErr_NoMemory();
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001444
Brian Curtin60853212010-05-26 17:43:50 +00001445 while (1) {
1446 retSize = bufSize;
Zachary Warefd2d4822015-05-13 01:21:57 -05001447 rc = RegQueryValueW(key, sub_key, retBuf, &retSize);
Brian Curtin60853212010-05-26 17:43:50 +00001448 if (rc != ERROR_MORE_DATA)
1449 break;
1450
1451 bufSize *= 2;
1452 tmp = (wchar_t *) PyMem_Realloc(retBuf, bufSize);
1453 if (tmp == NULL) {
1454 PyMem_Free(retBuf);
1455 return PyErr_NoMemory();
1456 }
1457 retBuf = tmp;
1458 }
1459
1460 if (rc != ERROR_SUCCESS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 PyMem_Free(retBuf);
1462 return PyErr_SetFromWindowsErrWithFunction(rc,
1463 "RegQueryValue");
1464 }
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001465
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001466 retStr = PyUnicode_FromWideChar(retBuf, wcslen(retBuf));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 PyMem_Free(retBuf);
1468 return retStr;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001469}
1470
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001471
Zachary Warefd2d4822015-05-13 01:21:57 -05001472/*[clinic input]
1473winreg.QueryValueEx
1474
1475 key: HKEY
1476 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001477 name: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001478 A string indicating the value to query.
1479 /
1480
1481Retrieves the type and value of a specified sub-key.
1482
1483Behaves mostly like QueryValue(), but also returns the type of the
1484specified value name associated with the given open registry key.
1485
1486The return value is a tuple of the value and the type_id.
1487[clinic start generated code]*/
1488
1489static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001490winreg_QueryValueEx_impl(PyObject *module, HKEY key, const Py_UNICODE *name)
1491/*[clinic end generated code: output=f1b85b1c3d887ec7 input=cf366cada4836891]*/
Zachary Warefd2d4822015-05-13 01:21:57 -05001492{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 long rc;
Brian Curtin60853212010-05-26 17:43:50 +00001494 BYTE *retBuf, *tmp;
1495 DWORD bufSize = 0, retSize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 DWORD typ;
1497 PyObject *obData;
1498 PyObject *result;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001499
Zachary Warefd2d4822015-05-13 01:21:57 -05001500 rc = RegQueryValueExW(key, name, NULL, NULL, NULL, &bufSize);
Brian Curtin60853212010-05-26 17:43:50 +00001501 if (rc == ERROR_MORE_DATA)
1502 bufSize = 256;
1503 else if (rc != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 return PyErr_SetFromWindowsErrWithFunction(rc,
1505 "RegQueryValueEx");
1506 retBuf = (BYTE *)PyMem_Malloc(bufSize);
1507 if (retBuf == NULL)
1508 return PyErr_NoMemory();
Brian Curtin60853212010-05-26 17:43:50 +00001509
1510 while (1) {
1511 retSize = bufSize;
Zachary Warefd2d4822015-05-13 01:21:57 -05001512 rc = RegQueryValueExW(key, name, NULL, &typ,
Brian Curtin60853212010-05-26 17:43:50 +00001513 (BYTE *)retBuf, &retSize);
1514 if (rc != ERROR_MORE_DATA)
1515 break;
1516
1517 bufSize *= 2;
1518 tmp = (char *) PyMem_Realloc(retBuf, bufSize);
1519 if (tmp == NULL) {
1520 PyMem_Free(retBuf);
1521 return PyErr_NoMemory();
1522 }
1523 retBuf = tmp;
1524 }
1525
1526 if (rc != ERROR_SUCCESS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 PyMem_Free(retBuf);
1528 return PyErr_SetFromWindowsErrWithFunction(rc,
1529 "RegQueryValueEx");
1530 }
1531 obData = Reg2Py(retBuf, bufSize, typ);
1532 PyMem_Free(retBuf);
1533 if (obData == NULL)
1534 return NULL;
1535 result = Py_BuildValue("Oi", obData, typ);
1536 Py_DECREF(obData);
1537 return result;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001538}
1539
Zachary Warefd2d4822015-05-13 01:21:57 -05001540/*[clinic input]
1541winreg.SaveKey
1542
1543 key: HKEY
1544 An already open key, or any one of the predefined HKEY_* constants.
1545 file_name: Py_UNICODE
1546 The name of the file to save registry data to. This file cannot
1547 already exist. If this filename includes an extension, it cannot be
1548 used on file allocation table (FAT) file systems by the LoadKey(),
1549 ReplaceKey() or RestoreKey() methods.
1550 /
1551
1552Saves the specified key, and all its subkeys to the specified file.
1553
1554If key represents a key on a remote computer, the path described by
1555file_name is relative to the remote computer.
1556
1557The caller of this method must possess the SeBackupPrivilege
1558security privilege. This function passes NULL for security_attributes
1559to the API.
1560[clinic start generated code]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001561
1562static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001563winreg_SaveKey_impl(PyObject *module, HKEY key, const Py_UNICODE *file_name)
1564/*[clinic end generated code: output=ca94b835c88f112b input=da735241f91ac7a2]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 LPSECURITY_ATTRIBUTES pSA = NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 long rc;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001569/* One day we may get security into the core?
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE))
1571 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001572*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001574 rc = RegSaveKeyW(key, file_name, pSA );
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 Py_END_ALLOW_THREADS
1576 if (rc != ERROR_SUCCESS)
1577 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001578 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001579}
1580
Zachary Warefd2d4822015-05-13 01:21:57 -05001581/*[clinic input]
1582winreg.SetValue
1583
1584 key: HKEY
1585 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001586 sub_key: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001587 A string that names the subkey with which the value is associated.
1588 type: DWORD
1589 An integer that specifies the type of the data. Currently this must
1590 be REG_SZ, meaning only strings are supported.
Zachary Ware77772c02015-05-13 10:58:35 -05001591 value: Py_UNICODE(zeroes=True)
Zachary Warefd2d4822015-05-13 01:21:57 -05001592 A string that specifies the new value.
1593 /
1594
1595Associates a value with a specified key.
1596
1597If the key specified by the sub_key parameter does not exist, the
1598SetValue function creates it.
1599
1600Value lengths are limited by available memory. Long values (more than
16012048 bytes) should be stored as files with the filenames stored in
1602the configuration registry to help the registry perform efficiently.
1603
1604The key identified by the key parameter must have been opened with
1605KEY_SET_VALUE access.
1606[clinic start generated code]*/
1607
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001608static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001609winreg_SetValue_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key,
1610 DWORD type, const Py_UNICODE *value,
Zachary Ware77772c02015-05-13 10:58:35 -05001611 Py_ssize_clean_t value_length)
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001612/*[clinic end generated code: output=686bedb1cbb4367b input=2cd2adab79339c53]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001613{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 long rc;
Zachary Warefd2d4822015-05-13 01:21:57 -05001615
1616 if (type != REG_SZ) {
Inada Naokicc60cdd92019-03-20 20:53:08 +09001617 PyErr_SetString(PyExc_TypeError, "type must be winreg.REG_SZ");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 return NULL;
1619 }
Inada Naokicc60cdd92019-03-20 20:53:08 +09001620 if ((size_t)value_length >= PY_DWORD_MAX) {
1621 PyErr_SetString(PyExc_OverflowError, "value is too long");
Inada Naokid5f18a62019-03-20 19:10:17 +09001622 return NULL;
1623 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 Py_BEGIN_ALLOW_THREADS
Zackery Spytz34366b72019-04-22 11:08:05 -06001626 rc = RegSetValueW(key, sub_key, REG_SZ, value, (DWORD)(value_length + 1));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 Py_END_ALLOW_THREADS
1628 if (rc != ERROR_SUCCESS)
1629 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue");
Zachary Warefd2d4822015-05-13 01:21:57 -05001630 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001631}
1632
Zachary Warefd2d4822015-05-13 01:21:57 -05001633/*[clinic input]
1634winreg.SetValueEx
1635
1636 key: HKEY
1637 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001638 value_name: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001639 A string containing the name of the value to set, or None.
1640 reserved: object
1641 Can be anything - zero is always passed to the API.
1642 type: DWORD
1643 An integer that specifies the type of the data, one of:
1644 REG_BINARY -- Binary data in any form.
1645 REG_DWORD -- A 32-bit number.
Steve Dower80ac11d2016-05-24 15:42:04 -07001646 REG_DWORD_LITTLE_ENDIAN -- A 32-bit number in little-endian format. Equivalent to REG_DWORD
Zachary Warefd2d4822015-05-13 01:21:57 -05001647 REG_DWORD_BIG_ENDIAN -- A 32-bit number in big-endian format.
1648 REG_EXPAND_SZ -- A null-terminated string that contains unexpanded
1649 references to environment variables (for example,
1650 %PATH%).
1651 REG_LINK -- A Unicode symbolic link.
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03001652 REG_MULTI_SZ -- A sequence of null-terminated strings, terminated
Zachary Warefd2d4822015-05-13 01:21:57 -05001653 by two null characters. Note that Python handles
1654 this termination automatically.
1655 REG_NONE -- No defined value type.
Steve Dower80ac11d2016-05-24 15:42:04 -07001656 REG_QWORD -- A 64-bit number.
1657 REG_QWORD_LITTLE_ENDIAN -- A 64-bit number in little-endian format. Equivalent to REG_QWORD.
Zachary Warefd2d4822015-05-13 01:21:57 -05001658 REG_RESOURCE_LIST -- A device-driver resource list.
1659 REG_SZ -- A null-terminated string.
1660 value: object
1661 A string that specifies the new value.
1662 /
1663
1664Stores data in the value field of an open registry key.
1665
1666This method can also set additional value and type information for the
1667specified key. The key identified by the key parameter must have been
1668opened with KEY_SET_VALUE access.
1669
1670To open the key, use the CreateKeyEx() or OpenKeyEx() methods.
1671
1672Value lengths are limited by available memory. Long values (more than
16732048 bytes) should be stored as files with the filenames stored in
1674the configuration registry to help the registry perform efficiently.
1675[clinic start generated code]*/
1676
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001677static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001678winreg_SetValueEx_impl(PyObject *module, HKEY key,
1679 const Py_UNICODE *value_name, PyObject *reserved,
1680 DWORD type, PyObject *value)
1681/*[clinic end generated code: output=811b769a66ae11b7 input=900a9e3990bfb196]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001682{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 BYTE *data;
1684 DWORD len;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 LONG rc;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001687
Zachary Warefd2d4822015-05-13 01:21:57 -05001688 if (!Py2Reg(value, type, &data, &len))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 {
1690 if (!PyErr_Occurred())
1691 PyErr_SetString(PyExc_ValueError,
1692 "Could not convert the data to the specified type.");
1693 return NULL;
1694 }
1695 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001696 rc = RegSetValueExW(key, value_name, 0, type, data, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 Py_END_ALLOW_THREADS
1698 PyMem_DEL(data);
1699 if (rc != ERROR_SUCCESS)
1700 return PyErr_SetFromWindowsErrWithFunction(rc,
1701 "RegSetValueEx");
Zachary Warefd2d4822015-05-13 01:21:57 -05001702 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001703}
1704
Zachary Warefd2d4822015-05-13 01:21:57 -05001705/*[clinic input]
1706winreg.DisableReflectionKey
1707
1708 key: HKEY
1709 An already open key, or any one of the predefined HKEY_* constants.
1710 /
1711
1712Disables registry reflection for 32bit processes running on a 64bit OS.
1713
Miss Islington (bot)dd5f8ab2019-08-04 06:43:30 -07001714Will generally raise NotImplementedError if executed on a 32bit OS.
Zachary Warefd2d4822015-05-13 01:21:57 -05001715
1716If the key is not on the reflection list, the function succeeds but has
1717no effect. Disabling reflection for a key does not affect reflection
1718of any subkeys.
1719[clinic start generated code]*/
1720
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001721static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001722winreg_DisableReflectionKey_impl(PyObject *module, HKEY key)
Miss Islington (bot)dd5f8ab2019-08-04 06:43:30 -07001723/*[clinic end generated code: output=830cce504cc764b4 input=70bece2dee02e073]*/
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001724{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 HMODULE hMod;
1726 typedef LONG (WINAPI *RDRKFunc)(HKEY);
1727 RDRKFunc pfn = NULL;
1728 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 /* Only available on 64bit platforms, so we must load it
1731 dynamically.*/
Tony Roberts4860f012019-02-02 18:16:42 +01001732 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis50590f12012-01-14 17:54:09 +01001733 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 if (hMod)
1735 pfn = (RDRKFunc)GetProcAddress(hMod,
1736 "RegDisableReflectionKey");
Tony Roberts4860f012019-02-02 18:16:42 +01001737 Py_END_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 if (!pfn) {
1739 PyErr_SetString(PyExc_NotImplementedError,
1740 "not implemented on this platform");
1741 return NULL;
1742 }
1743 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001744 rc = (*pfn)(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 Py_END_ALLOW_THREADS
1746 if (rc != ERROR_SUCCESS)
1747 return PyErr_SetFromWindowsErrWithFunction(rc,
1748 "RegDisableReflectionKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001749 Py_RETURN_NONE;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001750}
1751
Zachary Warefd2d4822015-05-13 01:21:57 -05001752/*[clinic input]
1753winreg.EnableReflectionKey
1754
1755 key: HKEY
1756 An already open key, or any one of the predefined HKEY_* constants.
1757 /
1758
1759Restores registry reflection for the specified disabled key.
1760
Miss Islington (bot)dd5f8ab2019-08-04 06:43:30 -07001761Will generally raise NotImplementedError if executed on a 32bit OS.
Zachary Warefd2d4822015-05-13 01:21:57 -05001762Restoring reflection for a key does not affect reflection of any
1763subkeys.
1764[clinic start generated code]*/
1765
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001766static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001767winreg_EnableReflectionKey_impl(PyObject *module, HKEY key)
Miss Islington (bot)dd5f8ab2019-08-04 06:43:30 -07001768/*[clinic end generated code: output=86fa1385fdd9ce57 input=eeae770c6eb9f559]*/
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 HMODULE hMod;
1771 typedef LONG (WINAPI *RERKFunc)(HKEY);
1772 RERKFunc pfn = NULL;
1773 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 /* Only available on 64bit platforms, so we must load it
1776 dynamically.*/
Tony Roberts4860f012019-02-02 18:16:42 +01001777 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis50590f12012-01-14 17:54:09 +01001778 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 if (hMod)
1780 pfn = (RERKFunc)GetProcAddress(hMod,
1781 "RegEnableReflectionKey");
Tony Roberts4860f012019-02-02 18:16:42 +01001782 Py_END_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 if (!pfn) {
1784 PyErr_SetString(PyExc_NotImplementedError,
1785 "not implemented on this platform");
1786 return NULL;
1787 }
1788 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001789 rc = (*pfn)(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 Py_END_ALLOW_THREADS
1791 if (rc != ERROR_SUCCESS)
1792 return PyErr_SetFromWindowsErrWithFunction(rc,
1793 "RegEnableReflectionKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001794 Py_RETURN_NONE;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001795}
1796
Zachary Warefd2d4822015-05-13 01:21:57 -05001797/*[clinic input]
1798winreg.QueryReflectionKey
1799
1800 key: HKEY
1801 An already open key, or any one of the predefined HKEY_* constants.
1802 /
1803
1804Returns the reflection state for the specified key as a bool.
1805
Miss Islington (bot)dd5f8ab2019-08-04 06:43:30 -07001806Will generally raise NotImplementedError if executed on a 32bit OS.
Zachary Warefd2d4822015-05-13 01:21:57 -05001807[clinic start generated code]*/
1808
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001809static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001810winreg_QueryReflectionKey_impl(PyObject *module, HKEY key)
Miss Islington (bot)dd5f8ab2019-08-04 06:43:30 -07001811/*[clinic end generated code: output=4e774af288c3ebb9 input=a98fa51d55ade186]*/
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001812{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 HMODULE hMod;
1814 typedef LONG (WINAPI *RQRKFunc)(HKEY, BOOL *);
1815 RQRKFunc pfn = NULL;
1816 BOOL result;
1817 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 /* Only available on 64bit platforms, so we must load it
1820 dynamically.*/
Tony Roberts4860f012019-02-02 18:16:42 +01001821 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis50590f12012-01-14 17:54:09 +01001822 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823 if (hMod)
1824 pfn = (RQRKFunc)GetProcAddress(hMod,
1825 "RegQueryReflectionKey");
Tony Roberts4860f012019-02-02 18:16:42 +01001826 Py_END_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 if (!pfn) {
1828 PyErr_SetString(PyExc_NotImplementedError,
1829 "not implemented on this platform");
1830 return NULL;
1831 }
1832 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001833 rc = (*pfn)(key, &result);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 Py_END_ALLOW_THREADS
1835 if (rc != ERROR_SUCCESS)
1836 return PyErr_SetFromWindowsErrWithFunction(rc,
1837 "RegQueryReflectionKey");
1838 return PyBool_FromLong(result);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001839}
1840
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001841static struct PyMethodDef winreg_methods[] = {
Zachary Warefd2d4822015-05-13 01:21:57 -05001842 WINREG_CLOSEKEY_METHODDEF
1843 WINREG_CONNECTREGISTRY_METHODDEF
1844 WINREG_CREATEKEY_METHODDEF
1845 WINREG_CREATEKEYEX_METHODDEF
1846 WINREG_DELETEKEY_METHODDEF
1847 WINREG_DELETEKEYEX_METHODDEF
1848 WINREG_DELETEVALUE_METHODDEF
1849 WINREG_DISABLEREFLECTIONKEY_METHODDEF
1850 WINREG_ENABLEREFLECTIONKEY_METHODDEF
1851 WINREG_ENUMKEY_METHODDEF
1852 WINREG_ENUMVALUE_METHODDEF
1853 WINREG_EXPANDENVIRONMENTSTRINGS_METHODDEF
1854 WINREG_FLUSHKEY_METHODDEF
1855 WINREG_LOADKEY_METHODDEF
1856 WINREG_OPENKEY_METHODDEF
1857 WINREG_OPENKEYEX_METHODDEF
1858 WINREG_QUERYVALUE_METHODDEF
1859 WINREG_QUERYVALUEEX_METHODDEF
1860 WINREG_QUERYINFOKEY_METHODDEF
1861 WINREG_QUERYREFLECTIONKEY_METHODDEF
1862 WINREG_SAVEKEY_METHODDEF
1863 WINREG_SETVALUE_METHODDEF
1864 WINREG_SETVALUEEX_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 NULL,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001866};
1867
1868static void
1869insint(PyObject * d, char * name, long value)
1870{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 PyObject *v = PyLong_FromLong(value);
1872 if (!v || PyDict_SetItemString(d, name, v))
1873 PyErr_Clear();
1874 Py_XDECREF(v);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001875}
1876
1877#define ADD_INT(val) insint(d, #val, val)
1878
1879static void
1880inskey(PyObject * d, char * name, HKEY key)
1881{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 PyObject *v = PyLong_FromVoidPtr(key);
1883 if (!v || PyDict_SetItemString(d, name, v))
1884 PyErr_Clear();
1885 Py_XDECREF(v);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001886}
1887
1888#define ADD_KEY(val) inskey(d, #val, val)
1889
Martin v. Löwis1a214512008-06-11 05:26:20 +00001890
1891static struct PyModuleDef winregmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 PyModuleDef_HEAD_INIT,
1893 "winreg",
1894 module_doc,
1895 -1,
1896 winreg_methods,
1897 NULL,
1898 NULL,
1899 NULL,
1900 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001901};
1902
1903PyMODINIT_FUNC PyInit_winreg(void)
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001904{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 PyObject *m, *d;
1906 m = PyModule_Create(&winregmodule);
1907 if (m == NULL)
1908 return NULL;
1909 d = PyModule_GetDict(m);
1910 PyHKEY_Type.tp_doc = PyHKEY_doc;
1911 if (PyType_Ready(&PyHKEY_Type) < 0)
1912 return NULL;
1913 Py_INCREF(&PyHKEY_Type);
1914 if (PyDict_SetItemString(d, "HKEYType",
1915 (PyObject *)&PyHKEY_Type) != 0)
1916 return NULL;
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001917 Py_INCREF(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 if (PyDict_SetItemString(d, "error",
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001919 PyExc_OSError) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 /* Add the relevant constants */
1923 ADD_KEY(HKEY_CLASSES_ROOT);
1924 ADD_KEY(HKEY_CURRENT_USER);
1925 ADD_KEY(HKEY_LOCAL_MACHINE);
1926 ADD_KEY(HKEY_USERS);
1927 ADD_KEY(HKEY_PERFORMANCE_DATA);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001928#ifdef HKEY_CURRENT_CONFIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001929 ADD_KEY(HKEY_CURRENT_CONFIG);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001930#endif
1931#ifdef HKEY_DYN_DATA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 ADD_KEY(HKEY_DYN_DATA);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001933#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 ADD_INT(KEY_QUERY_VALUE);
1935 ADD_INT(KEY_SET_VALUE);
1936 ADD_INT(KEY_CREATE_SUB_KEY);
1937 ADD_INT(KEY_ENUMERATE_SUB_KEYS);
1938 ADD_INT(KEY_NOTIFY);
1939 ADD_INT(KEY_CREATE_LINK);
1940 ADD_INT(KEY_READ);
1941 ADD_INT(KEY_WRITE);
1942 ADD_INT(KEY_EXECUTE);
1943 ADD_INT(KEY_ALL_ACCESS);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001944#ifdef KEY_WOW64_64KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 ADD_INT(KEY_WOW64_64KEY);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001946#endif
1947#ifdef KEY_WOW64_32KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 ADD_INT(KEY_WOW64_32KEY);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001949#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 ADD_INT(REG_OPTION_RESERVED);
1951 ADD_INT(REG_OPTION_NON_VOLATILE);
1952 ADD_INT(REG_OPTION_VOLATILE);
1953 ADD_INT(REG_OPTION_CREATE_LINK);
1954 ADD_INT(REG_OPTION_BACKUP_RESTORE);
1955 ADD_INT(REG_OPTION_OPEN_LINK);
1956 ADD_INT(REG_LEGAL_OPTION);
1957 ADD_INT(REG_CREATED_NEW_KEY);
1958 ADD_INT(REG_OPENED_EXISTING_KEY);
1959 ADD_INT(REG_WHOLE_HIVE_VOLATILE);
1960 ADD_INT(REG_REFRESH_HIVE);
1961 ADD_INT(REG_NO_LAZY_FLUSH);
1962 ADD_INT(REG_NOTIFY_CHANGE_NAME);
1963 ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES);
1964 ADD_INT(REG_NOTIFY_CHANGE_LAST_SET);
1965 ADD_INT(REG_NOTIFY_CHANGE_SECURITY);
1966 ADD_INT(REG_LEGAL_CHANGE_FILTER);
1967 ADD_INT(REG_NONE);
1968 ADD_INT(REG_SZ);
1969 ADD_INT(REG_EXPAND_SZ);
1970 ADD_INT(REG_BINARY);
1971 ADD_INT(REG_DWORD);
1972 ADD_INT(REG_DWORD_LITTLE_ENDIAN);
1973 ADD_INT(REG_DWORD_BIG_ENDIAN);
Steve Dower80ac11d2016-05-24 15:42:04 -07001974 ADD_INT(REG_QWORD);
1975 ADD_INT(REG_QWORD_LITTLE_ENDIAN);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 ADD_INT(REG_LINK);
1977 ADD_INT(REG_MULTI_SZ);
1978 ADD_INT(REG_RESOURCE_LIST);
1979 ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
1980 ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
1981 return m;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001982}
1983
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001984