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 | |
Miss Islington (bot) | ebca7eb | 2019-09-09 03:11:00 -0700 | [diff] [blame] | 521 | if (len > 0 && data[len - 1] == '\0') { |
| 522 | Q = data + len - 1; |
| 523 | } |
| 524 | else { |
| 525 | Q = data + len; |
| 526 | } |
| 527 | |
| 528 | for (P = data, i = 0; P < Q; P++, i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 529 | str[i] = P; |
Miss Islington (bot) | ebca7eb | 2019-09-09 03:11:00 -0700 | [diff] [blame] | 530 | for (; P < Q && *P != '\0'; P++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 531 | ; |
Miss Islington (bot) | ebca7eb | 2019-09-09 03:11:00 -0700 | [diff] [blame] | 532 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 533 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | static int |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 537 | countStrings(wchar_t *data, int len) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 538 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 539 | int strings; |
Miss Islington (bot) | ebca7eb | 2019-09-09 03:11:00 -0700 | [diff] [blame] | 540 | wchar_t *P, *Q; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 541 | |
Miss Islington (bot) | ebca7eb | 2019-09-09 03:11:00 -0700 | [diff] [blame] | 542 | if (len > 0 && data[len - 1] == '\0') { |
| 543 | Q = data + len - 1; |
| 544 | } |
| 545 | else { |
| 546 | Q = data + len; |
| 547 | } |
| 548 | |
| 549 | for (P = data, strings = 0; P < Q; P++, strings++) { |
| 550 | for (; P < Q && *P != '\0'; P++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 551 | ; |
Miss Islington (bot) | ebca7eb | 2019-09-09 03:11:00 -0700 | [diff] [blame] | 552 | } |
| 553 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 554 | return strings; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | /* Convert PyObject into Registry data. |
| 558 | Allocates space as needed. */ |
| 559 | static BOOL |
| 560 | Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize) |
| 561 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 562 | Py_ssize_t i,j; |
| 563 | switch (typ) { |
| 564 | case REG_DWORD: |
| 565 | if (value != Py_None && !PyLong_Check(value)) |
| 566 | return FALSE; |
| 567 | *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1); |
Steve Dower | 4d4bc42 | 2016-05-25 11:26:07 -0700 | [diff] [blame] | 568 | if (*retDataBuf == NULL){ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 569 | PyErr_NoMemory(); |
| 570 | return FALSE; |
| 571 | } |
| 572 | *retDataSize = sizeof(DWORD); |
| 573 | if (value == Py_None) { |
| 574 | DWORD zero = 0; |
| 575 | memcpy(*retDataBuf, &zero, sizeof(DWORD)); |
| 576 | } |
| 577 | else { |
Brian Curtin | 12706f2 | 2012-12-27 10:12:45 -0600 | [diff] [blame] | 578 | DWORD d = PyLong_AsUnsignedLong(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 579 | memcpy(*retDataBuf, &d, sizeof(DWORD)); |
| 580 | } |
| 581 | break; |
Steve Dower | 80ac11d | 2016-05-24 15:42:04 -0700 | [diff] [blame] | 582 | case REG_QWORD: |
| 583 | if (value != Py_None && !PyLong_Check(value)) |
| 584 | return FALSE; |
| 585 | *retDataBuf = (BYTE *)PyMem_NEW(DWORD64, 1); |
Steve Dower | 4d4bc42 | 2016-05-25 11:26:07 -0700 | [diff] [blame] | 586 | if (*retDataBuf == NULL){ |
Steve Dower | 80ac11d | 2016-05-24 15:42:04 -0700 | [diff] [blame] | 587 | PyErr_NoMemory(); |
| 588 | return FALSE; |
| 589 | } |
| 590 | *retDataSize = sizeof(DWORD64); |
| 591 | if (value == Py_None) { |
| 592 | DWORD64 zero = 0; |
| 593 | memcpy(*retDataBuf, &zero, sizeof(DWORD64)); |
| 594 | } |
| 595 | else { |
| 596 | DWORD64 d = PyLong_AsUnsignedLongLong(value); |
| 597 | memcpy(*retDataBuf, &d, sizeof(DWORD64)); |
| 598 | } |
| 599 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 600 | case REG_SZ: |
| 601 | case REG_EXPAND_SZ: |
| 602 | { |
Victor Stinner | be49244 | 2011-11-21 12:43:50 +0100 | [diff] [blame] | 603 | if (value != Py_None) { |
| 604 | Py_ssize_t len; |
| 605 | if (!PyUnicode_Check(value)) |
| 606 | return FALSE; |
| 607 | *retDataBuf = (BYTE*)PyUnicode_AsWideCharString(value, &len); |
| 608 | if (*retDataBuf == NULL) |
| 609 | return FALSE; |
| 610 | *retDataSize = Py_SAFE_DOWNCAST( |
| 611 | (len + 1) * sizeof(wchar_t), |
| 612 | Py_ssize_t, DWORD); |
| 613 | } |
| 614 | else { |
| 615 | *retDataBuf = (BYTE *)PyMem_NEW(wchar_t, 1); |
| 616 | if (*retDataBuf == NULL) { |
| 617 | PyErr_NoMemory(); |
| 618 | return FALSE; |
| 619 | } |
| 620 | ((wchar_t *)*retDataBuf)[0] = L'\0'; |
| 621 | *retDataSize = 1 * sizeof(wchar_t); |
| 622 | } |
| 623 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 624 | } |
| 625 | case REG_MULTI_SZ: |
| 626 | { |
| 627 | DWORD size = 0; |
| 628 | wchar_t *P; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 629 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 630 | if (value == Py_None) |
| 631 | i = 0; |
| 632 | else { |
| 633 | if (!PyList_Check(value)) |
| 634 | return FALSE; |
| 635 | i = PyList_Size(value); |
| 636 | } |
| 637 | for (j = 0; j < i; j++) |
| 638 | { |
| 639 | PyObject *t; |
Victor Stinner | be49244 | 2011-11-21 12:43:50 +0100 | [diff] [blame] | 640 | wchar_t *wstr; |
| 641 | Py_ssize_t len; |
| 642 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 643 | t = PyList_GET_ITEM(value, j); |
| 644 | if (!PyUnicode_Check(t)) |
| 645 | return FALSE; |
Victor Stinner | be49244 | 2011-11-21 12:43:50 +0100 | [diff] [blame] | 646 | wstr = PyUnicode_AsUnicodeAndSize(t, &len); |
| 647 | if (wstr == NULL) |
| 648 | return FALSE; |
| 649 | size += Py_SAFE_DOWNCAST((len + 1) * sizeof(wchar_t), |
Brian Curtin | abb3351 | 2010-08-17 20:08:40 +0000 | [diff] [blame] | 650 | size_t, DWORD); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 651 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 652 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 653 | *retDataSize = size + 2; |
| 654 | *retDataBuf = (BYTE *)PyMem_NEW(char, |
| 655 | *retDataSize); |
Steve Dower | 4d4bc42 | 2016-05-25 11:26:07 -0700 | [diff] [blame] | 656 | if (*retDataBuf == NULL){ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 657 | PyErr_NoMemory(); |
| 658 | return FALSE; |
| 659 | } |
| 660 | P = (wchar_t *)*retDataBuf; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 661 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 662 | for (j = 0; j < i; j++) |
| 663 | { |
| 664 | PyObject *t; |
Victor Stinner | be49244 | 2011-11-21 12:43:50 +0100 | [diff] [blame] | 665 | wchar_t *wstr; |
| 666 | Py_ssize_t len; |
| 667 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 668 | t = PyList_GET_ITEM(value, j); |
Victor Stinner | be49244 | 2011-11-21 12:43:50 +0100 | [diff] [blame] | 669 | wstr = PyUnicode_AsUnicodeAndSize(t, &len); |
Zackery Spytz | 5d95312 | 2018-11-08 02:45:00 -0700 | [diff] [blame] | 670 | assert(wstr); |
Victor Stinner | be49244 | 2011-11-21 12:43:50 +0100 | [diff] [blame] | 671 | wcscpy(P, wstr); |
| 672 | P += (len + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 673 | } |
| 674 | /* And doubly-terminate the list... */ |
| 675 | *P = '\0'; |
| 676 | break; |
| 677 | } |
| 678 | case REG_BINARY: |
| 679 | /* ALSO handle ALL unknown data types here. Even if we can't |
| 680 | support it natively, we should handle the bits. */ |
| 681 | default: |
Zachary Ware | ad4690f | 2014-07-03 10:58:06 -0500 | [diff] [blame] | 682 | if (value == Py_None) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 683 | *retDataSize = 0; |
Zachary Ware | ad4690f | 2014-07-03 10:58:06 -0500 | [diff] [blame] | 684 | *retDataBuf = NULL; |
| 685 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 686 | else { |
| 687 | Py_buffer view; |
Neal Norwitz | 1385b89 | 2007-08-26 23:07:13 +0000 | [diff] [blame] | 688 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 689 | if (!PyObject_CheckBuffer(value)) { |
| 690 | PyErr_Format(PyExc_TypeError, |
| 691 | "Objects of type '%s' can not " |
| 692 | "be used as binary registry values", |
| 693 | value->ob_type->tp_name); |
| 694 | return FALSE; |
| 695 | } |
Neal Norwitz | 1385b89 | 2007-08-26 23:07:13 +0000 | [diff] [blame] | 696 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 697 | if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0) |
| 698 | return FALSE; |
Neal Norwitz | 1385b89 | 2007-08-26 23:07:13 +0000 | [diff] [blame] | 699 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 700 | *retDataBuf = (BYTE *)PyMem_NEW(char, view.len); |
Steve Dower | 4d4bc42 | 2016-05-25 11:26:07 -0700 | [diff] [blame] | 701 | if (*retDataBuf == NULL){ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 702 | PyBuffer_Release(&view); |
| 703 | PyErr_NoMemory(); |
| 704 | return FALSE; |
| 705 | } |
Brian Curtin | abb3351 | 2010-08-17 20:08:40 +0000 | [diff] [blame] | 706 | *retDataSize = Py_SAFE_DOWNCAST(view.len, Py_ssize_t, DWORD); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 707 | memcpy(*retDataBuf, view.buf, view.len); |
| 708 | PyBuffer_Release(&view); |
| 709 | } |
| 710 | break; |
| 711 | } |
| 712 | return TRUE; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | /* Convert Registry data into PyObject*/ |
| 716 | static PyObject * |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 717 | Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 718 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 719 | PyObject *obData; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 720 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 721 | switch (typ) { |
| 722 | case REG_DWORD: |
| 723 | if (retDataSize == 0) |
Brian Curtin | 172e422 | 2012-12-27 14:04:42 -0600 | [diff] [blame] | 724 | obData = PyLong_FromUnsignedLong(0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 725 | else |
Steve Dower | 80ac11d | 2016-05-24 15:42:04 -0700 | [diff] [blame] | 726 | obData = PyLong_FromUnsignedLong(*(DWORD *)retDataBuf); |
| 727 | break; |
| 728 | case REG_QWORD: |
| 729 | if (retDataSize == 0) |
| 730 | obData = PyLong_FromUnsignedLongLong(0); |
| 731 | else |
| 732 | obData = PyLong_FromUnsignedLongLong(*(DWORD64 *)retDataBuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 733 | break; |
| 734 | case REG_SZ: |
| 735 | case REG_EXPAND_SZ: |
| 736 | { |
Steve Dower | 40fa266 | 2016-12-17 13:30:27 -0800 | [diff] [blame] | 737 | /* REG_SZ should be a NUL terminated string, but only by |
| 738 | * convention. The buffer may have been saved without a NUL |
| 739 | * or with embedded NULs. To be consistent with reg.exe and |
| 740 | * regedit.exe, consume only up to the first NUL. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 741 | wchar_t *data = (wchar_t *)retDataBuf; |
Steve Dower | 40fa266 | 2016-12-17 13:30:27 -0800 | [diff] [blame] | 742 | size_t len = wcsnlen(data, retDataSize / sizeof(wchar_t)); |
| 743 | obData = PyUnicode_FromWideChar(data, len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 744 | break; |
| 745 | } |
| 746 | case REG_MULTI_SZ: |
| 747 | if (retDataSize == 0) |
| 748 | obData = PyList_New(0); |
| 749 | else |
| 750 | { |
| 751 | int index = 0; |
| 752 | wchar_t *data = (wchar_t *)retDataBuf; |
| 753 | int len = retDataSize / 2; |
| 754 | int s = countStrings(data, len); |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 755 | wchar_t **str = PyMem_New(wchar_t *, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 756 | if (str == NULL) |
| 757 | return PyErr_NoMemory(); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 758 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 759 | fixupMultiSZ(str, data, len); |
| 760 | obData = PyList_New(s); |
Jesus Cea | 782c4cf | 2014-03-13 17:35:32 +0100 | [diff] [blame] | 761 | if (obData == NULL) { |
Victor Stinner | 9cb1ec5 | 2014-03-13 19:08:10 +0100 | [diff] [blame] | 762 | PyMem_Free(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 763 | return NULL; |
Jesus Cea | 782c4cf | 2014-03-13 17:35:32 +0100 | [diff] [blame] | 764 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 765 | for (index = 0; index < s; index++) |
| 766 | { |
Miss Islington (bot) | ebca7eb | 2019-09-09 03:11:00 -0700 | [diff] [blame] | 767 | size_t slen = wcsnlen(str[index], len); |
| 768 | PyObject *uni = PyUnicode_FromWideChar(str[index], slen); |
Zackery Spytz | 99d56b5 | 2018-12-08 07:16:55 -0700 | [diff] [blame] | 769 | if (uni == NULL) { |
| 770 | Py_DECREF(obData); |
| 771 | PyMem_Free(str); |
| 772 | return NULL; |
| 773 | } |
| 774 | PyList_SET_ITEM(obData, index, uni); |
Miss Islington (bot) | 44729c9 | 2019-09-09 07:23:37 -0700 | [diff] [blame] | 775 | len -= Py_SAFE_DOWNCAST(slen + 1, size_t, int); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 776 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 777 | PyMem_Free(str); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 778 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 779 | break; |
| 780 | } |
| 781 | case REG_BINARY: |
| 782 | /* ALSO handle ALL unknown data types here. Even if we can't |
| 783 | support it natively, we should handle the bits. */ |
| 784 | default: |
| 785 | if (retDataSize == 0) { |
| 786 | Py_INCREF(Py_None); |
| 787 | obData = Py_None; |
| 788 | } |
| 789 | else |
| 790 | obData = PyBytes_FromStringAndSize( |
| 791 | (char *)retDataBuf, retDataSize); |
| 792 | break; |
| 793 | } |
| 794 | return obData; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | /* The Python methods */ |
| 798 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 799 | /*[clinic input] |
| 800 | winreg.CloseKey |
| 801 | |
| 802 | hkey: object |
| 803 | A previously opened key. |
| 804 | / |
| 805 | |
| 806 | Closes a previously opened registry key. |
| 807 | |
| 808 | Note that if the key is not closed using this method, it will be |
| 809 | closed when the hkey object is destroyed by Python. |
| 810 | [clinic start generated code]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 811 | |
| 812 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 813 | winreg_CloseKey(PyObject *module, PyObject *hkey) |
| 814 | /*[clinic end generated code: output=a4fa537019a80d15 input=5b1aac65ba5127ad]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 815 | { |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 816 | if (!PyHKEY_Close(hkey)) |
| 817 | return NULL; |
| 818 | Py_RETURN_NONE; |
| 819 | } |
| 820 | |
| 821 | /*[clinic input] |
| 822 | winreg.ConnectRegistry -> HKEY |
| 823 | |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 824 | computer_name: Py_UNICODE(accept={str, NoneType}) |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 825 | The name of the remote computer, of the form r"\\computername". If |
| 826 | None, the local computer is used. |
| 827 | key: HKEY |
| 828 | The predefined key to connect to. |
| 829 | / |
| 830 | |
Serhiy Storchaka | 6a7b3a7 | 2016-04-17 08:32:47 +0300 | [diff] [blame] | 831 | Establishes a connection to the registry on another computer. |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 832 | |
| 833 | The return value is the handle of the opened key. |
| 834 | If the function fails, an OSError exception is raised. |
| 835 | [clinic start generated code]*/ |
| 836 | |
| 837 | static HKEY |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 838 | winreg_ConnectRegistry_impl(PyObject *module, |
| 839 | const Py_UNICODE *computer_name, HKEY key) |
| 840 | /*[clinic end generated code: output=cd4f70fb9ec901fb input=5f98a891a347e68e]*/ |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 841 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 842 | HKEY retKey; |
| 843 | long rc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 844 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 845 | rc = RegConnectRegistryW(computer_name, key, &retKey); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 846 | Py_END_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 847 | if (rc != ERROR_SUCCESS) { |
| 848 | PyErr_SetFromWindowsErrWithFunction(rc, "ConnectRegistry"); |
| 849 | return NULL; |
| 850 | } |
| 851 | return retKey; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 854 | /*[clinic input] |
| 855 | winreg.CreateKey -> HKEY |
| 856 | |
| 857 | key: HKEY |
| 858 | An already open key, or one of the predefined HKEY_* constants. |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 859 | sub_key: Py_UNICODE(accept={str, NoneType}) |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 860 | The name of the key this method opens or creates. |
| 861 | / |
| 862 | |
| 863 | Creates or opens the specified key. |
| 864 | |
| 865 | If key is one of the predefined keys, sub_key may be None. In that case, |
| 866 | the handle returned is the same key handle passed in to the function. |
| 867 | |
| 868 | If the key already exists, this function opens the existing key. |
| 869 | |
| 870 | The return value is the handle of the opened key. |
| 871 | If the function fails, an OSError exception is raised. |
| 872 | [clinic start generated code]*/ |
| 873 | |
| 874 | static HKEY |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 875 | winreg_CreateKey_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key) |
| 876 | /*[clinic end generated code: output=2af13910d56eae26 input=3cdd1622488acea2]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 877 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 878 | HKEY retKey; |
| 879 | long rc; |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 880 | |
| 881 | rc = RegCreateKeyW(key, sub_key, &retKey); |
| 882 | if (rc != ERROR_SUCCESS) { |
| 883 | PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 884 | return NULL; |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 885 | } |
| 886 | return retKey; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 889 | /*[clinic input] |
| 890 | winreg.CreateKeyEx -> HKEY |
| 891 | |
| 892 | key: HKEY |
| 893 | An already open key, or one of the predefined HKEY_* constants. |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 894 | sub_key: Py_UNICODE(accept={str, NoneType}) |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 895 | The name of the key this method opens or creates. |
| 896 | reserved: int = 0 |
| 897 | A reserved integer, and must be zero. Default is zero. |
| 898 | access: REGSAM(c_default='KEY_WRITE') = winreg.KEY_WRITE |
| 899 | An integer that specifies an access mask that describes the |
| 900 | desired security access for the key. Default is KEY_WRITE. |
| 901 | |
| 902 | Creates or opens the specified key. |
| 903 | |
| 904 | If key is one of the predefined keys, sub_key may be None. In that case, |
| 905 | the handle returned is the same key handle passed in to the function. |
| 906 | |
| 907 | If the key already exists, this function opens the existing key |
| 908 | |
| 909 | The return value is the handle of the opened key. |
| 910 | If the function fails, an OSError exception is raised. |
| 911 | [clinic start generated code]*/ |
| 912 | |
| 913 | static HKEY |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 914 | winreg_CreateKeyEx_impl(PyObject *module, HKEY key, |
| 915 | const Py_UNICODE *sub_key, int reserved, |
| 916 | REGSAM access) |
| 917 | /*[clinic end generated code: output=643a70ad6a361a97 input=42c2b03f98406b66]*/ |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 918 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 919 | HKEY retKey; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 920 | long rc; |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 921 | |
Segev Finer | 679b566 | 2017-07-27 01:17:57 +0300 | [diff] [blame] | 922 | rc = RegCreateKeyExW(key, sub_key, reserved, NULL, 0, |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 923 | access, NULL, &retKey, NULL); |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 924 | if (rc != ERROR_SUCCESS) { |
| 925 | PyErr_SetFromWindowsErrWithFunction(rc, "CreateKeyEx"); |
| 926 | return NULL; |
| 927 | } |
| 928 | return retKey; |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 929 | } |
| 930 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 931 | /*[clinic input] |
| 932 | winreg.DeleteKey |
| 933 | key: HKEY |
| 934 | An already open key, or any one of the predefined HKEY_* constants. |
| 935 | sub_key: Py_UNICODE |
| 936 | A string that must be the name of a subkey of the key identified by |
| 937 | the key parameter. This value must not be None, and the key may not |
| 938 | have subkeys. |
| 939 | / |
| 940 | |
| 941 | Deletes the specified key. |
| 942 | |
| 943 | This method can not delete keys with subkeys. |
| 944 | |
| 945 | If the function succeeds, the entire key, including all of its values, |
| 946 | is removed. If the function fails, an OSError exception is raised. |
| 947 | [clinic start generated code]*/ |
| 948 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 949 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 950 | winreg_DeleteKey_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key) |
| 951 | /*[clinic end generated code: output=d2652a84f70e0862 input=b31d225b935e4211]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 952 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 953 | long rc; |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 954 | rc = RegDeleteKeyW(key, sub_key ); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 955 | if (rc != ERROR_SUCCESS) |
| 956 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey"); |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 957 | Py_RETURN_NONE; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 960 | /*[clinic input] |
| 961 | winreg.DeleteKeyEx |
| 962 | |
| 963 | key: HKEY |
| 964 | An already open key, or any one of the predefined HKEY_* constants. |
| 965 | sub_key: Py_UNICODE |
| 966 | A string that must be the name of a subkey of the key identified by |
| 967 | the key parameter. This value must not be None, and the key may not |
| 968 | have subkeys. |
| 969 | access: REGSAM(c_default='KEY_WOW64_64KEY') = winreg.KEY_WOW64_64KEY |
| 970 | An integer that specifies an access mask that describes the |
| 971 | desired security access for the key. Default is KEY_WOW64_64KEY. |
| 972 | reserved: int = 0 |
| 973 | A reserved integer, and must be zero. Default is zero. |
| 974 | |
| 975 | Deletes the specified key (64-bit OS only). |
| 976 | |
| 977 | This method can not delete keys with subkeys. |
| 978 | |
| 979 | If the function succeeds, the entire key, including all of its values, |
| 980 | is removed. If the function fails, an OSError exception is raised. |
| 981 | On unsupported Windows versions, NotImplementedError is raised. |
| 982 | [clinic start generated code]*/ |
| 983 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 984 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 985 | winreg_DeleteKeyEx_impl(PyObject *module, HKEY key, |
| 986 | const Py_UNICODE *sub_key, REGSAM access, |
| 987 | int reserved) |
| 988 | /*[clinic end generated code: output=52a1c8b374ebc003 input=711d9d89e7ecbed7]*/ |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 989 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 990 | HMODULE hMod; |
| 991 | typedef LONG (WINAPI *RDKEFunc)(HKEY, const wchar_t*, REGSAM, int); |
| 992 | RDKEFunc pfn = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 993 | long rc; |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 994 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 995 | /* Only available on 64bit platforms, so we must load it |
| 996 | dynamically. */ |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 997 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 998 | hMod = GetModuleHandleW(L"advapi32.dll"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 999 | if (hMod) |
| 1000 | pfn = (RDKEFunc)GetProcAddress(hMod, |
| 1001 | "RegDeleteKeyExW"); |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 1002 | Py_END_ALLOW_THREADS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1003 | if (!pfn) { |
| 1004 | PyErr_SetString(PyExc_NotImplementedError, |
| 1005 | "not implemented on this platform"); |
| 1006 | return NULL; |
| 1007 | } |
| 1008 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1009 | rc = (*pfn)(key, sub_key, access, reserved); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1010 | Py_END_ALLOW_THREADS |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 1011 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1012 | if (rc != ERROR_SUCCESS) |
| 1013 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKeyEx"); |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1014 | Py_RETURN_NONE; |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1017 | /*[clinic input] |
| 1018 | winreg.DeleteValue |
| 1019 | |
| 1020 | key: HKEY |
| 1021 | An already open key, or any one of the predefined HKEY_* constants. |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1022 | value: Py_UNICODE(accept={str, NoneType}) |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1023 | A string that identifies the value to remove. |
| 1024 | / |
| 1025 | |
| 1026 | Removes a named value from a registry key. |
| 1027 | [clinic start generated code]*/ |
| 1028 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 1029 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1030 | winreg_DeleteValue_impl(PyObject *module, HKEY key, const Py_UNICODE *value) |
| 1031 | /*[clinic end generated code: output=56fa9d21f3a54371 input=a78d3407a4197b21]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1032 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1033 | long rc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1034 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1035 | rc = RegDeleteValueW(key, value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1036 | Py_END_ALLOW_THREADS |
| 1037 | if (rc !=ERROR_SUCCESS) |
| 1038 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1039 | "RegDeleteValue"); |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1040 | Py_RETURN_NONE; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1043 | /*[clinic input] |
| 1044 | winreg.EnumKey |
| 1045 | |
| 1046 | key: HKEY |
| 1047 | An already open key, or any one of the predefined HKEY_* constants. |
| 1048 | index: int |
| 1049 | An integer that identifies the index of the key to retrieve. |
| 1050 | / |
| 1051 | |
| 1052 | Enumerates subkeys of an open registry key. |
| 1053 | |
| 1054 | The function retrieves the name of one subkey each time it is called. |
| 1055 | It is typically called repeatedly until an OSError exception is |
| 1056 | raised, indicating no more values are available. |
| 1057 | [clinic start generated code]*/ |
| 1058 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1059 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1060 | winreg_EnumKey_impl(PyObject *module, HKEY key, int index) |
| 1061 | /*[clinic end generated code: output=25a6ec52cd147bc4 input=fad9a7c00ab0e04b]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1062 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1063 | long rc; |
| 1064 | PyObject *retStr; |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1065 | |
| 1066 | /* The Windows docs claim that the max key name length is 255 |
| 1067 | * characters, plus a terminating nul character. However, |
| 1068 | * empirical testing demonstrates that it is possible to |
| 1069 | * create a 256 character key that is missing the terminating |
| 1070 | * nul. RegEnumKeyEx requires a 257 character buffer to |
| 1071 | * retrieve such a key name. */ |
| 1072 | wchar_t tmpbuf[257]; |
Kristján Valur Jónsson | 984dfa7 | 2012-04-02 15:23:29 +0000 | [diff] [blame] | 1073 | DWORD len = sizeof(tmpbuf)/sizeof(wchar_t); /* includes NULL terminator */ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1074 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1075 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1076 | rc = RegEnumKeyExW(key, index, tmpbuf, &len, NULL, NULL, NULL, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1077 | Py_END_ALLOW_THREADS |
| 1078 | if (rc != ERROR_SUCCESS) |
| 1079 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx"); |
Tim Peters | 313fcd4 | 2006-02-19 04:05:39 +0000 | [diff] [blame] | 1080 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1081 | retStr = PyUnicode_FromWideChar(tmpbuf, len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1082 | return retStr; /* can be NULL */ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1085 | /*[clinic input] |
| 1086 | winreg.EnumValue |
| 1087 | |
| 1088 | key: HKEY |
| 1089 | An already open key, or any one of the predefined HKEY_* constants. |
| 1090 | index: int |
| 1091 | An integer that identifies the index of the value to retrieve. |
| 1092 | / |
| 1093 | |
| 1094 | Enumerates values of an open registry key. |
| 1095 | |
| 1096 | The function retrieves the name of one subkey each time it is called. |
| 1097 | It is typically called repeatedly, until an OSError exception |
| 1098 | is raised, indicating no more values. |
| 1099 | |
| 1100 | The result is a tuple of 3 items: |
| 1101 | value_name |
| 1102 | A string that identifies the value. |
| 1103 | value_data |
| 1104 | An object that holds the value data, and whose type depends |
| 1105 | on the underlying registry type. |
| 1106 | data_type |
| 1107 | An integer that identifies the type of the value data. |
| 1108 | [clinic start generated code]*/ |
| 1109 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1110 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1111 | winreg_EnumValue_impl(PyObject *module, HKEY key, int index) |
| 1112 | /*[clinic end generated code: output=d363b5a06f8789ac input=4414f47a6fb238b5]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1113 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1114 | long rc; |
| 1115 | wchar_t *retValueBuf; |
Amaury Forgeot d'Arc | f2b69df | 2010-08-16 22:11:29 +0000 | [diff] [blame] | 1116 | BYTE *tmpBuf; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1117 | BYTE *retDataBuf; |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1118 | DWORD retValueSize, bufValueSize; |
| 1119 | DWORD retDataSize, bufDataSize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1120 | DWORD typ; |
| 1121 | PyObject *obData; |
| 1122 | PyObject *retVal; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1123 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1124 | if ((rc = RegQueryInfoKeyW(key, NULL, NULL, NULL, NULL, NULL, NULL, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1125 | NULL, |
| 1126 | &retValueSize, &retDataSize, NULL, NULL)) |
| 1127 | != ERROR_SUCCESS) |
| 1128 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1129 | "RegQueryInfoKey"); |
| 1130 | ++retValueSize; /* include null terminators */ |
| 1131 | ++retDataSize; |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1132 | bufDataSize = retDataSize; |
| 1133 | bufValueSize = retValueSize; |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 1134 | retValueBuf = PyMem_New(wchar_t, retValueSize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1135 | if (retValueBuf == NULL) |
| 1136 | return PyErr_NoMemory(); |
| 1137 | retDataBuf = (BYTE *)PyMem_Malloc(retDataSize); |
| 1138 | if (retDataBuf == NULL) { |
| 1139 | PyMem_Free(retValueBuf); |
| 1140 | return PyErr_NoMemory(); |
| 1141 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1142 | |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1143 | while (1) { |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1144 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1145 | rc = RegEnumValueW(key, |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1146 | index, |
| 1147 | retValueBuf, |
| 1148 | &retValueSize, |
| 1149 | NULL, |
| 1150 | &typ, |
| 1151 | (BYTE *)retDataBuf, |
| 1152 | &retDataSize); |
| 1153 | Py_END_ALLOW_THREADS |
| 1154 | |
| 1155 | if (rc != ERROR_MORE_DATA) |
| 1156 | break; |
| 1157 | |
| 1158 | bufDataSize *= 2; |
Amaury Forgeot d'Arc | f2b69df | 2010-08-16 22:11:29 +0000 | [diff] [blame] | 1159 | tmpBuf = (BYTE *)PyMem_Realloc(retDataBuf, bufDataSize); |
Brian Curtin | 9b7e2d1 | 2010-06-08 20:57:52 +0000 | [diff] [blame] | 1160 | if (tmpBuf == NULL) { |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1161 | PyErr_NoMemory(); |
| 1162 | retVal = NULL; |
| 1163 | goto fail; |
| 1164 | } |
Brian Curtin | 9b7e2d1 | 2010-06-08 20:57:52 +0000 | [diff] [blame] | 1165 | retDataBuf = tmpBuf; |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1166 | retDataSize = bufDataSize; |
| 1167 | retValueSize = bufValueSize; |
| 1168 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1169 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1170 | if (rc != ERROR_SUCCESS) { |
| 1171 | retVal = PyErr_SetFromWindowsErrWithFunction(rc, |
| 1172 | "PyRegEnumValue"); |
| 1173 | goto fail; |
| 1174 | } |
| 1175 | obData = Reg2Py(retDataBuf, retDataSize, typ); |
| 1176 | if (obData == NULL) { |
| 1177 | retVal = NULL; |
| 1178 | goto fail; |
| 1179 | } |
| 1180 | retVal = Py_BuildValue("uOi", retValueBuf, obData, typ); |
| 1181 | Py_DECREF(obData); |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1182 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1183 | PyMem_Free(retValueBuf); |
| 1184 | PyMem_Free(retDataBuf); |
| 1185 | return retVal; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1188 | /*[clinic input] |
| 1189 | winreg.ExpandEnvironmentStrings |
| 1190 | |
| 1191 | string: Py_UNICODE |
| 1192 | / |
| 1193 | |
| 1194 | Expand environment vars. |
| 1195 | [clinic start generated code]*/ |
| 1196 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1197 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1198 | winreg_ExpandEnvironmentStrings_impl(PyObject *module, |
| 1199 | const Py_UNICODE *string) |
| 1200 | /*[clinic end generated code: output=8fa4e959747a7312 input=b2a9714d2b751aa6]*/ |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1201 | { |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 1202 | wchar_t *retValue = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1203 | DWORD retValueSize; |
| 1204 | DWORD rc; |
| 1205 | PyObject *o; |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1206 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1207 | retValueSize = ExpandEnvironmentStringsW(string, retValue, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1208 | if (retValueSize == 0) { |
| 1209 | return PyErr_SetFromWindowsErrWithFunction(retValueSize, |
| 1210 | "ExpandEnvironmentStrings"); |
| 1211 | } |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 1212 | retValue = PyMem_New(wchar_t, retValueSize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1213 | if (retValue == NULL) { |
| 1214 | return PyErr_NoMemory(); |
| 1215 | } |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1216 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1217 | rc = ExpandEnvironmentStringsW(string, retValue, retValueSize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1218 | if (rc == 0) { |
| 1219 | PyMem_Free(retValue); |
| 1220 | return PyErr_SetFromWindowsErrWithFunction(retValueSize, |
| 1221 | "ExpandEnvironmentStrings"); |
| 1222 | } |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 1223 | o = PyUnicode_FromWideChar(retValue, wcslen(retValue)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1224 | PyMem_Free(retValue); |
| 1225 | return o; |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1228 | /*[clinic input] |
| 1229 | winreg.FlushKey |
| 1230 | |
| 1231 | key: HKEY |
| 1232 | An already open key, or any one of the predefined HKEY_* constants. |
| 1233 | / |
| 1234 | |
| 1235 | Writes all the attributes of a key to the registry. |
| 1236 | |
| 1237 | It is not necessary to call FlushKey to change a key. Registry changes |
| 1238 | are flushed to disk by the registry using its lazy flusher. Registry |
| 1239 | changes are also flushed to disk at system shutdown. Unlike |
| 1240 | CloseKey(), the FlushKey() method returns only when all the data has |
| 1241 | been written to the registry. |
| 1242 | |
| 1243 | An application should only call FlushKey() if it requires absolute |
| 1244 | certainty that registry changes are on disk. If you don't know whether |
| 1245 | a FlushKey() call is required, it probably isn't. |
| 1246 | [clinic start generated code]*/ |
| 1247 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1248 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1249 | winreg_FlushKey_impl(PyObject *module, HKEY key) |
| 1250 | /*[clinic end generated code: output=e6fc230d4c5dc049 input=f57457c12297d82f]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1251 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1252 | long rc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1253 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1254 | rc = RegFlushKey(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1255 | Py_END_ALLOW_THREADS |
| 1256 | if (rc != ERROR_SUCCESS) |
| 1257 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey"); |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1258 | Py_RETURN_NONE; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1259 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1260 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1261 | |
| 1262 | /*[clinic input] |
| 1263 | winreg.LoadKey |
| 1264 | |
| 1265 | key: HKEY |
| 1266 | An already open key, or any one of the predefined HKEY_* constants. |
| 1267 | sub_key: Py_UNICODE |
| 1268 | A string that identifies the sub-key to load. |
| 1269 | file_name: Py_UNICODE |
| 1270 | The name of the file to load registry data from. This file must |
| 1271 | have been created with the SaveKey() function. Under the file |
| 1272 | allocation table (FAT) file system, the filename may not have an |
| 1273 | extension. |
| 1274 | / |
| 1275 | |
| 1276 | Insert data into the registry from a file. |
| 1277 | |
| 1278 | Creates a subkey under the specified key and stores registration |
| 1279 | information from a specified file into that subkey. |
| 1280 | |
| 1281 | A call to LoadKey() fails if the calling process does not have the |
| 1282 | SE_RESTORE_PRIVILEGE privilege. |
| 1283 | |
| 1284 | If key is a handle returned by ConnectRegistry(), then the path |
| 1285 | specified in fileName is relative to the remote computer. |
| 1286 | |
| 1287 | The MSDN docs imply key must be in the HKEY_USER or HKEY_LOCAL_MACHINE |
| 1288 | tree. |
| 1289 | [clinic start generated code]*/ |
| 1290 | |
| 1291 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1292 | winreg_LoadKey_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key, |
| 1293 | const Py_UNICODE *file_name) |
| 1294 | /*[clinic end generated code: output=65f89f2548cb27c7 input=e3b5b45ade311582]*/ |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1295 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1296 | long rc; |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1298 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1299 | rc = RegLoadKeyW(key, sub_key, file_name ); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1300 | Py_END_ALLOW_THREADS |
| 1301 | if (rc != ERROR_SUCCESS) |
| 1302 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey"); |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1303 | Py_RETURN_NONE; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1306 | /*[clinic input] |
| 1307 | winreg.OpenKey -> HKEY |
| 1308 | |
| 1309 | key: HKEY |
| 1310 | An already open key, or any one of the predefined HKEY_* constants. |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1311 | sub_key: Py_UNICODE(accept={str, NoneType}) |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1312 | A string that identifies the sub_key to open. |
| 1313 | reserved: int = 0 |
| 1314 | A reserved integer that must be zero. Default is zero. |
| 1315 | access: REGSAM(c_default='KEY_READ') = winreg.KEY_READ |
| 1316 | An integer that specifies an access mask that describes the desired |
| 1317 | security access for the key. Default is KEY_READ. |
| 1318 | |
| 1319 | Opens the specified key. |
| 1320 | |
| 1321 | The result is a new handle to the specified key. |
| 1322 | If the function fails, an OSError exception is raised. |
| 1323 | [clinic start generated code]*/ |
| 1324 | |
| 1325 | static HKEY |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1326 | winreg_OpenKey_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1327 | int reserved, REGSAM access) |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1328 | /*[clinic end generated code: output=8849bff2c30104ad input=098505ac36a9ae28]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1329 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1330 | HKEY retKey; |
| 1331 | long rc; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1332 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1333 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1334 | rc = RegOpenKeyExW(key, sub_key, reserved, access, &retKey); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1335 | Py_END_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1336 | if (rc != ERROR_SUCCESS) { |
| 1337 | PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx"); |
| 1338 | return NULL; |
| 1339 | } |
| 1340 | return retKey; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1343 | /*[clinic input] |
| 1344 | winreg.OpenKeyEx = winreg.OpenKey |
| 1345 | |
| 1346 | Opens the specified key. |
| 1347 | |
| 1348 | The result is a new handle to the specified key. |
| 1349 | If the function fails, an OSError exception is raised. |
| 1350 | [clinic start generated code]*/ |
| 1351 | |
| 1352 | static HKEY |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1353 | winreg_OpenKeyEx_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1354 | int reserved, REGSAM access) |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1355 | /*[clinic end generated code: output=81bc2bd684bc77ae input=c6c4972af8622959]*/ |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1356 | { |
| 1357 | return winreg_OpenKey_impl(module, key, sub_key, reserved, access); |
| 1358 | } |
| 1359 | |
| 1360 | /*[clinic input] |
| 1361 | winreg.QueryInfoKey |
| 1362 | |
| 1363 | key: HKEY |
| 1364 | An already open key, or any one of the predefined HKEY_* constants. |
| 1365 | / |
| 1366 | |
| 1367 | Returns information about a key. |
| 1368 | |
| 1369 | The result is a tuple of 3 items: |
| 1370 | An integer that identifies the number of sub keys this key has. |
| 1371 | An integer that identifies the number of values this key has. |
| 1372 | An integer that identifies when the key was last modified (if available) |
| 1373 | as 100's of nanoseconds since Jan 1, 1600. |
| 1374 | [clinic start generated code]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1375 | |
| 1376 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1377 | winreg_QueryInfoKey_impl(PyObject *module, HKEY key) |
| 1378 | /*[clinic end generated code: output=dc657b8356a4f438 input=c3593802390cde1f]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1379 | { |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1380 | long rc; |
| 1381 | DWORD nSubKeys, nValues; |
| 1382 | FILETIME ft; |
| 1383 | LARGE_INTEGER li; |
| 1384 | PyObject *l; |
| 1385 | PyObject *ret; |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1386 | |
| 1387 | if ((rc = RegQueryInfoKey(key, NULL, NULL, 0, &nSubKeys, NULL, NULL, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1388 | &nValues, NULL, NULL, NULL, &ft)) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1389 | != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1390 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey"); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1391 | li.LowPart = ft.dwLowDateTime; |
| 1392 | li.HighPart = ft.dwHighDateTime; |
| 1393 | l = PyLong_FromLongLong(li.QuadPart); |
| 1394 | if (l == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1395 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1396 | ret = Py_BuildValue("iiO", nSubKeys, nValues, l); |
| 1397 | Py_DECREF(l); |
| 1398 | return ret; |
| 1399 | } |
| 1400 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1401 | /*[clinic input] |
| 1402 | winreg.QueryValue |
| 1403 | |
| 1404 | key: HKEY |
| 1405 | An already open key, or any one of the predefined HKEY_* constants. |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1406 | sub_key: Py_UNICODE(accept={str, NoneType}) |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1407 | A string that holds the name of the subkey with which the value |
| 1408 | is associated. If this parameter is None or empty, the function |
| 1409 | retrieves the value set by the SetValue() method for the key |
| 1410 | identified by key. |
| 1411 | / |
| 1412 | |
| 1413 | Retrieves the unnamed value for a key. |
| 1414 | |
| 1415 | Values in the registry have name, type, and data components. This method |
| 1416 | retrieves the data for a key's first value that has a NULL name. |
| 1417 | But since the underlying API call doesn't return the type, you'll |
| 1418 | probably be happier using QueryValueEx; this function is just here for |
| 1419 | completeness. |
| 1420 | [clinic start generated code]*/ |
| 1421 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1422 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1423 | winreg_QueryValue_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key) |
| 1424 | /*[clinic end generated code: output=c655810ae50c63a9 input=41cafbbf423b21d6]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1425 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1426 | long rc; |
| 1427 | PyObject *retStr; |
| 1428 | wchar_t *retBuf; |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1429 | DWORD bufSize = 0; |
| 1430 | DWORD retSize = 0; |
| 1431 | wchar_t *tmp; |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1432 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1433 | rc = RegQueryValueW(key, sub_key, NULL, &retSize); |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1434 | if (rc == ERROR_MORE_DATA) |
| 1435 | retSize = 256; |
| 1436 | else if (rc != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1437 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1438 | "RegQueryValue"); |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1439 | |
| 1440 | bufSize = retSize; |
| 1441 | retBuf = (wchar_t *) PyMem_Malloc(bufSize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1442 | if (retBuf == NULL) |
| 1443 | return PyErr_NoMemory(); |
Guido van Rossum | a8c360e | 2007-07-17 20:50:43 +0000 | [diff] [blame] | 1444 | |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1445 | while (1) { |
| 1446 | retSize = bufSize; |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1447 | rc = RegQueryValueW(key, sub_key, retBuf, &retSize); |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1448 | if (rc != ERROR_MORE_DATA) |
| 1449 | break; |
| 1450 | |
| 1451 | bufSize *= 2; |
| 1452 | tmp = (wchar_t *) PyMem_Realloc(retBuf, bufSize); |
| 1453 | if (tmp == NULL) { |
| 1454 | PyMem_Free(retBuf); |
| 1455 | return PyErr_NoMemory(); |
| 1456 | } |
| 1457 | retBuf = tmp; |
| 1458 | } |
| 1459 | |
| 1460 | if (rc != ERROR_SUCCESS) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1461 | PyMem_Free(retBuf); |
| 1462 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1463 | "RegQueryValue"); |
| 1464 | } |
Guido van Rossum | a8c360e | 2007-07-17 20:50:43 +0000 | [diff] [blame] | 1465 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1466 | retStr = PyUnicode_FromWideChar(retBuf, wcslen(retBuf)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1467 | PyMem_Free(retBuf); |
| 1468 | return retStr; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1471 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1472 | /*[clinic input] |
| 1473 | winreg.QueryValueEx |
| 1474 | |
| 1475 | key: HKEY |
| 1476 | An already open key, or any one of the predefined HKEY_* constants. |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1477 | name: Py_UNICODE(accept={str, NoneType}) |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1478 | A string indicating the value to query. |
| 1479 | / |
| 1480 | |
| 1481 | Retrieves the type and value of a specified sub-key. |
| 1482 | |
| 1483 | Behaves mostly like QueryValue(), but also returns the type of the |
| 1484 | specified value name associated with the given open registry key. |
| 1485 | |
| 1486 | The return value is a tuple of the value and the type_id. |
| 1487 | [clinic start generated code]*/ |
| 1488 | |
| 1489 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1490 | winreg_QueryValueEx_impl(PyObject *module, HKEY key, const Py_UNICODE *name) |
| 1491 | /*[clinic end generated code: output=f1b85b1c3d887ec7 input=cf366cada4836891]*/ |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1492 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1493 | long rc; |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1494 | BYTE *retBuf, *tmp; |
| 1495 | DWORD bufSize = 0, retSize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1496 | DWORD typ; |
| 1497 | PyObject *obData; |
| 1498 | PyObject *result; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1499 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1500 | rc = RegQueryValueExW(key, name, NULL, NULL, NULL, &bufSize); |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1501 | if (rc == ERROR_MORE_DATA) |
| 1502 | bufSize = 256; |
| 1503 | else if (rc != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1504 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1505 | "RegQueryValueEx"); |
| 1506 | retBuf = (BYTE *)PyMem_Malloc(bufSize); |
| 1507 | if (retBuf == NULL) |
| 1508 | return PyErr_NoMemory(); |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1509 | |
| 1510 | while (1) { |
| 1511 | retSize = bufSize; |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1512 | rc = RegQueryValueExW(key, name, NULL, &typ, |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1513 | (BYTE *)retBuf, &retSize); |
| 1514 | if (rc != ERROR_MORE_DATA) |
| 1515 | break; |
| 1516 | |
| 1517 | bufSize *= 2; |
| 1518 | tmp = (char *) PyMem_Realloc(retBuf, bufSize); |
| 1519 | if (tmp == NULL) { |
| 1520 | PyMem_Free(retBuf); |
| 1521 | return PyErr_NoMemory(); |
| 1522 | } |
| 1523 | retBuf = tmp; |
| 1524 | } |
| 1525 | |
| 1526 | if (rc != ERROR_SUCCESS) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1527 | PyMem_Free(retBuf); |
| 1528 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1529 | "RegQueryValueEx"); |
| 1530 | } |
| 1531 | obData = Reg2Py(retBuf, bufSize, typ); |
| 1532 | PyMem_Free(retBuf); |
| 1533 | if (obData == NULL) |
| 1534 | return NULL; |
| 1535 | result = Py_BuildValue("Oi", obData, typ); |
| 1536 | Py_DECREF(obData); |
| 1537 | return result; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1538 | } |
| 1539 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1540 | /*[clinic input] |
| 1541 | winreg.SaveKey |
| 1542 | |
| 1543 | key: HKEY |
| 1544 | An already open key, or any one of the predefined HKEY_* constants. |
| 1545 | file_name: Py_UNICODE |
| 1546 | The name of the file to save registry data to. This file cannot |
| 1547 | already exist. If this filename includes an extension, it cannot be |
| 1548 | used on file allocation table (FAT) file systems by the LoadKey(), |
| 1549 | ReplaceKey() or RestoreKey() methods. |
| 1550 | / |
| 1551 | |
| 1552 | Saves the specified key, and all its subkeys to the specified file. |
| 1553 | |
| 1554 | If key represents a key on a remote computer, the path described by |
| 1555 | file_name is relative to the remote computer. |
| 1556 | |
| 1557 | The caller of this method must possess the SeBackupPrivilege |
| 1558 | security privilege. This function passes NULL for security_attributes |
| 1559 | to the API. |
| 1560 | [clinic start generated code]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1561 | |
| 1562 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1563 | winreg_SaveKey_impl(PyObject *module, HKEY key, const Py_UNICODE *file_name) |
| 1564 | /*[clinic end generated code: output=ca94b835c88f112b input=da735241f91ac7a2]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1565 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1566 | LPSECURITY_ATTRIBUTES pSA = NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1567 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1568 | long rc; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1569 | /* One day we may get security into the core? |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1570 | if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE)) |
| 1571 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1572 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1573 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1574 | rc = RegSaveKeyW(key, file_name, pSA ); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1575 | Py_END_ALLOW_THREADS |
| 1576 | if (rc != ERROR_SUCCESS) |
| 1577 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey"); |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1578 | Py_RETURN_NONE; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1581 | /*[clinic input] |
| 1582 | winreg.SetValue |
| 1583 | |
| 1584 | key: HKEY |
| 1585 | An already open key, or any one of the predefined HKEY_* constants. |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1586 | sub_key: Py_UNICODE(accept={str, NoneType}) |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1587 | A string that names the subkey with which the value is associated. |
| 1588 | type: DWORD |
| 1589 | An integer that specifies the type of the data. Currently this must |
| 1590 | be REG_SZ, meaning only strings are supported. |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1591 | value: Py_UNICODE(zeroes=True) |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1592 | A string that specifies the new value. |
| 1593 | / |
| 1594 | |
| 1595 | Associates a value with a specified key. |
| 1596 | |
| 1597 | If the key specified by the sub_key parameter does not exist, the |
| 1598 | SetValue function creates it. |
| 1599 | |
| 1600 | Value lengths are limited by available memory. Long values (more than |
| 1601 | 2048 bytes) should be stored as files with the filenames stored in |
| 1602 | the configuration registry to help the registry perform efficiently. |
| 1603 | |
| 1604 | The key identified by the key parameter must have been opened with |
| 1605 | KEY_SET_VALUE access. |
| 1606 | [clinic start generated code]*/ |
| 1607 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1608 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1609 | winreg_SetValue_impl(PyObject *module, HKEY key, const Py_UNICODE *sub_key, |
| 1610 | DWORD type, const Py_UNICODE *value, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1611 | Py_ssize_clean_t value_length) |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1612 | /*[clinic end generated code: output=686bedb1cbb4367b input=2cd2adab79339c53]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1613 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1614 | long rc; |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1615 | |
| 1616 | if (type != REG_SZ) { |
Inada Naoki | cc60cdd9 | 2019-03-20 20:53:08 +0900 | [diff] [blame] | 1617 | PyErr_SetString(PyExc_TypeError, "type must be winreg.REG_SZ"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1618 | return NULL; |
| 1619 | } |
Inada Naoki | cc60cdd9 | 2019-03-20 20:53:08 +0900 | [diff] [blame] | 1620 | if ((size_t)value_length >= PY_DWORD_MAX) { |
| 1621 | PyErr_SetString(PyExc_OverflowError, "value is too long"); |
Inada Naoki | d5f18a6 | 2019-03-20 19:10:17 +0900 | [diff] [blame] | 1622 | return NULL; |
| 1623 | } |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1624 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1625 | Py_BEGIN_ALLOW_THREADS |
Zackery Spytz | 34366b7 | 2019-04-22 11:08:05 -0600 | [diff] [blame] | 1626 | rc = RegSetValueW(key, sub_key, REG_SZ, value, (DWORD)(value_length + 1)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1627 | Py_END_ALLOW_THREADS |
| 1628 | if (rc != ERROR_SUCCESS) |
| 1629 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue"); |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1630 | Py_RETURN_NONE; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1633 | /*[clinic input] |
| 1634 | winreg.SetValueEx |
| 1635 | |
| 1636 | key: HKEY |
| 1637 | An already open key, or any one of the predefined HKEY_* constants. |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1638 | value_name: Py_UNICODE(accept={str, NoneType}) |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1639 | A string containing the name of the value to set, or None. |
| 1640 | reserved: object |
| 1641 | Can be anything - zero is always passed to the API. |
| 1642 | type: DWORD |
| 1643 | An integer that specifies the type of the data, one of: |
| 1644 | REG_BINARY -- Binary data in any form. |
| 1645 | REG_DWORD -- A 32-bit number. |
Steve Dower | 80ac11d | 2016-05-24 15:42:04 -0700 | [diff] [blame] | 1646 | 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] | 1647 | REG_DWORD_BIG_ENDIAN -- A 32-bit number in big-endian format. |
| 1648 | REG_EXPAND_SZ -- A null-terminated string that contains unexpanded |
| 1649 | references to environment variables (for example, |
| 1650 | %PATH%). |
| 1651 | REG_LINK -- A Unicode symbolic link. |
Serhiy Storchaka | 6a7b3a7 | 2016-04-17 08:32:47 +0300 | [diff] [blame] | 1652 | REG_MULTI_SZ -- A sequence of null-terminated strings, terminated |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1653 | by two null characters. Note that Python handles |
| 1654 | this termination automatically. |
| 1655 | REG_NONE -- No defined value type. |
Steve Dower | 80ac11d | 2016-05-24 15:42:04 -0700 | [diff] [blame] | 1656 | REG_QWORD -- A 64-bit number. |
| 1657 | 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] | 1658 | REG_RESOURCE_LIST -- A device-driver resource list. |
| 1659 | REG_SZ -- A null-terminated string. |
| 1660 | value: object |
| 1661 | A string that specifies the new value. |
| 1662 | / |
| 1663 | |
| 1664 | Stores data in the value field of an open registry key. |
| 1665 | |
| 1666 | This method can also set additional value and type information for the |
| 1667 | specified key. The key identified by the key parameter must have been |
| 1668 | opened with KEY_SET_VALUE access. |
| 1669 | |
| 1670 | To open the key, use the CreateKeyEx() or OpenKeyEx() methods. |
| 1671 | |
| 1672 | Value lengths are limited by available memory. Long values (more than |
| 1673 | 2048 bytes) should be stored as files with the filenames stored in |
| 1674 | the configuration registry to help the registry perform efficiently. |
| 1675 | [clinic start generated code]*/ |
| 1676 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1677 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1678 | winreg_SetValueEx_impl(PyObject *module, HKEY key, |
| 1679 | const Py_UNICODE *value_name, PyObject *reserved, |
| 1680 | DWORD type, PyObject *value) |
| 1681 | /*[clinic end generated code: output=811b769a66ae11b7 input=900a9e3990bfb196]*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1682 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1683 | BYTE *data; |
| 1684 | DWORD len; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1685 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1686 | LONG rc; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1687 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1688 | if (!Py2Reg(value, type, &data, &len)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1689 | { |
| 1690 | if (!PyErr_Occurred()) |
| 1691 | PyErr_SetString(PyExc_ValueError, |
| 1692 | "Could not convert the data to the specified type."); |
| 1693 | return NULL; |
| 1694 | } |
| 1695 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1696 | rc = RegSetValueExW(key, value_name, 0, type, data, len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1697 | Py_END_ALLOW_THREADS |
| 1698 | PyMem_DEL(data); |
| 1699 | if (rc != ERROR_SUCCESS) |
| 1700 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1701 | "RegSetValueEx"); |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1702 | Py_RETURN_NONE; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1705 | /*[clinic input] |
| 1706 | winreg.DisableReflectionKey |
| 1707 | |
| 1708 | key: HKEY |
| 1709 | An already open key, or any one of the predefined HKEY_* constants. |
| 1710 | / |
| 1711 | |
| 1712 | Disables registry reflection for 32bit processes running on a 64bit OS. |
| 1713 | |
Miss Islington (bot) | dd5f8ab | 2019-08-04 06:43:30 -0700 | [diff] [blame] | 1714 | Will generally raise NotImplementedError if executed on a 32bit OS. |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1715 | |
| 1716 | If the key is not on the reflection list, the function succeeds but has |
| 1717 | no effect. Disabling reflection for a key does not affect reflection |
| 1718 | of any subkeys. |
| 1719 | [clinic start generated code]*/ |
| 1720 | |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1721 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1722 | winreg_DisableReflectionKey_impl(PyObject *module, HKEY key) |
Miss Islington (bot) | dd5f8ab | 2019-08-04 06:43:30 -0700 | [diff] [blame] | 1723 | /*[clinic end generated code: output=830cce504cc764b4 input=70bece2dee02e073]*/ |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1724 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1725 | HMODULE hMod; |
| 1726 | typedef LONG (WINAPI *RDRKFunc)(HKEY); |
| 1727 | RDRKFunc pfn = NULL; |
| 1728 | LONG rc; |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1729 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1730 | /* Only available on 64bit platforms, so we must load it |
| 1731 | dynamically.*/ |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 1732 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 1733 | hMod = GetModuleHandleW(L"advapi32.dll"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1734 | if (hMod) |
| 1735 | pfn = (RDRKFunc)GetProcAddress(hMod, |
| 1736 | "RegDisableReflectionKey"); |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 1737 | Py_END_ALLOW_THREADS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1738 | if (!pfn) { |
| 1739 | PyErr_SetString(PyExc_NotImplementedError, |
| 1740 | "not implemented on this platform"); |
| 1741 | return NULL; |
| 1742 | } |
| 1743 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1744 | rc = (*pfn)(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1745 | Py_END_ALLOW_THREADS |
| 1746 | if (rc != ERROR_SUCCESS) |
| 1747 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1748 | "RegDisableReflectionKey"); |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1749 | Py_RETURN_NONE; |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1752 | /*[clinic input] |
| 1753 | winreg.EnableReflectionKey |
| 1754 | |
| 1755 | key: HKEY |
| 1756 | An already open key, or any one of the predefined HKEY_* constants. |
| 1757 | / |
| 1758 | |
| 1759 | Restores registry reflection for the specified disabled key. |
| 1760 | |
Miss Islington (bot) | dd5f8ab | 2019-08-04 06:43:30 -0700 | [diff] [blame] | 1761 | Will generally raise NotImplementedError if executed on a 32bit OS. |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1762 | Restoring reflection for a key does not affect reflection of any |
| 1763 | subkeys. |
| 1764 | [clinic start generated code]*/ |
| 1765 | |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1766 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1767 | winreg_EnableReflectionKey_impl(PyObject *module, HKEY key) |
Miss Islington (bot) | dd5f8ab | 2019-08-04 06:43:30 -0700 | [diff] [blame] | 1768 | /*[clinic end generated code: output=86fa1385fdd9ce57 input=eeae770c6eb9f559]*/ |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1769 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1770 | HMODULE hMod; |
| 1771 | typedef LONG (WINAPI *RERKFunc)(HKEY); |
| 1772 | RERKFunc pfn = NULL; |
| 1773 | LONG rc; |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1774 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1775 | /* Only available on 64bit platforms, so we must load it |
| 1776 | dynamically.*/ |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 1777 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 1778 | hMod = GetModuleHandleW(L"advapi32.dll"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1779 | if (hMod) |
| 1780 | pfn = (RERKFunc)GetProcAddress(hMod, |
| 1781 | "RegEnableReflectionKey"); |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 1782 | Py_END_ALLOW_THREADS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1783 | if (!pfn) { |
| 1784 | PyErr_SetString(PyExc_NotImplementedError, |
| 1785 | "not implemented on this platform"); |
| 1786 | return NULL; |
| 1787 | } |
| 1788 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1789 | rc = (*pfn)(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1790 | Py_END_ALLOW_THREADS |
| 1791 | if (rc != ERROR_SUCCESS) |
| 1792 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1793 | "RegEnableReflectionKey"); |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1794 | Py_RETURN_NONE; |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1797 | /*[clinic input] |
| 1798 | winreg.QueryReflectionKey |
| 1799 | |
| 1800 | key: HKEY |
| 1801 | An already open key, or any one of the predefined HKEY_* constants. |
| 1802 | / |
| 1803 | |
| 1804 | Returns the reflection state for the specified key as a bool. |
| 1805 | |
Miss Islington (bot) | dd5f8ab | 2019-08-04 06:43:30 -0700 | [diff] [blame] | 1806 | Will generally raise NotImplementedError if executed on a 32bit OS. |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1807 | [clinic start generated code]*/ |
| 1808 | |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1809 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1810 | winreg_QueryReflectionKey_impl(PyObject *module, HKEY key) |
Miss Islington (bot) | dd5f8ab | 2019-08-04 06:43:30 -0700 | [diff] [blame] | 1811 | /*[clinic end generated code: output=4e774af288c3ebb9 input=a98fa51d55ade186]*/ |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1812 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1813 | HMODULE hMod; |
| 1814 | typedef LONG (WINAPI *RQRKFunc)(HKEY, BOOL *); |
| 1815 | RQRKFunc pfn = NULL; |
| 1816 | BOOL result; |
| 1817 | LONG rc; |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1818 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1819 | /* Only available on 64bit platforms, so we must load it |
| 1820 | dynamically.*/ |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 1821 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 1822 | hMod = GetModuleHandleW(L"advapi32.dll"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1823 | if (hMod) |
| 1824 | pfn = (RQRKFunc)GetProcAddress(hMod, |
| 1825 | "RegQueryReflectionKey"); |
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 1826 | Py_END_ALLOW_THREADS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1827 | if (!pfn) { |
| 1828 | PyErr_SetString(PyExc_NotImplementedError, |
| 1829 | "not implemented on this platform"); |
| 1830 | return NULL; |
| 1831 | } |
| 1832 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1833 | rc = (*pfn)(key, &result); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1834 | Py_END_ALLOW_THREADS |
| 1835 | if (rc != ERROR_SUCCESS) |
| 1836 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1837 | "RegQueryReflectionKey"); |
| 1838 | return PyBool_FromLong(result); |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1839 | } |
| 1840 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1841 | static struct PyMethodDef winreg_methods[] = { |
Zachary Ware | fd2d482 | 2015-05-13 01:21:57 -0500 | [diff] [blame] | 1842 | WINREG_CLOSEKEY_METHODDEF |
| 1843 | WINREG_CONNECTREGISTRY_METHODDEF |
| 1844 | WINREG_CREATEKEY_METHODDEF |
| 1845 | WINREG_CREATEKEYEX_METHODDEF |
| 1846 | WINREG_DELETEKEY_METHODDEF |
| 1847 | WINREG_DELETEKEYEX_METHODDEF |
| 1848 | WINREG_DELETEVALUE_METHODDEF |
| 1849 | WINREG_DISABLEREFLECTIONKEY_METHODDEF |
| 1850 | WINREG_ENABLEREFLECTIONKEY_METHODDEF |
| 1851 | WINREG_ENUMKEY_METHODDEF |
| 1852 | WINREG_ENUMVALUE_METHODDEF |
| 1853 | WINREG_EXPANDENVIRONMENTSTRINGS_METHODDEF |
| 1854 | WINREG_FLUSHKEY_METHODDEF |
| 1855 | WINREG_LOADKEY_METHODDEF |
| 1856 | WINREG_OPENKEY_METHODDEF |
| 1857 | WINREG_OPENKEYEX_METHODDEF |
| 1858 | WINREG_QUERYVALUE_METHODDEF |
| 1859 | WINREG_QUERYVALUEEX_METHODDEF |
| 1860 | WINREG_QUERYINFOKEY_METHODDEF |
| 1861 | WINREG_QUERYREFLECTIONKEY_METHODDEF |
| 1862 | WINREG_SAVEKEY_METHODDEF |
| 1863 | WINREG_SETVALUE_METHODDEF |
| 1864 | WINREG_SETVALUEEX_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1865 | NULL, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1866 | }; |
| 1867 | |
| 1868 | static void |
| 1869 | insint(PyObject * d, char * name, long value) |
| 1870 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1871 | PyObject *v = PyLong_FromLong(value); |
| 1872 | if (!v || PyDict_SetItemString(d, name, v)) |
| 1873 | PyErr_Clear(); |
| 1874 | Py_XDECREF(v); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
| 1877 | #define ADD_INT(val) insint(d, #val, val) |
| 1878 | |
| 1879 | static void |
| 1880 | inskey(PyObject * d, char * name, HKEY key) |
| 1881 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1882 | PyObject *v = PyLong_FromVoidPtr(key); |
| 1883 | if (!v || PyDict_SetItemString(d, name, v)) |
| 1884 | PyErr_Clear(); |
| 1885 | Py_XDECREF(v); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | #define ADD_KEY(val) inskey(d, #val, val) |
| 1889 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1890 | |
| 1891 | static struct PyModuleDef winregmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1892 | PyModuleDef_HEAD_INIT, |
| 1893 | "winreg", |
| 1894 | module_doc, |
| 1895 | -1, |
| 1896 | winreg_methods, |
| 1897 | NULL, |
| 1898 | NULL, |
| 1899 | NULL, |
| 1900 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1901 | }; |
| 1902 | |
| 1903 | PyMODINIT_FUNC PyInit_winreg(void) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1904 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1905 | PyObject *m, *d; |
| 1906 | m = PyModule_Create(&winregmodule); |
| 1907 | if (m == NULL) |
| 1908 | return NULL; |
| 1909 | d = PyModule_GetDict(m); |
| 1910 | PyHKEY_Type.tp_doc = PyHKEY_doc; |
| 1911 | if (PyType_Ready(&PyHKEY_Type) < 0) |
| 1912 | return NULL; |
| 1913 | Py_INCREF(&PyHKEY_Type); |
| 1914 | if (PyDict_SetItemString(d, "HKEYType", |
| 1915 | (PyObject *)&PyHKEY_Type) != 0) |
| 1916 | return NULL; |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1917 | Py_INCREF(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1918 | if (PyDict_SetItemString(d, "error", |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1919 | PyExc_OSError) != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1920 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1921 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1922 | /* Add the relevant constants */ |
| 1923 | ADD_KEY(HKEY_CLASSES_ROOT); |
| 1924 | ADD_KEY(HKEY_CURRENT_USER); |
| 1925 | ADD_KEY(HKEY_LOCAL_MACHINE); |
| 1926 | ADD_KEY(HKEY_USERS); |
| 1927 | ADD_KEY(HKEY_PERFORMANCE_DATA); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1928 | #ifdef HKEY_CURRENT_CONFIG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1929 | ADD_KEY(HKEY_CURRENT_CONFIG); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1930 | #endif |
| 1931 | #ifdef HKEY_DYN_DATA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1932 | ADD_KEY(HKEY_DYN_DATA); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1933 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1934 | ADD_INT(KEY_QUERY_VALUE); |
| 1935 | ADD_INT(KEY_SET_VALUE); |
| 1936 | ADD_INT(KEY_CREATE_SUB_KEY); |
| 1937 | ADD_INT(KEY_ENUMERATE_SUB_KEYS); |
| 1938 | ADD_INT(KEY_NOTIFY); |
| 1939 | ADD_INT(KEY_CREATE_LINK); |
| 1940 | ADD_INT(KEY_READ); |
| 1941 | ADD_INT(KEY_WRITE); |
| 1942 | ADD_INT(KEY_EXECUTE); |
| 1943 | ADD_INT(KEY_ALL_ACCESS); |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1944 | #ifdef KEY_WOW64_64KEY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1945 | ADD_INT(KEY_WOW64_64KEY); |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1946 | #endif |
| 1947 | #ifdef KEY_WOW64_32KEY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1948 | ADD_INT(KEY_WOW64_32KEY); |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1949 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1950 | ADD_INT(REG_OPTION_RESERVED); |
| 1951 | ADD_INT(REG_OPTION_NON_VOLATILE); |
| 1952 | ADD_INT(REG_OPTION_VOLATILE); |
| 1953 | ADD_INT(REG_OPTION_CREATE_LINK); |
| 1954 | ADD_INT(REG_OPTION_BACKUP_RESTORE); |
| 1955 | ADD_INT(REG_OPTION_OPEN_LINK); |
| 1956 | ADD_INT(REG_LEGAL_OPTION); |
| 1957 | ADD_INT(REG_CREATED_NEW_KEY); |
| 1958 | ADD_INT(REG_OPENED_EXISTING_KEY); |
| 1959 | ADD_INT(REG_WHOLE_HIVE_VOLATILE); |
| 1960 | ADD_INT(REG_REFRESH_HIVE); |
| 1961 | ADD_INT(REG_NO_LAZY_FLUSH); |
| 1962 | ADD_INT(REG_NOTIFY_CHANGE_NAME); |
| 1963 | ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES); |
| 1964 | ADD_INT(REG_NOTIFY_CHANGE_LAST_SET); |
| 1965 | ADD_INT(REG_NOTIFY_CHANGE_SECURITY); |
| 1966 | ADD_INT(REG_LEGAL_CHANGE_FILTER); |
| 1967 | ADD_INT(REG_NONE); |
| 1968 | ADD_INT(REG_SZ); |
| 1969 | ADD_INT(REG_EXPAND_SZ); |
| 1970 | ADD_INT(REG_BINARY); |
| 1971 | ADD_INT(REG_DWORD); |
| 1972 | ADD_INT(REG_DWORD_LITTLE_ENDIAN); |
| 1973 | ADD_INT(REG_DWORD_BIG_ENDIAN); |
Steve Dower | 80ac11d | 2016-05-24 15:42:04 -0700 | [diff] [blame] | 1974 | ADD_INT(REG_QWORD); |
| 1975 | ADD_INT(REG_QWORD_LITTLE_ENDIAN); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1976 | ADD_INT(REG_LINK); |
| 1977 | ADD_INT(REG_MULTI_SZ); |
| 1978 | ADD_INT(REG_RESOURCE_LIST); |
| 1979 | ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR); |
| 1980 | ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST); |
| 1981 | return m; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1984 | |