blob: 6f3106644b2c5d63ec8b9e3eeac6008be8e164a2 [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
Guido van Rossum9f3712c2000-03-28 20:37:15 +000015#include "Python.h"
16#include "structmember.h"
Guido van Rossume7ba4952007-06-06 23:52:48 +000017#include "windows.h"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000018
19static BOOL PyHKEY_AsHKEY(PyObject *ob, HKEY *pRes, BOOL bNoneOK);
Zachary Warefd2d4822015-05-13 01:21:57 -050020static BOOL clinic_HKEY_converter(PyObject *ob, void *p);
Guido van Rossum9f3712c2000-03-28 20:37:15 +000021static PyObject *PyHKEY_FromHKEY(HKEY h);
22static BOOL PyHKEY_Close(PyObject *obHandle);
23
24static char errNotAHandle[] = "Object is not a handle";
25
26/* The win32api module reports the function name that failed,
27 but this concept is not in the Python core.
luzpaza5293b42017-11-05 07:37:50 -060028 Hopefully it will one day, and in the meantime I don't
Guido van Rossum9f3712c2000-03-28 20:37:15 +000029 want to lose this info...
30*/
31#define PyErr_SetFromWindowsErrWithFunction(rc, fnname) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000032 PyErr_SetFromWindowsErr(rc)
Guido van Rossum9f3712c2000-03-28 20:37:15 +000033
34/* Forward declares */
35
36/* Doc strings */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000037PyDoc_STRVAR(module_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +000038"This module provides access to the Windows registry API.\n"
39"\n"
40"Functions:\n"
41"\n"
42"CloseKey() - Closes a registry key.\n"
43"ConnectRegistry() - Establishes a connection to a predefined registry handle\n"
44" on another computer.\n"
45"CreateKey() - Creates the specified key, or opens it if it already exists.\n"
46"DeleteKey() - Deletes the specified key.\n"
47"DeleteValue() - Removes a named value from the specified registry key.\n"
48"EnumKey() - Enumerates subkeys of the specified open registry key.\n"
49"EnumValue() - Enumerates values of the specified open registry key.\n"
Zachary Warefd2d4822015-05-13 01:21:57 -050050"ExpandEnvironmentStrings() - Expand the env strings in a REG_EXPAND_SZ\n"
51" string.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000052"FlushKey() - Writes all the attributes of the specified key to the registry.\n"
Zachary Warefd2d4822015-05-13 01:21:57 -050053"LoadKey() - Creates a subkey under HKEY_USER or HKEY_LOCAL_MACHINE and\n"
54" stores registration information from a specified file into that\n"
55" subkey.\n"
Brian Curtine9aeca72012-10-29 18:16:39 -050056"OpenKey() - Opens the specified key.\n"
57"OpenKeyEx() - Alias of OpenKey().\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000058"QueryValue() - Retrieves the value associated with the unnamed value for a\n"
59" specified key in the registry.\n"
60"QueryValueEx() - Retrieves the type and data for a specified value name\n"
61" associated with an open registry key.\n"
62"QueryInfoKey() - Returns information about the specified key.\n"
63"SaveKey() - Saves the specified key, and all its subkeys a file.\n"
64"SetValue() - Associates a value with a specified key.\n"
65"SetValueEx() - Stores data in the value field of an open registry key.\n"
66"\n"
67"Special objects:\n"
68"\n"
69"HKEYType -- type object for HKEY objects\n"
70"error -- exception raised for Win32 errors\n"
71"\n"
72"Integer constants:\n"
73"Many constants are defined - see the documentation for each function\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000074"to see what constants are used, and where.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +000075
76
Martin v. Löwisd218dc12008-04-07 03:17:54 +000077
Guido van Rossum9f3712c2000-03-28 20:37:15 +000078/* PyHKEY docstrings */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000079PyDoc_STRVAR(PyHKEY_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +000080"PyHKEY Object - A Python object, representing a win32 registry key.\n"
81"\n"
Mark Hammondb422f952000-06-09 06:01:47 +000082"This object wraps a Windows HKEY object, automatically closing it when\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000083"the object is destroyed. To guarantee cleanup, you can call either\n"
84"the Close() method on the PyHKEY, or the CloseKey() method.\n"
85"\n"
86"All functions which accept a handle object also accept an integer - \n"
87"however, use of the handle object is encouraged.\n"
88"\n"
89"Functions:\n"
90"Close() - Closes the underlying handle.\n"
91"Detach() - Returns the integer Win32 handle, detaching it from the object\n"
92"\n"
93"Properties:\n"
94"handle - The integer Win32 handle.\n"
95"\n"
96"Operations:\n"
Jack Diederich4dafcc42006-11-28 19:15:13 +000097"__bool__ - Handles with an open object return true, otherwise false.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000098"__int__ - Converting a handle to an integer returns the Win32 handle.\n"
Mark Dickinson211c6252009-02-01 10:28:51 +000099"rich comparison - Handle objects are compared using the handle value.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000100
101
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000102
103/************************************************************************
104
105 The PyHKEY object definition
106
107************************************************************************/
108typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 PyObject_VAR_HEAD
110 HKEY hkey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000111} PyHKEYObject;
112
113#define PyHKEY_Check(op) ((op)->ob_type == &PyHKEY_Type)
114
115static char *failMsg = "bad operand type";
116
117static PyObject *
118PyHKEY_unaryFailureFunc(PyObject *ob)
119{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 PyErr_SetString(PyExc_TypeError, failMsg);
121 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000122}
123static PyObject *
124PyHKEY_binaryFailureFunc(PyObject *ob1, PyObject *ob2)
125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 PyErr_SetString(PyExc_TypeError, failMsg);
127 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000128}
129static PyObject *
130PyHKEY_ternaryFailureFunc(PyObject *ob1, PyObject *ob2, PyObject *ob3)
131{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 PyErr_SetString(PyExc_TypeError, failMsg);
133 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000134}
135
136static void
137PyHKEY_deallocFunc(PyObject *ob)
138{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 /* Can not call PyHKEY_Close, as the ob->tp_type
140 has already been cleared, thus causing the type
141 check to fail!
142 */
143 PyHKEYObject *obkey = (PyHKEYObject *)ob;
144 if (obkey->hkey)
145 RegCloseKey((HKEY)obkey->hkey);
146 PyObject_DEL(ob);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000147}
148
149static int
Jack Diederich4dafcc42006-11-28 19:15:13 +0000150PyHKEY_boolFunc(PyObject *ob)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000151{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 return ((PyHKEYObject *)ob)->hkey != 0;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000153}
154
155static PyObject *
156PyHKEY_intFunc(PyObject *ob)
157{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
159 return PyLong_FromVoidPtr(pyhkey->hkey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000160}
161
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000162static PyObject *
163PyHKEY_strFunc(PyObject *ob)
164{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
166 return PyUnicode_FromFormat("<PyHKEY:%p>", pyhkey->hkey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000167}
168
169static int
170PyHKEY_compareFunc(PyObject *ob1, PyObject *ob2)
171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 PyHKEYObject *pyhkey1 = (PyHKEYObject *)ob1;
173 PyHKEYObject *pyhkey2 = (PyHKEYObject *)ob2;
174 return pyhkey1 == pyhkey2 ? 0 :
175 (pyhkey1 < pyhkey2 ? -1 : 1);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000176}
177
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000178static Py_hash_t
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000179PyHKEY_hashFunc(PyObject *ob)
180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 /* Just use the address.
182 XXX - should we use the handle value?
183 */
184 return _Py_HashPointer(ob);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000185}
186
187
188static PyNumberMethods PyHKEY_NumberMethods =
189{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 PyHKEY_binaryFailureFunc, /* nb_add */
191 PyHKEY_binaryFailureFunc, /* nb_subtract */
192 PyHKEY_binaryFailureFunc, /* nb_multiply */
193 PyHKEY_binaryFailureFunc, /* nb_remainder */
194 PyHKEY_binaryFailureFunc, /* nb_divmod */
195 PyHKEY_ternaryFailureFunc, /* nb_power */
196 PyHKEY_unaryFailureFunc, /* nb_negative */
197 PyHKEY_unaryFailureFunc, /* nb_positive */
198 PyHKEY_unaryFailureFunc, /* nb_absolute */
199 PyHKEY_boolFunc, /* nb_bool */
200 PyHKEY_unaryFailureFunc, /* nb_invert */
201 PyHKEY_binaryFailureFunc, /* nb_lshift */
202 PyHKEY_binaryFailureFunc, /* nb_rshift */
203 PyHKEY_binaryFailureFunc, /* nb_and */
204 PyHKEY_binaryFailureFunc, /* nb_xor */
205 PyHKEY_binaryFailureFunc, /* nb_or */
206 PyHKEY_intFunc, /* nb_int */
207 0, /* nb_reserved */
208 PyHKEY_unaryFailureFunc, /* nb_float */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000209};
210
Zachary Warefd2d4822015-05-13 01:21:57 -0500211/*[clinic input]
212module winreg
213class winreg.HKEYType "PyHKEYObject *" "&PyHKEY_Type"
214[clinic start generated code]*/
215/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4c964eba3bf914d6]*/
216
217/*[python input]
218class REGSAM_converter(CConverter):
219 type = 'REGSAM'
220 format_unit = 'i'
221
222class DWORD_converter(CConverter):
223 type = 'DWORD'
224 format_unit = 'k'
225
226class HKEY_converter(CConverter):
227 type = 'HKEY'
228 converter = 'clinic_HKEY_converter'
229
230class HKEY_return_converter(CReturnConverter):
231 type = 'HKEY'
232
233 def render(self, function, data):
234 self.declare(data)
235 self.err_occurred_if_null_pointer("_return_value", data)
236 data.return_conversion.append(
237 'return_value = PyHKEY_FromHKEY(_return_value);\n')
238
239# HACK: this only works for PyHKEYObjects, nothing else.
240# Should this be generalized and enshrined in clinic.py,
241# destroy this converter with prejudice.
242class self_return_converter(CReturnConverter):
243 type = 'PyHKEYObject *'
244
245 def render(self, function, data):
246 self.declare(data)
247 data.return_conversion.append(
248 'return_value = (PyObject *)_return_value;\n')
249[python start generated code]*/
250/*[python end generated code: output=da39a3ee5e6b4b0d input=22f7aedc6d68e80e]*/
251
252#include "clinic/winreg.c.h"
253
254/************************************************************************
255
256 The PyHKEY object methods
257
258************************************************************************/
259/*[clinic input]
260winreg.HKEYType.Close
261
262Closes the underlying Windows handle.
263
264If the handle is already closed, no error is raised.
265[clinic start generated code]*/
266
267static PyObject *
268winreg_HKEYType_Close_impl(PyHKEYObject *self)
269/*[clinic end generated code: output=fced3a624fb0c344 input=6786ac75f6b89de6]*/
270{
271 if (!PyHKEY_Close((PyObject *)self))
272 return NULL;
273 Py_RETURN_NONE;
274}
275
276/*[clinic input]
277winreg.HKEYType.Detach
278
279Detaches the Windows handle from the handle object.
280
281The result is the value of the handle before it is detached. If the
282handle is already detached, this will return zero.
283
284After calling this function, the handle is effectively invalidated,
285but the handle is not closed. You would call this function when you
286need the underlying win32 handle to exist beyond the lifetime of the
287handle object.
288[clinic start generated code]*/
289
290static PyObject *
291winreg_HKEYType_Detach_impl(PyHKEYObject *self)
292/*[clinic end generated code: output=dda5a9e1a01ae78f input=dd2cc09e6c6ba833]*/
293{
294 void* ret;
295 ret = (void*)self->hkey;
296 self->hkey = 0;
297 return PyLong_FromVoidPtr(ret);
298}
299
300/*[clinic input]
301winreg.HKEYType.__enter__ -> self
302[clinic start generated code]*/
303
304static PyHKEYObject *
305winreg_HKEYType___enter___impl(PyHKEYObject *self)
306/*[clinic end generated code: output=52c34986dab28990 input=c40fab1f0690a8e2]*/
307{
308 Py_XINCREF(self);
309 return self;
310}
311
312
313/*[clinic input]
314winreg.HKEYType.__exit__
315
316 exc_type: object
317 exc_value: object
318 traceback: object
319[clinic start generated code]*/
320
321static PyObject *
Zachary Ware77772c02015-05-13 10:58:35 -0500322winreg_HKEYType___exit___impl(PyHKEYObject *self, PyObject *exc_type,
323 PyObject *exc_value, PyObject *traceback)
324/*[clinic end generated code: output=923ebe7389e6a263 input=fb32489ee92403c7]*/
Zachary Warefd2d4822015-05-13 01:21:57 -0500325{
326 if (!PyHKEY_Close((PyObject *)self))
327 return NULL;
328 Py_RETURN_NONE;
329}
330
331/*[clinic input]
332[clinic start generated code]*/
333/*[clinic end generated code: output=da39a3ee5e6b4b0d input=da39a3ee5e6b4b0d]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000334
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000335static struct PyMethodDef PyHKEY_methods[] = {
Zachary Warefd2d4822015-05-13 01:21:57 -0500336 WINREG_HKEYTYPE_CLOSE_METHODDEF
337 WINREG_HKEYTYPE_DETACH_METHODDEF
338 WINREG_HKEYTYPE___ENTER___METHODDEF
339 WINREG_HKEYTYPE___EXIT___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 {NULL}
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000341};
342
343#define OFF(e) offsetof(PyHKEYObject, e)
344static PyMemberDef PyHKEY_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 {"handle", T_INT, OFF(hkey), READONLY},
346 {NULL} /* Sentinel */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000347};
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000348
349/* The type itself */
350PyTypeObject PyHKEY_Type =
351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 PyVarObject_HEAD_INIT(0, 0) /* fill in type at module init */
353 "PyHKEY",
354 sizeof(PyHKEYObject),
355 0,
356 PyHKEY_deallocFunc, /* tp_dealloc */
357 0, /* tp_print */
358 0, /* tp_getattr */
359 0, /* tp_setattr */
360 0, /* tp_reserved */
361 0, /* tp_repr */
362 &PyHKEY_NumberMethods, /* tp_as_number */
363 0, /* tp_as_sequence */
364 0, /* tp_as_mapping */
365 PyHKEY_hashFunc, /* tp_hash */
366 0, /* tp_call */
367 PyHKEY_strFunc, /* tp_str */
368 0, /* tp_getattro */
369 0, /* tp_setattro */
370 0, /* tp_as_buffer */
371 0, /* tp_flags */
372 PyHKEY_doc, /* tp_doc */
373 0, /*tp_traverse*/
374 0, /*tp_clear*/
375 0, /*tp_richcompare*/
376 0, /*tp_weaklistoffset*/
377 0, /*tp_iter*/
378 0, /*tp_iternext*/
379 PyHKEY_methods, /*tp_methods*/
380 PyHKEY_memberlist, /*tp_members*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000381};
382
383/************************************************************************
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000384 The public PyHKEY API (well, not public yet :-)
385************************************************************************/
386PyObject *
387PyHKEY_New(HKEY hInit)
388{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 PyHKEYObject *key = PyObject_NEW(PyHKEYObject, &PyHKEY_Type);
390 if (key)
391 key->hkey = hInit;
392 return (PyObject *)key;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000393}
394
395BOOL
396PyHKEY_Close(PyObject *ob_handle)
397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 LONG rc;
399 PyHKEYObject *key;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 if (!PyHKEY_Check(ob_handle)) {
402 PyErr_SetString(PyExc_TypeError, "bad operand type");
403 return FALSE;
404 }
405 key = (PyHKEYObject *)ob_handle;
406 rc = key->hkey ? RegCloseKey((HKEY)key->hkey) : ERROR_SUCCESS;
407 key->hkey = 0;
408 if (rc != ERROR_SUCCESS)
409 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
410 return rc == ERROR_SUCCESS;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000411}
412
413BOOL
414PyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK)
415{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 if (ob == Py_None) {
417 if (!bNoneOK) {
418 PyErr_SetString(
419 PyExc_TypeError,
420 "None is not a valid HKEY in this context");
421 return FALSE;
422 }
423 *pHANDLE = (HKEY)0;
424 }
425 else if (PyHKEY_Check(ob)) {
426 PyHKEYObject *pH = (PyHKEYObject *)ob;
427 *pHANDLE = pH->hkey;
428 }
429 else if (PyLong_Check(ob)) {
430 /* We also support integers */
431 PyErr_Clear();
432 *pHANDLE = (HKEY)PyLong_AsVoidPtr(ob);
433 if (PyErr_Occurred())
434 return FALSE;
435 }
436 else {
437 PyErr_SetString(
438 PyExc_TypeError,
439 "The object is not a PyHKEY object");
440 return FALSE;
441 }
442 return TRUE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000443}
444
Zachary Warefd2d4822015-05-13 01:21:57 -0500445BOOL
446clinic_HKEY_converter(PyObject *ob, void *p)
447{
448 if (!PyHKEY_AsHKEY(ob, (HKEY *)p, FALSE))
449 return FALSE;
450 return TRUE;
451}
452
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000453PyObject *
454PyHKEY_FromHKEY(HKEY h)
455{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 PyHKEYObject *op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 /* Inline PyObject_New */
459 op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject));
460 if (op == NULL)
461 return PyErr_NoMemory();
462 PyObject_INIT(op, &PyHKEY_Type);
463 op->hkey = h;
464 return (PyObject *)op;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000465}
466
467
468/************************************************************************
469 The module methods
470************************************************************************/
471BOOL
472PyWinObject_CloseHKEY(PyObject *obHandle)
473{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 BOOL ok;
475 if (PyHKEY_Check(obHandle)) {
476 ok = PyHKEY_Close(obHandle);
477 }
Fred Drake25e17262000-06-30 17:48:51 +0000478#if SIZEOF_LONG >= SIZEOF_HKEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 else if (PyLong_Check(obHandle)) {
480 long rc = RegCloseKey((HKEY)PyLong_AsLong(obHandle));
481 ok = (rc == ERROR_SUCCESS);
482 if (!ok)
483 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
484 }
Fred Drake25e17262000-06-30 17:48:51 +0000485#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 else if (PyLong_Check(obHandle)) {
487 long rc = RegCloseKey((HKEY)PyLong_AsVoidPtr(obHandle));
488 ok = (rc == ERROR_SUCCESS);
489 if (!ok)
490 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
491 }
Fred Drake25e17262000-06-30 17:48:51 +0000492#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 else {
494 PyErr_SetString(
495 PyExc_TypeError,
496 "A handle must be a HKEY object or an integer");
497 return FALSE;
498 }
499 return ok;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000500}
501
502
503/*
504 Private Helper functions for the registry interfaces
505
506** Note that fixupMultiSZ and countString have both had changes
507** made to support "incorrect strings". The registry specification
508** calls for strings to be terminated with 2 null bytes. It seems
luzpaza5293b42017-11-05 07:37:50 -0600509** some commercial packages install strings which don't conform,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000510** causing this code to fail - however, "regedit" etc still work
luzpaza5293b42017-11-05 07:37:50 -0600511** with these strings (ie only we don't!).
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000512*/
513static void
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000514fixupMultiSZ(wchar_t **str, wchar_t *data, int len)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000515{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 wchar_t *P;
517 int i;
518 wchar_t *Q;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 Q = data + len;
521 for (P = data, i = 0; P < Q && *P != '\0'; P++, i++) {
522 str[i] = P;
523 for(; *P != '\0'; P++)
524 ;
525 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000526}
527
528static int
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000529countStrings(wchar_t *data, int len)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 int strings;
532 wchar_t *P;
533 wchar_t *Q = data + len;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 for (P = data, strings = 0; P < Q && *P != '\0'; P++, strings++)
536 for (; P < Q && *P != '\0'; P++)
537 ;
538 return strings;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000539}
540
541/* Convert PyObject into Registry data.
542 Allocates space as needed. */
543static BOOL
544Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 Py_ssize_t i,j;
547 switch (typ) {
548 case REG_DWORD:
549 if (value != Py_None && !PyLong_Check(value))
550 return FALSE;
551 *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1);
Steve Dower4d4bc422016-05-25 11:26:07 -0700552 if (*retDataBuf == NULL){
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 PyErr_NoMemory();
554 return FALSE;
555 }
556 *retDataSize = sizeof(DWORD);
557 if (value == Py_None) {
558 DWORD zero = 0;
559 memcpy(*retDataBuf, &zero, sizeof(DWORD));
560 }
561 else {
Brian Curtin12706f22012-12-27 10:12:45 -0600562 DWORD d = PyLong_AsUnsignedLong(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 memcpy(*retDataBuf, &d, sizeof(DWORD));
564 }
565 break;
Steve Dower80ac11d2016-05-24 15:42:04 -0700566 case REG_QWORD:
567 if (value != Py_None && !PyLong_Check(value))
568 return FALSE;
569 *retDataBuf = (BYTE *)PyMem_NEW(DWORD64, 1);
Steve Dower4d4bc422016-05-25 11:26:07 -0700570 if (*retDataBuf == NULL){
Steve Dower80ac11d2016-05-24 15:42:04 -0700571 PyErr_NoMemory();
572 return FALSE;
573 }
574 *retDataSize = sizeof(DWORD64);
575 if (value == Py_None) {
576 DWORD64 zero = 0;
577 memcpy(*retDataBuf, &zero, sizeof(DWORD64));
578 }
579 else {
580 DWORD64 d = PyLong_AsUnsignedLongLong(value);
581 memcpy(*retDataBuf, &d, sizeof(DWORD64));
582 }
583 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 case REG_SZ:
585 case REG_EXPAND_SZ:
586 {
Victor Stinnerbe492442011-11-21 12:43:50 +0100587 if (value != Py_None) {
588 Py_ssize_t len;
589 if (!PyUnicode_Check(value))
590 return FALSE;
591 *retDataBuf = (BYTE*)PyUnicode_AsWideCharString(value, &len);
592 if (*retDataBuf == NULL)
593 return FALSE;
594 *retDataSize = Py_SAFE_DOWNCAST(
595 (len + 1) * sizeof(wchar_t),
596 Py_ssize_t, DWORD);
597 }
598 else {
599 *retDataBuf = (BYTE *)PyMem_NEW(wchar_t, 1);
600 if (*retDataBuf == NULL) {
601 PyErr_NoMemory();
602 return FALSE;
603 }
604 ((wchar_t *)*retDataBuf)[0] = L'\0';
605 *retDataSize = 1 * sizeof(wchar_t);
606 }
607 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 }
609 case REG_MULTI_SZ:
610 {
611 DWORD size = 0;
612 wchar_t *P;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 if (value == Py_None)
615 i = 0;
616 else {
617 if (!PyList_Check(value))
618 return FALSE;
619 i = PyList_Size(value);
620 }
621 for (j = 0; j < i; j++)
622 {
623 PyObject *t;
Victor Stinnerbe492442011-11-21 12:43:50 +0100624 wchar_t *wstr;
625 Py_ssize_t len;
626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 t = PyList_GET_ITEM(value, j);
628 if (!PyUnicode_Check(t))
629 return FALSE;
Victor Stinnerbe492442011-11-21 12:43:50 +0100630 wstr = PyUnicode_AsUnicodeAndSize(t, &len);
631 if (wstr == NULL)
632 return FALSE;
633 size += Py_SAFE_DOWNCAST((len + 1) * sizeof(wchar_t),
Brian Curtinabb33512010-08-17 20:08:40 +0000634 size_t, DWORD);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 *retDataSize = size + 2;
638 *retDataBuf = (BYTE *)PyMem_NEW(char,
639 *retDataSize);
Steve Dower4d4bc422016-05-25 11:26:07 -0700640 if (*retDataBuf == NULL){
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 PyErr_NoMemory();
642 return FALSE;
643 }
644 P = (wchar_t *)*retDataBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 for (j = 0; j < i; j++)
647 {
648 PyObject *t;
Victor Stinnerbe492442011-11-21 12:43:50 +0100649 wchar_t *wstr;
650 Py_ssize_t len;
651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 t = PyList_GET_ITEM(value, j);
Victor Stinnerbe492442011-11-21 12:43:50 +0100653 wstr = PyUnicode_AsUnicodeAndSize(t, &len);
654 if (wstr == NULL)
655 return FALSE;
656 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 }
Miss Islington (bot)8b7d8ac2018-12-08 06:34:49 -0800760 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 Storchaka1a2b24f2016-07-07 17:35:15 +0300829winreg_ConnectRegistry_impl(PyObject *module, Py_UNICODE *computer_name,
Zachary Ware77772c02015-05-13 10:58:35 -0500830 HKEY key)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300831/*[clinic end generated code: output=5ab79d02aa3167b4 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 Storchaka1a2b24f2016-07-07 17:35:15 +0300866winreg_CreateKey_impl(PyObject *module, HKEY key, Py_UNICODE *sub_key)
867/*[clinic end generated code: output=9c81d4095527c927 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 Storchaka1a2b24f2016-07-07 17:35:15 +0300905winreg_CreateKeyEx_impl(PyObject *module, HKEY key, Py_UNICODE *sub_key,
Zachary Ware77772c02015-05-13 10:58:35 -0500906 int reserved, REGSAM access)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300907/*[clinic end generated code: output=b9fce6dc5c4e39b1 input=42c2b03f98406b66]*/
Brian Curtin3035c392010-04-21 23:56:21 +0000908{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 HKEY retKey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 long rc;
Brian Curtin1771b542010-09-27 17:56:36 +0000911
Segev Finer679b5662017-07-27 01:17:57 +0300912 rc = RegCreateKeyExW(key, sub_key, reserved, NULL, 0,
Brian Curtin1771b542010-09-27 17:56:36 +0000913 access, NULL, &retKey, NULL);
Zachary Warefd2d4822015-05-13 01:21:57 -0500914 if (rc != ERROR_SUCCESS) {
915 PyErr_SetFromWindowsErrWithFunction(rc, "CreateKeyEx");
916 return NULL;
917 }
918 return retKey;
Brian Curtin3035c392010-04-21 23:56:21 +0000919}
920
Zachary Warefd2d4822015-05-13 01:21:57 -0500921/*[clinic input]
922winreg.DeleteKey
923 key: HKEY
924 An already open key, or any one of the predefined HKEY_* constants.
925 sub_key: Py_UNICODE
926 A string that must be the name of a subkey of the key identified by
927 the key parameter. This value must not be None, and the key may not
928 have subkeys.
929 /
930
931Deletes the specified key.
932
933This method can not delete keys with subkeys.
934
935If the function succeeds, the entire key, including all of its values,
936is removed. If the function fails, an OSError exception is raised.
937[clinic start generated code]*/
938
Brian Curtin3035c392010-04-21 23:56:21 +0000939static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300940winreg_DeleteKey_impl(PyObject *module, HKEY key, Py_UNICODE *sub_key)
941/*[clinic end generated code: output=7734b1e431991ae4 input=b31d225b935e4211]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000942{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 long rc;
Zachary Warefd2d4822015-05-13 01:21:57 -0500944 rc = RegDeleteKeyW(key, sub_key );
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 if (rc != ERROR_SUCCESS)
946 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey");
Zachary Warefd2d4822015-05-13 01:21:57 -0500947 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000948}
949
Zachary Warefd2d4822015-05-13 01:21:57 -0500950/*[clinic input]
951winreg.DeleteKeyEx
952
953 key: HKEY
954 An already open key, or any one of the predefined HKEY_* constants.
955 sub_key: Py_UNICODE
956 A string that must be the name of a subkey of the key identified by
957 the key parameter. This value must not be None, and the key may not
958 have subkeys.
959 access: REGSAM(c_default='KEY_WOW64_64KEY') = winreg.KEY_WOW64_64KEY
960 An integer that specifies an access mask that describes the
961 desired security access for the key. Default is KEY_WOW64_64KEY.
962 reserved: int = 0
963 A reserved integer, and must be zero. Default is zero.
964
965Deletes the specified key (64-bit OS only).
966
967This method can not delete keys with subkeys.
968
969If the function succeeds, the entire key, including all of its values,
970is removed. If the function fails, an OSError exception is raised.
971On unsupported Windows versions, NotImplementedError is raised.
972[clinic start generated code]*/
973
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000974static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300975winreg_DeleteKeyEx_impl(PyObject *module, HKEY key, Py_UNICODE *sub_key,
Zachary Ware77772c02015-05-13 10:58:35 -0500976 REGSAM access, int reserved)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300977/*[clinic end generated code: output=01378d86ad3eb936 input=711d9d89e7ecbed7]*/
Brian Curtin3035c392010-04-21 23:56:21 +0000978{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 HMODULE hMod;
980 typedef LONG (WINAPI *RDKEFunc)(HKEY, const wchar_t*, REGSAM, int);
981 RDKEFunc pfn = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 long rc;
Brian Curtin3035c392010-04-21 23:56:21 +0000983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 /* Only available on 64bit platforms, so we must load it
985 dynamically. */
Martin v. Löwis50590f12012-01-14 17:54:09 +0100986 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 if (hMod)
988 pfn = (RDKEFunc)GetProcAddress(hMod,
989 "RegDeleteKeyExW");
990 if (!pfn) {
991 PyErr_SetString(PyExc_NotImplementedError,
992 "not implemented on this platform");
993 return NULL;
994 }
995 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -0500996 rc = (*pfn)(key, sub_key, access, reserved);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 Py_END_ALLOW_THREADS
Brian Curtin3035c392010-04-21 23:56:21 +0000998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 if (rc != ERROR_SUCCESS)
1000 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKeyEx");
Zachary Warefd2d4822015-05-13 01:21:57 -05001001 Py_RETURN_NONE;
Brian Curtin3035c392010-04-21 23:56:21 +00001002}
1003
Zachary Warefd2d4822015-05-13 01:21:57 -05001004/*[clinic input]
1005winreg.DeleteValue
1006
1007 key: HKEY
1008 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001009 value: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001010 A string that identifies the value to remove.
1011 /
1012
1013Removes a named value from a registry key.
1014[clinic start generated code]*/
1015
Brian Curtin3035c392010-04-21 23:56:21 +00001016static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001017winreg_DeleteValue_impl(PyObject *module, HKEY key, Py_UNICODE *value)
1018/*[clinic end generated code: output=67e7e9a514f84951 input=a78d3407a4197b21]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001019{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 long rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001022 rc = RegDeleteValueW(key, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 Py_END_ALLOW_THREADS
1024 if (rc !=ERROR_SUCCESS)
1025 return PyErr_SetFromWindowsErrWithFunction(rc,
1026 "RegDeleteValue");
Zachary Warefd2d4822015-05-13 01:21:57 -05001027 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001028}
1029
Zachary Warefd2d4822015-05-13 01:21:57 -05001030/*[clinic input]
1031winreg.EnumKey
1032
1033 key: HKEY
1034 An already open key, or any one of the predefined HKEY_* constants.
1035 index: int
1036 An integer that identifies the index of the key to retrieve.
1037 /
1038
1039Enumerates subkeys of an open registry key.
1040
1041The function retrieves the name of one subkey each time it is called.
1042It is typically called repeatedly until an OSError exception is
1043raised, indicating no more values are available.
1044[clinic start generated code]*/
1045
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001046static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001047winreg_EnumKey_impl(PyObject *module, HKEY key, int index)
1048/*[clinic end generated code: output=25a6ec52cd147bc4 input=fad9a7c00ab0e04b]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001049{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 long rc;
1051 PyObject *retStr;
Brian Curtin60853212010-05-26 17:43:50 +00001052
1053 /* The Windows docs claim that the max key name length is 255
1054 * characters, plus a terminating nul character. However,
1055 * empirical testing demonstrates that it is possible to
1056 * create a 256 character key that is missing the terminating
1057 * nul. RegEnumKeyEx requires a 257 character buffer to
1058 * retrieve such a key name. */
1059 wchar_t tmpbuf[257];
Kristján Valur Jónsson984dfa72012-04-02 15:23:29 +00001060 DWORD len = sizeof(tmpbuf)/sizeof(wchar_t); /* includes NULL terminator */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001063 rc = RegEnumKeyExW(key, index, tmpbuf, &len, NULL, NULL, NULL, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 Py_END_ALLOW_THREADS
1065 if (rc != ERROR_SUCCESS)
1066 return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx");
Tim Peters313fcd42006-02-19 04:05:39 +00001067
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001068 retStr = PyUnicode_FromWideChar(tmpbuf, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 return retStr; /* can be NULL */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001070}
1071
Zachary Warefd2d4822015-05-13 01:21:57 -05001072/*[clinic input]
1073winreg.EnumValue
1074
1075 key: HKEY
1076 An already open key, or any one of the predefined HKEY_* constants.
1077 index: int
1078 An integer that identifies the index of the value to retrieve.
1079 /
1080
1081Enumerates values of an open registry key.
1082
1083The function retrieves the name of one subkey each time it is called.
1084It is typically called repeatedly, until an OSError exception
1085is raised, indicating no more values.
1086
1087The result is a tuple of 3 items:
1088 value_name
1089 A string that identifies the value.
1090 value_data
1091 An object that holds the value data, and whose type depends
1092 on the underlying registry type.
1093 data_type
1094 An integer that identifies the type of the value data.
1095[clinic start generated code]*/
1096
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001097static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001098winreg_EnumValue_impl(PyObject *module, HKEY key, int index)
1099/*[clinic end generated code: output=d363b5a06f8789ac input=4414f47a6fb238b5]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 long rc;
1102 wchar_t *retValueBuf;
Amaury Forgeot d'Arcf2b69df2010-08-16 22:11:29 +00001103 BYTE *tmpBuf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 BYTE *retDataBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001105 DWORD retValueSize, bufValueSize;
1106 DWORD retDataSize, bufDataSize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 DWORD typ;
1108 PyObject *obData;
1109 PyObject *retVal;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001110
Zachary Warefd2d4822015-05-13 01:21:57 -05001111 if ((rc = RegQueryInfoKeyW(key, NULL, NULL, NULL, NULL, NULL, NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 NULL,
1113 &retValueSize, &retDataSize, NULL, NULL))
1114 != ERROR_SUCCESS)
1115 return PyErr_SetFromWindowsErrWithFunction(rc,
1116 "RegQueryInfoKey");
1117 ++retValueSize; /* include null terminators */
1118 ++retDataSize;
Brian Curtin60853212010-05-26 17:43:50 +00001119 bufDataSize = retDataSize;
1120 bufValueSize = retValueSize;
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02001121 retValueBuf = PyMem_New(wchar_t, retValueSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 if (retValueBuf == NULL)
1123 return PyErr_NoMemory();
1124 retDataBuf = (BYTE *)PyMem_Malloc(retDataSize);
1125 if (retDataBuf == NULL) {
1126 PyMem_Free(retValueBuf);
1127 return PyErr_NoMemory();
1128 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001129
Brian Curtin60853212010-05-26 17:43:50 +00001130 while (1) {
Brian Curtin60853212010-05-26 17:43:50 +00001131 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001132 rc = RegEnumValueW(key,
Brian Curtin60853212010-05-26 17:43:50 +00001133 index,
1134 retValueBuf,
1135 &retValueSize,
1136 NULL,
1137 &typ,
1138 (BYTE *)retDataBuf,
1139 &retDataSize);
1140 Py_END_ALLOW_THREADS
1141
1142 if (rc != ERROR_MORE_DATA)
1143 break;
1144
1145 bufDataSize *= 2;
Amaury Forgeot d'Arcf2b69df2010-08-16 22:11:29 +00001146 tmpBuf = (BYTE *)PyMem_Realloc(retDataBuf, bufDataSize);
Brian Curtin9b7e2d12010-06-08 20:57:52 +00001147 if (tmpBuf == NULL) {
Brian Curtin60853212010-05-26 17:43:50 +00001148 PyErr_NoMemory();
1149 retVal = NULL;
1150 goto fail;
1151 }
Brian Curtin9b7e2d12010-06-08 20:57:52 +00001152 retDataBuf = tmpBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001153 retDataSize = bufDataSize;
1154 retValueSize = bufValueSize;
1155 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 if (rc != ERROR_SUCCESS) {
1158 retVal = PyErr_SetFromWindowsErrWithFunction(rc,
1159 "PyRegEnumValue");
1160 goto fail;
1161 }
1162 obData = Reg2Py(retDataBuf, retDataSize, typ);
1163 if (obData == NULL) {
1164 retVal = NULL;
1165 goto fail;
1166 }
1167 retVal = Py_BuildValue("uOi", retValueBuf, obData, typ);
1168 Py_DECREF(obData);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001169 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 PyMem_Free(retValueBuf);
1171 PyMem_Free(retDataBuf);
1172 return retVal;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001173}
1174
Zachary Warefd2d4822015-05-13 01:21:57 -05001175/*[clinic input]
1176winreg.ExpandEnvironmentStrings
1177
1178 string: Py_UNICODE
1179 /
1180
1181Expand environment vars.
1182[clinic start generated code]*/
1183
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001184static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001185winreg_ExpandEnvironmentStrings_impl(PyObject *module, Py_UNICODE *string)
1186/*[clinic end generated code: output=cba46ac293a8af1a input=b2a9714d2b751aa6]*/
Christian Heimes2380ac72008-01-09 00:17:24 +00001187{
Victor Stinner9d3b93b2011-11-22 02:27:30 +01001188 wchar_t *retValue = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 DWORD retValueSize;
1190 DWORD rc;
1191 PyObject *o;
Christian Heimes2380ac72008-01-09 00:17:24 +00001192
Zachary Warefd2d4822015-05-13 01:21:57 -05001193 retValueSize = ExpandEnvironmentStringsW(string, retValue, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 if (retValueSize == 0) {
1195 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1196 "ExpandEnvironmentStrings");
1197 }
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02001198 retValue = PyMem_New(wchar_t, retValueSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 if (retValue == NULL) {
1200 return PyErr_NoMemory();
1201 }
Christian Heimes2380ac72008-01-09 00:17:24 +00001202
Zachary Warefd2d4822015-05-13 01:21:57 -05001203 rc = ExpandEnvironmentStringsW(string, retValue, retValueSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 if (rc == 0) {
1205 PyMem_Free(retValue);
1206 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1207 "ExpandEnvironmentStrings");
1208 }
Victor Stinner9d3b93b2011-11-22 02:27:30 +01001209 o = PyUnicode_FromWideChar(retValue, wcslen(retValue));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 PyMem_Free(retValue);
1211 return o;
Christian Heimes2380ac72008-01-09 00:17:24 +00001212}
1213
Zachary Warefd2d4822015-05-13 01:21:57 -05001214/*[clinic input]
1215winreg.FlushKey
1216
1217 key: HKEY
1218 An already open key, or any one of the predefined HKEY_* constants.
1219 /
1220
1221Writes all the attributes of a key to the registry.
1222
1223It is not necessary to call FlushKey to change a key. Registry changes
1224are flushed to disk by the registry using its lazy flusher. Registry
1225changes are also flushed to disk at system shutdown. Unlike
1226CloseKey(), the FlushKey() method returns only when all the data has
1227been written to the registry.
1228
1229An application should only call FlushKey() if it requires absolute
1230certainty that registry changes are on disk. If you don't know whether
1231a FlushKey() call is required, it probably isn't.
1232[clinic start generated code]*/
1233
Christian Heimes2380ac72008-01-09 00:17:24 +00001234static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001235winreg_FlushKey_impl(PyObject *module, HKEY key)
1236/*[clinic end generated code: output=e6fc230d4c5dc049 input=f57457c12297d82f]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001237{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 long rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001240 rc = RegFlushKey(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 Py_END_ALLOW_THREADS
1242 if (rc != ERROR_SUCCESS)
1243 return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001244 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001245}
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001246
Zachary Warefd2d4822015-05-13 01:21:57 -05001247
1248/*[clinic input]
1249winreg.LoadKey
1250
1251 key: HKEY
1252 An already open key, or any one of the predefined HKEY_* constants.
1253 sub_key: Py_UNICODE
1254 A string that identifies the sub-key to load.
1255 file_name: Py_UNICODE
1256 The name of the file to load registry data from. This file must
1257 have been created with the SaveKey() function. Under the file
1258 allocation table (FAT) file system, the filename may not have an
1259 extension.
1260 /
1261
1262Insert data into the registry from a file.
1263
1264Creates a subkey under the specified key and stores registration
1265information from a specified file into that subkey.
1266
1267A call to LoadKey() fails if the calling process does not have the
1268SE_RESTORE_PRIVILEGE privilege.
1269
1270If key is a handle returned by ConnectRegistry(), then the path
1271specified in fileName is relative to the remote computer.
1272
1273The MSDN docs imply key must be in the HKEY_USER or HKEY_LOCAL_MACHINE
1274tree.
1275[clinic start generated code]*/
1276
1277static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001278winreg_LoadKey_impl(PyObject *module, HKEY key, Py_UNICODE *sub_key,
Zachary Ware77772c02015-05-13 10:58:35 -05001279 Py_UNICODE *file_name)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001280/*[clinic end generated code: output=87344005c5905cde input=e3b5b45ade311582]*/
Zachary Warefd2d4822015-05-13 01:21:57 -05001281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 long rc;
Zachary Warefd2d4822015-05-13 01:21:57 -05001283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001285 rc = RegLoadKeyW(key, sub_key, file_name );
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 Py_END_ALLOW_THREADS
1287 if (rc != ERROR_SUCCESS)
1288 return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001289 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001290}
1291
Zachary Warefd2d4822015-05-13 01:21:57 -05001292/*[clinic input]
1293winreg.OpenKey -> HKEY
1294
1295 key: HKEY
1296 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001297 sub_key: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001298 A string that identifies the sub_key to open.
1299 reserved: int = 0
1300 A reserved integer that must be zero. Default is zero.
1301 access: REGSAM(c_default='KEY_READ') = winreg.KEY_READ
1302 An integer that specifies an access mask that describes the desired
1303 security access for the key. Default is KEY_READ.
1304
1305Opens the specified key.
1306
1307The result is a new handle to the specified key.
1308If the function fails, an OSError exception is raised.
1309[clinic start generated code]*/
1310
1311static HKEY
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001312winreg_OpenKey_impl(PyObject *module, HKEY key, Py_UNICODE *sub_key,
Zachary Ware77772c02015-05-13 10:58:35 -05001313 int reserved, REGSAM access)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001314/*[clinic end generated code: output=a905f1b947f3ce85 input=098505ac36a9ae28]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 HKEY retKey;
1317 long rc;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001320 rc = RegOpenKeyExW(key, sub_key, reserved, access, &retKey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 Py_END_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001322 if (rc != ERROR_SUCCESS) {
1323 PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx");
1324 return NULL;
1325 }
1326 return retKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001327}
1328
Zachary Warefd2d4822015-05-13 01:21:57 -05001329/*[clinic input]
1330winreg.OpenKeyEx = winreg.OpenKey
1331
1332Opens the specified key.
1333
1334The result is a new handle to the specified key.
1335If the function fails, an OSError exception is raised.
1336[clinic start generated code]*/
1337
1338static HKEY
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001339winreg_OpenKeyEx_impl(PyObject *module, HKEY key, Py_UNICODE *sub_key,
Zachary Ware77772c02015-05-13 10:58:35 -05001340 int reserved, REGSAM access)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001341/*[clinic end generated code: output=226042593b37e940 input=c6c4972af8622959]*/
Zachary Warefd2d4822015-05-13 01:21:57 -05001342{
1343 return winreg_OpenKey_impl(module, key, sub_key, reserved, access);
1344}
1345
1346/*[clinic input]
1347winreg.QueryInfoKey
1348
1349 key: HKEY
1350 An already open key, or any one of the predefined HKEY_* constants.
1351 /
1352
1353Returns information about a key.
1354
1355The result is a tuple of 3 items:
1356An integer that identifies the number of sub keys this key has.
1357An integer that identifies the number of values this key has.
1358An integer that identifies when the key was last modified (if available)
1359as 100's of nanoseconds since Jan 1, 1600.
1360[clinic start generated code]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001361
1362static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001363winreg_QueryInfoKey_impl(PyObject *module, HKEY key)
1364/*[clinic end generated code: output=dc657b8356a4f438 input=c3593802390cde1f]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001365{
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001366 long rc;
1367 DWORD nSubKeys, nValues;
1368 FILETIME ft;
1369 LARGE_INTEGER li;
1370 PyObject *l;
1371 PyObject *ret;
Zachary Warefd2d4822015-05-13 01:21:57 -05001372
1373 if ((rc = RegQueryInfoKey(key, NULL, NULL, 0, &nSubKeys, NULL, NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 &nValues, NULL, NULL, NULL, &ft))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001375 != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey");
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001377 li.LowPart = ft.dwLowDateTime;
1378 li.HighPart = ft.dwHighDateTime;
1379 l = PyLong_FromLongLong(li.QuadPart);
1380 if (l == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001382 ret = Py_BuildValue("iiO", nSubKeys, nValues, l);
1383 Py_DECREF(l);
1384 return ret;
1385}
1386
Zachary Warefd2d4822015-05-13 01:21:57 -05001387/*[clinic input]
1388winreg.QueryValue
1389
1390 key: HKEY
1391 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001392 sub_key: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001393 A string that holds the name of the subkey with which the value
1394 is associated. If this parameter is None or empty, the function
1395 retrieves the value set by the SetValue() method for the key
1396 identified by key.
1397 /
1398
1399Retrieves the unnamed value for a key.
1400
1401Values in the registry have name, type, and data components. This method
1402retrieves the data for a key's first value that has a NULL name.
1403But since the underlying API call doesn't return the type, you'll
1404probably be happier using QueryValueEx; this function is just here for
1405completeness.
1406[clinic start generated code]*/
1407
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001408static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001409winreg_QueryValue_impl(PyObject *module, HKEY key, Py_UNICODE *sub_key)
1410/*[clinic end generated code: output=2bb8d1e02c10d0b6 input=41cafbbf423b21d6]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 long rc;
1413 PyObject *retStr;
1414 wchar_t *retBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001415 DWORD bufSize = 0;
1416 DWORD retSize = 0;
1417 wchar_t *tmp;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001418
Zachary Warefd2d4822015-05-13 01:21:57 -05001419 rc = RegQueryValueW(key, sub_key, NULL, &retSize);
Brian Curtin60853212010-05-26 17:43:50 +00001420 if (rc == ERROR_MORE_DATA)
1421 retSize = 256;
1422 else if (rc != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 return PyErr_SetFromWindowsErrWithFunction(rc,
1424 "RegQueryValue");
Brian Curtin60853212010-05-26 17:43:50 +00001425
1426 bufSize = retSize;
1427 retBuf = (wchar_t *) PyMem_Malloc(bufSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 if (retBuf == NULL)
1429 return PyErr_NoMemory();
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001430
Brian Curtin60853212010-05-26 17:43:50 +00001431 while (1) {
1432 retSize = bufSize;
Zachary Warefd2d4822015-05-13 01:21:57 -05001433 rc = RegQueryValueW(key, sub_key, retBuf, &retSize);
Brian Curtin60853212010-05-26 17:43:50 +00001434 if (rc != ERROR_MORE_DATA)
1435 break;
1436
1437 bufSize *= 2;
1438 tmp = (wchar_t *) PyMem_Realloc(retBuf, bufSize);
1439 if (tmp == NULL) {
1440 PyMem_Free(retBuf);
1441 return PyErr_NoMemory();
1442 }
1443 retBuf = tmp;
1444 }
1445
1446 if (rc != ERROR_SUCCESS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 PyMem_Free(retBuf);
1448 return PyErr_SetFromWindowsErrWithFunction(rc,
1449 "RegQueryValue");
1450 }
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001451
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001452 retStr = PyUnicode_FromWideChar(retBuf, wcslen(retBuf));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 PyMem_Free(retBuf);
1454 return retStr;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001455}
1456
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001457
Zachary Warefd2d4822015-05-13 01:21:57 -05001458/*[clinic input]
1459winreg.QueryValueEx
1460
1461 key: HKEY
1462 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001463 name: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001464 A string indicating the value to query.
1465 /
1466
1467Retrieves the type and value of a specified sub-key.
1468
1469Behaves mostly like QueryValue(), but also returns the type of the
1470specified value name associated with the given open registry key.
1471
1472The return value is a tuple of the value and the type_id.
1473[clinic start generated code]*/
1474
1475static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001476winreg_QueryValueEx_impl(PyObject *module, HKEY key, Py_UNICODE *name)
1477/*[clinic end generated code: output=5b4fa3e33d6d3e8f input=cf366cada4836891]*/
Zachary Warefd2d4822015-05-13 01:21:57 -05001478{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 long rc;
Brian Curtin60853212010-05-26 17:43:50 +00001480 BYTE *retBuf, *tmp;
1481 DWORD bufSize = 0, retSize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 DWORD typ;
1483 PyObject *obData;
1484 PyObject *result;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001485
Zachary Warefd2d4822015-05-13 01:21:57 -05001486 rc = RegQueryValueExW(key, name, NULL, NULL, NULL, &bufSize);
Brian Curtin60853212010-05-26 17:43:50 +00001487 if (rc == ERROR_MORE_DATA)
1488 bufSize = 256;
1489 else if (rc != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 return PyErr_SetFromWindowsErrWithFunction(rc,
1491 "RegQueryValueEx");
1492 retBuf = (BYTE *)PyMem_Malloc(bufSize);
1493 if (retBuf == NULL)
1494 return PyErr_NoMemory();
Brian Curtin60853212010-05-26 17:43:50 +00001495
1496 while (1) {
1497 retSize = bufSize;
Zachary Warefd2d4822015-05-13 01:21:57 -05001498 rc = RegQueryValueExW(key, name, NULL, &typ,
Brian Curtin60853212010-05-26 17:43:50 +00001499 (BYTE *)retBuf, &retSize);
1500 if (rc != ERROR_MORE_DATA)
1501 break;
1502
1503 bufSize *= 2;
1504 tmp = (char *) PyMem_Realloc(retBuf, bufSize);
1505 if (tmp == NULL) {
1506 PyMem_Free(retBuf);
1507 return PyErr_NoMemory();
1508 }
1509 retBuf = tmp;
1510 }
1511
1512 if (rc != ERROR_SUCCESS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 PyMem_Free(retBuf);
1514 return PyErr_SetFromWindowsErrWithFunction(rc,
1515 "RegQueryValueEx");
1516 }
1517 obData = Reg2Py(retBuf, bufSize, typ);
1518 PyMem_Free(retBuf);
1519 if (obData == NULL)
1520 return NULL;
1521 result = Py_BuildValue("Oi", obData, typ);
1522 Py_DECREF(obData);
1523 return result;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001524}
1525
Zachary Warefd2d4822015-05-13 01:21:57 -05001526/*[clinic input]
1527winreg.SaveKey
1528
1529 key: HKEY
1530 An already open key, or any one of the predefined HKEY_* constants.
1531 file_name: Py_UNICODE
1532 The name of the file to save registry data to. This file cannot
1533 already exist. If this filename includes an extension, it cannot be
1534 used on file allocation table (FAT) file systems by the LoadKey(),
1535 ReplaceKey() or RestoreKey() methods.
1536 /
1537
1538Saves the specified key, and all its subkeys to the specified file.
1539
1540If key represents a key on a remote computer, the path described by
1541file_name is relative to the remote computer.
1542
1543The caller of this method must possess the SeBackupPrivilege
1544security privilege. This function passes NULL for security_attributes
1545to the API.
1546[clinic start generated code]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001547
1548static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001549winreg_SaveKey_impl(PyObject *module, HKEY key, Py_UNICODE *file_name)
1550/*[clinic end generated code: output=1dda1502bd4c30d8 input=da735241f91ac7a2]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001551{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 LPSECURITY_ATTRIBUTES pSA = NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 long rc;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001555/* One day we may get security into the core?
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE))
1557 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001558*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001560 rc = RegSaveKeyW(key, file_name, pSA );
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 Py_END_ALLOW_THREADS
1562 if (rc != ERROR_SUCCESS)
1563 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001564 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001565}
1566
Zachary Warefd2d4822015-05-13 01:21:57 -05001567/*[clinic input]
1568winreg.SetValue
1569
1570 key: HKEY
1571 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001572 sub_key: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001573 A string that names the subkey with which the value is associated.
1574 type: DWORD
1575 An integer that specifies the type of the data. Currently this must
1576 be REG_SZ, meaning only strings are supported.
Zachary Ware77772c02015-05-13 10:58:35 -05001577 value: Py_UNICODE(zeroes=True)
Zachary Warefd2d4822015-05-13 01:21:57 -05001578 A string that specifies the new value.
1579 /
1580
1581Associates a value with a specified key.
1582
1583If the key specified by the sub_key parameter does not exist, the
1584SetValue function creates it.
1585
1586Value lengths are limited by available memory. Long values (more than
15872048 bytes) should be stored as files with the filenames stored in
1588the configuration registry to help the registry perform efficiently.
1589
1590The key identified by the key parameter must have been opened with
1591KEY_SET_VALUE access.
1592[clinic start generated code]*/
1593
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001594static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001595winreg_SetValue_impl(PyObject *module, HKEY key, Py_UNICODE *sub_key,
Zachary Ware77772c02015-05-13 10:58:35 -05001596 DWORD type, Py_UNICODE *value,
1597 Py_ssize_clean_t value_length)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001598/*[clinic end generated code: output=1e31931174820631 input=2cd2adab79339c53]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001599{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 long rc;
Zachary Warefd2d4822015-05-13 01:21:57 -05001601
1602 if (type != REG_SZ) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 PyErr_SetString(PyExc_TypeError,
1604 "Type must be winreg.REG_SZ");
1605 return NULL;
1606 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001609 rc = RegSetValueW(key, sub_key, REG_SZ, value, value_length+1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 Py_END_ALLOW_THREADS
1611 if (rc != ERROR_SUCCESS)
1612 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue");
Zachary Warefd2d4822015-05-13 01:21:57 -05001613 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001614}
1615
Zachary Warefd2d4822015-05-13 01:21:57 -05001616/*[clinic input]
1617winreg.SetValueEx
1618
1619 key: HKEY
1620 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001621 value_name: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001622 A string containing the name of the value to set, or None.
1623 reserved: object
1624 Can be anything - zero is always passed to the API.
1625 type: DWORD
1626 An integer that specifies the type of the data, one of:
1627 REG_BINARY -- Binary data in any form.
1628 REG_DWORD -- A 32-bit number.
Steve Dower80ac11d2016-05-24 15:42:04 -07001629 REG_DWORD_LITTLE_ENDIAN -- A 32-bit number in little-endian format. Equivalent to REG_DWORD
Zachary Warefd2d4822015-05-13 01:21:57 -05001630 REG_DWORD_BIG_ENDIAN -- A 32-bit number in big-endian format.
1631 REG_EXPAND_SZ -- A null-terminated string that contains unexpanded
1632 references to environment variables (for example,
1633 %PATH%).
1634 REG_LINK -- A Unicode symbolic link.
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03001635 REG_MULTI_SZ -- A sequence of null-terminated strings, terminated
Zachary Warefd2d4822015-05-13 01:21:57 -05001636 by two null characters. Note that Python handles
1637 this termination automatically.
1638 REG_NONE -- No defined value type.
Steve Dower80ac11d2016-05-24 15:42:04 -07001639 REG_QWORD -- A 64-bit number.
1640 REG_QWORD_LITTLE_ENDIAN -- A 64-bit number in little-endian format. Equivalent to REG_QWORD.
Zachary Warefd2d4822015-05-13 01:21:57 -05001641 REG_RESOURCE_LIST -- A device-driver resource list.
1642 REG_SZ -- A null-terminated string.
1643 value: object
1644 A string that specifies the new value.
1645 /
1646
1647Stores data in the value field of an open registry key.
1648
1649This method can also set additional value and type information for the
1650specified key. The key identified by the key parameter must have been
1651opened with KEY_SET_VALUE access.
1652
1653To open the key, use the CreateKeyEx() or OpenKeyEx() methods.
1654
1655Value lengths are limited by available memory. Long values (more than
16562048 bytes) should be stored as files with the filenames stored in
1657the configuration registry to help the registry perform efficiently.
1658[clinic start generated code]*/
1659
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001660static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001661winreg_SetValueEx_impl(PyObject *module, HKEY key, Py_UNICODE *value_name,
Zachary Ware77772c02015-05-13 10:58:35 -05001662 PyObject *reserved, DWORD type, PyObject *value)
Serhiy Storchaka2954f832016-07-07 18:20:03 +03001663/*[clinic end generated code: output=c88c8426b6c00ec7 input=900a9e3990bfb196]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001664{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 BYTE *data;
1666 DWORD len;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 LONG rc;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001669
Zachary Warefd2d4822015-05-13 01:21:57 -05001670 if (!Py2Reg(value, type, &data, &len))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 {
1672 if (!PyErr_Occurred())
1673 PyErr_SetString(PyExc_ValueError,
1674 "Could not convert the data to the specified type.");
1675 return NULL;
1676 }
1677 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001678 rc = RegSetValueExW(key, value_name, 0, type, data, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 Py_END_ALLOW_THREADS
1680 PyMem_DEL(data);
1681 if (rc != ERROR_SUCCESS)
1682 return PyErr_SetFromWindowsErrWithFunction(rc,
1683 "RegSetValueEx");
Zachary Warefd2d4822015-05-13 01:21:57 -05001684 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001685}
1686
Zachary Warefd2d4822015-05-13 01:21:57 -05001687/*[clinic input]
1688winreg.DisableReflectionKey
1689
1690 key: HKEY
1691 An already open key, or any one of the predefined HKEY_* constants.
1692 /
1693
1694Disables registry reflection for 32bit processes running on a 64bit OS.
1695
1696Will generally raise NotImplemented if executed on a 32bit OS.
1697
1698If the key is not on the reflection list, the function succeeds but has
1699no effect. Disabling reflection for a key does not affect reflection
1700of any subkeys.
1701[clinic start generated code]*/
1702
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001703static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001704winreg_DisableReflectionKey_impl(PyObject *module, HKEY key)
1705/*[clinic end generated code: output=830cce504cc764b4 input=a6c9e5ca5410193c]*/
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001706{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 HMODULE hMod;
1708 typedef LONG (WINAPI *RDRKFunc)(HKEY);
1709 RDRKFunc pfn = NULL;
1710 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 /* Only available on 64bit platforms, so we must load it
1713 dynamically.*/
Martin v. Löwis50590f12012-01-14 17:54:09 +01001714 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 if (hMod)
1716 pfn = (RDRKFunc)GetProcAddress(hMod,
1717 "RegDisableReflectionKey");
1718 if (!pfn) {
1719 PyErr_SetString(PyExc_NotImplementedError,
1720 "not implemented on this platform");
1721 return NULL;
1722 }
1723 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001724 rc = (*pfn)(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 Py_END_ALLOW_THREADS
1726 if (rc != ERROR_SUCCESS)
1727 return PyErr_SetFromWindowsErrWithFunction(rc,
1728 "RegDisableReflectionKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001729 Py_RETURN_NONE;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001730}
1731
Zachary Warefd2d4822015-05-13 01:21:57 -05001732/*[clinic input]
1733winreg.EnableReflectionKey
1734
1735 key: HKEY
1736 An already open key, or any one of the predefined HKEY_* constants.
1737 /
1738
1739Restores registry reflection for the specified disabled key.
1740
1741Will generally raise NotImplemented if executed on a 32bit OS.
1742Restoring reflection for a key does not affect reflection of any
1743subkeys.
1744[clinic start generated code]*/
1745
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001746static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001747winreg_EnableReflectionKey_impl(PyObject *module, HKEY key)
1748/*[clinic end generated code: output=86fa1385fdd9ce57 input=7748abbacd1e166a]*/
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001749{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 HMODULE hMod;
1751 typedef LONG (WINAPI *RERKFunc)(HKEY);
1752 RERKFunc pfn = NULL;
1753 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 /* Only available on 64bit platforms, so we must load it
1756 dynamically.*/
Martin v. Löwis50590f12012-01-14 17:54:09 +01001757 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 if (hMod)
1759 pfn = (RERKFunc)GetProcAddress(hMod,
1760 "RegEnableReflectionKey");
1761 if (!pfn) {
1762 PyErr_SetString(PyExc_NotImplementedError,
1763 "not implemented on this platform");
1764 return NULL;
1765 }
1766 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001767 rc = (*pfn)(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 Py_END_ALLOW_THREADS
1769 if (rc != ERROR_SUCCESS)
1770 return PyErr_SetFromWindowsErrWithFunction(rc,
1771 "RegEnableReflectionKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001772 Py_RETURN_NONE;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001773}
1774
Zachary Warefd2d4822015-05-13 01:21:57 -05001775/*[clinic input]
1776winreg.QueryReflectionKey
1777
1778 key: HKEY
1779 An already open key, or any one of the predefined HKEY_* constants.
1780 /
1781
1782Returns the reflection state for the specified key as a bool.
1783
1784Will generally raise NotImplemented if executed on a 32bit OS.
1785[clinic start generated code]*/
1786
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001787static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001788winreg_QueryReflectionKey_impl(PyObject *module, HKEY key)
1789/*[clinic end generated code: output=4e774af288c3ebb9 input=9f325eacb5a65d88]*/
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001790{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 HMODULE hMod;
1792 typedef LONG (WINAPI *RQRKFunc)(HKEY, BOOL *);
1793 RQRKFunc pfn = NULL;
1794 BOOL result;
1795 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 /* Only available on 64bit platforms, so we must load it
1798 dynamically.*/
Martin v. Löwis50590f12012-01-14 17:54:09 +01001799 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 if (hMod)
1801 pfn = (RQRKFunc)GetProcAddress(hMod,
1802 "RegQueryReflectionKey");
1803 if (!pfn) {
1804 PyErr_SetString(PyExc_NotImplementedError,
1805 "not implemented on this platform");
1806 return NULL;
1807 }
1808 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001809 rc = (*pfn)(key, &result);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 Py_END_ALLOW_THREADS
1811 if (rc != ERROR_SUCCESS)
1812 return PyErr_SetFromWindowsErrWithFunction(rc,
1813 "RegQueryReflectionKey");
1814 return PyBool_FromLong(result);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001815}
1816
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001817static struct PyMethodDef winreg_methods[] = {
Zachary Warefd2d4822015-05-13 01:21:57 -05001818 WINREG_CLOSEKEY_METHODDEF
1819 WINREG_CONNECTREGISTRY_METHODDEF
1820 WINREG_CREATEKEY_METHODDEF
1821 WINREG_CREATEKEYEX_METHODDEF
1822 WINREG_DELETEKEY_METHODDEF
1823 WINREG_DELETEKEYEX_METHODDEF
1824 WINREG_DELETEVALUE_METHODDEF
1825 WINREG_DISABLEREFLECTIONKEY_METHODDEF
1826 WINREG_ENABLEREFLECTIONKEY_METHODDEF
1827 WINREG_ENUMKEY_METHODDEF
1828 WINREG_ENUMVALUE_METHODDEF
1829 WINREG_EXPANDENVIRONMENTSTRINGS_METHODDEF
1830 WINREG_FLUSHKEY_METHODDEF
1831 WINREG_LOADKEY_METHODDEF
1832 WINREG_OPENKEY_METHODDEF
1833 WINREG_OPENKEYEX_METHODDEF
1834 WINREG_QUERYVALUE_METHODDEF
1835 WINREG_QUERYVALUEEX_METHODDEF
1836 WINREG_QUERYINFOKEY_METHODDEF
1837 WINREG_QUERYREFLECTIONKEY_METHODDEF
1838 WINREG_SAVEKEY_METHODDEF
1839 WINREG_SETVALUE_METHODDEF
1840 WINREG_SETVALUEEX_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841 NULL,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001842};
1843
1844static void
1845insint(PyObject * d, char * name, long value)
1846{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 PyObject *v = PyLong_FromLong(value);
1848 if (!v || PyDict_SetItemString(d, name, v))
1849 PyErr_Clear();
1850 Py_XDECREF(v);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001851}
1852
1853#define ADD_INT(val) insint(d, #val, val)
1854
1855static void
1856inskey(PyObject * d, char * name, HKEY key)
1857{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 PyObject *v = PyLong_FromVoidPtr(key);
1859 if (!v || PyDict_SetItemString(d, name, v))
1860 PyErr_Clear();
1861 Py_XDECREF(v);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001862}
1863
1864#define ADD_KEY(val) inskey(d, #val, val)
1865
Martin v. Löwis1a214512008-06-11 05:26:20 +00001866
1867static struct PyModuleDef winregmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868 PyModuleDef_HEAD_INIT,
1869 "winreg",
1870 module_doc,
1871 -1,
1872 winreg_methods,
1873 NULL,
1874 NULL,
1875 NULL,
1876 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001877};
1878
1879PyMODINIT_FUNC PyInit_winreg(void)
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001880{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 PyObject *m, *d;
1882 m = PyModule_Create(&winregmodule);
1883 if (m == NULL)
1884 return NULL;
1885 d = PyModule_GetDict(m);
1886 PyHKEY_Type.tp_doc = PyHKEY_doc;
1887 if (PyType_Ready(&PyHKEY_Type) < 0)
1888 return NULL;
1889 Py_INCREF(&PyHKEY_Type);
1890 if (PyDict_SetItemString(d, "HKEYType",
1891 (PyObject *)&PyHKEY_Type) != 0)
1892 return NULL;
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001893 Py_INCREF(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 if (PyDict_SetItemString(d, "error",
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001895 PyExc_OSError) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 /* Add the relevant constants */
1899 ADD_KEY(HKEY_CLASSES_ROOT);
1900 ADD_KEY(HKEY_CURRENT_USER);
1901 ADD_KEY(HKEY_LOCAL_MACHINE);
1902 ADD_KEY(HKEY_USERS);
1903 ADD_KEY(HKEY_PERFORMANCE_DATA);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001904#ifdef HKEY_CURRENT_CONFIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 ADD_KEY(HKEY_CURRENT_CONFIG);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001906#endif
1907#ifdef HKEY_DYN_DATA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 ADD_KEY(HKEY_DYN_DATA);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001909#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 ADD_INT(KEY_QUERY_VALUE);
1911 ADD_INT(KEY_SET_VALUE);
1912 ADD_INT(KEY_CREATE_SUB_KEY);
1913 ADD_INT(KEY_ENUMERATE_SUB_KEYS);
1914 ADD_INT(KEY_NOTIFY);
1915 ADD_INT(KEY_CREATE_LINK);
1916 ADD_INT(KEY_READ);
1917 ADD_INT(KEY_WRITE);
1918 ADD_INT(KEY_EXECUTE);
1919 ADD_INT(KEY_ALL_ACCESS);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001920#ifdef KEY_WOW64_64KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 ADD_INT(KEY_WOW64_64KEY);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001922#endif
1923#ifdef KEY_WOW64_32KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 ADD_INT(KEY_WOW64_32KEY);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001925#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 ADD_INT(REG_OPTION_RESERVED);
1927 ADD_INT(REG_OPTION_NON_VOLATILE);
1928 ADD_INT(REG_OPTION_VOLATILE);
1929 ADD_INT(REG_OPTION_CREATE_LINK);
1930 ADD_INT(REG_OPTION_BACKUP_RESTORE);
1931 ADD_INT(REG_OPTION_OPEN_LINK);
1932 ADD_INT(REG_LEGAL_OPTION);
1933 ADD_INT(REG_CREATED_NEW_KEY);
1934 ADD_INT(REG_OPENED_EXISTING_KEY);
1935 ADD_INT(REG_WHOLE_HIVE_VOLATILE);
1936 ADD_INT(REG_REFRESH_HIVE);
1937 ADD_INT(REG_NO_LAZY_FLUSH);
1938 ADD_INT(REG_NOTIFY_CHANGE_NAME);
1939 ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES);
1940 ADD_INT(REG_NOTIFY_CHANGE_LAST_SET);
1941 ADD_INT(REG_NOTIFY_CHANGE_SECURITY);
1942 ADD_INT(REG_LEGAL_CHANGE_FILTER);
1943 ADD_INT(REG_NONE);
1944 ADD_INT(REG_SZ);
1945 ADD_INT(REG_EXPAND_SZ);
1946 ADD_INT(REG_BINARY);
1947 ADD_INT(REG_DWORD);
1948 ADD_INT(REG_DWORD_LITTLE_ENDIAN);
1949 ADD_INT(REG_DWORD_BIG_ENDIAN);
Steve Dower80ac11d2016-05-24 15:42:04 -07001950 ADD_INT(REG_QWORD);
1951 ADD_INT(REG_QWORD_LITTLE_ENDIAN);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 ADD_INT(REG_LINK);
1953 ADD_INT(REG_MULTI_SZ);
1954 ADD_INT(REG_RESOURCE_LIST);
1955 ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
1956 ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
1957 return m;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001958}
1959
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001960