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