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