Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1 | /* |
Georg Brandl | 38feaf0 | 2008-05-25 07:45:51 +0000 | [diff] [blame] | 2 | winreg.c |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 3 | |
| 4 | Windows Registry access module for Python. |
| 5 | |
| 6 | * Simple registry access written by Mark Hammond in win32api |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7 | module circa 1995. |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 8 | * Bill Tutt expanded the support significantly not long after. |
| 9 | * Numerous other people have submitted patches since then. |
| 10 | * Ripped from win32api module 03-Feb-2000 by Mark Hammond, and |
| 11 | basic Unicode support added. |
| 12 | |
| 13 | */ |
| 14 | |
Inada Naoki | d5f18a6 | 2019-03-20 19:10:17 +0900 | [diff] [blame] | 15 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 16 | #include "Python.h" |
| 17 | #include "structmember.h" |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 18 | #include "windows.h" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 19 | |
| 20 | static BOOL PyHKEY_AsHKEY(PyObject *ob, HKEY *pRes, BOOL bNoneOK); |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 21 | static BOOL clinic_HKEY_converter(PyObject *ob, void *p); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 22 | static PyObject *PyHKEY_FromHKEY(HKEY h); |
| 23 | static BOOL PyHKEY_Close(PyObject *obHandle); |
| 24 | |
| 25 | static char errNotAHandle[] = "Object is not a handle"; |
| 26 | |
| 27 | /* The win32api module reports the function name that failed, |
| 28 | but this concept is not in the Python core. |
luzpaz | a5293b4 | 2017-11-05 07:37:50 -0600 | [diff] [blame] | 29 | Hopefully it will one day, and in the meantime I don't |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 30 | want to lose this info... |
| 31 | */ |
| 32 | #define PyErr_SetFromWindowsErrWithFunction(rc, fnname) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 33 | PyErr_SetFromWindowsErr(rc) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 34 | |
| 35 | /* Forward declares */ |
| 36 | |
| 37 | /* Doc strings */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 38 | PyDoc_STRVAR(module_doc, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 39 | "This module provides access to the Windows registry API.\n" |
| 40 | "\n" |
| 41 | "Functions:\n" |
| 42 | "\n" |
| 43 | "CloseKey() - Closes a registry key.\n" |
| 44 | "ConnectRegistry() - Establishes a connection to a predefined registry handle\n" |
| 45 | " on another computer.\n" |
| 46 | "CreateKey() - Creates the specified key, or opens it if it already exists.\n" |
| 47 | "DeleteKey() - Deletes the specified key.\n" |
| 48 | "DeleteValue() - Removes a named value from the specified registry key.\n" |
| 49 | "EnumKey() - Enumerates subkeys of the specified open registry key.\n" |
| 50 | "EnumValue() - Enumerates values of the specified open registry key.\n" |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 51 | "ExpandEnvironmentStrings() - Expand the env strings in a REG_EXPAND_SZ\n" |
| 52 | " string.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 53 | "FlushKey() - Writes all the attributes of the specified key to the registry.\n" |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 54 | "LoadKey() - Creates a subkey under HKEY_USER or HKEY_LOCAL_MACHINE and\n" |
| 55 | " stores registration information from a specified file into that\n" |
| 56 | " subkey.\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 57 | "OpenKey() - Opens the specified key.\n" |
| 58 | "OpenKeyEx() - Alias of OpenKey().\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 59 | "QueryValue() - Retrieves the value associated with the unnamed value for a\n" |
| 60 | " specified key in the registry.\n" |
| 61 | "QueryValueEx() - Retrieves the type and data for a specified value name\n" |
| 62 | " associated with an open registry key.\n" |
| 63 | "QueryInfoKey() - Returns information about the specified key.\n" |
| 64 | "SaveKey() - Saves the specified key, and all its subkeys a file.\n" |
| 65 | "SetValue() - Associates a value with a specified key.\n" |
| 66 | "SetValueEx() - Stores data in the value field of an open registry key.\n" |
| 67 | "\n" |
| 68 | "Special objects:\n" |
| 69 | "\n" |
| 70 | "HKEYType -- type object for HKEY objects\n" |
| 71 | "error -- exception raised for Win32 errors\n" |
| 72 | "\n" |
| 73 | "Integer constants:\n" |
| 74 | "Many constants are defined - see the documentation for each function\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 75 | "to see what constants are used, and where."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 76 | |
| 77 | |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 78 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 79 | /* PyHKEY docstrings */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 80 | PyDoc_STRVAR(PyHKEY_doc, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 81 | "PyHKEY Object - A Python object, representing a win32 registry key.\n" |
| 82 | "\n" |
Mark Hammond | b422f95 | 2000-06-09 06:01:47 +0000 | [diff] [blame] | 83 | "This object wraps a Windows HKEY object, automatically closing it when\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 84 | "the object is destroyed. To guarantee cleanup, you can call either\n" |
| 85 | "the Close() method on the PyHKEY, or the CloseKey() method.\n" |
| 86 | "\n" |
oldk | aa0735f | 2018-02-02 16:52:55 +0800 | [diff] [blame] | 87 | "All functions which accept a handle object also accept an integer --\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 88 | "however, use of the handle object is encouraged.\n" |
| 89 | "\n" |
| 90 | "Functions:\n" |
| 91 | "Close() - Closes the underlying handle.\n" |
| 92 | "Detach() - Returns the integer Win32 handle, detaching it from the object\n" |
| 93 | "\n" |
| 94 | "Properties:\n" |
| 95 | "handle - The integer Win32 handle.\n" |
| 96 | "\n" |
| 97 | "Operations:\n" |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 98 | "__bool__ - Handles with an open object return true, otherwise false.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 99 | "__int__ - Converting a handle to an integer returns the Win32 handle.\n" |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 100 | "rich comparison - Handle objects are compared using the handle value."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 101 | |
| 102 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 103 | |
| 104 | /************************************************************************ |
| 105 | |
| 106 | The PyHKEY object definition |
| 107 | |
| 108 | ************************************************************************/ |
| 109 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 110 | PyObject_VAR_HEAD |
| 111 | HKEY hkey; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 112 | } PyHKEYObject; |
| 113 | |
| 114 | #define PyHKEY_Check(op) ((op)->ob_type == &PyHKEY_Type) |
| 115 | |
| 116 | static char *failMsg = "bad operand type"; |
| 117 | |
| 118 | static PyObject * |
| 119 | PyHKEY_unaryFailureFunc(PyObject *ob) |
| 120 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 121 | PyErr_SetString(PyExc_TypeError, failMsg); |
| 122 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 123 | } |
| 124 | static PyObject * |
| 125 | PyHKEY_binaryFailureFunc(PyObject *ob1, PyObject *ob2) |
| 126 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 127 | PyErr_SetString(PyExc_TypeError, failMsg); |
| 128 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 129 | } |
| 130 | static PyObject * |
| 131 | PyHKEY_ternaryFailureFunc(PyObject *ob1, PyObject *ob2, PyObject *ob3) |
| 132 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 133 | PyErr_SetString(PyExc_TypeError, failMsg); |
| 134 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static void |
| 138 | PyHKEY_deallocFunc(PyObject *ob) |
| 139 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 140 | /* Can not call PyHKEY_Close, as the ob->tp_type |
| 141 | has already been cleared, thus causing the type |
| 142 | check to fail! |
| 143 | */ |
| 144 | PyHKEYObject *obkey = (PyHKEYObject *)ob; |
| 145 | if (obkey->hkey) |
| 146 | RegCloseKey((HKEY)obkey->hkey); |
| 147 | PyObject_DEL(ob); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 151 | PyHKEY_boolFunc(PyObject *ob) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 152 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 153 | return ((PyHKEYObject *)ob)->hkey != 0; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | static PyObject * |
| 157 | PyHKEY_intFunc(PyObject *ob) |
| 158 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 159 | PyHKEYObject *pyhkey = (PyHKEYObject *)ob; |
| 160 | return PyLong_FromVoidPtr(pyhkey->hkey); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 163 | static PyObject * |
| 164 | PyHKEY_strFunc(PyObject *ob) |
| 165 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 166 | PyHKEYObject *pyhkey = (PyHKEYObject *)ob; |
| 167 | return PyUnicode_FromFormat("<PyHKEY:%p>", pyhkey->hkey); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | static int |
| 171 | PyHKEY_compareFunc(PyObject *ob1, PyObject *ob2) |
| 172 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 173 | PyHKEYObject *pyhkey1 = (PyHKEYObject *)ob1; |
| 174 | PyHKEYObject *pyhkey2 = (PyHKEYObject *)ob2; |
| 175 | return pyhkey1 == pyhkey2 ? 0 : |
| 176 | (pyhkey1 < pyhkey2 ? -1 : 1); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Antoine Pitrou | fbb1c61 | 2010-10-23 17:37:54 +0000 | [diff] [blame] | 179 | static Py_hash_t |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 180 | PyHKEY_hashFunc(PyObject *ob) |
| 181 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 182 | /* Just use the address. |
| 183 | XXX - should we use the handle value? |
| 184 | */ |
| 185 | return _Py_HashPointer(ob); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | |
| 189 | static PyNumberMethods PyHKEY_NumberMethods = |
| 190 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 191 | PyHKEY_binaryFailureFunc, /* nb_add */ |
| 192 | PyHKEY_binaryFailureFunc, /* nb_subtract */ |
| 193 | PyHKEY_binaryFailureFunc, /* nb_multiply */ |
| 194 | PyHKEY_binaryFailureFunc, /* nb_remainder */ |
| 195 | PyHKEY_binaryFailureFunc, /* nb_divmod */ |
| 196 | PyHKEY_ternaryFailureFunc, /* nb_power */ |
| 197 | PyHKEY_unaryFailureFunc, /* nb_negative */ |
| 198 | PyHKEY_unaryFailureFunc, /* nb_positive */ |
| 199 | PyHKEY_unaryFailureFunc, /* nb_absolute */ |
| 200 | PyHKEY_boolFunc, /* nb_bool */ |
| 201 | PyHKEY_unaryFailureFunc, /* nb_invert */ |
| 202 | PyHKEY_binaryFailureFunc, /* nb_lshift */ |
| 203 | PyHKEY_binaryFailureFunc, /* nb_rshift */ |
| 204 | PyHKEY_binaryFailureFunc, /* nb_and */ |
| 205 | PyHKEY_binaryFailureFunc, /* nb_xor */ |
| 206 | PyHKEY_binaryFailureFunc, /* nb_or */ |
| 207 | PyHKEY_intFunc, /* nb_int */ |
| 208 | 0, /* nb_reserved */ |
| 209 | PyHKEY_unaryFailureFunc, /* nb_float */ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 210 | }; |
| 211 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 212 | /*[clinic input] |
| 213 | module winreg |
| 214 | class winreg.HKEYType "PyHKEYObject *" "&PyHKEY_Type" |
| 215 | [clinic start generated code]*/ |
| 216 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=4c964eba3bf914d6]*/ |
| 217 | |
| 218 | /*[python input] |
| 219 | class REGSAM_converter(CConverter): |
| 220 | type = 'REGSAM' |
| 221 | format_unit = 'i' |
| 222 | |
| 223 | class DWORD_converter(CConverter): |
| 224 | type = 'DWORD' |
| 225 | format_unit = 'k' |
| 226 | |
| 227 | class HKEY_converter(CConverter): |
| 228 | type = 'HKEY' |
| 229 | converter = 'clinic_HKEY_converter' |
| 230 | |
| 231 | class HKEY_return_converter(CReturnConverter): |
| 232 | type = 'HKEY' |
| 233 | |
| 234 | def render(self, function, data): |
| 235 | self.declare(data) |
| 236 | self.err_occurred_if_null_pointer("_return_value", data) |
| 237 | data.return_conversion.append( |
| 238 | 'return_value = PyHKEY_FromHKEY(_return_value);\n') |
| 239 | |
| 240 | # HACK: this only works for PyHKEYObjects, nothing else. |
| 241 | # Should this be generalized and enshrined in clinic.py, |
| 242 | # destroy this converter with prejudice. |
| 243 | class self_return_converter(CReturnConverter): |
| 244 | type = 'PyHKEYObject *' |
| 245 | |
| 246 | def render(self, function, data): |
| 247 | self.declare(data) |
| 248 | data.return_conversion.append( |
| 249 | 'return_value = (PyObject *)_return_value;\n') |
| 250 | [python start generated code]*/ |
| 251 | /*[python end generated code: output=da39a3ee5e6b4b0d input=22f7aedc6d68e80e]*/ |
| 252 | |
| 253 | #include "clinic/winreg.c.h" |
| 254 | |
| 255 | /************************************************************************ |
| 256 | |
| 257 | The PyHKEY object methods |
| 258 | |
| 259 | ************************************************************************/ |
| 260 | /*[clinic input] |
| 261 | winreg.HKEYType.Close |
| 262 | |
| 263 | Closes the underlying Windows handle. |
| 264 | |
| 265 | If the handle is already closed, no error is raised. |
| 266 | [clinic start generated code]*/ |
| 267 | |
| 268 | static PyObject * |
| 269 | winreg_HKEYType_Close_impl(PyHKEYObject *self) |
| 270 | /*[clinic end generated code: output=fced3a624fb0c344 input=6786ac75f6b89de6]*/ |
| 271 | { |
| 272 | if (!PyHKEY_Close((PyObject *)self)) |
| 273 | return NULL; |
| 274 | Py_RETURN_NONE; |
| 275 | } |
| 276 | |
| 277 | /*[clinic input] |
| 278 | winreg.HKEYType.Detach |
| 279 | |
| 280 | Detaches the Windows handle from the handle object. |
| 281 | |
| 282 | The result is the value of the handle before it is detached. If the |
| 283 | handle is already detached, this will return zero. |
| 284 | |
| 285 | After calling this function, the handle is effectively invalidated, |
| 286 | but the handle is not closed. You would call this function when you |
| 287 | need the underlying win32 handle to exist beyond the lifetime of the |
| 288 | handle object. |
| 289 | [clinic start generated code]*/ |
| 290 | |
| 291 | static PyObject * |
| 292 | winreg_HKEYType_Detach_impl(PyHKEYObject *self) |
| 293 | /*[clinic end generated code: output=dda5a9e1a01ae78f input=dd2cc09e6c6ba833]*/ |
| 294 | { |
| 295 | void* ret; |
| 296 | ret = (void*)self->hkey; |
| 297 | self->hkey = 0; |
| 298 | return PyLong_FromVoidPtr(ret); |
| 299 | } |
| 300 | |
| 301 | /*[clinic input] |
| 302 | winreg.HKEYType.__enter__ -> self |
| 303 | [clinic start generated code]*/ |
| 304 | |
| 305 | static PyHKEYObject * |
| 306 | winreg_HKEYType___enter___impl(PyHKEYObject *self) |
| 307 | /*[clinic end generated code: output=52c34986dab28990 input=c40fab1f0690a8e2]*/ |
| 308 | { |
| 309 | Py_XINCREF(self); |
| 310 | return self; |
| 311 | } |
| 312 | |
| 313 | |
| 314 | /*[clinic input] |
| 315 | winreg.HKEYType.__exit__ |
| 316 | |
| 317 | exc_type: object |
| 318 | exc_value: object |
| 319 | traceback: object |
| 320 | [clinic start generated code]*/ |
| 321 | |
| 322 | static PyObject * |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 323 | winreg_HKEYType___exit___impl(PyHKEYObject *self, PyObject *exc_type, |
| 324 | PyObject *exc_value, PyObject *traceback) |
| 325 | /*[clinic end generated code: output=923ebe7389e6a263 input=fb32489ee92403c7]*/ |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 326 | { |
| 327 | if (!PyHKEY_Close((PyObject *)self)) |
| 328 | return NULL; |
| 329 | Py_RETURN_NONE; |
| 330 | } |
| 331 | |
| 332 | /*[clinic input] |
| 333 | [clinic start generated code]*/ |
| 334 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=da39a3ee5e6b4b0d]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 335 | |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 336 | static struct PyMethodDef PyHKEY_methods[] = { |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 337 | WINREG_HKEYTYPE_CLOSE_METHODDEF |
| 338 | WINREG_HKEYTYPE_DETACH_METHODDEF |
| 339 | WINREG_HKEYTYPE___ENTER___METHODDEF |
| 340 | WINREG_HKEYTYPE___EXIT___METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 341 | {NULL} |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 342 | }; |
| 343 | |
| 344 | #define OFF(e) offsetof(PyHKEYObject, e) |
| 345 | static PyMemberDef PyHKEY_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 346 | {"handle", T_INT, OFF(hkey), READONLY}, |
| 347 | {NULL} /* Sentinel */ |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 348 | }; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 349 | |
| 350 | /* The type itself */ |
| 351 | PyTypeObject PyHKEY_Type = |
| 352 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 353 | PyVarObject_HEAD_INIT(0, 0) /* fill in type at module init */ |
| 354 | "PyHKEY", |
| 355 | sizeof(PyHKEYObject), |
| 356 | 0, |
| 357 | PyHKEY_deallocFunc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 358 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 359 | 0, /* tp_getattr */ |
| 360 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 361 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 362 | 0, /* tp_repr */ |
| 363 | &PyHKEY_NumberMethods, /* tp_as_number */ |
| 364 | 0, /* tp_as_sequence */ |
| 365 | 0, /* tp_as_mapping */ |
| 366 | PyHKEY_hashFunc, /* tp_hash */ |
| 367 | 0, /* tp_call */ |
| 368 | PyHKEY_strFunc, /* tp_str */ |
| 369 | 0, /* tp_getattro */ |
| 370 | 0, /* tp_setattro */ |
| 371 | 0, /* tp_as_buffer */ |
| 372 | 0, /* tp_flags */ |
| 373 | PyHKEY_doc, /* tp_doc */ |
| 374 | 0, /*tp_traverse*/ |
| 375 | 0, /*tp_clear*/ |
| 376 | 0, /*tp_richcompare*/ |
| 377 | 0, /*tp_weaklistoffset*/ |
| 378 | 0, /*tp_iter*/ |
| 379 | 0, /*tp_iternext*/ |
| 380 | PyHKEY_methods, /*tp_methods*/ |
| 381 | PyHKEY_memberlist, /*tp_members*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 382 | }; |
| 383 | |
| 384 | /************************************************************************ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 385 | The public PyHKEY API (well, not public yet :-) |
| 386 | ************************************************************************/ |
| 387 | PyObject * |
| 388 | PyHKEY_New(HKEY hInit) |
| 389 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 390 | PyHKEYObject *key = PyObject_NEW(PyHKEYObject, &PyHKEY_Type); |
| 391 | if (key) |
| 392 | key->hkey = hInit; |
| 393 | return (PyObject *)key; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | BOOL |
| 397 | PyHKEY_Close(PyObject *ob_handle) |
| 398 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 399 | LONG rc; |
| 400 | PyHKEYObject *key; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 401 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 402 | if (!PyHKEY_Check(ob_handle)) { |
| 403 | PyErr_SetString(PyExc_TypeError, "bad operand type"); |
| 404 | return FALSE; |
| 405 | } |
| 406 | key = (PyHKEYObject *)ob_handle; |
| 407 | rc = key->hkey ? RegCloseKey((HKEY)key->hkey) : ERROR_SUCCESS; |
| 408 | key->hkey = 0; |
| 409 | if (rc != ERROR_SUCCESS) |
| 410 | PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey"); |
| 411 | return rc == ERROR_SUCCESS; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | BOOL |
| 415 | PyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK) |
| 416 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 417 | if (ob == Py_None) { |
| 418 | if (!bNoneOK) { |
| 419 | PyErr_SetString( |
| 420 | PyExc_TypeError, |
| 421 | "None is not a valid HKEY in this context"); |
| 422 | return FALSE; |
| 423 | } |
| 424 | *pHANDLE = (HKEY)0; |
| 425 | } |
| 426 | else if (PyHKEY_Check(ob)) { |
| 427 | PyHKEYObject *pH = (PyHKEYObject *)ob; |
| 428 | *pHANDLE = pH->hkey; |
| 429 | } |
| 430 | else if (PyLong_Check(ob)) { |
| 431 | /* We also support integers */ |
| 432 | PyErr_Clear(); |
| 433 | *pHANDLE = (HKEY)PyLong_AsVoidPtr(ob); |
| 434 | if (PyErr_Occurred()) |
| 435 | return FALSE; |
| 436 | } |
| 437 | else { |
| 438 | PyErr_SetString( |
| 439 | PyExc_TypeError, |
| 440 | "The object is not a PyHKEY object"); |
| 441 | return FALSE; |
| 442 | } |
| 443 | return TRUE; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 446 | BOOL |
| 447 | clinic_HKEY_converter(PyObject *ob, void *p) |
| 448 | { |
| 449 | if (!PyHKEY_AsHKEY(ob, (HKEY *)p, FALSE)) |
| 450 | return FALSE; |
| 451 | return TRUE; |
| 452 | } |
| 453 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 454 | PyObject * |
| 455 | PyHKEY_FromHKEY(HKEY h) |
| 456 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 457 | PyHKEYObject *op; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 458 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 459 | /* Inline PyObject_New */ |
| 460 | op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject)); |
| 461 | if (op == NULL) |
| 462 | return PyErr_NoMemory(); |
Victor Stinner | b509d52 | 2018-11-23 14:27:38 +0100 | [diff] [blame] | 463 | PyObject_INIT(op, &PyHKEY_Type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 464 | op->hkey = h; |
| 465 | return (PyObject *)op; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | |
| 469 | /************************************************************************ |
| 470 | The module methods |
| 471 | ************************************************************************/ |
| 472 | BOOL |
| 473 | PyWinObject_CloseHKEY(PyObject *obHandle) |
| 474 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 475 | BOOL ok; |
| 476 | if (PyHKEY_Check(obHandle)) { |
| 477 | ok = PyHKEY_Close(obHandle); |
| 478 | } |
Fred Drake | 25e1726 | 2000-06-30 17:48:51 +0000 | [diff] [blame] | 479 | #if SIZEOF_LONG >= SIZEOF_HKEY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 480 | else if (PyLong_Check(obHandle)) { |
| 481 | long rc = RegCloseKey((HKEY)PyLong_AsLong(obHandle)); |
| 482 | ok = (rc == ERROR_SUCCESS); |
| 483 | if (!ok) |
| 484 | PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey"); |
| 485 | } |
Fred Drake | 25e1726 | 2000-06-30 17:48:51 +0000 | [diff] [blame] | 486 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 487 | else if (PyLong_Check(obHandle)) { |
| 488 | long rc = RegCloseKey((HKEY)PyLong_AsVoidPtr(obHandle)); |
| 489 | ok = (rc == ERROR_SUCCESS); |
| 490 | if (!ok) |
| 491 | PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey"); |
| 492 | } |
Fred Drake | 25e1726 | 2000-06-30 17:48:51 +0000 | [diff] [blame] | 493 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 494 | else { |
| 495 | PyErr_SetString( |
| 496 | PyExc_TypeError, |
| 497 | "A handle must be a HKEY object or an integer"); |
| 498 | return FALSE; |
| 499 | } |
| 500 | return ok; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | |
| 504 | /* |
| 505 | Private Helper functions for the registry interfaces |
| 506 | |
| 507 | ** Note that fixupMultiSZ and countString have both had changes |
| 508 | ** made to support "incorrect strings". The registry specification |
| 509 | ** calls for strings to be terminated with 2 null bytes. It seems |
luzpaz | a5293b4 | 2017-11-05 07:37:50 -0600 | [diff] [blame] | 510 | ** some commercial packages install strings which don't conform, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 511 | ** causing this code to fail - however, "regedit" etc still work |
luzpaz | a5293b4 | 2017-11-05 07:37:50 -0600 | [diff] [blame] | 512 | ** with these strings (ie only we don't!). |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 513 | */ |
| 514 | static void |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 515 | fixupMultiSZ(wchar_t **str, wchar_t *data, int len) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 516 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 517 | wchar_t *P; |
| 518 | int i; |
| 519 | wchar_t *Q; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 520 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 521 | Q = data + len; |
| 522 | for (P = data, i = 0; P < Q && *P != '\0'; P++, i++) { |
| 523 | str[i] = P; |
Zackery Spytz | 56ed864 | 2019-04-22 11:01:32 -0600 | [diff] [blame] | 524 | for (; P < Q && *P != '\0'; P++) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 525 | ; |
| 526 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | static int |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 530 | countStrings(wchar_t *data, int len) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 531 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 532 | int strings; |
| 533 | wchar_t *P; |
| 534 | wchar_t *Q = data + len; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 535 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 536 | for (P = data, strings = 0; P < Q && *P != '\0'; P++, strings++) |
| 537 | for (; P < Q && *P != '\0'; P++) |
| 538 | ; |
| 539 | return strings; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | /* Convert PyObject into Registry data. |
| 543 | Allocates space as needed. */ |
| 544 | static BOOL |
| 545 | Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize) |
| 546 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 547 | Py_ssize_t i,j; |
| 548 | switch (typ) { |
| 549 | case REG_DWORD: |
| 550 | if (value != Py_None && !PyLong_Check(value)) |
| 551 | return FALSE; |
| 552 | *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1); |
Steve Dower | 4d4bc42 | 2016-05-25 11:26:07 -0700 | [diff] [blame] | 553 | if (*retDataBuf == NULL){ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 554 | PyErr_NoMemory(); |
| 555 | return FALSE; |
| 556 | } |
| 557 | *retDataSize = sizeof(DWORD); |
| 558 | if (value == Py_None) { |
| 559 | DWORD zero = 0; |
| 560 | memcpy(*retDataBuf, &zero, sizeof(DWORD)); |
| 561 | } |
| 562 | else { |
Brian Curtin | 12706f2 | 2012-12-27 10:12:45 -0600 | [diff] [blame] | 563 | DWORD d = PyLong_AsUnsignedLong(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 564 | memcpy(*retDataBuf, &d, sizeof(DWORD)); |
| 565 | } |
| 566 | break; |
Steve Dower | 80ac11d | 2016-05-24 15:42:04 -0700 | [diff] [blame] | 567 | case REG_QWORD: |
| 568 | if (value != Py_None && !PyLong_Check(value)) |
| 569 | return FALSE; |
| 570 | *retDataBuf = (BYTE *)PyMem_NEW(DWORD64, 1); |
Steve Dower | 4d4bc42 | 2016-05-25 11:26:07 -0700 | [diff] [blame] | 571 | if (*retDataBuf == NULL){ |
Steve Dower | 80ac11d | 2016-05-24 15:42:04 -0700 | [diff] [blame] | 572 | PyErr_NoMemory(); |
| 573 | return FALSE; |
| 574 | } |
| 575 | *retDataSize = sizeof(DWORD64); |
| 576 | if (value == Py_None) { |
| 577 | DWORD64 zero = 0; |
| 578 | memcpy(*retDataBuf, &zero, sizeof(DWORD64)); |
| 579 | } |
| 580 | else { |
| 581 | DWORD64 d = PyLong_AsUnsignedLongLong(value); |
| 582 | memcpy(*retDataBuf, &d, sizeof(DWORD64)); |
| 583 | } |
| 584 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 585 | case REG_SZ: |
| 586 | case REG_EXPAND_SZ: |
| 587 | { |
Victor Stinner | be49244 | 2011-11-21 12:43:50 +0100 | [diff] [blame] | 588 | if (value != Py_None) { |
| 589 | Py_ssize_t len; |
| 590 | if (!PyUnicode_Check(value)) |
| 591 | return FALSE; |
| 592 | *retDataBuf = (BYTE*)PyUnicode_AsWideCharString(value, &len); |
| 593 | if (*retDataBuf == NULL) |
| 594 | return FALSE; |
| 595 | *retDataSize = Py_SAFE_DOWNCAST( |
| 596 | (len + 1) * sizeof(wchar_t), |
| 597 | Py_ssize_t, DWORD); |
| 598 | } |
| 599 | else { |
| 600 | *retDataBuf = (BYTE *)PyMem_NEW(wchar_t, 1); |
| 601 | if (*retDataBuf == NULL) { |
| 602 | PyErr_NoMemory(); |
| 603 | return FALSE; |
| 604 | } |
| 605 | ((wchar_t *)*retDataBuf)[0] = L'\0'; |
| 606 | *retDataSize = 1 * sizeof(wchar_t); |
| 607 | } |
| 608 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 609 | } |
| 610 | case REG_MULTI_SZ: |
| 611 | { |
| 612 | DWORD size = 0; |
| 613 | wchar_t *P; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 614 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 615 | if (value == Py_None) |
| 616 | i = 0; |
| 617 | else { |
| 618 | if (!PyList_Check(value)) |
| 619 | return FALSE; |
| 620 | i = PyList_Size(value); |
| 621 | } |
| 622 | for (j = 0; j < i; j++) |
| 623 | { |
| 624 | PyObject *t; |
Victor Stinner | be49244 | 2011-11-21 12:43:50 +0100 | [diff] [blame] | 625 | wchar_t *wstr; |
| 626 | Py_ssize_t len; |
| 627 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 628 | t = PyList_GET_ITEM(value, j); |
| 629 | if (!PyUnicode_Check(t)) |
| 630 | return FALSE; |
Victor Stinner | be49244 | 2011-11-21 12:43:50 +0100 | [diff] [blame] | 631 | wstr = PyUnicode_AsUnicodeAndSize(t, &len); |
| 632 | if (wstr == NULL) |
| 633 | return FALSE; |
| 634 | size += Py_SAFE_DOWNCAST((len + 1) * sizeof(wchar_t), |
Brian Curtin | abb3351 | 2010-08-17 20:08:40 +0000 | [diff] [blame] | 635 | size_t, DWORD); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 636 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 637 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 638 | *retDataSize = size + 2; |
| 639 | *retDataBuf = (BYTE *)PyMem_NEW(char, |
| 640 | *retDataSize); |
Steve Dower | 4d4bc42 | 2016-05-25 11:26:07 -0700 | [diff] [blame] | 641 | if (*retDataBuf == NULL){ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 642 | PyErr_NoMemory(); |
| 643 | return FALSE; |
| 644 | } |
| 645 | P = (wchar_t *)*retDataBuf; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 646 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 647 | for (j = 0; j < i; j++) |
| 648 | { |
| 649 | PyObject *t; |
Victor Stinner | be49244 | 2011-11-21 12:43:50 +0100 | [diff] [blame] | 650 | wchar_t *wstr; |
| 651 | Py_ssize_t len; |
| 652 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 653 | t = PyList_GET_ITEM(value, j); |
Victor Stinner | be49244 | 2011-11-21 12:43:50 +0100 | [diff] [blame] | 654 | wstr = PyUnicode_AsUnicodeAndSize(t, &len); |
Zackery Spytz | 5d95312 | 2018-11-08 02:45:00 -0700 | [diff] [blame] | 655 | assert(wstr); |
Victor Stinner | be49244 | 2011-11-21 12:43:50 +0100 | [diff] [blame] | 656 | wcscpy(P, wstr); |
| 657 | P += (len + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 658 | } |
| 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 Ware | ad4690f | 2014-07-03 10:58:06 -0500 | [diff] [blame] | 667 | if (value == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 668 | *retDataSize = 0; |
Zachary Ware | ad4690f | 2014-07-03 10:58:06 -0500 | [diff] [blame] | 669 | *retDataBuf = NULL; |
| 670 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 671 | else { |
| 672 | Py_buffer view; |
Neal Norwitz | 1385b89 | 2007-08-26 23:07:13 +0000 | [diff] [blame] | 673 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 674 | 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 Norwitz | 1385b89 | 2007-08-26 23:07:13 +0000 | [diff] [blame] | 681 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 682 | if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0) |
| 683 | return FALSE; |
Neal Norwitz | 1385b89 | 2007-08-26 23:07:13 +0000 | [diff] [blame] | 684 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 685 | *retDataBuf = (BYTE *)PyMem_NEW(char, view.len); |
Steve Dower | 4d4bc42 | 2016-05-25 11:26:07 -0700 | [diff] [blame] | 686 | if (*retDataBuf == NULL){ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 687 | PyBuffer_Release(&view); |
| 688 | PyErr_NoMemory(); |
| 689 | return FALSE; |
| 690 | } |
Brian Curtin | abb3351 | 2010-08-17 20:08:40 +0000 | [diff] [blame] | 691 | *retDataSize = Py_SAFE_DOWNCAST(view.len, Py_ssize_t, DWORD); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 692 | memcpy(*retDataBuf, view.buf, view.len); |
| 693 | PyBuffer_Release(&view); |
| 694 | } |
| 695 | break; |
| 696 | } |
| 697 | return TRUE; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | /* Convert Registry data into PyObject*/ |
| 701 | static PyObject * |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 702 | Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 703 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 704 | PyObject *obData; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 705 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 706 | switch (typ) { |
| 707 | case REG_DWORD: |
| 708 | if (retDataSize == 0) |
Brian Curtin | 172e422 | 2012-12-27 14:04:42 -0600 | [diff] [blame] | 709 | obData = PyLong_FromUnsignedLong(0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 710 | else |
Steve Dower | 80ac11d | 2016-05-24 15:42:04 -0700 | [diff] [blame] | 711 | 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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 718 | break; |
| 719 | case REG_SZ: |
| 720 | case REG_EXPAND_SZ: |
| 721 | { |
Steve Dower | 40fa266 | 2016-12-17 13:30:27 -0800 | [diff] [blame] | 722 | /* 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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 726 | wchar_t *data = (wchar_t *)retDataBuf; |
Steve Dower | 40fa266 | 2016-12-17 13:30:27 -0800 | [diff] [blame] | 727 | size_t len = wcsnlen(data, retDataSize / sizeof(wchar_t)); |
| 728 | obData = PyUnicode_FromWideChar(data, len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 729 | 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 Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 740 | wchar_t **str = PyMem_New(wchar_t *, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 741 | if (str == NULL) |
| 742 | return PyErr_NoMemory(); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 743 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 744 | fixupMultiSZ(str, data, len); |
| 745 | obData = PyList_New(s); |
Jesus Cea | 782c4cf | 2014-03-13 17:35:32 +0100 | [diff] [blame] | 746 | if (obData == NULL) { |
Victor Stinner | 9cb1ec5 | 2014-03-13 19:08:10 +0100 | [diff] [blame] | 747 | PyMem_Free(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 748 | return NULL; |
Jesus Cea | 782c4cf | 2014-03-13 17:35:32 +0100 | [diff] [blame] | 749 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 750 | 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 Stinner | 9cb1ec5 | 2014-03-13 19:08:10 +0100 | [diff] [blame] | 757 | PyMem_Free(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 758 | return NULL; |
| 759 | } |
Zackery Spytz | 99d56b5 | 2018-12-08 07:16:55 -0700 | [diff] [blame] | 760 | 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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 767 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 768 | PyMem_Free(str); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 769 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 770 | 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 Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | /* The Python methods */ |
| 789 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 790 | /*[clinic input] |
| 791 | winreg.CloseKey |
| 792 | |
| 793 | hkey: object |
| 794 | A previously opened key. |
| 795 | / |
| 796 | |
| 797 | Closes a previously opened registry key. |
| 798 | |
| 799 | Note that if the key is not closed using this method, it will be |
| 800 | closed when the hkey object is destroyed by Python. |
| 801 | [clinic start generated code]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 802 | |
| 803 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 804 | winreg_CloseKey(PyObject *module, PyObject *hkey) |
| 805 | /*[clinic end generated code: output=a4fa537019a80d15 input=5b1aac65ba5127ad]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 806 | { |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 807 | if (!PyHKEY_Close(hkey)) |
| 808 | return NULL; |
| 809 | Py_RETURN_NONE; |
| 810 | } |
| 811 | |
| 812 | /*[clinic input] |
| 813 | winreg.ConnectRegistry -> HKEY |
| 814 | |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 815 | computer_name: Py_UNICODE(accept={str, NoneType}) |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 816 | 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 Storchaka | 6a7b3a7 | 2016-04-17 08:32:47 +0300 | [diff] [blame] | 822 | Establishes a connection to the registry on another computer. |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 823 | |
| 824 | The return value is the handle of the opened key. |
| 825 | If the function fails, an OSError exception is raised. |
| 826 | [clinic start generated code]*/ |
| 827 | |
| 828 | static HKEY |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 829 | winreg_ConnectRegistry_impl(PyObject *module, |
| 830 | const Py_UNICODE *computer_name, HKEY key) |
| 831 | /*[clinic end generated code: output=cd4f70fb9ec901fb input=5f98a891a347e68e]*/ |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 832 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 833 | HKEY retKey; |
| 834 | long rc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 835 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 836 | rc = RegConnectRegistryW(computer_name, key, &retKey); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 837 | Py_END_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 838 | if (rc != ERROR_SUCCESS) { |
| 839 | PyErr_SetFromWindowsErrWithFunction(rc, "ConnectRegistry"); |
| 840 | return NULL; |
| 841 | } |
| 842 | return retKey; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 845 | /*[clinic input] |
| 846 | winreg.CreateKey -> HKEY |
| 847 | |
| 848 | key: HKEY |
| 849 | An already open key, or one of the predefined HKEY_* constants. |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 850 | sub_key: Py_UNICODE(accept={str, NoneType}) |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 851 | The name of the key this method opens or creates. |
| 852 | / |
| 853 | |
| 854 | Creates or opens the specified key. |
| 855 | |
| 856 | If key is one of the predefined keys, sub_key may be None. In that case, |
| 857 | the handle returned is the same key handle passed in to the function. |
| 858 | |
| 859 | If the key already exists, this function opens the existing key. |
| 860 | |
| 861 | The return value is the handle of the opened key. |
| 862 | If the function fails, an OSError exception is raised. |
| 863 | [clinic start generated code]*/ |
| 864 | |
| 865 | static HKEY |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 866 | winreg_CreateKey_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key) |
| 867 | /*[clinic end generated code: output=2af13910d56eae26 input=3cdd1622488acea2]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 868 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 869 | HKEY retKey; |
| 870 | long rc; |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 871 | |
| 872 | rc = RegCreateKeyW(key, sub_key, &retKey); |
| 873 | if (rc != ERROR_SUCCESS) { |
| 874 | PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 875 | return NULL; |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 876 | } |
| 877 | return retKey; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 880 | /*[clinic input] |
| 881 | winreg.CreateKeyEx -> HKEY |
| 882 | |
| 883 | key: HKEY |
| 884 | An already open key, or one of the predefined HKEY_* constants. |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 885 | sub_key: Py_UNICODE(accept={str, NoneType}) |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 886 | 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 | |
| 893 | Creates or opens the specified key. |
| 894 | |
| 895 | If key is one of the predefined keys, sub_key may be None. In that case, |
| 896 | the handle returned is the same key handle passed in to the function. |
| 897 | |
| 898 | If the key already exists, this function opens the existing key |
| 899 | |
| 900 | The return value is the handle of the opened key. |
| 901 | If the function fails, an OSError exception is raised. |
| 902 | [clinic start generated code]*/ |
| 903 | |
| 904 | static HKEY |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 905 | winreg_CreateKeyEx_impl(PyObject *module, HKEY key, |
| 906 | const Py_UNICODE *sub_key, int reserved, |
| 907 | REGSAM access) |
| 908 | /*[clinic end generated code: output=643a70ad6a361a97 input=42c2b03f98406b66]*/ |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 909 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 910 | HKEY retKey; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 911 | long rc; |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 912 | |
Segev Finer | 679b566 | 2017-07-27 01:17:57 +0300 | [diff] [blame] | 913 | rc = RegCreateKeyExW(key, sub_key, reserved, NULL, 0, |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 914 | access, NULL, &retKey, NULL); |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 915 | if (rc != ERROR_SUCCESS) { |
| 916 | PyErr_SetFromWindowsErrWithFunction(rc, "CreateKeyEx"); |
| 917 | return NULL; |
| 918 | } |
| 919 | return retKey; |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 922 | /*[clinic input] |
| 923 | winreg.DeleteKey |
| 924 | key: HKEY |
| 925 | An already open key, or any one of the predefined HKEY_* constants. |
| 926 | sub_key: Py_UNICODE |
| 927 | A string that must be the name of a subkey of the key identified by |
| 928 | the key parameter. This value must not be None, and the key may not |
| 929 | have subkeys. |
| 930 | / |
| 931 | |
| 932 | Deletes the specified key. |
| 933 | |
| 934 | This method can not delete keys with subkeys. |
| 935 | |
| 936 | If the function succeeds, the entire key, including all of its values, |
| 937 | is removed. If the function fails, an OSError exception is raised. |
| 938 | [clinic start generated code]*/ |
| 939 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 940 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 941 | winreg_DeleteKey_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key) |
| 942 | /*[clinic end generated code: output=d2652a84f70e0862 input=b31d225b935e4211]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 943 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 944 | long rc; |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 945 | rc = RegDeleteKeyW(key, sub_key ); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 946 | if (rc != ERROR_SUCCESS) |
| 947 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey"); |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 948 | Py_RETURN_NONE; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 951 | /*[clinic input] |
| 952 | winreg.DeleteKeyEx |
| 953 | |
| 954 | key: HKEY |
| 955 | An already open key, or any one of the predefined HKEY_* constants. |
| 956 | sub_key: Py_UNICODE |
| 957 | A string that must be the name of a subkey of the key identified by |
| 958 | the key parameter. This value must not be None, and the key may not |
| 959 | have subkeys. |
| 960 | access: REGSAM(c_default='KEY_WOW64_64KEY') = winreg.KEY_WOW64_64KEY |
| 961 | An integer that specifies an access mask that describes the |
| 962 | desired security access for the key. Default is KEY_WOW64_64KEY. |
| 963 | reserved: int = 0 |
| 964 | A reserved integer, and must be zero. Default is zero. |
| 965 | |
| 966 | Deletes the specified key (64-bit OS only). |
| 967 | |
| 968 | This method can not delete keys with subkeys. |
| 969 | |
| 970 | If the function succeeds, the entire key, including all of its values, |
| 971 | is removed. If the function fails, an OSError exception is raised. |
| 972 | On unsupported Windows versions, NotImplementedError is raised. |
| 973 | [clinic start generated code]*/ |
| 974 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 975 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 976 | winreg_DeleteKeyEx_impl(PyObject *module, HKEY key, |
| 977 | const Py_UNICODE *sub_key, REGSAM access, |
| 978 | int reserved) |
| 979 | /*[clinic end generated code: output=52a1c8b374ebc003 input=711d9d89e7ecbed7]*/ |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 980 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 981 | HMODULE hMod; |
| 982 | typedef LONG (WINAPI *RDKEFunc)(HKEY, const wchar_t*, REGSAM, int); |
| 983 | RDKEFunc pfn = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 984 | long rc; |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 985 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 986 | /* Only available on 64bit platforms, so we must load it |
| 987 | dynamically. */ |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 988 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 989 | hMod = GetModuleHandleW(L"advapi32.dll"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 990 | if (hMod) |
| 991 | pfn = (RDKEFunc)GetProcAddress(hMod, |
| 992 | "RegDeleteKeyExW"); |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 993 | Py_END_ALLOW_THREADS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 994 | if (!pfn) { |
| 995 | PyErr_SetString(PyExc_NotImplementedError, |
| 996 | "not implemented on this platform"); |
| 997 | return NULL; |
| 998 | } |
| 999 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1000 | rc = (*pfn)(key, sub_key, access, reserved); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1001 | Py_END_ALLOW_THREADS |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 1002 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1003 | if (rc != ERROR_SUCCESS) |
| 1004 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKeyEx"); |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1005 | Py_RETURN_NONE; |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1008 | /*[clinic input] |
| 1009 | winreg.DeleteValue |
| 1010 | |
| 1011 | key: HKEY |
| 1012 | An already open key, or any one of the predefined HKEY_* constants. |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1013 | value: Py_UNICODE(accept={str, NoneType}) |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1014 | A string that identifies the value to remove. |
| 1015 | / |
| 1016 | |
| 1017 | Removes a named value from a registry key. |
| 1018 | [clinic start generated code]*/ |
| 1019 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 1020 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1021 | winreg_DeleteValue_impl(PyObject *module, HKEY key, const Py_UNICODE *value) |
| 1022 | /*[clinic end generated code: output=56fa9d21f3a54371 input=a78d3407a4197b21]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1023 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1024 | long rc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1025 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1026 | rc = RegDeleteValueW(key, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1027 | Py_END_ALLOW_THREADS |
| 1028 | if (rc !=ERROR_SUCCESS) |
| 1029 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1030 | "RegDeleteValue"); |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1031 | Py_RETURN_NONE; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1034 | /*[clinic input] |
| 1035 | winreg.EnumKey |
| 1036 | |
| 1037 | key: HKEY |
| 1038 | An already open key, or any one of the predefined HKEY_* constants. |
| 1039 | index: int |
| 1040 | An integer that identifies the index of the key to retrieve. |
| 1041 | / |
| 1042 | |
| 1043 | Enumerates subkeys of an open registry key. |
| 1044 | |
| 1045 | The function retrieves the name of one subkey each time it is called. |
| 1046 | It is typically called repeatedly until an OSError exception is |
| 1047 | raised, indicating no more values are available. |
| 1048 | [clinic start generated code]*/ |
| 1049 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1050 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1051 | winreg_EnumKey_impl(PyObject *module, HKEY key, int index) |
| 1052 | /*[clinic end generated code: output=25a6ec52cd147bc4 input=fad9a7c00ab0e04b]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1053 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1054 | long rc; |
| 1055 | PyObject *retStr; |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1056 | |
| 1057 | /* The Windows docs claim that the max key name length is 255 |
| 1058 | * characters, plus a terminating nul character. However, |
| 1059 | * empirical testing demonstrates that it is possible to |
| 1060 | * create a 256 character key that is missing the terminating |
| 1061 | * nul. RegEnumKeyEx requires a 257 character buffer to |
| 1062 | * retrieve such a key name. */ |
| 1063 | wchar_t tmpbuf[257]; |
Kristján Valur Jónsson | 984dfa7 | 2012-04-02 15:23:29 +0000 | [diff] [blame] | 1064 | DWORD len = sizeof(tmpbuf)/sizeof(wchar_t); /* includes NULL terminator */ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1065 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1066 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1067 | rc = RegEnumKeyExW(key, index, tmpbuf, &len, NULL, NULL, NULL, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1068 | Py_END_ALLOW_THREADS |
| 1069 | if (rc != ERROR_SUCCESS) |
| 1070 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx"); |
Tim Peters | 313fcd4 | 2006-02-19 04:05:39 +0000 | [diff] [blame] | 1071 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1072 | retStr = PyUnicode_FromWideChar(tmpbuf, len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1073 | return retStr; /* can be NULL */ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1076 | /*[clinic input] |
| 1077 | winreg.EnumValue |
| 1078 | |
| 1079 | key: HKEY |
| 1080 | An already open key, or any one of the predefined HKEY_* constants. |
| 1081 | index: int |
| 1082 | An integer that identifies the index of the value to retrieve. |
| 1083 | / |
| 1084 | |
| 1085 | Enumerates values of an open registry key. |
| 1086 | |
| 1087 | The function retrieves the name of one subkey each time it is called. |
| 1088 | It is typically called repeatedly, until an OSError exception |
| 1089 | is raised, indicating no more values. |
| 1090 | |
| 1091 | The result is a tuple of 3 items: |
| 1092 | value_name |
| 1093 | A string that identifies the value. |
| 1094 | value_data |
| 1095 | An object that holds the value data, and whose type depends |
| 1096 | on the underlying registry type. |
| 1097 | data_type |
| 1098 | An integer that identifies the type of the value data. |
| 1099 | [clinic start generated code]*/ |
| 1100 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1101 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1102 | winreg_EnumValue_impl(PyObject *module, HKEY key, int index) |
| 1103 | /*[clinic end generated code: output=d363b5a06f8789ac input=4414f47a6fb238b5]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1104 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1105 | long rc; |
| 1106 | wchar_t *retValueBuf; |
Amaury Forgeot d'Arc | f2b69df | 2010-08-16 22:11:29 +0000 | [diff] [blame] | 1107 | BYTE *tmpBuf; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1108 | BYTE *retDataBuf; |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1109 | DWORD retValueSize, bufValueSize; |
| 1110 | DWORD retDataSize, bufDataSize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1111 | DWORD typ; |
| 1112 | PyObject *obData; |
| 1113 | PyObject *retVal; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1114 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1115 | if ((rc = RegQueryInfoKeyW(key, NULL, NULL, NULL, NULL, NULL, NULL, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1116 | NULL, |
| 1117 | &retValueSize, &retDataSize, NULL, NULL)) |
| 1118 | != ERROR_SUCCESS) |
| 1119 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1120 | "RegQueryInfoKey"); |
| 1121 | ++retValueSize; /* include null terminators */ |
| 1122 | ++retDataSize; |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1123 | bufDataSize = retDataSize; |
| 1124 | bufValueSize = retValueSize; |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 1125 | retValueBuf = PyMem_New(wchar_t, retValueSize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1126 | if (retValueBuf == NULL) |
| 1127 | return PyErr_NoMemory(); |
| 1128 | retDataBuf = (BYTE *)PyMem_Malloc(retDataSize); |
| 1129 | if (retDataBuf == NULL) { |
| 1130 | PyMem_Free(retValueBuf); |
| 1131 | return PyErr_NoMemory(); |
| 1132 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1133 | |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1134 | while (1) { |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1135 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1136 | rc = RegEnumValueW(key, |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1137 | index, |
| 1138 | retValueBuf, |
| 1139 | &retValueSize, |
| 1140 | NULL, |
| 1141 | &typ, |
| 1142 | (BYTE *)retDataBuf, |
| 1143 | &retDataSize); |
| 1144 | Py_END_ALLOW_THREADS |
| 1145 | |
| 1146 | if (rc != ERROR_MORE_DATA) |
| 1147 | break; |
| 1148 | |
| 1149 | bufDataSize *= 2; |
Amaury Forgeot d'Arc | f2b69df | 2010-08-16 22:11:29 +0000 | [diff] [blame] | 1150 | tmpBuf = (BYTE *)PyMem_Realloc(retDataBuf, bufDataSize); |
Brian Curtin | 9b7e2d1 | 2010-06-08 20:57:52 +0000 | [diff] [blame] | 1151 | if (tmpBuf == NULL) { |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1152 | PyErr_NoMemory(); |
| 1153 | retVal = NULL; |
| 1154 | goto fail; |
| 1155 | } |
Brian Curtin | 9b7e2d1 | 2010-06-08 20:57:52 +0000 | [diff] [blame] | 1156 | retDataBuf = tmpBuf; |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1157 | retDataSize = bufDataSize; |
| 1158 | retValueSize = bufValueSize; |
| 1159 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1160 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1161 | if (rc != ERROR_SUCCESS) { |
| 1162 | retVal = PyErr_SetFromWindowsErrWithFunction(rc, |
| 1163 | "PyRegEnumValue"); |
| 1164 | goto fail; |
| 1165 | } |
| 1166 | obData = Reg2Py(retDataBuf, retDataSize, typ); |
| 1167 | if (obData == NULL) { |
| 1168 | retVal = NULL; |
| 1169 | goto fail; |
| 1170 | } |
| 1171 | retVal = Py_BuildValue("uOi", retValueBuf, obData, typ); |
| 1172 | Py_DECREF(obData); |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1173 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1174 | PyMem_Free(retValueBuf); |
| 1175 | PyMem_Free(retDataBuf); |
| 1176 | return retVal; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1179 | /*[clinic input] |
| 1180 | winreg.ExpandEnvironmentStrings |
| 1181 | |
| 1182 | string: Py_UNICODE |
| 1183 | / |
| 1184 | |
| 1185 | Expand environment vars. |
| 1186 | [clinic start generated code]*/ |
| 1187 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1188 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1189 | winreg_ExpandEnvironmentStrings_impl(PyObject *module, |
| 1190 | const Py_UNICODE *string) |
| 1191 | /*[clinic end generated code: output=8fa4e959747a7312 input=b2a9714d2b751aa6]*/ |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1192 | { |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 1193 | wchar_t *retValue = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1194 | DWORD retValueSize; |
| 1195 | DWORD rc; |
| 1196 | PyObject *o; |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1197 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1198 | retValueSize = ExpandEnvironmentStringsW(string, retValue, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1199 | if (retValueSize == 0) { |
| 1200 | return PyErr_SetFromWindowsErrWithFunction(retValueSize, |
| 1201 | "ExpandEnvironmentStrings"); |
| 1202 | } |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 1203 | retValue = PyMem_New(wchar_t, retValueSize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1204 | if (retValue == NULL) { |
| 1205 | return PyErr_NoMemory(); |
| 1206 | } |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1207 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1208 | rc = ExpandEnvironmentStringsW(string, retValue, retValueSize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1209 | if (rc == 0) { |
| 1210 | PyMem_Free(retValue); |
| 1211 | return PyErr_SetFromWindowsErrWithFunction(retValueSize, |
| 1212 | "ExpandEnvironmentStrings"); |
| 1213 | } |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 1214 | o = PyUnicode_FromWideChar(retValue, wcslen(retValue)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1215 | PyMem_Free(retValue); |
| 1216 | return o; |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1219 | /*[clinic input] |
| 1220 | winreg.FlushKey |
| 1221 | |
| 1222 | key: HKEY |
| 1223 | An already open key, or any one of the predefined HKEY_* constants. |
| 1224 | / |
| 1225 | |
| 1226 | Writes all the attributes of a key to the registry. |
| 1227 | |
| 1228 | It is not necessary to call FlushKey to change a key. Registry changes |
| 1229 | are flushed to disk by the registry using its lazy flusher. Registry |
| 1230 | changes are also flushed to disk at system shutdown. Unlike |
| 1231 | CloseKey(), the FlushKey() method returns only when all the data has |
| 1232 | been written to the registry. |
| 1233 | |
| 1234 | An application should only call FlushKey() if it requires absolute |
| 1235 | certainty that registry changes are on disk. If you don't know whether |
| 1236 | a FlushKey() call is required, it probably isn't. |
| 1237 | [clinic start generated code]*/ |
| 1238 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1239 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1240 | winreg_FlushKey_impl(PyObject *module, HKEY key) |
| 1241 | /*[clinic end generated code: output=e6fc230d4c5dc049 input=f57457c12297d82f]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1242 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1243 | long rc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1244 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1245 | rc = RegFlushKey(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1246 | Py_END_ALLOW_THREADS |
| 1247 | if (rc != ERROR_SUCCESS) |
| 1248 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey"); |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1249 | Py_RETURN_NONE; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1250 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1251 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1252 | |
| 1253 | /*[clinic input] |
| 1254 | winreg.LoadKey |
| 1255 | |
| 1256 | key: HKEY |
| 1257 | An already open key, or any one of the predefined HKEY_* constants. |
| 1258 | sub_key: Py_UNICODE |
| 1259 | A string that identifies the sub-key to load. |
| 1260 | file_name: Py_UNICODE |
| 1261 | The name of the file to load registry data from. This file must |
| 1262 | have been created with the SaveKey() function. Under the file |
| 1263 | allocation table (FAT) file system, the filename may not have an |
| 1264 | extension. |
| 1265 | / |
| 1266 | |
| 1267 | Insert data into the registry from a file. |
| 1268 | |
| 1269 | Creates a subkey under the specified key and stores registration |
| 1270 | information from a specified file into that subkey. |
| 1271 | |
| 1272 | A call to LoadKey() fails if the calling process does not have the |
| 1273 | SE_RESTORE_PRIVILEGE privilege. |
| 1274 | |
| 1275 | If key is a handle returned by ConnectRegistry(), then the path |
| 1276 | specified in fileName is relative to the remote computer. |
| 1277 | |
| 1278 | The MSDN docs imply key must be in the HKEY_USER or HKEY_LOCAL_MACHINE |
| 1279 | tree. |
| 1280 | [clinic start generated code]*/ |
| 1281 | |
| 1282 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1283 | winreg_LoadKey_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key, |
| 1284 | const Py_UNICODE *file_name) |
| 1285 | /*[clinic end generated code: output=65f89f2548cb27c7 input=e3b5b45ade311582]*/ |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1286 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1287 | long rc; |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1288 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1289 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1290 | rc = RegLoadKeyW(key, sub_key, file_name ); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1291 | Py_END_ALLOW_THREADS |
| 1292 | if (rc != ERROR_SUCCESS) |
| 1293 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey"); |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1294 | Py_RETURN_NONE; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1297 | /*[clinic input] |
| 1298 | winreg.OpenKey -> HKEY |
| 1299 | |
| 1300 | key: HKEY |
| 1301 | An already open key, or any one of the predefined HKEY_* constants. |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1302 | sub_key: Py_UNICODE(accept={str, NoneType}) |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1303 | A string that identifies the sub_key to open. |
| 1304 | reserved: int = 0 |
| 1305 | A reserved integer that must be zero. Default is zero. |
| 1306 | access: REGSAM(c_default='KEY_READ') = winreg.KEY_READ |
| 1307 | An integer that specifies an access mask that describes the desired |
| 1308 | security access for the key. Default is KEY_READ. |
| 1309 | |
| 1310 | Opens the specified key. |
| 1311 | |
| 1312 | The result is a new handle to the specified key. |
| 1313 | If the function fails, an OSError exception is raised. |
| 1314 | [clinic start generated code]*/ |
| 1315 | |
| 1316 | static HKEY |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1317 | winreg_OpenKey_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1318 | int reserved, REGSAM access) |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1319 | /*[clinic end generated code: output=8849bff2c30104ad input=098505ac36a9ae28]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1320 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1321 | HKEY retKey; |
| 1322 | long rc; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1323 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1324 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1325 | rc = RegOpenKeyExW(key, sub_key, reserved, access, &retKey); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1326 | Py_END_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1327 | if (rc != ERROR_SUCCESS) { |
| 1328 | PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx"); |
| 1329 | return NULL; |
| 1330 | } |
| 1331 | return retKey; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1334 | /*[clinic input] |
| 1335 | winreg.OpenKeyEx = winreg.OpenKey |
| 1336 | |
| 1337 | Opens the specified key. |
| 1338 | |
| 1339 | The result is a new handle to the specified key. |
| 1340 | If the function fails, an OSError exception is raised. |
| 1341 | [clinic start generated code]*/ |
| 1342 | |
| 1343 | static HKEY |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1344 | winreg_OpenKeyEx_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1345 | int reserved, REGSAM access) |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1346 | /*[clinic end generated code: output=81bc2bd684bc77ae input=c6c4972af8622959]*/ |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1347 | { |
| 1348 | return winreg_OpenKey_impl(module, key, sub_key, reserved, access); |
| 1349 | } |
| 1350 | |
| 1351 | /*[clinic input] |
| 1352 | winreg.QueryInfoKey |
| 1353 | |
| 1354 | key: HKEY |
| 1355 | An already open key, or any one of the predefined HKEY_* constants. |
| 1356 | / |
| 1357 | |
| 1358 | Returns information about a key. |
| 1359 | |
| 1360 | The result is a tuple of 3 items: |
| 1361 | An integer that identifies the number of sub keys this key has. |
| 1362 | An integer that identifies the number of values this key has. |
| 1363 | An integer that identifies when the key was last modified (if available) |
| 1364 | as 100's of nanoseconds since Jan 1, 1600. |
| 1365 | [clinic start generated code]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1366 | |
| 1367 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1368 | winreg_QueryInfoKey_impl(PyObject *module, HKEY key) |
| 1369 | /*[clinic end generated code: output=dc657b8356a4f438 input=c3593802390cde1f]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1370 | { |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1371 | long rc; |
| 1372 | DWORD nSubKeys, nValues; |
| 1373 | FILETIME ft; |
| 1374 | LARGE_INTEGER li; |
| 1375 | PyObject *l; |
| 1376 | PyObject *ret; |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1377 | |
| 1378 | if ((rc = RegQueryInfoKey(key, NULL, NULL, 0, &nSubKeys, NULL, NULL, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1379 | &nValues, NULL, NULL, NULL, &ft)) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1380 | != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1381 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey"); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1382 | li.LowPart = ft.dwLowDateTime; |
| 1383 | li.HighPart = ft.dwHighDateTime; |
| 1384 | l = PyLong_FromLongLong(li.QuadPart); |
| 1385 | if (l == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1386 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1387 | ret = Py_BuildValue("iiO", nSubKeys, nValues, l); |
| 1388 | Py_DECREF(l); |
| 1389 | return ret; |
| 1390 | } |
| 1391 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1392 | /*[clinic input] |
| 1393 | winreg.QueryValue |
| 1394 | |
| 1395 | key: HKEY |
| 1396 | An already open key, or any one of the predefined HKEY_* constants. |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1397 | sub_key: Py_UNICODE(accept={str, NoneType}) |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1398 | A string that holds the name of the subkey with which the value |
| 1399 | is associated. If this parameter is None or empty, the function |
| 1400 | retrieves the value set by the SetValue() method for the key |
| 1401 | identified by key. |
| 1402 | / |
| 1403 | |
| 1404 | Retrieves the unnamed value for a key. |
| 1405 | |
| 1406 | Values in the registry have name, type, and data components. This method |
| 1407 | retrieves the data for a key's first value that has a NULL name. |
| 1408 | But since the underlying API call doesn't return the type, you'll |
| 1409 | probably be happier using QueryValueEx; this function is just here for |
| 1410 | completeness. |
| 1411 | [clinic start generated code]*/ |
| 1412 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1413 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1414 | winreg_QueryValue_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key) |
| 1415 | /*[clinic end generated code: output=c655810ae50c63a9 input=41cafbbf423b21d6]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1416 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1417 | long rc; |
| 1418 | PyObject *retStr; |
| 1419 | wchar_t *retBuf; |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1420 | DWORD bufSize = 0; |
| 1421 | DWORD retSize = 0; |
| 1422 | wchar_t *tmp; |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1423 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1424 | rc = RegQueryValueW(key, sub_key, NULL, &retSize); |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1425 | if (rc == ERROR_MORE_DATA) |
| 1426 | retSize = 256; |
| 1427 | else if (rc != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1428 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1429 | "RegQueryValue"); |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1430 | |
| 1431 | bufSize = retSize; |
| 1432 | retBuf = (wchar_t *) PyMem_Malloc(bufSize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1433 | if (retBuf == NULL) |
| 1434 | return PyErr_NoMemory(); |
Guido van Rossum | a8c360e | 2007-07-17 20:50:43 +0000 | [diff] [blame] | 1435 | |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1436 | while (1) { |
| 1437 | retSize = bufSize; |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1438 | rc = RegQueryValueW(key, sub_key, retBuf, &retSize); |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1439 | if (rc != ERROR_MORE_DATA) |
| 1440 | break; |
| 1441 | |
| 1442 | bufSize *= 2; |
| 1443 | tmp = (wchar_t *) PyMem_Realloc(retBuf, bufSize); |
| 1444 | if (tmp == NULL) { |
| 1445 | PyMem_Free(retBuf); |
| 1446 | return PyErr_NoMemory(); |
| 1447 | } |
| 1448 | retBuf = tmp; |
| 1449 | } |
| 1450 | |
| 1451 | if (rc != ERROR_SUCCESS) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1452 | PyMem_Free(retBuf); |
| 1453 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1454 | "RegQueryValue"); |
| 1455 | } |
Guido van Rossum | a8c360e | 2007-07-17 20:50:43 +0000 | [diff] [blame] | 1456 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1457 | retStr = PyUnicode_FromWideChar(retBuf, wcslen(retBuf)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1458 | PyMem_Free(retBuf); |
| 1459 | return retStr; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1462 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1463 | /*[clinic input] |
| 1464 | winreg.QueryValueEx |
| 1465 | |
| 1466 | key: HKEY |
| 1467 | An already open key, or any one of the predefined HKEY_* constants. |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1468 | name: Py_UNICODE(accept={str, NoneType}) |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1469 | A string indicating the value to query. |
| 1470 | / |
| 1471 | |
| 1472 | Retrieves the type and value of a specified sub-key. |
| 1473 | |
| 1474 | Behaves mostly like QueryValue(), but also returns the type of the |
| 1475 | specified value name associated with the given open registry key. |
| 1476 | |
| 1477 | The return value is a tuple of the value and the type_id. |
| 1478 | [clinic start generated code]*/ |
| 1479 | |
| 1480 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1481 | winreg_QueryValueEx_impl(PyObject *module, HKEY key, const Py_UNICODE *name) |
| 1482 | /*[clinic end generated code: output=f1b85b1c3d887ec7 input=cf366cada4836891]*/ |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1483 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1484 | long rc; |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1485 | BYTE *retBuf, *tmp; |
| 1486 | DWORD bufSize = 0, retSize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1487 | DWORD typ; |
| 1488 | PyObject *obData; |
| 1489 | PyObject *result; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1490 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1491 | rc = RegQueryValueExW(key, name, NULL, NULL, NULL, &bufSize); |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1492 | if (rc == ERROR_MORE_DATA) |
| 1493 | bufSize = 256; |
| 1494 | else if (rc != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1495 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1496 | "RegQueryValueEx"); |
| 1497 | retBuf = (BYTE *)PyMem_Malloc(bufSize); |
| 1498 | if (retBuf == NULL) |
| 1499 | return PyErr_NoMemory(); |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1500 | |
| 1501 | while (1) { |
| 1502 | retSize = bufSize; |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1503 | rc = RegQueryValueExW(key, name, NULL, &typ, |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1504 | (BYTE *)retBuf, &retSize); |
| 1505 | if (rc != ERROR_MORE_DATA) |
| 1506 | break; |
| 1507 | |
| 1508 | bufSize *= 2; |
| 1509 | tmp = (char *) PyMem_Realloc(retBuf, bufSize); |
| 1510 | if (tmp == NULL) { |
| 1511 | PyMem_Free(retBuf); |
| 1512 | return PyErr_NoMemory(); |
| 1513 | } |
| 1514 | retBuf = tmp; |
| 1515 | } |
| 1516 | |
| 1517 | if (rc != ERROR_SUCCESS) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1518 | PyMem_Free(retBuf); |
| 1519 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1520 | "RegQueryValueEx"); |
| 1521 | } |
| 1522 | obData = Reg2Py(retBuf, bufSize, typ); |
| 1523 | PyMem_Free(retBuf); |
| 1524 | if (obData == NULL) |
| 1525 | return NULL; |
| 1526 | result = Py_BuildValue("Oi", obData, typ); |
| 1527 | Py_DECREF(obData); |
| 1528 | return result; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1531 | /*[clinic input] |
| 1532 | winreg.SaveKey |
| 1533 | |
| 1534 | key: HKEY |
| 1535 | An already open key, or any one of the predefined HKEY_* constants. |
| 1536 | file_name: Py_UNICODE |
| 1537 | The name of the file to save registry data to. This file cannot |
| 1538 | already exist. If this filename includes an extension, it cannot be |
| 1539 | used on file allocation table (FAT) file systems by the LoadKey(), |
| 1540 | ReplaceKey() or RestoreKey() methods. |
| 1541 | / |
| 1542 | |
| 1543 | Saves the specified key, and all its subkeys to the specified file. |
| 1544 | |
| 1545 | If key represents a key on a remote computer, the path described by |
| 1546 | file_name is relative to the remote computer. |
| 1547 | |
| 1548 | The caller of this method must possess the SeBackupPrivilege |
| 1549 | security privilege. This function passes NULL for security_attributes |
| 1550 | to the API. |
| 1551 | [clinic start generated code]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1552 | |
| 1553 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1554 | winreg_SaveKey_impl(PyObject *module, HKEY key, const Py_UNICODE *file_name) |
| 1555 | /*[clinic end generated code: output=ca94b835c88f112b input=da735241f91ac7a2]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1556 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1557 | LPSECURITY_ATTRIBUTES pSA = NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1558 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1559 | long rc; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1560 | /* One day we may get security into the core? |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1561 | if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE)) |
| 1562 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1563 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1564 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1565 | rc = RegSaveKeyW(key, file_name, pSA ); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1566 | Py_END_ALLOW_THREADS |
| 1567 | if (rc != ERROR_SUCCESS) |
| 1568 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey"); |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1569 | Py_RETURN_NONE; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1572 | /*[clinic input] |
| 1573 | winreg.SetValue |
| 1574 | |
| 1575 | key: HKEY |
| 1576 | An already open key, or any one of the predefined HKEY_* constants. |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1577 | sub_key: Py_UNICODE(accept={str, NoneType}) |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1578 | A string that names the subkey with which the value is associated. |
| 1579 | type: DWORD |
| 1580 | An integer that specifies the type of the data. Currently this must |
| 1581 | be REG_SZ, meaning only strings are supported. |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1582 | value: Py_UNICODE(zeroes=True) |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1583 | A string that specifies the new value. |
| 1584 | / |
| 1585 | |
| 1586 | Associates a value with a specified key. |
| 1587 | |
| 1588 | If the key specified by the sub_key parameter does not exist, the |
| 1589 | SetValue function creates it. |
| 1590 | |
| 1591 | Value lengths are limited by available memory. Long values (more than |
| 1592 | 2048 bytes) should be stored as files with the filenames stored in |
| 1593 | the configuration registry to help the registry perform efficiently. |
| 1594 | |
| 1595 | The key identified by the key parameter must have been opened with |
| 1596 | KEY_SET_VALUE access. |
| 1597 | [clinic start generated code]*/ |
| 1598 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1599 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1600 | winreg_SetValue_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key, |
| 1601 | DWORD type, const Py_UNICODE *value, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1602 | Py_ssize_clean_t value_length) |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1603 | /*[clinic end generated code: output=686bedb1cbb4367b input=2cd2adab79339c53]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1604 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1605 | long rc; |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1606 | |
| 1607 | if (type != REG_SZ) { |
Inada Naoki | cc60cdd9 | 2019-03-20 20:53:08 +0900 | [diff] [blame] | 1608 | PyErr_SetString(PyExc_TypeError, "type must be winreg.REG_SZ"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1609 | return NULL; |
| 1610 | } |
Inada Naoki | cc60cdd9 | 2019-03-20 20:53:08 +0900 | [diff] [blame] | 1611 | if ((size_t)value_length >= PY_DWORD_MAX) { |
| 1612 | PyErr_SetString(PyExc_OverflowError, "value is too long"); |
Inada Naoki | d5f18a6 | 2019-03-20 19:10:17 +0900 | [diff] [blame] | 1613 | return NULL; |
| 1614 | } |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1615 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1616 | Py_BEGIN_ALLOW_THREADS |
Zackery Spytz | 34366b7 | 2019-04-22 11:08:05 -0600 | [diff] [blame] | 1617 | rc = RegSetValueW(key, sub_key, REG_SZ, value, (DWORD)(value_length + 1)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1618 | Py_END_ALLOW_THREADS |
| 1619 | if (rc != ERROR_SUCCESS) |
| 1620 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue"); |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1621 | Py_RETURN_NONE; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1624 | /*[clinic input] |
| 1625 | winreg.SetValueEx |
| 1626 | |
| 1627 | key: HKEY |
| 1628 | An already open key, or any one of the predefined HKEY_* constants. |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1629 | value_name: Py_UNICODE(accept={str, NoneType}) |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1630 | A string containing the name of the value to set, or None. |
| 1631 | reserved: object |
| 1632 | Can be anything - zero is always passed to the API. |
| 1633 | type: DWORD |
| 1634 | An integer that specifies the type of the data, one of: |
| 1635 | REG_BINARY -- Binary data in any form. |
| 1636 | REG_DWORD -- A 32-bit number. |
Steve Dower | 80ac11d | 2016-05-24 15:42:04 -0700 | [diff] [blame] | 1637 | REG_DWORD_LITTLE_ENDIAN -- A 32-bit number in little-endian format. Equivalent to REG_DWORD |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1638 | REG_DWORD_BIG_ENDIAN -- A 32-bit number in big-endian format. |
| 1639 | REG_EXPAND_SZ -- A null-terminated string that contains unexpanded |
| 1640 | references to environment variables (for example, |
| 1641 | %PATH%). |
| 1642 | REG_LINK -- A Unicode symbolic link. |
Serhiy Storchaka | 6a7b3a7 | 2016-04-17 08:32:47 +0300 | [diff] [blame] | 1643 | REG_MULTI_SZ -- A sequence of null-terminated strings, terminated |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1644 | by two null characters. Note that Python handles |
| 1645 | this termination automatically. |
| 1646 | REG_NONE -- No defined value type. |
Steve Dower | 80ac11d | 2016-05-24 15:42:04 -0700 | [diff] [blame] | 1647 | REG_QWORD -- A 64-bit number. |
| 1648 | REG_QWORD_LITTLE_ENDIAN -- A 64-bit number in little-endian format. Equivalent to REG_QWORD. |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1649 | REG_RESOURCE_LIST -- A device-driver resource list. |
| 1650 | REG_SZ -- A null-terminated string. |
| 1651 | value: object |
| 1652 | A string that specifies the new value. |
| 1653 | / |
| 1654 | |
| 1655 | Stores data in the value field of an open registry key. |
| 1656 | |
| 1657 | This method can also set additional value and type information for the |
| 1658 | specified key. The key identified by the key parameter must have been |
| 1659 | opened with KEY_SET_VALUE access. |
| 1660 | |
| 1661 | To open the key, use the CreateKeyEx() or OpenKeyEx() methods. |
| 1662 | |
| 1663 | Value lengths are limited by available memory. Long values (more than |
| 1664 | 2048 bytes) should be stored as files with the filenames stored in |
| 1665 | the configuration registry to help the registry perform efficiently. |
| 1666 | [clinic start generated code]*/ |
| 1667 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1668 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1669 | winreg_SetValueEx_impl(PyObject *module, HKEY key, |
| 1670 | const Py_UNICODE *value_name, PyObject *reserved, |
| 1671 | DWORD type, PyObject *value) |
| 1672 | /*[clinic end generated code: output=811b769a66ae11b7 input=900a9e3990bfb196]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1673 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1674 | BYTE *data; |
| 1675 | DWORD len; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1676 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1677 | LONG rc; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1678 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1679 | if (!Py2Reg(value, type, &data, &len)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1680 | { |
| 1681 | if (!PyErr_Occurred()) |
| 1682 | PyErr_SetString(PyExc_ValueError, |
| 1683 | "Could not convert the data to the specified type."); |
| 1684 | return NULL; |
| 1685 | } |
| 1686 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1687 | rc = RegSetValueExW(key, value_name, 0, type, data, len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1688 | Py_END_ALLOW_THREADS |
| 1689 | PyMem_DEL(data); |
| 1690 | if (rc != ERROR_SUCCESS) |
| 1691 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1692 | "RegSetValueEx"); |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1693 | Py_RETURN_NONE; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1696 | /*[clinic input] |
| 1697 | winreg.DisableReflectionKey |
| 1698 | |
| 1699 | key: HKEY |
| 1700 | An already open key, or any one of the predefined HKEY_* constants. |
| 1701 | / |
| 1702 | |
| 1703 | Disables registry reflection for 32bit processes running on a 64bit OS. |
| 1704 | |
| 1705 | Will generally raise NotImplemented if executed on a 32bit OS. |
| 1706 | |
| 1707 | If the key is not on the reflection list, the function succeeds but has |
| 1708 | no effect. Disabling reflection for a key does not affect reflection |
| 1709 | of any subkeys. |
| 1710 | [clinic start generated code]*/ |
| 1711 | |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1712 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1713 | winreg_DisableReflectionKey_impl(PyObject *module, HKEY key) |
| 1714 | /*[clinic end generated code: output=830cce504cc764b4 input=a6c9e5ca5410193c]*/ |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1715 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1716 | HMODULE hMod; |
| 1717 | typedef LONG (WINAPI *RDRKFunc)(HKEY); |
| 1718 | RDRKFunc pfn = NULL; |
| 1719 | LONG rc; |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1720 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1721 | /* Only available on 64bit platforms, so we must load it |
| 1722 | dynamically.*/ |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 1723 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 1724 | hMod = GetModuleHandleW(L"advapi32.dll"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1725 | if (hMod) |
| 1726 | pfn = (RDRKFunc)GetProcAddress(hMod, |
| 1727 | "RegDisableReflectionKey"); |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 1728 | Py_END_ALLOW_THREADS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1729 | if (!pfn) { |
| 1730 | PyErr_SetString(PyExc_NotImplementedError, |
| 1731 | "not implemented on this platform"); |
| 1732 | return NULL; |
| 1733 | } |
| 1734 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1735 | rc = (*pfn)(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1736 | Py_END_ALLOW_THREADS |
| 1737 | if (rc != ERROR_SUCCESS) |
| 1738 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1739 | "RegDisableReflectionKey"); |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1740 | Py_RETURN_NONE; |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1743 | /*[clinic input] |
| 1744 | winreg.EnableReflectionKey |
| 1745 | |
| 1746 | key: HKEY |
| 1747 | An already open key, or any one of the predefined HKEY_* constants. |
| 1748 | / |
| 1749 | |
| 1750 | Restores registry reflection for the specified disabled key. |
| 1751 | |
| 1752 | Will generally raise NotImplemented if executed on a 32bit OS. |
| 1753 | Restoring reflection for a key does not affect reflection of any |
| 1754 | subkeys. |
| 1755 | [clinic start generated code]*/ |
| 1756 | |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1757 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1758 | winreg_EnableReflectionKey_impl(PyObject *module, HKEY key) |
| 1759 | /*[clinic end generated code: output=86fa1385fdd9ce57 input=7748abbacd1e166a]*/ |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1760 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1761 | HMODULE hMod; |
| 1762 | typedef LONG (WINAPI *RERKFunc)(HKEY); |
| 1763 | RERKFunc pfn = NULL; |
| 1764 | LONG rc; |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1765 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1766 | /* Only available on 64bit platforms, so we must load it |
| 1767 | dynamically.*/ |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 1768 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 1769 | hMod = GetModuleHandleW(L"advapi32.dll"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1770 | if (hMod) |
| 1771 | pfn = (RERKFunc)GetProcAddress(hMod, |
| 1772 | "RegEnableReflectionKey"); |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 1773 | Py_END_ALLOW_THREADS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1774 | if (!pfn) { |
| 1775 | PyErr_SetString(PyExc_NotImplementedError, |
| 1776 | "not implemented on this platform"); |
| 1777 | return NULL; |
| 1778 | } |
| 1779 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1780 | rc = (*pfn)(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1781 | Py_END_ALLOW_THREADS |
| 1782 | if (rc != ERROR_SUCCESS) |
| 1783 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1784 | "RegEnableReflectionKey"); |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1785 | Py_RETURN_NONE; |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1786 | } |
| 1787 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1788 | /*[clinic input] |
| 1789 | winreg.QueryReflectionKey |
| 1790 | |
| 1791 | key: HKEY |
| 1792 | An already open key, or any one of the predefined HKEY_* constants. |
| 1793 | / |
| 1794 | |
| 1795 | Returns the reflection state for the specified key as a bool. |
| 1796 | |
| 1797 | Will generally raise NotImplemented if executed on a 32bit OS. |
| 1798 | [clinic start generated code]*/ |
| 1799 | |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1800 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1801 | winreg_QueryReflectionKey_impl(PyObject *module, HKEY key) |
| 1802 | /*[clinic end generated code: output=4e774af288c3ebb9 input=9f325eacb5a65d88]*/ |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1803 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1804 | HMODULE hMod; |
| 1805 | typedef LONG (WINAPI *RQRKFunc)(HKEY, BOOL *); |
| 1806 | RQRKFunc pfn = NULL; |
| 1807 | BOOL result; |
| 1808 | LONG rc; |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1809 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1810 | /* Only available on 64bit platforms, so we must load it |
| 1811 | dynamically.*/ |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 1812 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 1813 | hMod = GetModuleHandleW(L"advapi32.dll"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1814 | if (hMod) |
| 1815 | pfn = (RQRKFunc)GetProcAddress(hMod, |
| 1816 | "RegQueryReflectionKey"); |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 1817 | Py_END_ALLOW_THREADS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1818 | if (!pfn) { |
| 1819 | PyErr_SetString(PyExc_NotImplementedError, |
| 1820 | "not implemented on this platform"); |
| 1821 | return NULL; |
| 1822 | } |
| 1823 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1824 | rc = (*pfn)(key, &result); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1825 | Py_END_ALLOW_THREADS |
| 1826 | if (rc != ERROR_SUCCESS) |
| 1827 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1828 | "RegQueryReflectionKey"); |
| 1829 | return PyBool_FromLong(result); |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1830 | } |
| 1831 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1832 | static struct PyMethodDef winreg_methods[] = { |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1833 | WINREG_CLOSEKEY_METHODDEF |
| 1834 | WINREG_CONNECTREGISTRY_METHODDEF |
| 1835 | WINREG_CREATEKEY_METHODDEF |
| 1836 | WINREG_CREATEKEYEX_METHODDEF |
| 1837 | WINREG_DELETEKEY_METHODDEF |
| 1838 | WINREG_DELETEKEYEX_METHODDEF |
| 1839 | WINREG_DELETEVALUE_METHODDEF |
| 1840 | WINREG_DISABLEREFLECTIONKEY_METHODDEF |
| 1841 | WINREG_ENABLEREFLECTIONKEY_METHODDEF |
| 1842 | WINREG_ENUMKEY_METHODDEF |
| 1843 | WINREG_ENUMVALUE_METHODDEF |
| 1844 | WINREG_EXPANDENVIRONMENTSTRINGS_METHODDEF |
| 1845 | WINREG_FLUSHKEY_METHODDEF |
| 1846 | WINREG_LOADKEY_METHODDEF |
| 1847 | WINREG_OPENKEY_METHODDEF |
| 1848 | WINREG_OPENKEYEX_METHODDEF |
| 1849 | WINREG_QUERYVALUE_METHODDEF |
| 1850 | WINREG_QUERYVALUEEX_METHODDEF |
| 1851 | WINREG_QUERYINFOKEY_METHODDEF |
| 1852 | WINREG_QUERYREFLECTIONKEY_METHODDEF |
| 1853 | WINREG_SAVEKEY_METHODDEF |
| 1854 | WINREG_SETVALUE_METHODDEF |
| 1855 | WINREG_SETVALUEEX_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1856 | NULL, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1857 | }; |
| 1858 | |
| 1859 | static void |
| 1860 | insint(PyObject * d, char * name, long value) |
| 1861 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1862 | PyObject *v = PyLong_FromLong(value); |
| 1863 | if (!v || PyDict_SetItemString(d, name, v)) |
| 1864 | PyErr_Clear(); |
| 1865 | Py_XDECREF(v); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1866 | } |
| 1867 | |
| 1868 | #define ADD_INT(val) insint(d, #val, val) |
| 1869 | |
| 1870 | static void |
| 1871 | inskey(PyObject * d, char * name, HKEY key) |
| 1872 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1873 | PyObject *v = PyLong_FromVoidPtr(key); |
| 1874 | if (!v || PyDict_SetItemString(d, name, v)) |
| 1875 | PyErr_Clear(); |
| 1876 | Py_XDECREF(v); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | #define ADD_KEY(val) inskey(d, #val, val) |
| 1880 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1881 | |
| 1882 | static struct PyModuleDef winregmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1883 | PyModuleDef_HEAD_INIT, |
| 1884 | "winreg", |
| 1885 | module_doc, |
| 1886 | -1, |
| 1887 | winreg_methods, |
| 1888 | NULL, |
| 1889 | NULL, |
| 1890 | NULL, |
| 1891 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1892 | }; |
| 1893 | |
| 1894 | PyMODINIT_FUNC PyInit_winreg(void) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1895 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1896 | PyObject *m, *d; |
| 1897 | m = PyModule_Create(&winregmodule); |
| 1898 | if (m == NULL) |
| 1899 | return NULL; |
| 1900 | d = PyModule_GetDict(m); |
| 1901 | PyHKEY_Type.tp_doc = PyHKEY_doc; |
| 1902 | if (PyType_Ready(&PyHKEY_Type) < 0) |
| 1903 | return NULL; |
| 1904 | Py_INCREF(&PyHKEY_Type); |
| 1905 | if (PyDict_SetItemString(d, "HKEYType", |
| 1906 | (PyObject *)&PyHKEY_Type) != 0) |
| 1907 | return NULL; |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1908 | Py_INCREF(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1909 | if (PyDict_SetItemString(d, "error", |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1910 | PyExc_OSError) != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1911 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1912 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1913 | /* Add the relevant constants */ |
| 1914 | ADD_KEY(HKEY_CLASSES_ROOT); |
| 1915 | ADD_KEY(HKEY_CURRENT_USER); |
| 1916 | ADD_KEY(HKEY_LOCAL_MACHINE); |
| 1917 | ADD_KEY(HKEY_USERS); |
| 1918 | ADD_KEY(HKEY_PERFORMANCE_DATA); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1919 | #ifdef HKEY_CURRENT_CONFIG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1920 | ADD_KEY(HKEY_CURRENT_CONFIG); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1921 | #endif |
| 1922 | #ifdef HKEY_DYN_DATA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1923 | ADD_KEY(HKEY_DYN_DATA); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1924 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1925 | ADD_INT(KEY_QUERY_VALUE); |
| 1926 | ADD_INT(KEY_SET_VALUE); |
| 1927 | ADD_INT(KEY_CREATE_SUB_KEY); |
| 1928 | ADD_INT(KEY_ENUMERATE_SUB_KEYS); |
| 1929 | ADD_INT(KEY_NOTIFY); |
| 1930 | ADD_INT(KEY_CREATE_LINK); |
| 1931 | ADD_INT(KEY_READ); |
| 1932 | ADD_INT(KEY_WRITE); |
| 1933 | ADD_INT(KEY_EXECUTE); |
| 1934 | ADD_INT(KEY_ALL_ACCESS); |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1935 | #ifdef KEY_WOW64_64KEY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1936 | ADD_INT(KEY_WOW64_64KEY); |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1937 | #endif |
| 1938 | #ifdef KEY_WOW64_32KEY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1939 | ADD_INT(KEY_WOW64_32KEY); |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1940 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1941 | ADD_INT(REG_OPTION_RESERVED); |
| 1942 | ADD_INT(REG_OPTION_NON_VOLATILE); |
| 1943 | ADD_INT(REG_OPTION_VOLATILE); |
| 1944 | ADD_INT(REG_OPTION_CREATE_LINK); |
| 1945 | ADD_INT(REG_OPTION_BACKUP_RESTORE); |
| 1946 | ADD_INT(REG_OPTION_OPEN_LINK); |
| 1947 | ADD_INT(REG_LEGAL_OPTION); |
| 1948 | ADD_INT(REG_CREATED_NEW_KEY); |
| 1949 | ADD_INT(REG_OPENED_EXISTING_KEY); |
| 1950 | ADD_INT(REG_WHOLE_HIVE_VOLATILE); |
| 1951 | ADD_INT(REG_REFRESH_HIVE); |
| 1952 | ADD_INT(REG_NO_LAZY_FLUSH); |
| 1953 | ADD_INT(REG_NOTIFY_CHANGE_NAME); |
| 1954 | ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES); |
| 1955 | ADD_INT(REG_NOTIFY_CHANGE_LAST_SET); |
| 1956 | ADD_INT(REG_NOTIFY_CHANGE_SECURITY); |
| 1957 | ADD_INT(REG_LEGAL_CHANGE_FILTER); |
| 1958 | ADD_INT(REG_NONE); |
| 1959 | ADD_INT(REG_SZ); |
| 1960 | ADD_INT(REG_EXPAND_SZ); |
| 1961 | ADD_INT(REG_BINARY); |
| 1962 | ADD_INT(REG_DWORD); |
| 1963 | ADD_INT(REG_DWORD_LITTLE_ENDIAN); |
| 1964 | ADD_INT(REG_DWORD_BIG_ENDIAN); |
Steve Dower | 80ac11d | 2016-05-24 15:42:04 -0700 | [diff] [blame] | 1965 | ADD_INT(REG_QWORD); |
| 1966 | ADD_INT(REG_QWORD_LITTLE_ENDIAN); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1967 | ADD_INT(REG_LINK); |
| 1968 | ADD_INT(REG_MULTI_SZ); |
| 1969 | ADD_INT(REG_RESOURCE_LIST); |
| 1970 | ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR); |
| 1971 | ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST); |
| 1972 | return m; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1975 | |