ยปCore Development>Code coverage>PC/_winreg.c

Python code coverage for PC/_winreg.c

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