blob: c6ad7ab64e167ab43cf9b1f9824298eba98cf541 [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"
oldkaa0735f2018-02-02 16:52:55 +080086"All functions which accept a handle object also accept an integer --\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000087"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();
Victor Stinnerb4435e22018-10-26 14:35:00 +0200462 PyObject_INIT((PyObject *)op, &PyHKEY_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 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);
Zackery Spytz5d953122018-11-08 02:45:00 -0700654 assert(wstr);
Victor Stinnerbe492442011-11-21 12:43:50 +0100655 wcscpy(P, wstr);
656 P += (len + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 }
658 /* And doubly-terminate the list... */
659 *P = '\0';
660 break;
661 }
662 case REG_BINARY:
663 /* ALSO handle ALL unknown data types here. Even if we can't
664 support it natively, we should handle the bits. */
665 default:
Zachary Waread4690f2014-07-03 10:58:06 -0500666 if (value == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 *retDataSize = 0;
Zachary Waread4690f2014-07-03 10:58:06 -0500668 *retDataBuf = NULL;
669 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 else {
671 Py_buffer view;
Neal Norwitz1385b892007-08-26 23:07:13 +0000672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 if (!PyObject_CheckBuffer(value)) {
674 PyErr_Format(PyExc_TypeError,
675 "Objects of type '%s' can not "
676 "be used as binary registry values",
677 value->ob_type->tp_name);
678 return FALSE;
679 }
Neal Norwitz1385b892007-08-26 23:07:13 +0000680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0)
682 return FALSE;
Neal Norwitz1385b892007-08-26 23:07:13 +0000683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 *retDataBuf = (BYTE *)PyMem_NEW(char, view.len);
Steve Dower4d4bc422016-05-25 11:26:07 -0700685 if (*retDataBuf == NULL){
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 PyBuffer_Release(&view);
687 PyErr_NoMemory();
688 return FALSE;
689 }
Brian Curtinabb33512010-08-17 20:08:40 +0000690 *retDataSize = Py_SAFE_DOWNCAST(view.len, Py_ssize_t, DWORD);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 memcpy(*retDataBuf, view.buf, view.len);
692 PyBuffer_Release(&view);
693 }
694 break;
695 }
696 return TRUE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000697}
698
699/* Convert Registry data into PyObject*/
700static PyObject *
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000701Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000702{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 PyObject *obData;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 switch (typ) {
706 case REG_DWORD:
707 if (retDataSize == 0)
Brian Curtin172e4222012-12-27 14:04:42 -0600708 obData = PyLong_FromUnsignedLong(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 else
Steve Dower80ac11d2016-05-24 15:42:04 -0700710 obData = PyLong_FromUnsignedLong(*(DWORD *)retDataBuf);
711 break;
712 case REG_QWORD:
713 if (retDataSize == 0)
714 obData = PyLong_FromUnsignedLongLong(0);
715 else
716 obData = PyLong_FromUnsignedLongLong(*(DWORD64 *)retDataBuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 break;
718 case REG_SZ:
719 case REG_EXPAND_SZ:
720 {
Steve Dower40fa2662016-12-17 13:30:27 -0800721 /* REG_SZ should be a NUL terminated string, but only by
722 * convention. The buffer may have been saved without a NUL
723 * or with embedded NULs. To be consistent with reg.exe and
724 * regedit.exe, consume only up to the first NUL. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 wchar_t *data = (wchar_t *)retDataBuf;
Steve Dower40fa2662016-12-17 13:30:27 -0800726 size_t len = wcsnlen(data, retDataSize / sizeof(wchar_t));
727 obData = PyUnicode_FromWideChar(data, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 break;
729 }
730 case REG_MULTI_SZ:
731 if (retDataSize == 0)
732 obData = PyList_New(0);
733 else
734 {
735 int index = 0;
736 wchar_t *data = (wchar_t *)retDataBuf;
737 int len = retDataSize / 2;
738 int s = countStrings(data, len);
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +0200739 wchar_t **str = PyMem_New(wchar_t *, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 if (str == NULL)
741 return PyErr_NoMemory();
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 fixupMultiSZ(str, data, len);
744 obData = PyList_New(s);
Jesus Cea782c4cf2014-03-13 17:35:32 +0100745 if (obData == NULL) {
Victor Stinner9cb1ec52014-03-13 19:08:10 +0100746 PyMem_Free(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 return NULL;
Jesus Cea782c4cf2014-03-13 17:35:32 +0100748 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 for (index = 0; index < s; index++)
750 {
751 size_t len = wcslen(str[index]);
752 if (len > INT_MAX) {
753 PyErr_SetString(PyExc_OverflowError,
754 "registry string is too long for a Python string");
755 Py_DECREF(obData);
Victor Stinner9cb1ec52014-03-13 19:08:10 +0100756 PyMem_Free(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 return NULL;
758 }
759 PyList_SetItem(obData,
760 index,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200761 PyUnicode_FromWideChar(str[index], len));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 }
Victor Stinnerb6404912013-07-07 16:21:41 +0200763 PyMem_Free(str);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 break;
766 }
767 case REG_BINARY:
768 /* ALSO handle ALL unknown data types here. Even if we can't
769 support it natively, we should handle the bits. */
770 default:
771 if (retDataSize == 0) {
772 Py_INCREF(Py_None);
773 obData = Py_None;
774 }
775 else
776 obData = PyBytes_FromStringAndSize(
777 (char *)retDataBuf, retDataSize);
778 break;
779 }
780 return obData;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000781}
782
783/* The Python methods */
784
Zachary Warefd2d4822015-05-13 01:21:57 -0500785/*[clinic input]
786winreg.CloseKey
787
788 hkey: object
789 A previously opened key.
790 /
791
792Closes a previously opened registry key.
793
794Note that if the key is not closed using this method, it will be
795closed when the hkey object is destroyed by Python.
796[clinic start generated code]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000797
798static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300799winreg_CloseKey(PyObject *module, PyObject *hkey)
800/*[clinic end generated code: output=a4fa537019a80d15 input=5b1aac65ba5127ad]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000801{
Zachary Warefd2d4822015-05-13 01:21:57 -0500802 if (!PyHKEY_Close(hkey))
803 return NULL;
804 Py_RETURN_NONE;
805}
806
807/*[clinic input]
808winreg.ConnectRegistry -> HKEY
809
Zachary Ware77772c02015-05-13 10:58:35 -0500810 computer_name: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -0500811 The name of the remote computer, of the form r"\\computername". If
812 None, the local computer is used.
813 key: HKEY
814 The predefined key to connect to.
815 /
816
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +0300817Establishes a connection to the registry on another computer.
Zachary Warefd2d4822015-05-13 01:21:57 -0500818
819The return value is the handle of the opened key.
820If the function fails, an OSError exception is raised.
821[clinic start generated code]*/
822
823static HKEY
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300824winreg_ConnectRegistry_impl(PyObject *module, Py_UNICODE *computer_name,
Zachary Ware77772c02015-05-13 10:58:35 -0500825 HKEY key)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300826/*[clinic end generated code: output=5ab79d02aa3167b4 input=5f98a891a347e68e]*/
Zachary Warefd2d4822015-05-13 01:21:57 -0500827{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 HKEY retKey;
829 long rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -0500831 rc = RegConnectRegistryW(computer_name, key, &retKey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 Py_END_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -0500833 if (rc != ERROR_SUCCESS) {
834 PyErr_SetFromWindowsErrWithFunction(rc, "ConnectRegistry");
835 return NULL;
836 }
837 return retKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000838}
839
Zachary Warefd2d4822015-05-13 01:21:57 -0500840/*[clinic input]
841winreg.CreateKey -> HKEY
842
843 key: HKEY
844 An already open key, or one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -0500845 sub_key: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -0500846 The name of the key this method opens or creates.
847 /
848
849Creates or opens the specified key.
850
851If key is one of the predefined keys, sub_key may be None. In that case,
852the handle returned is the same key handle passed in to the function.
853
854If the key already exists, this function opens the existing key.
855
856The return value is the handle of the opened key.
857If the function fails, an OSError exception is raised.
858[clinic start generated code]*/
859
860static HKEY
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300861winreg_CreateKey_impl(PyObject *module, HKEY key, Py_UNICODE *sub_key)
862/*[clinic end generated code: output=9c81d4095527c927 input=3cdd1622488acea2]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000863{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 HKEY retKey;
865 long rc;
Zachary Warefd2d4822015-05-13 01:21:57 -0500866
867 rc = RegCreateKeyW(key, sub_key, &retKey);
868 if (rc != ERROR_SUCCESS) {
869 PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 return NULL;
Zachary Warefd2d4822015-05-13 01:21:57 -0500871 }
872 return retKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000873}
874
Zachary Warefd2d4822015-05-13 01:21:57 -0500875/*[clinic input]
876winreg.CreateKeyEx -> HKEY
877
878 key: HKEY
879 An already open key, or one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -0500880 sub_key: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -0500881 The name of the key this method opens or creates.
882 reserved: int = 0
883 A reserved integer, and must be zero. Default is zero.
884 access: REGSAM(c_default='KEY_WRITE') = winreg.KEY_WRITE
885 An integer that specifies an access mask that describes the
886 desired security access for the key. Default is KEY_WRITE.
887
888Creates or opens the specified key.
889
890If key is one of the predefined keys, sub_key may be None. In that case,
891the handle returned is the same key handle passed in to the function.
892
893If the key already exists, this function opens the existing key
894
895The return value is the handle of the opened key.
896If the function fails, an OSError exception is raised.
897[clinic start generated code]*/
898
899static HKEY
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300900winreg_CreateKeyEx_impl(PyObject *module, HKEY key, Py_UNICODE *sub_key,
Zachary Ware77772c02015-05-13 10:58:35 -0500901 int reserved, REGSAM access)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300902/*[clinic end generated code: output=b9fce6dc5c4e39b1 input=42c2b03f98406b66]*/
Brian Curtin3035c392010-04-21 23:56:21 +0000903{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 HKEY retKey;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 long rc;
Brian Curtin1771b542010-09-27 17:56:36 +0000906
Segev Finer679b5662017-07-27 01:17:57 +0300907 rc = RegCreateKeyExW(key, sub_key, reserved, NULL, 0,
Brian Curtin1771b542010-09-27 17:56:36 +0000908 access, NULL, &retKey, NULL);
Zachary Warefd2d4822015-05-13 01:21:57 -0500909 if (rc != ERROR_SUCCESS) {
910 PyErr_SetFromWindowsErrWithFunction(rc, "CreateKeyEx");
911 return NULL;
912 }
913 return retKey;
Brian Curtin3035c392010-04-21 23:56:21 +0000914}
915
Zachary Warefd2d4822015-05-13 01:21:57 -0500916/*[clinic input]
917winreg.DeleteKey
918 key: HKEY
919 An already open key, or any one of the predefined HKEY_* constants.
920 sub_key: Py_UNICODE
921 A string that must be the name of a subkey of the key identified by
922 the key parameter. This value must not be None, and the key may not
923 have subkeys.
924 /
925
926Deletes the specified key.
927
928This method can not delete keys with subkeys.
929
930If the function succeeds, the entire key, including all of its values,
931is removed. If the function fails, an OSError exception is raised.
932[clinic start generated code]*/
933
Brian Curtin3035c392010-04-21 23:56:21 +0000934static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300935winreg_DeleteKey_impl(PyObject *module, HKEY key, Py_UNICODE *sub_key)
936/*[clinic end generated code: output=7734b1e431991ae4 input=b31d225b935e4211]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000937{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 long rc;
Zachary Warefd2d4822015-05-13 01:21:57 -0500939 rc = RegDeleteKeyW(key, sub_key );
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 if (rc != ERROR_SUCCESS)
941 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey");
Zachary Warefd2d4822015-05-13 01:21:57 -0500942 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000943}
944
Zachary Warefd2d4822015-05-13 01:21:57 -0500945/*[clinic input]
946winreg.DeleteKeyEx
947
948 key: HKEY
949 An already open key, or any one of the predefined HKEY_* constants.
950 sub_key: Py_UNICODE
951 A string that must be the name of a subkey of the key identified by
952 the key parameter. This value must not be None, and the key may not
953 have subkeys.
954 access: REGSAM(c_default='KEY_WOW64_64KEY') = winreg.KEY_WOW64_64KEY
955 An integer that specifies an access mask that describes the
956 desired security access for the key. Default is KEY_WOW64_64KEY.
957 reserved: int = 0
958 A reserved integer, and must be zero. Default is zero.
959
960Deletes the specified key (64-bit OS only).
961
962This method can not delete keys with subkeys.
963
964If the function succeeds, the entire key, including all of its values,
965is removed. If the function fails, an OSError exception is raised.
966On unsupported Windows versions, NotImplementedError is raised.
967[clinic start generated code]*/
968
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000969static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300970winreg_DeleteKeyEx_impl(PyObject *module, HKEY key, Py_UNICODE *sub_key,
Zachary Ware77772c02015-05-13 10:58:35 -0500971 REGSAM access, int reserved)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300972/*[clinic end generated code: output=01378d86ad3eb936 input=711d9d89e7ecbed7]*/
Brian Curtin3035c392010-04-21 23:56:21 +0000973{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 HMODULE hMod;
975 typedef LONG (WINAPI *RDKEFunc)(HKEY, const wchar_t*, REGSAM, int);
976 RDKEFunc pfn = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 long rc;
Brian Curtin3035c392010-04-21 23:56:21 +0000978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 /* Only available on 64bit platforms, so we must load it
980 dynamically. */
Martin v. Löwis50590f12012-01-14 17:54:09 +0100981 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 if (hMod)
983 pfn = (RDKEFunc)GetProcAddress(hMod,
984 "RegDeleteKeyExW");
985 if (!pfn) {
986 PyErr_SetString(PyExc_NotImplementedError,
987 "not implemented on this platform");
988 return NULL;
989 }
990 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -0500991 rc = (*pfn)(key, sub_key, access, reserved);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 Py_END_ALLOW_THREADS
Brian Curtin3035c392010-04-21 23:56:21 +0000993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 if (rc != ERROR_SUCCESS)
995 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKeyEx");
Zachary Warefd2d4822015-05-13 01:21:57 -0500996 Py_RETURN_NONE;
Brian Curtin3035c392010-04-21 23:56:21 +0000997}
998
Zachary Warefd2d4822015-05-13 01:21:57 -0500999/*[clinic input]
1000winreg.DeleteValue
1001
1002 key: HKEY
1003 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001004 value: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001005 A string that identifies the value to remove.
1006 /
1007
1008Removes a named value from a registry key.
1009[clinic start generated code]*/
1010
Brian Curtin3035c392010-04-21 23:56:21 +00001011static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001012winreg_DeleteValue_impl(PyObject *module, HKEY key, Py_UNICODE *value)
1013/*[clinic end generated code: output=67e7e9a514f84951 input=a78d3407a4197b21]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001014{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 long rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001017 rc = RegDeleteValueW(key, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 Py_END_ALLOW_THREADS
1019 if (rc !=ERROR_SUCCESS)
1020 return PyErr_SetFromWindowsErrWithFunction(rc,
1021 "RegDeleteValue");
Zachary Warefd2d4822015-05-13 01:21:57 -05001022 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001023}
1024
Zachary Warefd2d4822015-05-13 01:21:57 -05001025/*[clinic input]
1026winreg.EnumKey
1027
1028 key: HKEY
1029 An already open key, or any one of the predefined HKEY_* constants.
1030 index: int
1031 An integer that identifies the index of the key to retrieve.
1032 /
1033
1034Enumerates subkeys of an open registry key.
1035
1036The function retrieves the name of one subkey each time it is called.
1037It is typically called repeatedly until an OSError exception is
1038raised, indicating no more values are available.
1039[clinic start generated code]*/
1040
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001041static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001042winreg_EnumKey_impl(PyObject *module, HKEY key, int index)
1043/*[clinic end generated code: output=25a6ec52cd147bc4 input=fad9a7c00ab0e04b]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001044{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 long rc;
1046 PyObject *retStr;
Brian Curtin60853212010-05-26 17:43:50 +00001047
1048 /* The Windows docs claim that the max key name length is 255
1049 * characters, plus a terminating nul character. However,
1050 * empirical testing demonstrates that it is possible to
1051 * create a 256 character key that is missing the terminating
1052 * nul. RegEnumKeyEx requires a 257 character buffer to
1053 * retrieve such a key name. */
1054 wchar_t tmpbuf[257];
Kristján Valur Jónsson984dfa72012-04-02 15:23:29 +00001055 DWORD len = sizeof(tmpbuf)/sizeof(wchar_t); /* includes NULL terminator */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001058 rc = RegEnumKeyExW(key, index, tmpbuf, &len, NULL, NULL, NULL, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 Py_END_ALLOW_THREADS
1060 if (rc != ERROR_SUCCESS)
1061 return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx");
Tim Peters313fcd42006-02-19 04:05:39 +00001062
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001063 retStr = PyUnicode_FromWideChar(tmpbuf, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 return retStr; /* can be NULL */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001065}
1066
Zachary Warefd2d4822015-05-13 01:21:57 -05001067/*[clinic input]
1068winreg.EnumValue
1069
1070 key: HKEY
1071 An already open key, or any one of the predefined HKEY_* constants.
1072 index: int
1073 An integer that identifies the index of the value to retrieve.
1074 /
1075
1076Enumerates values of an open registry key.
1077
1078The function retrieves the name of one subkey each time it is called.
1079It is typically called repeatedly, until an OSError exception
1080is raised, indicating no more values.
1081
1082The result is a tuple of 3 items:
1083 value_name
1084 A string that identifies the value.
1085 value_data
1086 An object that holds the value data, and whose type depends
1087 on the underlying registry type.
1088 data_type
1089 An integer that identifies the type of the value data.
1090[clinic start generated code]*/
1091
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001092static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001093winreg_EnumValue_impl(PyObject *module, HKEY key, int index)
1094/*[clinic end generated code: output=d363b5a06f8789ac input=4414f47a6fb238b5]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001095{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 long rc;
1097 wchar_t *retValueBuf;
Amaury Forgeot d'Arcf2b69df2010-08-16 22:11:29 +00001098 BYTE *tmpBuf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 BYTE *retDataBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001100 DWORD retValueSize, bufValueSize;
1101 DWORD retDataSize, bufDataSize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 DWORD typ;
1103 PyObject *obData;
1104 PyObject *retVal;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001105
Zachary Warefd2d4822015-05-13 01:21:57 -05001106 if ((rc = RegQueryInfoKeyW(key, NULL, NULL, NULL, NULL, NULL, NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 NULL,
1108 &retValueSize, &retDataSize, NULL, NULL))
1109 != ERROR_SUCCESS)
1110 return PyErr_SetFromWindowsErrWithFunction(rc,
1111 "RegQueryInfoKey");
1112 ++retValueSize; /* include null terminators */
1113 ++retDataSize;
Brian Curtin60853212010-05-26 17:43:50 +00001114 bufDataSize = retDataSize;
1115 bufValueSize = retValueSize;
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02001116 retValueBuf = PyMem_New(wchar_t, retValueSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 if (retValueBuf == NULL)
1118 return PyErr_NoMemory();
1119 retDataBuf = (BYTE *)PyMem_Malloc(retDataSize);
1120 if (retDataBuf == NULL) {
1121 PyMem_Free(retValueBuf);
1122 return PyErr_NoMemory();
1123 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001124
Brian Curtin60853212010-05-26 17:43:50 +00001125 while (1) {
Brian Curtin60853212010-05-26 17:43:50 +00001126 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001127 rc = RegEnumValueW(key,
Brian Curtin60853212010-05-26 17:43:50 +00001128 index,
1129 retValueBuf,
1130 &retValueSize,
1131 NULL,
1132 &typ,
1133 (BYTE *)retDataBuf,
1134 &retDataSize);
1135 Py_END_ALLOW_THREADS
1136
1137 if (rc != ERROR_MORE_DATA)
1138 break;
1139
1140 bufDataSize *= 2;
Amaury Forgeot d'Arcf2b69df2010-08-16 22:11:29 +00001141 tmpBuf = (BYTE *)PyMem_Realloc(retDataBuf, bufDataSize);
Brian Curtin9b7e2d12010-06-08 20:57:52 +00001142 if (tmpBuf == NULL) {
Brian Curtin60853212010-05-26 17:43:50 +00001143 PyErr_NoMemory();
1144 retVal = NULL;
1145 goto fail;
1146 }
Brian Curtin9b7e2d12010-06-08 20:57:52 +00001147 retDataBuf = tmpBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001148 retDataSize = bufDataSize;
1149 retValueSize = bufValueSize;
1150 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 if (rc != ERROR_SUCCESS) {
1153 retVal = PyErr_SetFromWindowsErrWithFunction(rc,
1154 "PyRegEnumValue");
1155 goto fail;
1156 }
1157 obData = Reg2Py(retDataBuf, retDataSize, typ);
1158 if (obData == NULL) {
1159 retVal = NULL;
1160 goto fail;
1161 }
1162 retVal = Py_BuildValue("uOi", retValueBuf, obData, typ);
1163 Py_DECREF(obData);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001164 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 PyMem_Free(retValueBuf);
1166 PyMem_Free(retDataBuf);
1167 return retVal;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001168}
1169
Zachary Warefd2d4822015-05-13 01:21:57 -05001170/*[clinic input]
1171winreg.ExpandEnvironmentStrings
1172
1173 string: Py_UNICODE
1174 /
1175
1176Expand environment vars.
1177[clinic start generated code]*/
1178
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001179static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001180winreg_ExpandEnvironmentStrings_impl(PyObject *module, Py_UNICODE *string)
1181/*[clinic end generated code: output=cba46ac293a8af1a input=b2a9714d2b751aa6]*/
Christian Heimes2380ac72008-01-09 00:17:24 +00001182{
Victor Stinner9d3b93b2011-11-22 02:27:30 +01001183 wchar_t *retValue = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 DWORD retValueSize;
1185 DWORD rc;
1186 PyObject *o;
Christian Heimes2380ac72008-01-09 00:17:24 +00001187
Zachary Warefd2d4822015-05-13 01:21:57 -05001188 retValueSize = ExpandEnvironmentStringsW(string, retValue, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 if (retValueSize == 0) {
1190 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1191 "ExpandEnvironmentStrings");
1192 }
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02001193 retValue = PyMem_New(wchar_t, retValueSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 if (retValue == NULL) {
1195 return PyErr_NoMemory();
1196 }
Christian Heimes2380ac72008-01-09 00:17:24 +00001197
Zachary Warefd2d4822015-05-13 01:21:57 -05001198 rc = ExpandEnvironmentStringsW(string, retValue, retValueSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 if (rc == 0) {
1200 PyMem_Free(retValue);
1201 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1202 "ExpandEnvironmentStrings");
1203 }
Victor Stinner9d3b93b2011-11-22 02:27:30 +01001204 o = PyUnicode_FromWideChar(retValue, wcslen(retValue));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 PyMem_Free(retValue);
1206 return o;
Christian Heimes2380ac72008-01-09 00:17:24 +00001207}
1208
Zachary Warefd2d4822015-05-13 01:21:57 -05001209/*[clinic input]
1210winreg.FlushKey
1211
1212 key: HKEY
1213 An already open key, or any one of the predefined HKEY_* constants.
1214 /
1215
1216Writes all the attributes of a key to the registry.
1217
1218It is not necessary to call FlushKey to change a key. Registry changes
1219are flushed to disk by the registry using its lazy flusher. Registry
1220changes are also flushed to disk at system shutdown. Unlike
1221CloseKey(), the FlushKey() method returns only when all the data has
1222been written to the registry.
1223
1224An application should only call FlushKey() if it requires absolute
1225certainty that registry changes are on disk. If you don't know whether
1226a FlushKey() call is required, it probably isn't.
1227[clinic start generated code]*/
1228
Christian Heimes2380ac72008-01-09 00:17:24 +00001229static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001230winreg_FlushKey_impl(PyObject *module, HKEY key)
1231/*[clinic end generated code: output=e6fc230d4c5dc049 input=f57457c12297d82f]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001232{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 long rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001235 rc = RegFlushKey(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 Py_END_ALLOW_THREADS
1237 if (rc != ERROR_SUCCESS)
1238 return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001239 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001240}
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001241
Zachary Warefd2d4822015-05-13 01:21:57 -05001242
1243/*[clinic input]
1244winreg.LoadKey
1245
1246 key: HKEY
1247 An already open key, or any one of the predefined HKEY_* constants.
1248 sub_key: Py_UNICODE
1249 A string that identifies the sub-key to load.
1250 file_name: Py_UNICODE
1251 The name of the file to load registry data from. This file must
1252 have been created with the SaveKey() function. Under the file
1253 allocation table (FAT) file system, the filename may not have an
1254 extension.
1255 /
1256
1257Insert data into the registry from a file.
1258
1259Creates a subkey under the specified key and stores registration
1260information from a specified file into that subkey.
1261
1262A call to LoadKey() fails if the calling process does not have the
1263SE_RESTORE_PRIVILEGE privilege.
1264
1265If key is a handle returned by ConnectRegistry(), then the path
1266specified in fileName is relative to the remote computer.
1267
1268The MSDN docs imply key must be in the HKEY_USER or HKEY_LOCAL_MACHINE
1269tree.
1270[clinic start generated code]*/
1271
1272static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001273winreg_LoadKey_impl(PyObject *module, HKEY key, Py_UNICODE *sub_key,
Zachary Ware77772c02015-05-13 10:58:35 -05001274 Py_UNICODE *file_name)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001275/*[clinic end generated code: output=87344005c5905cde input=e3b5b45ade311582]*/
Zachary Warefd2d4822015-05-13 01:21:57 -05001276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 long rc;
Zachary Warefd2d4822015-05-13 01:21:57 -05001278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001280 rc = RegLoadKeyW(key, sub_key, file_name );
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 Py_END_ALLOW_THREADS
1282 if (rc != ERROR_SUCCESS)
1283 return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001284 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001285}
1286
Zachary Warefd2d4822015-05-13 01:21:57 -05001287/*[clinic input]
1288winreg.OpenKey -> HKEY
1289
1290 key: HKEY
1291 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001292 sub_key: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001293 A string that identifies the sub_key to open.
1294 reserved: int = 0
1295 A reserved integer that must be zero. Default is zero.
1296 access: REGSAM(c_default='KEY_READ') = winreg.KEY_READ
1297 An integer that specifies an access mask that describes the desired
1298 security access for the key. Default is KEY_READ.
1299
1300Opens the specified key.
1301
1302The result is a new handle to the specified key.
1303If the function fails, an OSError exception is raised.
1304[clinic start generated code]*/
1305
1306static HKEY
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001307winreg_OpenKey_impl(PyObject *module, HKEY key, Py_UNICODE *sub_key,
Zachary Ware77772c02015-05-13 10:58:35 -05001308 int reserved, REGSAM access)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001309/*[clinic end generated code: output=a905f1b947f3ce85 input=098505ac36a9ae28]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 HKEY retKey;
1312 long rc;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001315 rc = RegOpenKeyExW(key, sub_key, reserved, access, &retKey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 Py_END_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001317 if (rc != ERROR_SUCCESS) {
1318 PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx");
1319 return NULL;
1320 }
1321 return retKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001322}
1323
Zachary Warefd2d4822015-05-13 01:21:57 -05001324/*[clinic input]
1325winreg.OpenKeyEx = winreg.OpenKey
1326
1327Opens the specified key.
1328
1329The result is a new handle to the specified key.
1330If the function fails, an OSError exception is raised.
1331[clinic start generated code]*/
1332
1333static HKEY
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001334winreg_OpenKeyEx_impl(PyObject *module, HKEY key, Py_UNICODE *sub_key,
Zachary Ware77772c02015-05-13 10:58:35 -05001335 int reserved, REGSAM access)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001336/*[clinic end generated code: output=226042593b37e940 input=c6c4972af8622959]*/
Zachary Warefd2d4822015-05-13 01:21:57 -05001337{
1338 return winreg_OpenKey_impl(module, key, sub_key, reserved, access);
1339}
1340
1341/*[clinic input]
1342winreg.QueryInfoKey
1343
1344 key: HKEY
1345 An already open key, or any one of the predefined HKEY_* constants.
1346 /
1347
1348Returns information about a key.
1349
1350The result is a tuple of 3 items:
1351An integer that identifies the number of sub keys this key has.
1352An integer that identifies the number of values this key has.
1353An integer that identifies when the key was last modified (if available)
1354as 100's of nanoseconds since Jan 1, 1600.
1355[clinic start generated code]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001356
1357static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001358winreg_QueryInfoKey_impl(PyObject *module, HKEY key)
1359/*[clinic end generated code: output=dc657b8356a4f438 input=c3593802390cde1f]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001360{
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001361 long rc;
1362 DWORD nSubKeys, nValues;
1363 FILETIME ft;
1364 LARGE_INTEGER li;
1365 PyObject *l;
1366 PyObject *ret;
Zachary Warefd2d4822015-05-13 01:21:57 -05001367
1368 if ((rc = RegQueryInfoKey(key, NULL, NULL, 0, &nSubKeys, NULL, NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 &nValues, NULL, NULL, NULL, &ft))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001370 != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey");
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001372 li.LowPart = ft.dwLowDateTime;
1373 li.HighPart = ft.dwHighDateTime;
1374 l = PyLong_FromLongLong(li.QuadPart);
1375 if (l == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001377 ret = Py_BuildValue("iiO", nSubKeys, nValues, l);
1378 Py_DECREF(l);
1379 return ret;
1380}
1381
Zachary Warefd2d4822015-05-13 01:21:57 -05001382/*[clinic input]
1383winreg.QueryValue
1384
1385 key: HKEY
1386 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001387 sub_key: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001388 A string that holds the name of the subkey with which the value
1389 is associated. If this parameter is None or empty, the function
1390 retrieves the value set by the SetValue() method for the key
1391 identified by key.
1392 /
1393
1394Retrieves the unnamed value for a key.
1395
1396Values in the registry have name, type, and data components. This method
1397retrieves the data for a key's first value that has a NULL name.
1398But since the underlying API call doesn't return the type, you'll
1399probably be happier using QueryValueEx; this function is just here for
1400completeness.
1401[clinic start generated code]*/
1402
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001403static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001404winreg_QueryValue_impl(PyObject *module, HKEY key, Py_UNICODE *sub_key)
1405/*[clinic end generated code: output=2bb8d1e02c10d0b6 input=41cafbbf423b21d6]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001406{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 long rc;
1408 PyObject *retStr;
1409 wchar_t *retBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001410 DWORD bufSize = 0;
1411 DWORD retSize = 0;
1412 wchar_t *tmp;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001413
Zachary Warefd2d4822015-05-13 01:21:57 -05001414 rc = RegQueryValueW(key, sub_key, NULL, &retSize);
Brian Curtin60853212010-05-26 17:43:50 +00001415 if (rc == ERROR_MORE_DATA)
1416 retSize = 256;
1417 else if (rc != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 return PyErr_SetFromWindowsErrWithFunction(rc,
1419 "RegQueryValue");
Brian Curtin60853212010-05-26 17:43:50 +00001420
1421 bufSize = retSize;
1422 retBuf = (wchar_t *) PyMem_Malloc(bufSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 if (retBuf == NULL)
1424 return PyErr_NoMemory();
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001425
Brian Curtin60853212010-05-26 17:43:50 +00001426 while (1) {
1427 retSize = bufSize;
Zachary Warefd2d4822015-05-13 01:21:57 -05001428 rc = RegQueryValueW(key, sub_key, retBuf, &retSize);
Brian Curtin60853212010-05-26 17:43:50 +00001429 if (rc != ERROR_MORE_DATA)
1430 break;
1431
1432 bufSize *= 2;
1433 tmp = (wchar_t *) PyMem_Realloc(retBuf, bufSize);
1434 if (tmp == NULL) {
1435 PyMem_Free(retBuf);
1436 return PyErr_NoMemory();
1437 }
1438 retBuf = tmp;
1439 }
1440
1441 if (rc != ERROR_SUCCESS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 PyMem_Free(retBuf);
1443 return PyErr_SetFromWindowsErrWithFunction(rc,
1444 "RegQueryValue");
1445 }
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001446
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001447 retStr = PyUnicode_FromWideChar(retBuf, wcslen(retBuf));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 PyMem_Free(retBuf);
1449 return retStr;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001450}
1451
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001452
Zachary Warefd2d4822015-05-13 01:21:57 -05001453/*[clinic input]
1454winreg.QueryValueEx
1455
1456 key: HKEY
1457 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001458 name: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001459 A string indicating the value to query.
1460 /
1461
1462Retrieves the type and value of a specified sub-key.
1463
1464Behaves mostly like QueryValue(), but also returns the type of the
1465specified value name associated with the given open registry key.
1466
1467The return value is a tuple of the value and the type_id.
1468[clinic start generated code]*/
1469
1470static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001471winreg_QueryValueEx_impl(PyObject *module, HKEY key, Py_UNICODE *name)
1472/*[clinic end generated code: output=5b4fa3e33d6d3e8f input=cf366cada4836891]*/
Zachary Warefd2d4822015-05-13 01:21:57 -05001473{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 long rc;
Brian Curtin60853212010-05-26 17:43:50 +00001475 BYTE *retBuf, *tmp;
1476 DWORD bufSize = 0, retSize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 DWORD typ;
1478 PyObject *obData;
1479 PyObject *result;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001480
Zachary Warefd2d4822015-05-13 01:21:57 -05001481 rc = RegQueryValueExW(key, name, NULL, NULL, NULL, &bufSize);
Brian Curtin60853212010-05-26 17:43:50 +00001482 if (rc == ERROR_MORE_DATA)
1483 bufSize = 256;
1484 else if (rc != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 return PyErr_SetFromWindowsErrWithFunction(rc,
1486 "RegQueryValueEx");
1487 retBuf = (BYTE *)PyMem_Malloc(bufSize);
1488 if (retBuf == NULL)
1489 return PyErr_NoMemory();
Brian Curtin60853212010-05-26 17:43:50 +00001490
1491 while (1) {
1492 retSize = bufSize;
Zachary Warefd2d4822015-05-13 01:21:57 -05001493 rc = RegQueryValueExW(key, name, NULL, &typ,
Brian Curtin60853212010-05-26 17:43:50 +00001494 (BYTE *)retBuf, &retSize);
1495 if (rc != ERROR_MORE_DATA)
1496 break;
1497
1498 bufSize *= 2;
1499 tmp = (char *) PyMem_Realloc(retBuf, bufSize);
1500 if (tmp == NULL) {
1501 PyMem_Free(retBuf);
1502 return PyErr_NoMemory();
1503 }
1504 retBuf = tmp;
1505 }
1506
1507 if (rc != ERROR_SUCCESS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508 PyMem_Free(retBuf);
1509 return PyErr_SetFromWindowsErrWithFunction(rc,
1510 "RegQueryValueEx");
1511 }
1512 obData = Reg2Py(retBuf, bufSize, typ);
1513 PyMem_Free(retBuf);
1514 if (obData == NULL)
1515 return NULL;
1516 result = Py_BuildValue("Oi", obData, typ);
1517 Py_DECREF(obData);
1518 return result;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001519}
1520
Zachary Warefd2d4822015-05-13 01:21:57 -05001521/*[clinic input]
1522winreg.SaveKey
1523
1524 key: HKEY
1525 An already open key, or any one of the predefined HKEY_* constants.
1526 file_name: Py_UNICODE
1527 The name of the file to save registry data to. This file cannot
1528 already exist. If this filename includes an extension, it cannot be
1529 used on file allocation table (FAT) file systems by the LoadKey(),
1530 ReplaceKey() or RestoreKey() methods.
1531 /
1532
1533Saves the specified key, and all its subkeys to the specified file.
1534
1535If key represents a key on a remote computer, the path described by
1536file_name is relative to the remote computer.
1537
1538The caller of this method must possess the SeBackupPrivilege
1539security privilege. This function passes NULL for security_attributes
1540to the API.
1541[clinic start generated code]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001542
1543static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001544winreg_SaveKey_impl(PyObject *module, HKEY key, Py_UNICODE *file_name)
1545/*[clinic end generated code: output=1dda1502bd4c30d8 input=da735241f91ac7a2]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001546{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 LPSECURITY_ATTRIBUTES pSA = NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 long rc;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001550/* One day we may get security into the core?
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE))
1552 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001553*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001555 rc = RegSaveKeyW(key, file_name, pSA );
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 Py_END_ALLOW_THREADS
1557 if (rc != ERROR_SUCCESS)
1558 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001559 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001560}
1561
Zachary Warefd2d4822015-05-13 01:21:57 -05001562/*[clinic input]
1563winreg.SetValue
1564
1565 key: HKEY
1566 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001567 sub_key: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001568 A string that names the subkey with which the value is associated.
1569 type: DWORD
1570 An integer that specifies the type of the data. Currently this must
1571 be REG_SZ, meaning only strings are supported.
Zachary Ware77772c02015-05-13 10:58:35 -05001572 value: Py_UNICODE(zeroes=True)
Zachary Warefd2d4822015-05-13 01:21:57 -05001573 A string that specifies the new value.
1574 /
1575
1576Associates a value with a specified key.
1577
1578If the key specified by the sub_key parameter does not exist, the
1579SetValue function creates it.
1580
1581Value lengths are limited by available memory. Long values (more than
15822048 bytes) should be stored as files with the filenames stored in
1583the configuration registry to help the registry perform efficiently.
1584
1585The key identified by the key parameter must have been opened with
1586KEY_SET_VALUE access.
1587[clinic start generated code]*/
1588
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001589static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001590winreg_SetValue_impl(PyObject *module, HKEY key, Py_UNICODE *sub_key,
Zachary Ware77772c02015-05-13 10:58:35 -05001591 DWORD type, Py_UNICODE *value,
1592 Py_ssize_clean_t value_length)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001593/*[clinic end generated code: output=1e31931174820631 input=2cd2adab79339c53]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001594{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 long rc;
Zachary Warefd2d4822015-05-13 01:21:57 -05001596
1597 if (type != REG_SZ) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 PyErr_SetString(PyExc_TypeError,
1599 "Type must be winreg.REG_SZ");
1600 return NULL;
1601 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001604 rc = RegSetValueW(key, sub_key, REG_SZ, value, value_length+1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 Py_END_ALLOW_THREADS
1606 if (rc != ERROR_SUCCESS)
1607 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue");
Zachary Warefd2d4822015-05-13 01:21:57 -05001608 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001609}
1610
Zachary Warefd2d4822015-05-13 01:21:57 -05001611/*[clinic input]
1612winreg.SetValueEx
1613
1614 key: HKEY
1615 An already open key, or any one of the predefined HKEY_* constants.
Zachary Ware77772c02015-05-13 10:58:35 -05001616 value_name: Py_UNICODE(accept={str, NoneType})
Zachary Warefd2d4822015-05-13 01:21:57 -05001617 A string containing the name of the value to set, or None.
1618 reserved: object
1619 Can be anything - zero is always passed to the API.
1620 type: DWORD
1621 An integer that specifies the type of the data, one of:
1622 REG_BINARY -- Binary data in any form.
1623 REG_DWORD -- A 32-bit number.
Steve Dower80ac11d2016-05-24 15:42:04 -07001624 REG_DWORD_LITTLE_ENDIAN -- A 32-bit number in little-endian format. Equivalent to REG_DWORD
Zachary Warefd2d4822015-05-13 01:21:57 -05001625 REG_DWORD_BIG_ENDIAN -- A 32-bit number in big-endian format.
1626 REG_EXPAND_SZ -- A null-terminated string that contains unexpanded
1627 references to environment variables (for example,
1628 %PATH%).
1629 REG_LINK -- A Unicode symbolic link.
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03001630 REG_MULTI_SZ -- A sequence of null-terminated strings, terminated
Zachary Warefd2d4822015-05-13 01:21:57 -05001631 by two null characters. Note that Python handles
1632 this termination automatically.
1633 REG_NONE -- No defined value type.
Steve Dower80ac11d2016-05-24 15:42:04 -07001634 REG_QWORD -- A 64-bit number.
1635 REG_QWORD_LITTLE_ENDIAN -- A 64-bit number in little-endian format. Equivalent to REG_QWORD.
Zachary Warefd2d4822015-05-13 01:21:57 -05001636 REG_RESOURCE_LIST -- A device-driver resource list.
1637 REG_SZ -- A null-terminated string.
1638 value: object
1639 A string that specifies the new value.
1640 /
1641
1642Stores data in the value field of an open registry key.
1643
1644This method can also set additional value and type information for the
1645specified key. The key identified by the key parameter must have been
1646opened with KEY_SET_VALUE access.
1647
1648To open the key, use the CreateKeyEx() or OpenKeyEx() methods.
1649
1650Value lengths are limited by available memory. Long values (more than
16512048 bytes) should be stored as files with the filenames stored in
1652the configuration registry to help the registry perform efficiently.
1653[clinic start generated code]*/
1654
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001655static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001656winreg_SetValueEx_impl(PyObject *module, HKEY key, Py_UNICODE *value_name,
Zachary Ware77772c02015-05-13 10:58:35 -05001657 PyObject *reserved, DWORD type, PyObject *value)
Serhiy Storchaka2954f832016-07-07 18:20:03 +03001658/*[clinic end generated code: output=c88c8426b6c00ec7 input=900a9e3990bfb196]*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001659{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 BYTE *data;
1661 DWORD len;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 LONG rc;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001664
Zachary Warefd2d4822015-05-13 01:21:57 -05001665 if (!Py2Reg(value, type, &data, &len))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 {
1667 if (!PyErr_Occurred())
1668 PyErr_SetString(PyExc_ValueError,
1669 "Could not convert the data to the specified type.");
1670 return NULL;
1671 }
1672 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001673 rc = RegSetValueExW(key, value_name, 0, type, data, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 Py_END_ALLOW_THREADS
1675 PyMem_DEL(data);
1676 if (rc != ERROR_SUCCESS)
1677 return PyErr_SetFromWindowsErrWithFunction(rc,
1678 "RegSetValueEx");
Zachary Warefd2d4822015-05-13 01:21:57 -05001679 Py_RETURN_NONE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001680}
1681
Zachary Warefd2d4822015-05-13 01:21:57 -05001682/*[clinic input]
1683winreg.DisableReflectionKey
1684
1685 key: HKEY
1686 An already open key, or any one of the predefined HKEY_* constants.
1687 /
1688
1689Disables registry reflection for 32bit processes running on a 64bit OS.
1690
1691Will generally raise NotImplemented if executed on a 32bit OS.
1692
1693If the key is not on the reflection list, the function succeeds but has
1694no effect. Disabling reflection for a key does not affect reflection
1695of any subkeys.
1696[clinic start generated code]*/
1697
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001698static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001699winreg_DisableReflectionKey_impl(PyObject *module, HKEY key)
1700/*[clinic end generated code: output=830cce504cc764b4 input=a6c9e5ca5410193c]*/
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001701{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 HMODULE hMod;
1703 typedef LONG (WINAPI *RDRKFunc)(HKEY);
1704 RDRKFunc pfn = NULL;
1705 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 /* Only available on 64bit platforms, so we must load it
1708 dynamically.*/
Martin v. Löwis50590f12012-01-14 17:54:09 +01001709 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 if (hMod)
1711 pfn = (RDRKFunc)GetProcAddress(hMod,
1712 "RegDisableReflectionKey");
1713 if (!pfn) {
1714 PyErr_SetString(PyExc_NotImplementedError,
1715 "not implemented on this platform");
1716 return NULL;
1717 }
1718 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001719 rc = (*pfn)(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 Py_END_ALLOW_THREADS
1721 if (rc != ERROR_SUCCESS)
1722 return PyErr_SetFromWindowsErrWithFunction(rc,
1723 "RegDisableReflectionKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001724 Py_RETURN_NONE;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001725}
1726
Zachary Warefd2d4822015-05-13 01:21:57 -05001727/*[clinic input]
1728winreg.EnableReflectionKey
1729
1730 key: HKEY
1731 An already open key, or any one of the predefined HKEY_* constants.
1732 /
1733
1734Restores registry reflection for the specified disabled key.
1735
1736Will generally raise NotImplemented if executed on a 32bit OS.
1737Restoring reflection for a key does not affect reflection of any
1738subkeys.
1739[clinic start generated code]*/
1740
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001741static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001742winreg_EnableReflectionKey_impl(PyObject *module, HKEY key)
1743/*[clinic end generated code: output=86fa1385fdd9ce57 input=7748abbacd1e166a]*/
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 HMODULE hMod;
1746 typedef LONG (WINAPI *RERKFunc)(HKEY);
1747 RERKFunc pfn = NULL;
1748 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 /* Only available on 64bit platforms, so we must load it
1751 dynamically.*/
Martin v. Löwis50590f12012-01-14 17:54:09 +01001752 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 if (hMod)
1754 pfn = (RERKFunc)GetProcAddress(hMod,
1755 "RegEnableReflectionKey");
1756 if (!pfn) {
1757 PyErr_SetString(PyExc_NotImplementedError,
1758 "not implemented on this platform");
1759 return NULL;
1760 }
1761 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001762 rc = (*pfn)(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 Py_END_ALLOW_THREADS
1764 if (rc != ERROR_SUCCESS)
1765 return PyErr_SetFromWindowsErrWithFunction(rc,
1766 "RegEnableReflectionKey");
Zachary Warefd2d4822015-05-13 01:21:57 -05001767 Py_RETURN_NONE;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001768}
1769
Zachary Warefd2d4822015-05-13 01:21:57 -05001770/*[clinic input]
1771winreg.QueryReflectionKey
1772
1773 key: HKEY
1774 An already open key, or any one of the predefined HKEY_* constants.
1775 /
1776
1777Returns the reflection state for the specified key as a bool.
1778
1779Will generally raise NotImplemented if executed on a 32bit OS.
1780[clinic start generated code]*/
1781
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001782static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001783winreg_QueryReflectionKey_impl(PyObject *module, HKEY key)
1784/*[clinic end generated code: output=4e774af288c3ebb9 input=9f325eacb5a65d88]*/
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001785{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 HMODULE hMod;
1787 typedef LONG (WINAPI *RQRKFunc)(HKEY, BOOL *);
1788 RQRKFunc pfn = NULL;
1789 BOOL result;
1790 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 /* Only available on 64bit platforms, so we must load it
1793 dynamically.*/
Martin v. Löwis50590f12012-01-14 17:54:09 +01001794 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 if (hMod)
1796 pfn = (RQRKFunc)GetProcAddress(hMod,
1797 "RegQueryReflectionKey");
1798 if (!pfn) {
1799 PyErr_SetString(PyExc_NotImplementedError,
1800 "not implemented on this platform");
1801 return NULL;
1802 }
1803 Py_BEGIN_ALLOW_THREADS
Zachary Warefd2d4822015-05-13 01:21:57 -05001804 rc = (*pfn)(key, &result);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 Py_END_ALLOW_THREADS
1806 if (rc != ERROR_SUCCESS)
1807 return PyErr_SetFromWindowsErrWithFunction(rc,
1808 "RegQueryReflectionKey");
1809 return PyBool_FromLong(result);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001810}
1811
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001812static struct PyMethodDef winreg_methods[] = {
Zachary Warefd2d4822015-05-13 01:21:57 -05001813 WINREG_CLOSEKEY_METHODDEF
1814 WINREG_CONNECTREGISTRY_METHODDEF
1815 WINREG_CREATEKEY_METHODDEF
1816 WINREG_CREATEKEYEX_METHODDEF
1817 WINREG_DELETEKEY_METHODDEF
1818 WINREG_DELETEKEYEX_METHODDEF
1819 WINREG_DELETEVALUE_METHODDEF
1820 WINREG_DISABLEREFLECTIONKEY_METHODDEF
1821 WINREG_ENABLEREFLECTIONKEY_METHODDEF
1822 WINREG_ENUMKEY_METHODDEF
1823 WINREG_ENUMVALUE_METHODDEF
1824 WINREG_EXPANDENVIRONMENTSTRINGS_METHODDEF
1825 WINREG_FLUSHKEY_METHODDEF
1826 WINREG_LOADKEY_METHODDEF
1827 WINREG_OPENKEY_METHODDEF
1828 WINREG_OPENKEYEX_METHODDEF
1829 WINREG_QUERYVALUE_METHODDEF
1830 WINREG_QUERYVALUEEX_METHODDEF
1831 WINREG_QUERYINFOKEY_METHODDEF
1832 WINREG_QUERYREFLECTIONKEY_METHODDEF
1833 WINREG_SAVEKEY_METHODDEF
1834 WINREG_SETVALUE_METHODDEF
1835 WINREG_SETVALUEEX_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 NULL,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001837};
1838
1839static void
1840insint(PyObject * d, char * name, long value)
1841{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001842 PyObject *v = PyLong_FromLong(value);
1843 if (!v || PyDict_SetItemString(d, name, v))
1844 PyErr_Clear();
1845 Py_XDECREF(v);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001846}
1847
1848#define ADD_INT(val) insint(d, #val, val)
1849
1850static void
1851inskey(PyObject * d, char * name, HKEY key)
1852{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 PyObject *v = PyLong_FromVoidPtr(key);
1854 if (!v || PyDict_SetItemString(d, name, v))
1855 PyErr_Clear();
1856 Py_XDECREF(v);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001857}
1858
1859#define ADD_KEY(val) inskey(d, #val, val)
1860
Martin v. Löwis1a214512008-06-11 05:26:20 +00001861
1862static struct PyModuleDef winregmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 PyModuleDef_HEAD_INIT,
1864 "winreg",
1865 module_doc,
1866 -1,
1867 winreg_methods,
1868 NULL,
1869 NULL,
1870 NULL,
1871 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001872};
1873
1874PyMODINIT_FUNC PyInit_winreg(void)
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001875{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 PyObject *m, *d;
1877 m = PyModule_Create(&winregmodule);
1878 if (m == NULL)
1879 return NULL;
1880 d = PyModule_GetDict(m);
1881 PyHKEY_Type.tp_doc = PyHKEY_doc;
1882 if (PyType_Ready(&PyHKEY_Type) < 0)
1883 return NULL;
1884 Py_INCREF(&PyHKEY_Type);
1885 if (PyDict_SetItemString(d, "HKEYType",
1886 (PyObject *)&PyHKEY_Type) != 0)
1887 return NULL;
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001888 Py_INCREF(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 if (PyDict_SetItemString(d, "error",
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001890 PyExc_OSError) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 /* Add the relevant constants */
1894 ADD_KEY(HKEY_CLASSES_ROOT);
1895 ADD_KEY(HKEY_CURRENT_USER);
1896 ADD_KEY(HKEY_LOCAL_MACHINE);
1897 ADD_KEY(HKEY_USERS);
1898 ADD_KEY(HKEY_PERFORMANCE_DATA);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001899#ifdef HKEY_CURRENT_CONFIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 ADD_KEY(HKEY_CURRENT_CONFIG);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001901#endif
1902#ifdef HKEY_DYN_DATA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 ADD_KEY(HKEY_DYN_DATA);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001904#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 ADD_INT(KEY_QUERY_VALUE);
1906 ADD_INT(KEY_SET_VALUE);
1907 ADD_INT(KEY_CREATE_SUB_KEY);
1908 ADD_INT(KEY_ENUMERATE_SUB_KEYS);
1909 ADD_INT(KEY_NOTIFY);
1910 ADD_INT(KEY_CREATE_LINK);
1911 ADD_INT(KEY_READ);
1912 ADD_INT(KEY_WRITE);
1913 ADD_INT(KEY_EXECUTE);
1914 ADD_INT(KEY_ALL_ACCESS);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001915#ifdef KEY_WOW64_64KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 ADD_INT(KEY_WOW64_64KEY);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001917#endif
1918#ifdef KEY_WOW64_32KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 ADD_INT(KEY_WOW64_32KEY);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001920#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 ADD_INT(REG_OPTION_RESERVED);
1922 ADD_INT(REG_OPTION_NON_VOLATILE);
1923 ADD_INT(REG_OPTION_VOLATILE);
1924 ADD_INT(REG_OPTION_CREATE_LINK);
1925 ADD_INT(REG_OPTION_BACKUP_RESTORE);
1926 ADD_INT(REG_OPTION_OPEN_LINK);
1927 ADD_INT(REG_LEGAL_OPTION);
1928 ADD_INT(REG_CREATED_NEW_KEY);
1929 ADD_INT(REG_OPENED_EXISTING_KEY);
1930 ADD_INT(REG_WHOLE_HIVE_VOLATILE);
1931 ADD_INT(REG_REFRESH_HIVE);
1932 ADD_INT(REG_NO_LAZY_FLUSH);
1933 ADD_INT(REG_NOTIFY_CHANGE_NAME);
1934 ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES);
1935 ADD_INT(REG_NOTIFY_CHANGE_LAST_SET);
1936 ADD_INT(REG_NOTIFY_CHANGE_SECURITY);
1937 ADD_INT(REG_LEGAL_CHANGE_FILTER);
1938 ADD_INT(REG_NONE);
1939 ADD_INT(REG_SZ);
1940 ADD_INT(REG_EXPAND_SZ);
1941 ADD_INT(REG_BINARY);
1942 ADD_INT(REG_DWORD);
1943 ADD_INT(REG_DWORD_LITTLE_ENDIAN);
1944 ADD_INT(REG_DWORD_BIG_ENDIAN);
Steve Dower80ac11d2016-05-24 15:42:04 -07001945 ADD_INT(REG_QWORD);
1946 ADD_INT(REG_QWORD_LITTLE_ENDIAN);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 ADD_INT(REG_LINK);
1948 ADD_INT(REG_MULTI_SZ);
1949 ADD_INT(REG_RESOURCE_LIST);
1950 ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
1951 ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
1952 return m;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001953}
1954
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001955