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