blob: 5f5fc85d2250759234a74b57a9b613f6dc9ca647 [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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 Q = data + len;
522 for (P = data, i = 0; P < Q && *P != '\0'; P++, i++) {
523 str[i] = P;
Zackery Spytz56ed8642019-04-22 11:01:32 -0600524 for (; P < Q && *P != '\0'; P++)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 ;
526 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000527}
528
529static int
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000530countStrings(wchar_t *data, int len)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000531{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 int strings;
533 wchar_t *P;
534 wchar_t *Q = data + len;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 for (P = data, strings = 0; P < Q && *P != '\0'; P++, strings++)
537 for (; P < Q && *P != '\0'; P++)
538 ;
539 return strings;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000540}
541
542/* Convert PyObject into Registry data.
543 Allocates space as needed. */
544static BOOL
545Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
546{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 Py_ssize_t i,j;
548 switch (typ) {
549 case REG_DWORD:
550 if (value != Py_None && !PyLong_Check(value))
551 return FALSE;
552 *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1);
Steve Dower4d4bc422016-05-25 11:26:07 -0700553 if (*retDataBuf == NULL){
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 PyErr_NoMemory();
555 return FALSE;
556 }
557 *retDataSize = sizeof(DWORD);
558 if (value == Py_None) {
559 DWORD zero = 0;
560 memcpy(*retDataBuf, &zero, sizeof(DWORD));
561 }
562 else {
Brian Curtin12706f22012-12-27 10:12:45 -0600563 DWORD d = PyLong_AsUnsignedLong(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 memcpy(*retDataBuf, &d, sizeof(DWORD));
565 }
566 break;
Steve Dower80ac11d2016-05-24 15:42:04 -0700567 case REG_QWORD:
568 if (value != Py_None && !PyLong_Check(value))
569 return FALSE;
570 *retDataBuf = (BYTE *)PyMem_NEW(DWORD64, 1);
Steve Dower4d4bc422016-05-25 11:26:07 -0700571 if (*retDataBuf == NULL){
Steve Dower80ac11d2016-05-24 15:42:04 -0700572 PyErr_NoMemory();
573 return FALSE;
574 }
575 *retDataSize = sizeof(DWORD64);
576 if (value == Py_None) {
577 DWORD64 zero = 0;
578 memcpy(*retDataBuf, &zero, sizeof(DWORD64));
579 }
580 else {
581 DWORD64 d = PyLong_AsUnsignedLongLong(value);
582 memcpy(*retDataBuf, &d, sizeof(DWORD64));
583 }
584 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 case REG_SZ:
586 case REG_EXPAND_SZ:
587 {
Victor Stinnerbe492442011-11-21 12:43:50 +0100588 if (value != Py_None) {
589 Py_ssize_t len;
590 if (!PyUnicode_Check(value))
591 return FALSE;
592 *retDataBuf = (BYTE*)PyUnicode_AsWideCharString(value, &len);
593 if (*retDataBuf == NULL)
594 return FALSE;
595 *retDataSize = Py_SAFE_DOWNCAST(
596 (len + 1) * sizeof(wchar_t),
597 Py_ssize_t, DWORD);
598 }
599 else {
600 *retDataBuf = (BYTE *)PyMem_NEW(wchar_t, 1);
601 if (*retDataBuf == NULL) {
602 PyErr_NoMemory();
603 return FALSE;
604 }
605 ((wchar_t *)*retDataBuf)[0] = L'\0';
606 *retDataSize = 1 * sizeof(wchar_t);
607 }
608 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 }
610 case REG_MULTI_SZ:
611 {
612 DWORD size = 0;
613 wchar_t *P;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 if (value == Py_None)
616 i = 0;
617 else {
618 if (!PyList_Check(value))
619 return FALSE;
620 i = PyList_Size(value);
621 }
622 for (j = 0; j < i; j++)
623 {
624 PyObject *t;
Victor Stinnerbe492442011-11-21 12:43:50 +0100625 wchar_t *wstr;
626 Py_ssize_t len;
627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 t = PyList_GET_ITEM(value, j);
629 if (!PyUnicode_Check(t))
630 return FALSE;
Victor Stinnerbe492442011-11-21 12:43:50 +0100631 wstr = PyUnicode_AsUnicodeAndSize(t, &len);
632 if (wstr == NULL)
633 return FALSE;
634 size += Py_SAFE_DOWNCAST((len + 1) * sizeof(wchar_t),
Brian Curtinabb33512010-08-17 20:08:40 +0000635 size_t, DWORD);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 *retDataSize = size + 2;
639 *retDataBuf = (BYTE *)PyMem_NEW(char,
640 *retDataSize);
Steve Dower4d4bc422016-05-25 11:26:07 -0700641 if (*retDataBuf == NULL){
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 PyErr_NoMemory();
643 return FALSE;
644 }
645 P = (wchar_t *)*retDataBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 for (j = 0; j < i; j++)
648 {
649 PyObject *t;
Victor Stinnerbe492442011-11-21 12:43:50 +0100650 wchar_t *wstr;
651 Py_ssize_t len;
652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 t = PyList_GET_ITEM(value, j);
Victor Stinnerbe492442011-11-21 12:43:50 +0100654 wstr = PyUnicode_AsUnicodeAndSize(t, &len);
Zackery Spytz5d953122018-11-08 02:45:00 -0700655 assert(wstr);
Victor Stinnerbe492442011-11-21 12:43:50 +0100656 wcscpy(P, wstr);
657 P += (len + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 }
659 /* And doubly-terminate the list... */
660 *P = '\0';
661 break;
662 }
663 case REG_BINARY:
664 /* ALSO handle ALL unknown data types here. Even if we can't
665 support it natively, we should handle the bits. */
666 default:
Zachary Waread4690f2014-07-03 10:58:06 -0500667 if (value == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 *retDataSize = 0;
Zachary Waread4690f2014-07-03 10:58:06 -0500669 *retDataBuf = NULL;
670 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 else {
672 Py_buffer view;
Neal Norwitz1385b892007-08-26 23:07:13 +0000673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 if (!PyObject_CheckBuffer(value)) {
675 PyErr_Format(PyExc_TypeError,
676 "Objects of type '%s' can not "
677 "be used as binary registry values",
678 value->ob_type->tp_name);
679 return FALSE;
680 }
Neal Norwitz1385b892007-08-26 23:07:13 +0000681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0)
683 return FALSE;
Neal Norwitz1385b892007-08-26 23:07:13 +0000684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 *retDataBuf = (BYTE *)PyMem_NEW(char, view.len);
Steve Dower4d4bc422016-05-25 11:26:07 -0700686 if (*retDataBuf == NULL){
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 PyBuffer_Release(&view);
688 PyErr_NoMemory();
689 return FALSE;
690 }
Brian Curtinabb33512010-08-17 20:08:40 +0000691 *retDataSize = Py_SAFE_DOWNCAST(view.len, Py_ssize_t, DWORD);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 memcpy(*retDataBuf, view.buf, view.len);
693 PyBuffer_Release(&view);
694 }
695 break;
696 }
697 return TRUE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000698}
699
700/* Convert Registry data into PyObject*/
701static PyObject *
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000702Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000703{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 PyObject *obData;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 switch (typ) {
707 case REG_DWORD:
708 if (retDataSize == 0)
Brian Curtin172e4222012-12-27 14:04:42 -0600709 obData = PyLong_FromUnsignedLong(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 else
Steve Dower80ac11d2016-05-24 15:42:04 -0700711 obData = PyLong_FromUnsignedLong(*(DWORD *)retDataBuf);
712 break;
713 case REG_QWORD:
714 if (retDataSize == 0)
715 obData = PyLong_FromUnsignedLongLong(0);
716 else
717 obData = PyLong_FromUnsignedLongLong(*(DWORD64 *)retDataBuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 break;
719 case REG_SZ:
720 case REG_EXPAND_SZ:
721 {
Steve Dower40fa2662016-12-17 13:30:27 -0800722 /* REG_SZ should be a NUL terminated string, but only by
723 * convention. The buffer may have been saved without a NUL
724 * or with embedded NULs. To be consistent with reg.exe and
725 * regedit.exe, consume only up to the first NUL. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 wchar_t *data = (wchar_t *)retDataBuf;
Steve Dower40fa2662016-12-17 13:30:27 -0800727 size_t len = wcsnlen(data, retDataSize / sizeof(wchar_t));
728 obData = PyUnicode_FromWideChar(data, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 break;
730 }
731 case REG_MULTI_SZ:
732 if (retDataSize == 0)
733 obData = PyList_New(0);
734 else
735 {
736 int index = 0;
737 wchar_t *data = (wchar_t *)retDataBuf;
738 int len = retDataSize / 2;
739 int s = countStrings(data, len);
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +0200740 wchar_t **str = PyMem_New(wchar_t *, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 if (str == NULL)
742 return PyErr_NoMemory();
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 fixupMultiSZ(str, data, len);
745 obData = PyList_New(s);
Jesus Cea782c4cf2014-03-13 17:35:32 +0100746 if (obData == NULL) {
Victor Stinner9cb1ec52014-03-13 19:08:10 +0100747 PyMem_Free(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 return NULL;
Jesus Cea782c4cf2014-03-13 17:35:32 +0100749 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 for (index = 0; index < s; index++)
751 {
752 size_t len = wcslen(str[index]);
753 if (len > INT_MAX) {
754 PyErr_SetString(PyExc_OverflowError,
755 "registry string is too long for a Python string");
756 Py_DECREF(obData);
Victor Stinner9cb1ec52014-03-13 19:08:10 +0100757 PyMem_Free(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 return NULL;
759 }
Zackery Spytz99d56b52018-12-08 07:16:55 -0700760 PyObject *uni = PyUnicode_FromWideChar(str[index], len);
761 if (uni == NULL) {
762 Py_DECREF(obData);
763 PyMem_Free(str);
764 return NULL;
765 }
766 PyList_SET_ITEM(obData, index, uni);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 }
Victor Stinnerb6404912013-07-07 16:21:41 +0200768 PyMem_Free(str);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 break;
771 }
772 case REG_BINARY:
773 /* ALSO handle ALL unknown data types here. Even if we can't
774 support it natively, we should handle the bits. */
775 default:
776 if (retDataSize == 0) {
777 Py_INCREF(Py_None);
778 obData = Py_None;
779 }
780 else
781 obData = PyBytes_FromStringAndSize(
782 (char *)retDataBuf, retDataSize);
783 break;
784 }
785 return obData;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000786}
787
788/* The Python methods */
789
Zachary Warefd2d4822015-05-13 01:21:57 -0500790/*[clinic input]
791winreg.CloseKey
792
793 hkey: object
794 A previously opened key.
795 /
796
797Closes a previously opened registry key.
798
799Note that if the key is not closed using this method, it will be
800closed when the hkey object is destroyed by Python.
801[clinic start generated code]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000802
803static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300804winreg_CloseKey(PyObject *module, PyObject *hkey)
805/*[clinic end generated code: output=a4fa537019a80d15 input=5b1aac65ba5127ad]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000806{
Zachary Warefd2d4822015-05-13 01:21:57 -0500807 if (!PyHKEY_Close(hkey))
808 return NULL;
809 Py_RETURN_NONE;
810}
811
812/*[clinic input]
813winreg.ConnectRegistry -> HKEY
814
Zachary Ware77772c02015-05-13 10:58:35 -0500815 computer_name: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -0500816 The name of the remote computer, of the form r"\\computername". If
817 None, the local computer is used.
818 key: HKEY
819 The predefined key to connect to.
820 /
821
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +0300822Establishes a connection to the registry on another computer.
Zachary Warefd2d4822015-05-13 01:21:57 -0500823
824The return value is the handle of the opened key.
825If the function fails, an OSError exception is raised.
826[clinic start generated code]*/
827
828static HKEY
Serhiy Storchakaafb3e712018-12-14 11:19:51 +0200829winreg_ConnectRegistry_impl(PyObject *module,
830 const Py_UNICODE *computer_name, HKEY key)
831/*[clinic end generated code: output=cd4f70fb9ec901fb input=5f98a891a347e68e]*/
Zachary Warefd2d4822015-05-13 01:21:57 -0500832{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 HKEY retKey;
834 long rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -0500836 rc = RegConnectRegistryW(computer_name, key, &retKey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 Py_END_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -0500838 if (rc != ERROR_SUCCESS) {
839 PyErr_SetFromWindowsErrWithFunction(rc, "ConnectRegistry");
840 return NULL;
841 }
842 return retKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000843}
844
Zachary Warefd2d4822015-05-13 01:21:57 -0500845/*[clinic input]
846winreg.CreateKey -> HKEY
847
848 key: HKEY
849 An already open key, or one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -0500850 sub_key: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -0500851 The name of the key this method opens or creates.
852 /
853
854Creates or opens the specified key.
855
856If key is one of the predefined keys, sub_key may be None. In that case,
857the handle returned is the same key handle passed in to the function.
858
859If the key already exists, this function opens the existing key.
860
861The return value is the handle of the opened key.
862If the function fails, an OSError exception is raised.
863[clinic start generated code]*/
864
865static HKEY
Serhiy Storchakaafb3e712018-12-14 11:19:51 +0200866winreg_CreateKey_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key)
867/*[clinic end generated code: output=2af13910d56eae26 input=3cdd1622488acea2]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000868{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 HKEY retKey;
870 long rc;
Zachary Warefd2d4822015-05-13 01:21:57 -0500871
872 rc = RegCreateKeyW(key, sub_key, &retKey);
873 if (rc != ERROR_SUCCESS) {
874 PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 return NULL;
Zachary Warefd2d4822015-05-13 01:21:57 -0500876 }
877 return retKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000878}
879
Zachary Warefd2d4822015-05-13 01:21:57 -0500880/*[clinic input]
881winreg.CreateKeyEx -> HKEY
882
883 key: HKEY
884 An already open key, or one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -0500885 sub_key: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -0500886 The name of the key this method opens or creates.
887 reserved: int = 0
888 A reserved integer, and must be zero. Default is zero.
889 access: REGSAM(c_default='KEY_WRITE') = winreg.KEY_WRITE
890 An integer that specifies an access mask that describes the
891 desired security access for the key. Default is KEY_WRITE.
892
893Creates or opens the specified key.
894
895If key is one of the predefined keys, sub_key may be None. In that case,
896the handle returned is the same key handle passed in to the function.
897
898If the key already exists, this function opens the existing key
899
900The return value is the handle of the opened key.
901If the function fails, an OSError exception is raised.
902[clinic start generated code]*/
903
904static HKEY
Serhiy Storchakaafb3e712018-12-14 11:19:51 +0200905winreg_CreateKeyEx_impl(PyObject *module, HKEY key,
906 const Py_UNICODE *sub_key, int reserved,
907 REGSAM access)
908/*[clinic end generated code: output=643a70ad6a361a97 input=42c2b03f98406b66]*/
Brian Curtin3035c392010-04-21 23:56:21 +0000909{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 HKEY retKey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 long rc;
Brian Curtin1771b542010-09-27 17:56:36 +0000912
Segev Finer679b5662017-07-27 01:17:57 +0300913 rc = RegCreateKeyExW(key, sub_key, reserved, NULL, 0,
Brian Curtin1771b542010-09-27 17:56:36 +0000914 access, NULL, &retKey, NULL);
Zachary Warefd2d4822015-05-13 01:21:57 -0500915 if (rc != ERROR_SUCCESS) {
916 PyErr_SetFromWindowsErrWithFunction(rc, "CreateKeyEx");
917 return NULL;
918 }
919 return retKey;
Brian Curtin3035c392010-04-21 23:56:21 +0000920}
921
Zachary Warefd2d4822015-05-13 01:21:57 -0500922/*[clinic input]
923winreg.DeleteKey
924 key: HKEY
925 An already open key, or any one of the predefined HKEY_* constants.
926 sub_key: Py_UNICODE
927 A string that must be the name of a subkey of the key identified by
928 the key parameter. This value must not be None, and the key may not
929 have subkeys.
930 /
931
932Deletes the specified key.
933
934This method can not delete keys with subkeys.
935
936If the function succeeds, the entire key, including all of its values,
937is removed. If the function fails, an OSError exception is raised.
938[clinic start generated code]*/
939
Brian Curtin3035c392010-04-21 23:56:21 +0000940static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +0200941winreg_DeleteKey_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key)
942/*[clinic end generated code: output=d2652a84f70e0862 input=b31d225b935e4211]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000943{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 long rc;
Zachary Warefd2d4822015-05-13 01:21:57 -0500945 rc = RegDeleteKeyW(key, sub_key );
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 if (rc != ERROR_SUCCESS)
947 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey");
Zachary Warefd2d4822015-05-13 01:21:57 -0500948 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000949}
950
Zachary Warefd2d4822015-05-13 01:21:57 -0500951/*[clinic input]
952winreg.DeleteKeyEx
953
954 key: HKEY
955 An already open key, or any one of the predefined HKEY_* constants.
956 sub_key: Py_UNICODE
957 A string that must be the name of a subkey of the key identified by
958 the key parameter. This value must not be None, and the key may not
959 have subkeys.
960 access: REGSAM(c_default='KEY_WOW64_64KEY') = winreg.KEY_WOW64_64KEY
961 An integer that specifies an access mask that describes the
962 desired security access for the key. Default is KEY_WOW64_64KEY.
963 reserved: int = 0
964 A reserved integer, and must be zero. Default is zero.
965
966Deletes the specified key (64-bit OS only).
967
968This method can not delete keys with subkeys.
969
970If the function succeeds, the entire key, including all of its values,
971is removed. If the function fails, an OSError exception is raised.
972On unsupported Windows versions, NotImplementedError is raised.
973[clinic start generated code]*/
974
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000975static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +0200976winreg_DeleteKeyEx_impl(PyObject *module, HKEY key,
977 const Py_UNICODE *sub_key, REGSAM access,
978 int reserved)
979/*[clinic end generated code: output=52a1c8b374ebc003 input=711d9d89e7ecbed7]*/
Brian Curtin3035c392010-04-21 23:56:21 +0000980{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 HMODULE hMod;
982 typedef LONG (WINAPI *RDKEFunc)(HKEY, const wchar_t*, REGSAM, int);
983 RDKEFunc pfn = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 long rc;
Brian Curtin3035c392010-04-21 23:56:21 +0000985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 /* Only available on 64bit platforms, so we must load it
987 dynamically. */
Tony Roberts4860f012019-02-02 18:16:42 +0100988 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis50590f12012-01-14 17:54:09 +0100989 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 if (hMod)
991 pfn = (RDKEFunc)GetProcAddress(hMod,
992 "RegDeleteKeyExW");
Tony Roberts4860f012019-02-02 18:16:42 +0100993 Py_END_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 if (!pfn) {
995 PyErr_SetString(PyExc_NotImplementedError,
996 "not implemented on this platform");
997 return NULL;
998 }
999 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001000 rc = (*pfn)(key, sub_key, access, reserved);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 Py_END_ALLOW_THREADS
Brian Curtin3035c392010-04-21 23:56:21 +00001002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 if (rc != ERROR_SUCCESS)
1004 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKeyEx");
Zachary Warefd2d4822015-05-13 01:21:57 -05001005 Py_RETURN_NONE;
Brian Curtin3035c392010-04-21 23:56:21 +00001006}
1007
Zachary Warefd2d4822015-05-13 01:21:57 -05001008/*[clinic input]
1009winreg.DeleteValue
1010
1011 key: HKEY
1012 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001013 value: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001014 A string that identifies the value to remove.
1015 /
1016
1017Removes a named value from a registry key.
1018[clinic start generated code]*/
1019
Brian Curtin3035c392010-04-21 23:56:21 +00001020static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001021winreg_DeleteValue_impl(PyObject *module, HKEY key, const Py_UNICODE *value)
1022/*[clinic end generated code: output=56fa9d21f3a54371 input=a78d3407a4197b21]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001023{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 long rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001026 rc = RegDeleteValueW(key, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 Py_END_ALLOW_THREADS
1028 if (rc !=ERROR_SUCCESS)
1029 return PyErr_SetFromWindowsErrWithFunction(rc,
1030 "RegDeleteValue");
Zachary Warefd2d4822015-05-13 01:21:57 -05001031 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001032}
1033
Zachary Warefd2d4822015-05-13 01:21:57 -05001034/*[clinic input]
1035winreg.EnumKey
1036
1037 key: HKEY
1038 An already open key, or any one of the predefined HKEY_* constants.
1039 index: int
1040 An integer that identifies the index of the key to retrieve.
1041 /
1042
1043Enumerates subkeys of an open registry key.
1044
1045The function retrieves the name of one subkey each time it is called.
1046It is typically called repeatedly until an OSError exception is
1047raised, indicating no more values are available.
1048[clinic start generated code]*/
1049
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001050static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001051winreg_EnumKey_impl(PyObject *module, HKEY key, int index)
1052/*[clinic end generated code: output=25a6ec52cd147bc4 input=fad9a7c00ab0e04b]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 long rc;
1055 PyObject *retStr;
Brian Curtin60853212010-05-26 17:43:50 +00001056
1057 /* The Windows docs claim that the max key name length is 255
1058 * characters, plus a terminating nul character. However,
1059 * empirical testing demonstrates that it is possible to
1060 * create a 256 character key that is missing the terminating
1061 * nul. RegEnumKeyEx requires a 257 character buffer to
1062 * retrieve such a key name. */
1063 wchar_t tmpbuf[257];
Kristján Valur Jónsson984dfa72012-04-02 15:23:29 +00001064 DWORD len = sizeof(tmpbuf)/sizeof(wchar_t); /* includes NULL terminator */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001067 rc = RegEnumKeyExW(key, index, tmpbuf, &len, NULL, NULL, NULL, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 Py_END_ALLOW_THREADS
1069 if (rc != ERROR_SUCCESS)
1070 return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx");
Tim Peters313fcd42006-02-19 04:05:39 +00001071
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001072 retStr = PyUnicode_FromWideChar(tmpbuf, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 return retStr; /* can be NULL */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001074}
1075
Zachary Warefd2d4822015-05-13 01:21:57 -05001076/*[clinic input]
1077winreg.EnumValue
1078
1079 key: HKEY
1080 An already open key, or any one of the predefined HKEY_* constants.
1081 index: int
1082 An integer that identifies the index of the value to retrieve.
1083 /
1084
1085Enumerates values of an open registry key.
1086
1087The function retrieves the name of one subkey each time it is called.
1088It is typically called repeatedly, until an OSError exception
1089is raised, indicating no more values.
1090
1091The result is a tuple of 3 items:
1092 value_name
1093 A string that identifies the value.
1094 value_data
1095 An object that holds the value data, and whose type depends
1096 on the underlying registry type.
1097 data_type
1098 An integer that identifies the type of the value data.
1099[clinic start generated code]*/
1100
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001101static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001102winreg_EnumValue_impl(PyObject *module, HKEY key, int index)
1103/*[clinic end generated code: output=d363b5a06f8789ac input=4414f47a6fb238b5]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 long rc;
1106 wchar_t *retValueBuf;
Amaury Forgeot d'Arcf2b69df2010-08-16 22:11:29 +00001107 BYTE *tmpBuf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 BYTE *retDataBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001109 DWORD retValueSize, bufValueSize;
1110 DWORD retDataSize, bufDataSize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 DWORD typ;
1112 PyObject *obData;
1113 PyObject *retVal;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001114
Zachary Warefd2d4822015-05-13 01:21:57 -05001115 if ((rc = RegQueryInfoKeyW(key, NULL, NULL, NULL, NULL, NULL, NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 NULL,
1117 &retValueSize, &retDataSize, NULL, NULL))
1118 != ERROR_SUCCESS)
1119 return PyErr_SetFromWindowsErrWithFunction(rc,
1120 "RegQueryInfoKey");
1121 ++retValueSize; /* include null terminators */
1122 ++retDataSize;
Brian Curtin60853212010-05-26 17:43:50 +00001123 bufDataSize = retDataSize;
1124 bufValueSize = retValueSize;
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02001125 retValueBuf = PyMem_New(wchar_t, retValueSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 if (retValueBuf == NULL)
1127 return PyErr_NoMemory();
1128 retDataBuf = (BYTE *)PyMem_Malloc(retDataSize);
1129 if (retDataBuf == NULL) {
1130 PyMem_Free(retValueBuf);
1131 return PyErr_NoMemory();
1132 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001133
Brian Curtin60853212010-05-26 17:43:50 +00001134 while (1) {
Brian Curtin60853212010-05-26 17:43:50 +00001135 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001136 rc = RegEnumValueW(key,
Brian Curtin60853212010-05-26 17:43:50 +00001137 index,
1138 retValueBuf,
1139 &retValueSize,
1140 NULL,
1141 &typ,
1142 (BYTE *)retDataBuf,
1143 &retDataSize);
1144 Py_END_ALLOW_THREADS
1145
1146 if (rc != ERROR_MORE_DATA)
1147 break;
1148
1149 bufDataSize *= 2;
Amaury Forgeot d'Arcf2b69df2010-08-16 22:11:29 +00001150 tmpBuf = (BYTE *)PyMem_Realloc(retDataBuf, bufDataSize);
Brian Curtin9b7e2d12010-06-08 20:57:52 +00001151 if (tmpBuf == NULL) {
Brian Curtin60853212010-05-26 17:43:50 +00001152 PyErr_NoMemory();
1153 retVal = NULL;
1154 goto fail;
1155 }
Brian Curtin9b7e2d12010-06-08 20:57:52 +00001156 retDataBuf = tmpBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001157 retDataSize = bufDataSize;
1158 retValueSize = bufValueSize;
1159 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 if (rc != ERROR_SUCCESS) {
1162 retVal = PyErr_SetFromWindowsErrWithFunction(rc,
1163 "PyRegEnumValue");
1164 goto fail;
1165 }
1166 obData = Reg2Py(retDataBuf, retDataSize, typ);
1167 if (obData == NULL) {
1168 retVal = NULL;
1169 goto fail;
1170 }
1171 retVal = Py_BuildValue("uOi", retValueBuf, obData, typ);
1172 Py_DECREF(obData);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001173 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 PyMem_Free(retValueBuf);
1175 PyMem_Free(retDataBuf);
1176 return retVal;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001177}
1178
Zachary Warefd2d4822015-05-13 01:21:57 -05001179/*[clinic input]
1180winreg.ExpandEnvironmentStrings
1181
1182 string: Py_UNICODE
1183 /
1184
1185Expand environment vars.
1186[clinic start generated code]*/
1187
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001188static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001189winreg_ExpandEnvironmentStrings_impl(PyObject *module,
1190 const Py_UNICODE *string)
1191/*[clinic end generated code: output=8fa4e959747a7312 input=b2a9714d2b751aa6]*/
Christian Heimes2380ac72008-01-09 00:17:24 +00001192{
Victor Stinner9d3b93b2011-11-22 02:27:30 +01001193 wchar_t *retValue = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 DWORD retValueSize;
1195 DWORD rc;
1196 PyObject *o;
Christian Heimes2380ac72008-01-09 00:17:24 +00001197
Zachary Warefd2d4822015-05-13 01:21:57 -05001198 retValueSize = ExpandEnvironmentStringsW(string, retValue, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 if (retValueSize == 0) {
1200 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1201 "ExpandEnvironmentStrings");
1202 }
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02001203 retValue = PyMem_New(wchar_t, retValueSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 if (retValue == NULL) {
1205 return PyErr_NoMemory();
1206 }
Christian Heimes2380ac72008-01-09 00:17:24 +00001207
Zachary Warefd2d4822015-05-13 01:21:57 -05001208 rc = ExpandEnvironmentStringsW(string, retValue, retValueSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 if (rc == 0) {
1210 PyMem_Free(retValue);
1211 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1212 "ExpandEnvironmentStrings");
1213 }
Victor Stinner9d3b93b2011-11-22 02:27:30 +01001214 o = PyUnicode_FromWideChar(retValue, wcslen(retValue));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 PyMem_Free(retValue);
1216 return o;
Christian Heimes2380ac72008-01-09 00:17:24 +00001217}
1218
Zachary Warefd2d4822015-05-13 01:21:57 -05001219/*[clinic input]
1220winreg.FlushKey
1221
1222 key: HKEY
1223 An already open key, or any one of the predefined HKEY_* constants.
1224 /
1225
1226Writes all the attributes of a key to the registry.
1227
1228It is not necessary to call FlushKey to change a key. Registry changes
1229are flushed to disk by the registry using its lazy flusher. Registry
1230changes are also flushed to disk at system shutdown. Unlike
1231CloseKey(), the FlushKey() method returns only when all the data has
1232been written to the registry.
1233
1234An application should only call FlushKey() if it requires absolute
1235certainty that registry changes are on disk. If you don't know whether
1236a FlushKey() call is required, it probably isn't.
1237[clinic start generated code]*/
1238
Christian Heimes2380ac72008-01-09 00:17:24 +00001239static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001240winreg_FlushKey_impl(PyObject *module, HKEY key)
1241/*[clinic end generated code: output=e6fc230d4c5dc049 input=f57457c12297d82f]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001242{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 long rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001245 rc = RegFlushKey(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 Py_END_ALLOW_THREADS
1247 if (rc != ERROR_SUCCESS)
1248 return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001249 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001250}
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001251
Zachary Warefd2d4822015-05-13 01:21:57 -05001252
1253/*[clinic input]
1254winreg.LoadKey
1255
1256 key: HKEY
1257 An already open key, or any one of the predefined HKEY_* constants.
1258 sub_key: Py_UNICODE
1259 A string that identifies the sub-key to load.
1260 file_name: Py_UNICODE
1261 The name of the file to load registry data from. This file must
1262 have been created with the SaveKey() function. Under the file
1263 allocation table (FAT) file system, the filename may not have an
1264 extension.
1265 /
1266
1267Insert data into the registry from a file.
1268
1269Creates a subkey under the specified key and stores registration
1270information from a specified file into that subkey.
1271
1272A call to LoadKey() fails if the calling process does not have the
1273SE_RESTORE_PRIVILEGE privilege.
1274
1275If key is a handle returned by ConnectRegistry(), then the path
1276specified in fileName is relative to the remote computer.
1277
1278The MSDN docs imply key must be in the HKEY_USER or HKEY_LOCAL_MACHINE
1279tree.
1280[clinic start generated code]*/
1281
1282static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001283winreg_LoadKey_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key,
1284 const Py_UNICODE *file_name)
1285/*[clinic end generated code: output=65f89f2548cb27c7 input=e3b5b45ade311582]*/
Zachary Warefd2d4822015-05-13 01:21:57 -05001286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 long rc;
Zachary Warefd2d4822015-05-13 01:21:57 -05001288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001290 rc = RegLoadKeyW(key, sub_key, file_name );
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 Py_END_ALLOW_THREADS
1292 if (rc != ERROR_SUCCESS)
1293 return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001294 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001295}
1296
Zachary Warefd2d4822015-05-13 01:21:57 -05001297/*[clinic input]
1298winreg.OpenKey -> HKEY
1299
1300 key: HKEY
1301 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001302 sub_key: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001303 A string that identifies the sub_key to open.
1304 reserved: int = 0
1305 A reserved integer that must be zero. Default is zero.
1306 access: REGSAM(c_default='KEY_READ') = winreg.KEY_READ
1307 An integer that specifies an access mask that describes the desired
1308 security access for the key. Default is KEY_READ.
1309
1310Opens the specified key.
1311
1312The result is a new handle to the specified key.
1313If the function fails, an OSError exception is raised.
1314[clinic start generated code]*/
1315
1316static HKEY
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001317winreg_OpenKey_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key,
Zachary Ware77772c02015-05-13 10:58:35 -05001318 int reserved, REGSAM access)
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001319/*[clinic end generated code: output=8849bff2c30104ad input=098505ac36a9ae28]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001320{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 HKEY retKey;
1322 long rc;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001325 rc = RegOpenKeyExW(key, sub_key, reserved, access, &retKey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 Py_END_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001327 if (rc != ERROR_SUCCESS) {
1328 PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx");
1329 return NULL;
1330 }
1331 return retKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001332}
1333
Zachary Warefd2d4822015-05-13 01:21:57 -05001334/*[clinic input]
1335winreg.OpenKeyEx = winreg.OpenKey
1336
1337Opens the specified key.
1338
1339The result is a new handle to the specified key.
1340If the function fails, an OSError exception is raised.
1341[clinic start generated code]*/
1342
1343static HKEY
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001344winreg_OpenKeyEx_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key,
Zachary Ware77772c02015-05-13 10:58:35 -05001345 int reserved, REGSAM access)
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001346/*[clinic end generated code: output=81bc2bd684bc77ae input=c6c4972af8622959]*/
Zachary Warefd2d4822015-05-13 01:21:57 -05001347{
1348 return winreg_OpenKey_impl(module, key, sub_key, reserved, access);
1349}
1350
1351/*[clinic input]
1352winreg.QueryInfoKey
1353
1354 key: HKEY
1355 An already open key, or any one of the predefined HKEY_* constants.
1356 /
1357
1358Returns information about a key.
1359
1360The result is a tuple of 3 items:
1361An integer that identifies the number of sub keys this key has.
1362An integer that identifies the number of values this key has.
1363An integer that identifies when the key was last modified (if available)
1364as 100's of nanoseconds since Jan 1, 1600.
1365[clinic start generated code]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001366
1367static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001368winreg_QueryInfoKey_impl(PyObject *module, HKEY key)
1369/*[clinic end generated code: output=dc657b8356a4f438 input=c3593802390cde1f]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001370{
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001371 long rc;
1372 DWORD nSubKeys, nValues;
1373 FILETIME ft;
1374 LARGE_INTEGER li;
1375 PyObject *l;
1376 PyObject *ret;
Zachary Warefd2d4822015-05-13 01:21:57 -05001377
1378 if ((rc = RegQueryInfoKey(key, NULL, NULL, 0, &nSubKeys, NULL, NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 &nValues, NULL, NULL, NULL, &ft))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001380 != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey");
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001382 li.LowPart = ft.dwLowDateTime;
1383 li.HighPart = ft.dwHighDateTime;
1384 l = PyLong_FromLongLong(li.QuadPart);
1385 if (l == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001387 ret = Py_BuildValue("iiO", nSubKeys, nValues, l);
1388 Py_DECREF(l);
1389 return ret;
1390}
1391
Zachary Warefd2d4822015-05-13 01:21:57 -05001392/*[clinic input]
1393winreg.QueryValue
1394
1395 key: HKEY
1396 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001397 sub_key: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001398 A string that holds the name of the subkey with which the value
1399 is associated. If this parameter is None or empty, the function
1400 retrieves the value set by the SetValue() method for the key
1401 identified by key.
1402 /
1403
1404Retrieves the unnamed value for a key.
1405
1406Values in the registry have name, type, and data components. This method
1407retrieves the data for a key's first value that has a NULL name.
1408But since the underlying API call doesn't return the type, you'll
1409probably be happier using QueryValueEx; this function is just here for
1410completeness.
1411[clinic start generated code]*/
1412
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001413static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001414winreg_QueryValue_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key)
1415/*[clinic end generated code: output=c655810ae50c63a9 input=41cafbbf423b21d6]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001416{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 long rc;
1418 PyObject *retStr;
1419 wchar_t *retBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001420 DWORD bufSize = 0;
1421 DWORD retSize = 0;
1422 wchar_t *tmp;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001423
Zachary Warefd2d4822015-05-13 01:21:57 -05001424 rc = RegQueryValueW(key, sub_key, NULL, &retSize);
Brian Curtin60853212010-05-26 17:43:50 +00001425 if (rc == ERROR_MORE_DATA)
1426 retSize = 256;
1427 else if (rc != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 return PyErr_SetFromWindowsErrWithFunction(rc,
1429 "RegQueryValue");
Brian Curtin60853212010-05-26 17:43:50 +00001430
1431 bufSize = retSize;
1432 retBuf = (wchar_t *) PyMem_Malloc(bufSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 if (retBuf == NULL)
1434 return PyErr_NoMemory();
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001435
Brian Curtin60853212010-05-26 17:43:50 +00001436 while (1) {
1437 retSize = bufSize;
Zachary Warefd2d4822015-05-13 01:21:57 -05001438 rc = RegQueryValueW(key, sub_key, retBuf, &retSize);
Brian Curtin60853212010-05-26 17:43:50 +00001439 if (rc != ERROR_MORE_DATA)
1440 break;
1441
1442 bufSize *= 2;
1443 tmp = (wchar_t *) PyMem_Realloc(retBuf, bufSize);
1444 if (tmp == NULL) {
1445 PyMem_Free(retBuf);
1446 return PyErr_NoMemory();
1447 }
1448 retBuf = tmp;
1449 }
1450
1451 if (rc != ERROR_SUCCESS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 PyMem_Free(retBuf);
1453 return PyErr_SetFromWindowsErrWithFunction(rc,
1454 "RegQueryValue");
1455 }
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001456
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001457 retStr = PyUnicode_FromWideChar(retBuf, wcslen(retBuf));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 PyMem_Free(retBuf);
1459 return retStr;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001460}
1461
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001462
Zachary Warefd2d4822015-05-13 01:21:57 -05001463/*[clinic input]
1464winreg.QueryValueEx
1465
1466 key: HKEY
1467 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001468 name: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001469 A string indicating the value to query.
1470 /
1471
1472Retrieves the type and value of a specified sub-key.
1473
1474Behaves mostly like QueryValue(), but also returns the type of the
1475specified value name associated with the given open registry key.
1476
1477The return value is a tuple of the value and the type_id.
1478[clinic start generated code]*/
1479
1480static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001481winreg_QueryValueEx_impl(PyObject *module, HKEY key, const Py_UNICODE *name)
1482/*[clinic end generated code: output=f1b85b1c3d887ec7 input=cf366cada4836891]*/
Zachary Warefd2d4822015-05-13 01:21:57 -05001483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 long rc;
Brian Curtin60853212010-05-26 17:43:50 +00001485 BYTE *retBuf, *tmp;
1486 DWORD bufSize = 0, retSize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 DWORD typ;
1488 PyObject *obData;
1489 PyObject *result;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001490
Zachary Warefd2d4822015-05-13 01:21:57 -05001491 rc = RegQueryValueExW(key, name, NULL, NULL, NULL, &bufSize);
Brian Curtin60853212010-05-26 17:43:50 +00001492 if (rc == ERROR_MORE_DATA)
1493 bufSize = 256;
1494 else if (rc != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 return PyErr_SetFromWindowsErrWithFunction(rc,
1496 "RegQueryValueEx");
1497 retBuf = (BYTE *)PyMem_Malloc(bufSize);
1498 if (retBuf == NULL)
1499 return PyErr_NoMemory();
Brian Curtin60853212010-05-26 17:43:50 +00001500
1501 while (1) {
1502 retSize = bufSize;
Zachary Warefd2d4822015-05-13 01:21:57 -05001503 rc = RegQueryValueExW(key, name, NULL, &typ,
Brian Curtin60853212010-05-26 17:43:50 +00001504 (BYTE *)retBuf, &retSize);
1505 if (rc != ERROR_MORE_DATA)
1506 break;
1507
1508 bufSize *= 2;
1509 tmp = (char *) PyMem_Realloc(retBuf, bufSize);
1510 if (tmp == NULL) {
1511 PyMem_Free(retBuf);
1512 return PyErr_NoMemory();
1513 }
1514 retBuf = tmp;
1515 }
1516
1517 if (rc != ERROR_SUCCESS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 PyMem_Free(retBuf);
1519 return PyErr_SetFromWindowsErrWithFunction(rc,
1520 "RegQueryValueEx");
1521 }
1522 obData = Reg2Py(retBuf, bufSize, typ);
1523 PyMem_Free(retBuf);
1524 if (obData == NULL)
1525 return NULL;
1526 result = Py_BuildValue("Oi", obData, typ);
1527 Py_DECREF(obData);
1528 return result;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001529}
1530
Zachary Warefd2d4822015-05-13 01:21:57 -05001531/*[clinic input]
1532winreg.SaveKey
1533
1534 key: HKEY
1535 An already open key, or any one of the predefined HKEY_* constants.
1536 file_name: Py_UNICODE
1537 The name of the file to save registry data to. This file cannot
1538 already exist. If this filename includes an extension, it cannot be
1539 used on file allocation table (FAT) file systems by the LoadKey(),
1540 ReplaceKey() or RestoreKey() methods.
1541 /
1542
1543Saves the specified key, and all its subkeys to the specified file.
1544
1545If key represents a key on a remote computer, the path described by
1546file_name is relative to the remote computer.
1547
1548The caller of this method must possess the SeBackupPrivilege
1549security privilege. This function passes NULL for security_attributes
1550to the API.
1551[clinic start generated code]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001552
1553static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001554winreg_SaveKey_impl(PyObject *module, HKEY key, const Py_UNICODE *file_name)
1555/*[clinic end generated code: output=ca94b835c88f112b input=da735241f91ac7a2]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 LPSECURITY_ATTRIBUTES pSA = NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 long rc;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001560/* One day we may get security into the core?
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE))
1562 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001563*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001565 rc = RegSaveKeyW(key, file_name, pSA );
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 Py_END_ALLOW_THREADS
1567 if (rc != ERROR_SUCCESS)
1568 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001569 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001570}
1571
Zachary Warefd2d4822015-05-13 01:21:57 -05001572/*[clinic input]
1573winreg.SetValue
1574
1575 key: HKEY
1576 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001577 sub_key: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001578 A string that names the subkey with which the value is associated.
1579 type: DWORD
1580 An integer that specifies the type of the data. Currently this must
1581 be REG_SZ, meaning only strings are supported.
Zachary Ware77772c02015-05-13 10:58:35 -05001582 value: Py_UNICODE(zeroes=True)
Zachary Warefd2d4822015-05-13 01:21:57 -05001583 A string that specifies the new value.
1584 /
1585
1586Associates a value with a specified key.
1587
1588If the key specified by the sub_key parameter does not exist, the
1589SetValue function creates it.
1590
1591Value lengths are limited by available memory. Long values (more than
15922048 bytes) should be stored as files with the filenames stored in
1593the configuration registry to help the registry perform efficiently.
1594
1595The key identified by the key parameter must have been opened with
1596KEY_SET_VALUE access.
1597[clinic start generated code]*/
1598
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001599static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001600winreg_SetValue_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key,
1601 DWORD type, const Py_UNICODE *value,
Zachary Ware77772c02015-05-13 10:58:35 -05001602 Py_ssize_clean_t value_length)
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001603/*[clinic end generated code: output=686bedb1cbb4367b input=2cd2adab79339c53]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001604{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 long rc;
Zachary Warefd2d4822015-05-13 01:21:57 -05001606
1607 if (type != REG_SZ) {
Inada Naokicc60cdd92019-03-20 20:53:08 +09001608 PyErr_SetString(PyExc_TypeError, "type must be winreg.REG_SZ");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 return NULL;
1610 }
Inada Naokicc60cdd92019-03-20 20:53:08 +09001611 if ((size_t)value_length >= PY_DWORD_MAX) {
1612 PyErr_SetString(PyExc_OverflowError, "value is too long");
Inada Naokid5f18a62019-03-20 19:10:17 +09001613 return NULL;
1614 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 Py_BEGIN_ALLOW_THREADS
Zackery Spytz34366b72019-04-22 11:08:05 -06001617 rc = RegSetValueW(key, sub_key, REG_SZ, value, (DWORD)(value_length + 1));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 Py_END_ALLOW_THREADS
1619 if (rc != ERROR_SUCCESS)
1620 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue");
Zachary Warefd2d4822015-05-13 01:21:57 -05001621 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001622}
1623
Zachary Warefd2d4822015-05-13 01:21:57 -05001624/*[clinic input]
1625winreg.SetValueEx
1626
1627 key: HKEY
1628 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001629 value_name: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001630 A string containing the name of the value to set, or None.
1631 reserved: object
1632 Can be anything - zero is always passed to the API.
1633 type: DWORD
1634 An integer that specifies the type of the data, one of:
1635 REG_BINARY -- Binary data in any form.
1636 REG_DWORD -- A 32-bit number.
Steve Dower80ac11d2016-05-24 15:42:04 -07001637 REG_DWORD_LITTLE_ENDIAN -- A 32-bit number in little-endian format. Equivalent to REG_DWORD
Zachary Warefd2d4822015-05-13 01:21:57 -05001638 REG_DWORD_BIG_ENDIAN -- A 32-bit number in big-endian format.
1639 REG_EXPAND_SZ -- A null-terminated string that contains unexpanded
1640 references to environment variables (for example,
1641 %PATH%).
1642 REG_LINK -- A Unicode symbolic link.
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03001643 REG_MULTI_SZ -- A sequence of null-terminated strings, terminated
Zachary Warefd2d4822015-05-13 01:21:57 -05001644 by two null characters. Note that Python handles
1645 this termination automatically.
1646 REG_NONE -- No defined value type.
Steve Dower80ac11d2016-05-24 15:42:04 -07001647 REG_QWORD -- A 64-bit number.
1648 REG_QWORD_LITTLE_ENDIAN -- A 64-bit number in little-endian format. Equivalent to REG_QWORD.
Zachary Warefd2d4822015-05-13 01:21:57 -05001649 REG_RESOURCE_LIST -- A device-driver resource list.
1650 REG_SZ -- A null-terminated string.
1651 value: object
1652 A string that specifies the new value.
1653 /
1654
1655Stores data in the value field of an open registry key.
1656
1657This method can also set additional value and type information for the
1658specified key. The key identified by the key parameter must have been
1659opened with KEY_SET_VALUE access.
1660
1661To open the key, use the CreateKeyEx() or OpenKeyEx() methods.
1662
1663Value lengths are limited by available memory. Long values (more than
16642048 bytes) should be stored as files with the filenames stored in
1665the configuration registry to help the registry perform efficiently.
1666[clinic start generated code]*/
1667
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001668static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001669winreg_SetValueEx_impl(PyObject *module, HKEY key,
1670 const Py_UNICODE *value_name, PyObject *reserved,
1671 DWORD type, PyObject *value)
1672/*[clinic end generated code: output=811b769a66ae11b7 input=900a9e3990bfb196]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001673{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 BYTE *data;
1675 DWORD len;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 LONG rc;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001678
Zachary Warefd2d4822015-05-13 01:21:57 -05001679 if (!Py2Reg(value, type, &data, &len))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 {
1681 if (!PyErr_Occurred())
1682 PyErr_SetString(PyExc_ValueError,
1683 "Could not convert the data to the specified type.");
1684 return NULL;
1685 }
1686 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001687 rc = RegSetValueExW(key, value_name, 0, type, data, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 Py_END_ALLOW_THREADS
1689 PyMem_DEL(data);
1690 if (rc != ERROR_SUCCESS)
1691 return PyErr_SetFromWindowsErrWithFunction(rc,
1692 "RegSetValueEx");
Zachary Warefd2d4822015-05-13 01:21:57 -05001693 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001694}
1695
Zachary Warefd2d4822015-05-13 01:21:57 -05001696/*[clinic input]
1697winreg.DisableReflectionKey
1698
1699 key: HKEY
1700 An already open key, or any one of the predefined HKEY_* constants.
1701 /
1702
1703Disables registry reflection for 32bit processes running on a 64bit OS.
1704
1705Will generally raise NotImplemented if executed on a 32bit OS.
1706
1707If the key is not on the reflection list, the function succeeds but has
1708no effect. Disabling reflection for a key does not affect reflection
1709of any subkeys.
1710[clinic start generated code]*/
1711
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001712static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001713winreg_DisableReflectionKey_impl(PyObject *module, HKEY key)
1714/*[clinic end generated code: output=830cce504cc764b4 input=a6c9e5ca5410193c]*/
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001715{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 HMODULE hMod;
1717 typedef LONG (WINAPI *RDRKFunc)(HKEY);
1718 RDRKFunc pfn = NULL;
1719 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 /* Only available on 64bit platforms, so we must load it
1722 dynamically.*/
Tony Roberts4860f012019-02-02 18:16:42 +01001723 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis50590f12012-01-14 17:54:09 +01001724 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 if (hMod)
1726 pfn = (RDRKFunc)GetProcAddress(hMod,
1727 "RegDisableReflectionKey");
Tony Roberts4860f012019-02-02 18:16:42 +01001728 Py_END_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 if (!pfn) {
1730 PyErr_SetString(PyExc_NotImplementedError,
1731 "not implemented on this platform");
1732 return NULL;
1733 }
1734 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001735 rc = (*pfn)(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 Py_END_ALLOW_THREADS
1737 if (rc != ERROR_SUCCESS)
1738 return PyErr_SetFromWindowsErrWithFunction(rc,
1739 "RegDisableReflectionKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001740 Py_RETURN_NONE;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001741}
1742
Zachary Warefd2d4822015-05-13 01:21:57 -05001743/*[clinic input]
1744winreg.EnableReflectionKey
1745
1746 key: HKEY
1747 An already open key, or any one of the predefined HKEY_* constants.
1748 /
1749
1750Restores registry reflection for the specified disabled key.
1751
1752Will generally raise NotImplemented if executed on a 32bit OS.
1753Restoring reflection for a key does not affect reflection of any
1754subkeys.
1755[clinic start generated code]*/
1756
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001757static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001758winreg_EnableReflectionKey_impl(PyObject *module, HKEY key)
1759/*[clinic end generated code: output=86fa1385fdd9ce57 input=7748abbacd1e166a]*/
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 HMODULE hMod;
1762 typedef LONG (WINAPI *RERKFunc)(HKEY);
1763 RERKFunc pfn = NULL;
1764 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 /* Only available on 64bit platforms, so we must load it
1767 dynamically.*/
Tony Roberts4860f012019-02-02 18:16:42 +01001768 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis50590f12012-01-14 17:54:09 +01001769 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 if (hMod)
1771 pfn = (RERKFunc)GetProcAddress(hMod,
1772 "RegEnableReflectionKey");
Tony Roberts4860f012019-02-02 18:16:42 +01001773 Py_END_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 if (!pfn) {
1775 PyErr_SetString(PyExc_NotImplementedError,
1776 "not implemented on this platform");
1777 return NULL;
1778 }
1779 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001780 rc = (*pfn)(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 Py_END_ALLOW_THREADS
1782 if (rc != ERROR_SUCCESS)
1783 return PyErr_SetFromWindowsErrWithFunction(rc,
1784 "RegEnableReflectionKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001785 Py_RETURN_NONE;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001786}
1787
Zachary Warefd2d4822015-05-13 01:21:57 -05001788/*[clinic input]
1789winreg.QueryReflectionKey
1790
1791 key: HKEY
1792 An already open key, or any one of the predefined HKEY_* constants.
1793 /
1794
1795Returns the reflection state for the specified key as a bool.
1796
1797Will generally raise NotImplemented if executed on a 32bit OS.
1798[clinic start generated code]*/
1799
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001800static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001801winreg_QueryReflectionKey_impl(PyObject *module, HKEY key)
1802/*[clinic end generated code: output=4e774af288c3ebb9 input=9f325eacb5a65d88]*/
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001803{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001804 HMODULE hMod;
1805 typedef LONG (WINAPI *RQRKFunc)(HKEY, BOOL *);
1806 RQRKFunc pfn = NULL;
1807 BOOL result;
1808 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 /* Only available on 64bit platforms, so we must load it
1811 dynamically.*/
Tony Roberts4860f012019-02-02 18:16:42 +01001812 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis50590f12012-01-14 17:54:09 +01001813 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 if (hMod)
1815 pfn = (RQRKFunc)GetProcAddress(hMod,
1816 "RegQueryReflectionKey");
Tony Roberts4860f012019-02-02 18:16:42 +01001817 Py_END_ALLOW_THREADS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 if (!pfn) {
1819 PyErr_SetString(PyExc_NotImplementedError,
1820 "not implemented on this platform");
1821 return NULL;
1822 }
1823 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001824 rc = (*pfn)(key, &result);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 Py_END_ALLOW_THREADS
1826 if (rc != ERROR_SUCCESS)
1827 return PyErr_SetFromWindowsErrWithFunction(rc,
1828 "RegQueryReflectionKey");
1829 return PyBool_FromLong(result);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001830}
1831
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001832static struct PyMethodDef winreg_methods[] = {
Zachary Warefd2d4822015-05-13 01:21:57 -05001833 WINREG_CLOSEKEY_METHODDEF
1834 WINREG_CONNECTREGISTRY_METHODDEF
1835 WINREG_CREATEKEY_METHODDEF
1836 WINREG_CREATEKEYEX_METHODDEF
1837 WINREG_DELETEKEY_METHODDEF
1838 WINREG_DELETEKEYEX_METHODDEF
1839 WINREG_DELETEVALUE_METHODDEF
1840 WINREG_DISABLEREFLECTIONKEY_METHODDEF
1841 WINREG_ENABLEREFLECTIONKEY_METHODDEF
1842 WINREG_ENUMKEY_METHODDEF
1843 WINREG_ENUMVALUE_METHODDEF
1844 WINREG_EXPANDENVIRONMENTSTRINGS_METHODDEF
1845 WINREG_FLUSHKEY_METHODDEF
1846 WINREG_LOADKEY_METHODDEF
1847 WINREG_OPENKEY_METHODDEF
1848 WINREG_OPENKEYEX_METHODDEF
1849 WINREG_QUERYVALUE_METHODDEF
1850 WINREG_QUERYVALUEEX_METHODDEF
1851 WINREG_QUERYINFOKEY_METHODDEF
1852 WINREG_QUERYREFLECTIONKEY_METHODDEF
1853 WINREG_SAVEKEY_METHODDEF
1854 WINREG_SETVALUE_METHODDEF
1855 WINREG_SETVALUEEX_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 NULL,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001857};
1858
1859static void
1860insint(PyObject * d, char * name, long value)
1861{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 PyObject *v = PyLong_FromLong(value);
1863 if (!v || PyDict_SetItemString(d, name, v))
1864 PyErr_Clear();
1865 Py_XDECREF(v);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001866}
1867
1868#define ADD_INT(val) insint(d, #val, val)
1869
1870static void
1871inskey(PyObject * d, char * name, HKEY key)
1872{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 PyObject *v = PyLong_FromVoidPtr(key);
1874 if (!v || PyDict_SetItemString(d, name, v))
1875 PyErr_Clear();
1876 Py_XDECREF(v);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001877}
1878
1879#define ADD_KEY(val) inskey(d, #val, val)
1880
Martin v. Löwis1a214512008-06-11 05:26:20 +00001881
1882static struct PyModuleDef winregmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 PyModuleDef_HEAD_INIT,
1884 "winreg",
1885 module_doc,
1886 -1,
1887 winreg_methods,
1888 NULL,
1889 NULL,
1890 NULL,
1891 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001892};
1893
1894PyMODINIT_FUNC PyInit_winreg(void)
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001895{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 PyObject *m, *d;
1897 m = PyModule_Create(&winregmodule);
1898 if (m == NULL)
1899 return NULL;
1900 d = PyModule_GetDict(m);
1901 PyHKEY_Type.tp_doc = PyHKEY_doc;
1902 if (PyType_Ready(&PyHKEY_Type) < 0)
1903 return NULL;
1904 Py_INCREF(&PyHKEY_Type);
1905 if (PyDict_SetItemString(d, "HKEYType",
1906 (PyObject *)&PyHKEY_Type) != 0)
1907 return NULL;
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001908 Py_INCREF(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 if (PyDict_SetItemString(d, "error",
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001910 PyExc_OSError) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 /* Add the relevant constants */
1914 ADD_KEY(HKEY_CLASSES_ROOT);
1915 ADD_KEY(HKEY_CURRENT_USER);
1916 ADD_KEY(HKEY_LOCAL_MACHINE);
1917 ADD_KEY(HKEY_USERS);
1918 ADD_KEY(HKEY_PERFORMANCE_DATA);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001919#ifdef HKEY_CURRENT_CONFIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 ADD_KEY(HKEY_CURRENT_CONFIG);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001921#endif
1922#ifdef HKEY_DYN_DATA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 ADD_KEY(HKEY_DYN_DATA);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001924#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925 ADD_INT(KEY_QUERY_VALUE);
1926 ADD_INT(KEY_SET_VALUE);
1927 ADD_INT(KEY_CREATE_SUB_KEY);
1928 ADD_INT(KEY_ENUMERATE_SUB_KEYS);
1929 ADD_INT(KEY_NOTIFY);
1930 ADD_INT(KEY_CREATE_LINK);
1931 ADD_INT(KEY_READ);
1932 ADD_INT(KEY_WRITE);
1933 ADD_INT(KEY_EXECUTE);
1934 ADD_INT(KEY_ALL_ACCESS);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001935#ifdef KEY_WOW64_64KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 ADD_INT(KEY_WOW64_64KEY);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001937#endif
1938#ifdef KEY_WOW64_32KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001939 ADD_INT(KEY_WOW64_32KEY);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001940#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 ADD_INT(REG_OPTION_RESERVED);
1942 ADD_INT(REG_OPTION_NON_VOLATILE);
1943 ADD_INT(REG_OPTION_VOLATILE);
1944 ADD_INT(REG_OPTION_CREATE_LINK);
1945 ADD_INT(REG_OPTION_BACKUP_RESTORE);
1946 ADD_INT(REG_OPTION_OPEN_LINK);
1947 ADD_INT(REG_LEGAL_OPTION);
1948 ADD_INT(REG_CREATED_NEW_KEY);
1949 ADD_INT(REG_OPENED_EXISTING_KEY);
1950 ADD_INT(REG_WHOLE_HIVE_VOLATILE);
1951 ADD_INT(REG_REFRESH_HIVE);
1952 ADD_INT(REG_NO_LAZY_FLUSH);
1953 ADD_INT(REG_NOTIFY_CHANGE_NAME);
1954 ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES);
1955 ADD_INT(REG_NOTIFY_CHANGE_LAST_SET);
1956 ADD_INT(REG_NOTIFY_CHANGE_SECURITY);
1957 ADD_INT(REG_LEGAL_CHANGE_FILTER);
1958 ADD_INT(REG_NONE);
1959 ADD_INT(REG_SZ);
1960 ADD_INT(REG_EXPAND_SZ);
1961 ADD_INT(REG_BINARY);
1962 ADD_INT(REG_DWORD);
1963 ADD_INT(REG_DWORD_LITTLE_ENDIAN);
1964 ADD_INT(REG_DWORD_BIG_ENDIAN);
Steve Dower80ac11d2016-05-24 15:42:04 -07001965 ADD_INT(REG_QWORD);
1966 ADD_INT(REG_QWORD_LITTLE_ENDIAN);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 ADD_INT(REG_LINK);
1968 ADD_INT(REG_MULTI_SZ);
1969 ADD_INT(REG_RESOURCE_LIST);
1970 ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
1971 ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
1972 return m;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001973}
1974
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001975