4 Windows Registry access module for Python.
6 * Simple registry access written by Mark Hammond in win32api
8 * Bill Tutt expanded the support significantly not long after.
9 * Numerous other people have submitted patches since then.
10 * Ripped from win32api module 03-Feb-2000 by Mark Hammond, and
11 basic Unicode support added.
16 #include "structmember.h"
17 #include "malloc.h" /* for alloca */
20 static BOOL
PyHKEY_AsHKEY(PyObject
*ob
, HKEY
*pRes
, BOOL bNoneOK
);
21 static PyObject
*PyHKEY_FromHKEY(HKEY h
);
22 static BOOL
PyHKEY_Close(PyObject
*obHandle
);
24 static char errNotAHandle
[] = "Object is not a handle";
26 /* The win32api module reports the function name that failed,
27 but this concept is not in the Python core.
28 Hopefully it will one day, and in the meantime I dont
29 want to lose this info...
31 #define PyErr_SetFromWindowsErrWithFunction(rc, fnname) \
32 PyErr_SetFromWindowsErr(rc)
34 /* Forward declares */
37 PyDoc_STRVAR(module_doc
,
38 "This module provides access to the Windows registry API.\n"
42 "CloseKey() - Closes a registry key.\n"
43 "ConnectRegistry() - Establishes a connection to a predefined registry handle\n"
44 " on another computer.\n"
45 "CreateKey() - Creates the specified key, or opens it if it already exists.\n"
46 "DeleteKey() - Deletes the specified key.\n"
47 "DeleteValue() - Removes a named value from the specified registry key.\n"
48 "EnumKey() - Enumerates subkeys of the specified open registry key.\n"
49 "EnumValue() - Enumerates values of the specified open registry key.\n"
50 "ExpandEnvironmentStrings() - Expand the env strings in a REG_EXPAND_SZ string.\n"
51 "FlushKey() - Writes all the attributes of the specified key to the registry.\n"
52 "LoadKey() - Creates a subkey under HKEY_USER or HKEY_LOCAL_MACHINE and stores\n"
53 " registration information from a specified file into that subkey.\n"
54 "OpenKey() - Alias for <om win32api.RegOpenKeyEx>\n"
55 "OpenKeyEx() - Opens the specified key.\n"
56 "QueryValue() - Retrieves the value associated with the unnamed value for a\n"
57 " specified key in the registry.\n"
58 "QueryValueEx() - Retrieves the type and data for a specified value name\n"
59 " associated with an open registry key.\n"
60 "QueryInfoKey() - Returns information about the specified key.\n"
61 "SaveKey() - Saves the specified key, and all its subkeys a file.\n"
62 "SetValue() - Associates a value with a specified key.\n"
63 "SetValueEx() - Stores data in the value field of an open registry key.\n"
67 "HKEYType -- type object for HKEY objects\n"
68 "error -- exception raised for Win32 errors\n"
70 "Integer constants:\n"
71 "Many constants are defined - see the documentation for each function\n"
72 "to see what constants are used, and where.");
75 PyDoc_STRVAR(CloseKey_doc
,
76 "CloseKey(hkey) - Closes a previously opened registry key.\n"
78 "The hkey argument specifies a previously opened key.\n"
80 "Note that if the key is not closed using this method, it will be\n"
81 "closed when the hkey object is destroyed by Python.");
83 PyDoc_STRVAR(ConnectRegistry_doc
,
84 "key = ConnectRegistry(computer_name, key) - "
85 "Establishes a connection to a predefined registry handle on another computer.\n"
87 "computer_name is the name of the remote computer, of the form \\\\computername.\n"
88 " If None, the local computer is used.\n"
89 "key is the predefined handle to connect to.\n"
91 "The return value is the handle of the opened key.\n"
92 "If the function fails, an EnvironmentError exception is raised.");
94 PyDoc_STRVAR(CreateKey_doc
,
95 "key = CreateKey(key, sub_key) - Creates or opens the specified key.\n"
97 "key is an already open key, or one of the predefined HKEY_* constants\n"
98 "sub_key is a string that names the key this method opens or creates.\n"
99 " If key is one of the predefined keys, sub_key may be None. In that case,\n"
100 " the handle returned is the same key handle passed in to the function.\n"
102 "If the key already exists, this function opens the existing key\n"
104 "The return value is the handle of the opened key.\n"
105 "If the function fails, an exception is raised.");
107 PyDoc_STRVAR(DeleteKey_doc
,
108 "DeleteKey(key, sub_key) - Deletes the specified key.\n"
110 "key is an already open key, or any one of the predefined HKEY_* constants.\n"
111 "sub_key is a string that must be a subkey of the key identified by the key parameter.\n"
112 " This value must not be None, and the key may not have subkeys.\n"
114 "This method can not delete keys with subkeys.\n"
116 "If the method succeeds, the entire key, including all of its values,\n"
117 "is removed. If the method fails, an EnvironmentError exception is raised.");
119 PyDoc_STRVAR(DeleteValue_doc
,
120 "DeleteValue(key, value) - Removes a named value from a registry key.\n"
122 "key is an already open key, or any one of the predefined HKEY_* constants.\n"
123 "value is a string that identifies the value to remove.");
125 PyDoc_STRVAR(EnumKey_doc
,
126 "string = EnumKey(key, index) - Enumerates subkeys of an open registry key.\n"
128 "key is an already open key, or any one of the predefined HKEY_* constants.\n"
129 "index is an integer that identifies the index of the key to retrieve.\n"
131 "The function retrieves the name of one subkey each time it is called.\n"
132 "It is typically called repeatedly until an EnvironmentError exception is\n"
133 "raised, indicating no more values are available.");
135 PyDoc_STRVAR(EnumValue_doc
,
136 "tuple = EnumValue(key, index) - Enumerates values of an open registry key.\n"
137 "key is an already open key, or any one of the predefined HKEY_* constants.\n"
138 "index is an integer that identifies the index of the value to retrieve.\n"
140 "The function retrieves the name of one subkey each time it is called.\n"
141 "It is typically called repeatedly, until an EnvironmentError exception\n"
142 "is raised, indicating no more values.\n"
144 "The result is a tuple of 3 items:\n"
145 "value_name is a string that identifies the value.\n"
146 "value_data is an object that holds the value data, and whose type depends\n"
147 " on the underlying registry type.\n"
148 "data_type is an integer that identifies the type of the value data.");
150 PyDoc_STRVAR(ExpandEnvironmentStrings_doc
,
151 "string = ExpandEnvironmentStrings(string) - Expand environment vars.\n");
153 PyDoc_STRVAR(FlushKey_doc
,
154 "FlushKey(key) - Writes all the attributes of a key to the registry.\n"
156 "key is an already open key, or any one of the predefined HKEY_* constants.\n"
158 "It is not necessary to call RegFlushKey to change a key.\n"
159 "Registry changes are flushed to disk by the registry using its lazy flusher.\n"
160 "Registry changes are also flushed to disk at system shutdown.\n"
161 "Unlike CloseKey(), the FlushKey() method returns only when all the data has\n"
162 "been written to the registry.\n"
163 "An application should only call FlushKey() if it requires absolute certainty that registry changes are on disk.\n"
164 "If you don't know whether a FlushKey() call is required, it probably isn't.");
166 PyDoc_STRVAR(LoadKey_doc
,
167 "LoadKey(key, sub_key, file_name) - Creates a subkey under the specified key\n"
168 "and stores registration information from a specified file into that subkey.\n"
170 "key is an already open key, or any one of the predefined HKEY_* constants.\n"
171 "sub_key is a string that identifies the sub_key to load\n"
172 "file_name is the name of the file to load registry data from.\n"
173 " This file must have been created with the SaveKey() function.\n"
174 " Under the file allocation table (FAT) file system, the filename may not\n"
175 "have an extension.\n"
177 "A call to LoadKey() fails if the calling process does not have the\n"
178 "SE_RESTORE_PRIVILEGE privilege.\n"
180 "If key is a handle returned by ConnectRegistry(), then the path specified\n"
181 "in fileName is relative to the remote computer.\n"
183 "The docs imply key must be in the HKEY_USER or HKEY_LOCAL_MACHINE tree");
185 PyDoc_STRVAR(OpenKey_doc
,
186 "key = OpenKey(key, sub_key, res = 0, sam = KEY_READ) - Opens the specified key.\n"
188 "key is an already open key, or any one of the predefined HKEY_* constants.\n"
189 "sub_key is a string that identifies the sub_key to open\n"
190 "res is a reserved integer, and must be zero. Default is zero.\n"
191 "sam is an integer that specifies an access mask that describes the desired\n"
192 " security access for the key. Default is KEY_READ\n"
194 "The result is a new handle to the specified key\n"
195 "If the function fails, an EnvironmentError exception is raised.");
197 PyDoc_STRVAR(OpenKeyEx_doc
, "See OpenKey()");
199 PyDoc_STRVAR(QueryInfoKey_doc
,
200 "tuple = QueryInfoKey(key) - Returns information about a key.\n"
202 "key is an already open key, or any one of the predefined HKEY_* constants.\n"
204 "The result is a tuple of 3 items:"
205 "An integer that identifies the number of sub keys this key has.\n"
206 "An integer that identifies the number of values this key has.\n"
207 "A long integer that identifies when the key was last modified (if available)\n"
208 " as 100's of nanoseconds since Jan 1, 1600.");
210 PyDoc_STRVAR(QueryValue_doc
,
211 "string = QueryValue(key, sub_key) - retrieves the unnamed value for a key.\n"
213 "key is an already open key, or any one of the predefined HKEY_* constants.\n"
214 "sub_key is a string that holds the name of the subkey with which the value\n"
215 " is associated. If this parameter is None or empty, the function retrieves\n"
216 " the value set by the SetValue() method for the key identified by key."
218 "Values in the registry have name, type, and data components. This method\n"
219 "retrieves the data for a key's first value that has a NULL name.\n"
220 "But the underlying API call doesn't return the type, Lame Lame Lame, DONT USE THIS!!!");
222 PyDoc_STRVAR(QueryValueEx_doc
,
223 "value,type_id = QueryValueEx(key, value_name) - Retrieves the type and data for a specified value name associated with an open registry key.\n"
225 "key is an already open key, or any one of the predefined HKEY_* constants.\n"
226 "value_name is a string indicating the value to query");
228 PyDoc_STRVAR(SaveKey_doc
,
229 "SaveKey(key, file_name) - Saves the specified key, and all its subkeys to the specified file.\n"
231 "key is an already open key, or any one of the predefined HKEY_* constants.\n"
232 "file_name is the name of the file to save registry data to.\n"
233 " This file cannot already exist. If this filename includes an extension,\n"
234 " it cannot be used on file allocation table (FAT) file systems by the\n"
235 " LoadKey(), ReplaceKey() or RestoreKey() methods.\n"
237 "If key represents a key on a remote computer, the path described by\n"
238 "file_name is relative to the remote computer.\n"
239 "The caller of this method must possess the SeBackupPrivilege security privilege.\n"
240 "This function passes NULL for security_attributes to the API.");
242 PyDoc_STRVAR(SetValue_doc
,
243 "SetValue(key, sub_key, type, value) - Associates a value with a specified key.\n"
245 "key is an already open key, or any one of the predefined HKEY_* constants.\n"
246 "sub_key is a string that names the subkey with which the value is associated.\n"
247 "type is an integer that specifies the type of the data. Currently this\n"
248 " must be REG_SZ, meaning only strings are supported.\n"
249 "value is a string that specifies the new value.\n"
251 "If the key specified by the sub_key parameter does not exist, the SetValue\n"
252 "function creates it.\n"
254 "Value lengths are limited by available memory. Long values (more than\n"
255 "2048 bytes) should be stored as files with the filenames stored in \n"
256 "the configuration registry. This helps the registry perform efficiently.\n"
258 "The key identified by the key parameter must have been opened with\n"
259 "KEY_SET_VALUE access.");
261 PyDoc_STRVAR(SetValueEx_doc
,
262 "SetValueEx(key, value_name, reserved, type, value) - Stores data in the value field of an open registry key.\n"
264 "key is an already open key, or any one of the predefined HKEY_* constants.\n"
265 "value_name is a string containing the name of the value to set, or None\n"
266 "type is an integer that specifies the type of the data. This should be one of:\n"
267 " REG_BINARY -- Binary data in any form.\n"
268 " REG_DWORD -- A 32-bit number.\n"
269 " REG_DWORD_LITTLE_ENDIAN -- A 32-bit number in little-endian format.\n"
270 " REG_DWORD_BIG_ENDIAN -- A 32-bit number in big-endian format.\n"
271 " REG_EXPAND_SZ -- A null-terminated string that contains unexpanded references\n"
272 " to environment variables (for example, %PATH%).\n"
273 " REG_LINK -- A Unicode symbolic link.\n"
274 " REG_MULTI_SZ -- An sequence of null-terminated strings, terminated by\n"
275 " two null characters. Note that Python handles this\n"
276 " termination automatically.\n"
277 " REG_NONE -- No defined value type.\n"
278 " REG_RESOURCE_LIST -- A device-driver resource list.\n"
279 " REG_SZ -- A null-terminated string.\n"
280 "reserved can be anything - zero is always passed to the API.\n"
281 "value is a string that specifies the new value.\n"
283 "This method can also set additional value and type information for the\n"
284 "specified key. The key identified by the key parameter must have been\n"
285 "opened with KEY_SET_VALUE access.\n"
287 "To open the key, use the CreateKeyEx() or OpenKeyEx() methods.\n"
289 "Value lengths are limited by available memory. Long values (more than\n"
290 "2048 bytes) should be stored as files with the filenames stored in \n"
291 "the configuration registry. This helps the registry perform efficiently.");
293 PyDoc_STRVAR(DisableReflectionKey_doc
,
294 "Disables registry reflection for 32-bit processes running on a 64-bit\n"
295 "Operating System. Will generally raise NotImplemented if executed on\n"
296 "a 32-bit Operating System.\n"
297 "If the key is not on the reflection list, the function succeeds but has no effect.\n"
298 "Disabling reflection for a key does not affect reflection of any subkeys.");
300 PyDoc_STRVAR(EnableReflectionKey_doc
,
301 "Restores registry reflection for the specified disabled key.\n"
302 "Will generally raise NotImplemented if executed on a 32-bit Operating System.\n"
303 "Restoring reflection for a key does not affect reflection of any subkeys.");
305 PyDoc_STRVAR(QueryReflectionKey_doc
,
306 "bool = QueryReflectionKey(hkey) - Determines the reflection state for the specified key.\n"
307 "Will generally raise NotImplemented if executed on a 32-bit Operating System.\n");
309 /* PyHKEY docstrings */
310 PyDoc_STRVAR(PyHKEY_doc
,
311 "PyHKEY Object - A Python object, representing a win32 registry key.\n"
313 "This object wraps a Windows HKEY object, automatically closing it when\n"
314 "the object is destroyed. To guarantee cleanup, you can call either\n"
315 "the Close() method on the PyHKEY, or the CloseKey() method.\n"
317 "All functions which accept a handle object also accept an integer - \n"
318 "however, use of the handle object is encouraged.\n"
321 "Close() - Closes the underlying handle.\n"
322 "Detach() - Returns the integer Win32 handle, detaching it from the object\n"
325 "handle - The integer Win32 handle.\n"
328 "__nonzero__ - Handles with an open object return true, otherwise false.\n"
329 "__int__ - Converting a handle to an integer returns the Win32 handle.\n"
330 "__cmp__ - Handle objects are compared using the handle value.");
333 PyDoc_STRVAR(PyHKEY_Close_doc
,
334 "key.Close() - Closes the underlying Windows handle.\n"
336 "If the handle is already closed, no error is raised.");
338 PyDoc_STRVAR(PyHKEY_Detach_doc
,
339 "int = key.Detach() - Detaches the Windows handle from the handle object.\n"
341 "The result is the value of the handle before it is detached. If the\n"
342 "handle is already detached, this will return zero.\n"
344 "After calling this function, the handle is effectively invalidated,\n"
345 "but the handle is not closed. You would call this function when you\n"
346 "need the underlying win32 handle to exist beyond the lifetime of the\n"
348 "On 64 bit windows, the result of this function is a long integer");
351 /************************************************************************
353 The PyHKEY object definition
355 ************************************************************************/
361 #define PyHKEY_Check(op) ((op)->ob_type == &PyHKEY_Type)
363 static char *failMsg
= "bad operand type";
366 PyHKEY_unaryFailureFunc(PyObject
*ob
)
368 PyErr_SetString(PyExc_TypeError
, failMsg
);
372 PyHKEY_binaryFailureFunc(PyObject
*ob1
, PyObject
*ob2
)
374 PyErr_SetString(PyExc_TypeError
, failMsg
);
378 PyHKEY_ternaryFailureFunc(PyObject
*ob1
, PyObject
*ob2
, PyObject
*ob3
)
380 PyErr_SetString(PyExc_TypeError
, failMsg
);
385 PyHKEY_deallocFunc(PyObject
*ob
)
387 /* Can not call PyHKEY_Close, as the ob->tp_type
388 has already been cleared, thus causing the type
391 PyHKEYObject
*obkey
= (PyHKEYObject
*)ob
;
393 RegCloseKey((HKEY
)obkey
->hkey
);
398 PyHKEY_nonzeroFunc(PyObject
*ob
)
400 return ((PyHKEYObject
*)ob
)->hkey
!= 0;
404 PyHKEY_intFunc(PyObject
*ob
)
406 PyHKEYObject
*pyhkey
= (PyHKEYObject
*)ob
;
407 return PyLong_FromVoidPtr(pyhkey
->hkey
);
411 PyHKEY_printFunc(PyObject
*ob
, FILE *fp
, int flags
)
413 PyHKEYObject
*pyhkey
= (PyHKEYObject
*)ob
;
415 wsprintf(resBuf
, "<PyHKEY at %p (%p)>",
422 PyHKEY_strFunc(PyObject
*ob
)
424 PyHKEYObject
*pyhkey
= (PyHKEYObject
*)ob
;
426 wsprintf(resBuf
, "<PyHKEY:%p>", pyhkey
->hkey
);
427 return PyString_FromString(resBuf
);
431 PyHKEY_compareFunc(PyObject
*ob1
, PyObject
*ob2
)
433 PyHKEYObject
*pyhkey1
= (PyHKEYObject
*)ob1
;
434 PyHKEYObject
*pyhkey2
= (PyHKEYObject
*)ob2
;
435 return pyhkey1
== pyhkey2
? 0 :
436 (pyhkey1
< pyhkey2
? -1 : 1);
440 PyHKEY_hashFunc(PyObject
*ob
)
442 /* Just use the address.
443 XXX - should we use the handle value?
445 return _Py_HashPointer(ob
);
449 static PyNumberMethods PyHKEY_NumberMethods
=
451 PyHKEY_binaryFailureFunc
, /* nb_add */
452 PyHKEY_binaryFailureFunc
, /* nb_subtract */
453 PyHKEY_binaryFailureFunc
, /* nb_multiply */
454 PyHKEY_binaryFailureFunc
, /* nb_divide */
455 PyHKEY_binaryFailureFunc
, /* nb_remainder */
456 PyHKEY_binaryFailureFunc
, /* nb_divmod */
457 PyHKEY_ternaryFailureFunc
, /* nb_power */
458 PyHKEY_unaryFailureFunc
, /* nb_negative */
459 PyHKEY_unaryFailureFunc
, /* nb_positive */
460 PyHKEY_unaryFailureFunc
, /* nb_absolute */
461 PyHKEY_nonzeroFunc
, /* nb_nonzero */
462 PyHKEY_unaryFailureFunc
, /* nb_invert */
463 PyHKEY_binaryFailureFunc
, /* nb_lshift */
464 PyHKEY_binaryFailureFunc
, /* nb_rshift */
465 PyHKEY_binaryFailureFunc
, /* nb_and */
466 PyHKEY_binaryFailureFunc
, /* nb_xor */
467 PyHKEY_binaryFailureFunc
, /* nb_or */
468 0, /* nb_coerce (allowed to be zero) */
469 PyHKEY_intFunc
, /* nb_int */
470 PyHKEY_unaryFailureFunc
, /* nb_long */
471 PyHKEY_unaryFailureFunc
, /* nb_float */
472 PyHKEY_unaryFailureFunc
, /* nb_oct */
473 PyHKEY_unaryFailureFunc
, /* nb_hex */
477 /* fwd declare __getattr__ */
478 static PyObject
*PyHKEY_getattr(PyObject
*self
, const char *name
);
480 /* The type itself */
481 PyTypeObject PyHKEY_Type
=
483 PyVarObject_HEAD_INIT(0, 0) /* fill in type at module init */
485 sizeof(PyHKEYObject
),
487 PyHKEY_deallocFunc
, /* tp_dealloc */
488 PyHKEY_printFunc
, /* tp_print */
489 PyHKEY_getattr
, /* tp_getattr */
491 PyHKEY_compareFunc
, /* tp_compare */
493 &PyHKEY_NumberMethods
, /* tp_as_number */
494 0, /* tp_as_sequence */
495 0, /* tp_as_mapping */
496 PyHKEY_hashFunc
, /* tp_hash */
498 PyHKEY_strFunc
, /* tp_str */
501 0, /* tp_as_buffer */
503 PyHKEY_doc
, /* tp_doc */
506 #define OFF(e) offsetof(PyHKEYObject, e)
508 static struct memberlist PyHKEY_memberlist
[] = {
509 {"handle", T_INT
, OFF(hkey
)},
510 {NULL
} /* Sentinel */
513 /************************************************************************
515 The PyHKEY object methods
517 ************************************************************************/
519 PyHKEY_CloseMethod(PyObject
*self
, PyObject
*args
)
521 if (!PyArg_ParseTuple(args
, ":Close"))
523 if (!PyHKEY_Close(self
))
530 PyHKEY_DetachMethod(PyObject
*self
, PyObject
*args
)
533 PyHKEYObject
*pThis
= (PyHKEYObject
*)self
;
534 if (!PyArg_ParseTuple(args
, ":Detach"))
536 ret
= (void*)pThis
->hkey
;
538 return PyLong_FromVoidPtr(ret
);
542 PyHKEY_Enter(PyObject
*self
)
549 PyHKEY_Exit(PyObject
*self
, PyObject
*args
)
551 if (!PyHKEY_Close(self
))
557 static struct PyMethodDef PyHKEY_methods
[] = {
558 {"Close", PyHKEY_CloseMethod
, METH_VARARGS
, PyHKEY_Close_doc
},
559 {"Detach", PyHKEY_DetachMethod
, METH_VARARGS
, PyHKEY_Detach_doc
},
560 {"__enter__", (PyCFunction
)PyHKEY_Enter
, METH_NOARGS
, NULL
},
561 {"__exit__", PyHKEY_Exit
, METH_VARARGS
, NULL
},
565 /*static*/ PyObject
*
566 PyHKEY_getattr(PyObject
*self
, const char *name
)
570 res
= Py_FindMethod(PyHKEY_methods
, self
, name
);
574 if (strcmp(name
, "handle") == 0)
575 return PyLong_FromVoidPtr(((PyHKEYObject
*)self
)->hkey
);
576 return PyMember_Get((char *)self
, PyHKEY_memberlist
, name
);
579 /************************************************************************
580 The public PyHKEY API (well, not public yet :-)
581 ************************************************************************/
583 PyHKEY_New(HKEY hInit
)
585 PyHKEYObject
*key
= PyObject_NEW(PyHKEYObject
, &PyHKEY_Type
);
588 return (PyObject
*)key
;
592 PyHKEY_Close(PyObject
*ob_handle
)
597 if (!PyHKEY_Check(ob_handle
)) {
598 PyErr_SetString(PyExc_TypeError
, "bad operand type");
601 key
= (PyHKEYObject
*)ob_handle
;
602 rc
= key
->hkey
? RegCloseKey((HKEY
)key
->hkey
) : ERROR_SUCCESS
;
604 if (rc
!= ERROR_SUCCESS
)
605 PyErr_SetFromWindowsErrWithFunction(rc
, "RegCloseKey");
606 return rc
== ERROR_SUCCESS
;
610 PyHKEY_AsHKEY(PyObject
*ob
, HKEY
*pHANDLE
, BOOL bNoneOK
)
616 "None is not a valid HKEY in this context");
621 else if (PyHKEY_Check(ob
)) {
622 PyHKEYObject
*pH
= (PyHKEYObject
*)ob
;
625 else if (PyInt_Check(ob
) || PyLong_Check(ob
)) {
626 /* We also support integers */
628 *pHANDLE
= (HKEY
)PyLong_AsVoidPtr(ob
);
629 if (PyErr_Occurred())
635 "The object is not a PyHKEY object");
642 PyHKEY_FromHKEY(HKEY h
)
646 /* Inline PyObject_New */
647 op
= (PyHKEYObject
*) PyObject_MALLOC(sizeof(PyHKEYObject
));
649 return PyErr_NoMemory();
650 PyObject_INIT(op
, &PyHKEY_Type
);
652 return (PyObject
*)op
;
656 /************************************************************************
658 ************************************************************************/
660 PyWinObject_CloseHKEY(PyObject
*obHandle
)
663 if (PyHKEY_Check(obHandle
)) {
664 ok
= PyHKEY_Close(obHandle
);
666 #if SIZEOF_LONG >= SIZEOF_HKEY
667 else if (PyInt_Check(obHandle
)) {
668 long rc
= RegCloseKey((HKEY
)PyInt_AsLong(obHandle
));
669 ok
= (rc
== ERROR_SUCCESS
);
671 PyErr_SetFromWindowsErrWithFunction(rc
, "RegCloseKey");
674 else if (PyLong_Check(obHandle
)) {
675 long rc
= RegCloseKey((HKEY
)PyLong_AsVoidPtr(obHandle
));
676 ok
= (rc
== ERROR_SUCCESS
);
678 PyErr_SetFromWindowsErrWithFunction(rc
, "RegCloseKey");
684 "A handle must be a HKEY object or an integer");
692 Private Helper functions for the registry interfaces
694 ** Note that fixupMultiSZ and countString have both had changes
695 ** made to support "incorrect strings". The registry specification
696 ** calls for strings to be terminated with 2 null bytes. It seems
697 ** some commercial packages install strings which dont conform,
698 ** causing this code to fail - however, "regedit" etc still work
699 ** with these strings (ie only we dont!).
702 fixupMultiSZ(char **str
, char *data
, int len
)
709 for (P
= data
, i
= 0; P
< Q
&& *P
!= '\0'; P
++, i
++) {
711 for(; *P
!= '\0'; P
++)
717 countStrings(char *data
, int len
)
721 char *Q
= data
+ len
;
723 for (P
= data
, strings
= 0; P
< Q
&& *P
!= '\0'; P
++, strings
++)
724 for (; P
< Q
&& *P
!= '\0'; P
++)
729 /* Convert PyObject into Registry data.
730 Allocates space as needed. */
732 Py2Reg(PyObject
*value
, DWORD typ
, BYTE
**retDataBuf
, DWORD
*retDataSize
)
737 if (value
!= Py_None
&& !PyInt_Check(value
))
739 *retDataBuf
= (BYTE
*)PyMem_NEW(DWORD
, 1);
740 if (*retDataBuf
==NULL
){
744 *retDataSize
= sizeof(DWORD
);
745 if (value
== Py_None
) {
747 memcpy(*retDataBuf
, &zero
, sizeof(DWORD
));
751 &PyInt_AS_LONG((PyIntObject
*)value
),
758 if (value
== Py_None
)
761 if (PyUnicode_Check(value
)) {
762 value
= PyUnicode_AsEncodedString(
770 if (!PyString_Check(value
))
772 *retDataSize
= 1 + strlen(
774 (PyStringObject
*)value
));
776 *retDataBuf
= (BYTE
*)PyMem_NEW(DWORD
, *retDataSize
);
777 if (*retDataBuf
==NULL
){
781 if (value
== Py_None
)
782 strcpy((char *)*retDataBuf
, "");
784 strcpy((char *)*retDataBuf
,
786 (PyStringObject
*)value
));
795 PyObject
**obs
= NULL
;
797 if (value
== Py_None
)
800 if (!PyList_Check(value
))
802 i
= PyList_Size(value
);
804 obs
= malloc(sizeof(PyObject
*) * i
);
805 memset(obs
, 0, sizeof(PyObject
*) * i
);
806 for (j
= 0; j
< i
; j
++)
810 (PyListObject
*)value
,j
);
811 if (PyString_Check(t
)) {
814 } else if (PyUnicode_Check(t
)) {
815 obs
[j
] = PyUnicode_AsEncodedString(
825 (PyStringObject
*)obs
[j
]));
828 *retDataSize
= size
+ 1;
829 *retDataBuf
= (BYTE
*)PyMem_NEW(char,
831 if (*retDataBuf
==NULL
){
835 P
= (char *)*retDataBuf
;
837 for (j
= 0; j
< i
; j
++)
843 (PyStringObject
*)t
));
846 (PyStringObject
*)t
));
849 /* And doubly-terminate the list... */
855 for (j
= 0; j
< i
; j
++)
863 /* ALSO handle ALL unknown data types here. Even if we can't
864 support it natively, we should handle the bits. */
866 if (value
== Py_None
)
870 PyBufferProcs
*pb
= value
->ob_type
->tp_as_buffer
;
872 PyErr_Format(PyExc_TypeError
,
873 "Objects of type '%s' can not "
874 "be used as binary registry values",
875 value
->ob_type
->tp_name
);
878 *retDataSize
= (*pb
->bf_getreadbuffer
)(value
, 0, &src_buf
);
879 *retDataBuf
= (BYTE
*)PyMem_NEW(char,
881 if (*retDataBuf
==NULL
){
885 memcpy(*retDataBuf
, src_buf
, *retDataSize
);
892 /* Convert Registry data into PyObject*/
894 Reg2Py(char *retDataBuf
, DWORD retDataSize
, DWORD typ
)
900 if (retDataSize
== 0)
901 obData
= Py_BuildValue("i", 0);
903 obData
= Py_BuildValue("i",
908 /* retDataBuf may or may not have a trailing NULL in
910 if (retDataSize
&& retDataBuf
[retDataSize
-1] == '\0')
914 obData
= PyUnicode_DecodeMBCS(retDataBuf
,
919 if (retDataSize
== 0)
920 obData
= PyList_New(0);
924 int s
= countStrings(retDataBuf
, retDataSize
);
925 char **str
= (char **)malloc(sizeof(char *)*s
);
927 return PyErr_NoMemory();
929 fixupMultiSZ(str
, retDataBuf
, retDataSize
);
930 obData
= PyList_New(s
);
933 for (index
= 0; index
< s
; index
++)
935 size_t len
= _mbstrlen(str
[index
]);
937 PyErr_SetString(PyExc_OverflowError
,
938 "registry string is too long for a Python string");
942 PyList_SetItem(obData
,
944 PyUnicode_DecodeMBCS(
945 (const char *)str
[index
],
955 /* ALSO handle ALL unknown data types here. Even if we can't
956 support it natively, we should handle the bits. */
958 if (retDataSize
== 0) {
963 obData
= Py_BuildValue("s#",
974 /* The Python methods */
977 PyCloseKey(PyObject
*self
, PyObject
*args
)
980 if (!PyArg_ParseTuple(args
, "O:CloseKey", &obKey
))
982 if (!PyHKEY_Close(obKey
))
989 PyConnectRegistry(PyObject
*self
, PyObject
*args
)
993 char *szCompName
= NULL
;
996 if (!PyArg_ParseTuple(args
, "zO:ConnectRegistry", &szCompName
, &obKey
))
998 if (!PyHKEY_AsHKEY(obKey
, &hKey
, FALSE
))
1000 Py_BEGIN_ALLOW_THREADS
1001 rc
= RegConnectRegistry(szCompName
, hKey
, &retKey
);
1002 Py_END_ALLOW_THREADS
1003 if (rc
!= ERROR_SUCCESS
)
1004 return PyErr_SetFromWindowsErrWithFunction(rc
,
1006 return PyHKEY_FromHKEY(retKey
);
1010 PyCreateKey(PyObject
*self
, PyObject
*args
)
1017 if (!PyArg_ParseTuple(args
, "Oz:CreateKey", &obKey
, &subKey
))
1019 if (!PyHKEY_AsHKEY(obKey
, &hKey
, FALSE
))
1021 rc
= RegCreateKey(hKey
, subKey
, &retKey
);
1022 if (rc
!= ERROR_SUCCESS
)
1023 return PyErr_SetFromWindowsErrWithFunction(rc
, "CreateKey");
1024 return PyHKEY_FromHKEY(retKey
);
1028 PyDeleteKey(PyObject
*self
, PyObject
*args
)
1034 if (!PyArg_ParseTuple(args
, "Os:DeleteKey", &obKey
, &subKey
))
1036 if (!PyHKEY_AsHKEY(obKey
, &hKey
, FALSE
))
1038 rc
= RegDeleteKey(hKey
, subKey
);
1039 if (rc
!= ERROR_SUCCESS
)
1040 return PyErr_SetFromWindowsErrWithFunction(rc
, "RegDeleteKey");
1046 PyDeleteValue(PyObject
*self
, PyObject
*args
)
1052 if (!PyArg_ParseTuple(args
, "Oz:DeleteValue", &obKey
, &subKey
))
1054 if (!PyHKEY_AsHKEY(obKey
, &hKey
, FALSE
))
1056 Py_BEGIN_ALLOW_THREADS
1057 rc
= RegDeleteValue(hKey
, subKey
);
1058 Py_END_ALLOW_THREADS
1059 if (rc
!=ERROR_SUCCESS
)
1060 return PyErr_SetFromWindowsErrWithFunction(rc
,
1067 PyEnumKey(PyObject
*self
, PyObject
*args
)
1074 char tmpbuf
[256]; /* max key name length is 255 */
1075 DWORD len
= sizeof(tmpbuf
); /* includes NULL terminator */
1077 if (!PyArg_ParseTuple(args
, "Oi:EnumKey", &obKey
, &index
))
1079 if (!PyHKEY_AsHKEY(obKey
, &hKey
, FALSE
))
1082 Py_BEGIN_ALLOW_THREADS
1083 rc
= RegEnumKeyEx(hKey
, index
, tmpbuf
, &len
, NULL
, NULL
, NULL
, NULL
);
1084 Py_END_ALLOW_THREADS
1085 if (rc
!= ERROR_SUCCESS
)
1086 return PyErr_SetFromWindowsErrWithFunction(rc
, "RegEnumKeyEx");
1088 retStr
= PyString_FromStringAndSize(tmpbuf
, len
);
1089 return retStr
; /* can be NULL */
1093 PyEnumValue(PyObject
*self
, PyObject
*args
)
1107 if (!PyArg_ParseTuple(args
, "Oi:EnumValue", &obKey
, &index
))
1109 if (!PyHKEY_AsHKEY(obKey
, &hKey
, FALSE
))
1112 if ((rc
= RegQueryInfoKey(hKey
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
1114 &retValueSize
, &retDataSize
, NULL
, NULL
))
1116 return PyErr_SetFromWindowsErrWithFunction(rc
,
1118 ++retValueSize
; /* include null terminators */
1120 retValueBuf
= (char *)PyMem_Malloc(retValueSize
);
1121 if (retValueBuf
== NULL
)
1122 return PyErr_NoMemory();
1123 retDataBuf
= (char *)PyMem_Malloc(retDataSize
);
1124 if (retDataBuf
== NULL
) {
1125 PyMem_Free(retValueBuf
);
1126 return PyErr_NoMemory();
1129 Py_BEGIN_ALLOW_THREADS
1130 rc
= RegEnumValue(hKey
,
1138 Py_END_ALLOW_THREADS
1140 if (rc
!= ERROR_SUCCESS
) {
1141 retVal
= PyErr_SetFromWindowsErrWithFunction(rc
,
1145 obData
= Reg2Py(retDataBuf
, retDataSize
, typ
);
1146 if (obData
== NULL
) {
1150 retVal
= Py_BuildValue("sOi", retValueBuf
, obData
, typ
);
1153 PyMem_Free(retValueBuf
);
1154 PyMem_Free(retDataBuf
);
1159 PyExpandEnvironmentStrings(PyObject
*self
, PyObject
*args
)
1161 Py_UNICODE
*retValue
= NULL
;
1167 if (!PyArg_ParseTuple(args
, "u:ExpandEnvironmentStrings", &src
))
1170 retValueSize
= ExpandEnvironmentStringsW(src
, retValue
, 0);
1171 if (retValueSize
== 0) {
1172 return PyErr_SetFromWindowsErrWithFunction(retValueSize
,
1173 "ExpandEnvironmentStrings");
1175 retValue
= (Py_UNICODE
*)PyMem_Malloc(retValueSize
* sizeof(Py_UNICODE
));
1176 if (retValue
== NULL
) {
1177 return PyErr_NoMemory();
1180 rc
= ExpandEnvironmentStringsW(src
, retValue
, retValueSize
);
1182 PyMem_Free(retValue
);
1183 return PyErr_SetFromWindowsErrWithFunction(retValueSize
,
1184 "ExpandEnvironmentStrings");
1186 o
= PyUnicode_FromUnicode(retValue
, wcslen(retValue
));
1187 PyMem_Free(retValue
);
1192 PyFlushKey(PyObject
*self
, PyObject
*args
)
1197 if (!PyArg_ParseTuple(args
, "O:FlushKey", &obKey
))
1199 if (!PyHKEY_AsHKEY(obKey
, &hKey
, FALSE
))
1201 Py_BEGIN_ALLOW_THREADS
1202 rc
= RegFlushKey(hKey
);
1203 Py_END_ALLOW_THREADS
1204 if (rc
!= ERROR_SUCCESS
)
1205 return PyErr_SetFromWindowsErrWithFunction(rc
, "RegFlushKey");
1210 PyLoadKey(PyObject
*self
, PyObject
*args
)
1218 if (!PyArg_ParseTuple(args
, "Oss:LoadKey", &obKey
, &subKey
, &fileName
))
1220 if (!PyHKEY_AsHKEY(obKey
, &hKey
, FALSE
))
1222 Py_BEGIN_ALLOW_THREADS
1223 rc
= RegLoadKey(hKey
, subKey
, fileName
);
1224 Py_END_ALLOW_THREADS
1225 if (rc
!= ERROR_SUCCESS
)
1226 return PyErr_SetFromWindowsErrWithFunction(rc
, "RegLoadKey");
1232 PyOpenKey(PyObject
*self
, PyObject
*args
)
1241 REGSAM sam
= KEY_READ
;
1242 if (!PyArg_ParseTuple(args
, "Oz|ii:OpenKey", &obKey
, &subKey
,
1245 if (!PyHKEY_AsHKEY(obKey
, &hKey
, FALSE
))
1248 Py_BEGIN_ALLOW_THREADS
1249 rc
= RegOpenKeyEx(hKey
, subKey
, res
, sam
, &retKey
);
1250 Py_END_ALLOW_THREADS
1251 if (rc
!= ERROR_SUCCESS
)
1252 return PyErr_SetFromWindowsErrWithFunction(rc
, "RegOpenKeyEx");
1253 return PyHKEY_FromHKEY(retKey
);
1258 PyQueryInfoKey(PyObject
*self
, PyObject
*args
)
1263 DWORD nSubKeys
, nValues
;
1268 if (!PyArg_ParseTuple(args
, "O:QueryInfoKey", &obKey
))
1270 if (!PyHKEY_AsHKEY(obKey
, &hKey
, FALSE
))
1272 if ((rc
= RegQueryInfoKey(hKey
, NULL
, NULL
, 0, &nSubKeys
, NULL
, NULL
,
1273 &nValues
, NULL
, NULL
, NULL
, &ft
))
1275 return PyErr_SetFromWindowsErrWithFunction(rc
, "RegQueryInfoKey");
1276 li
.LowPart
= ft
.dwLowDateTime
;
1277 li
.HighPart
= ft
.dwHighDateTime
;
1278 l
= PyLong_FromLongLong(li
.QuadPart
);
1281 ret
= Py_BuildValue("iiO", nSubKeys
, nValues
, l
);
1287 PyQueryValue(PyObject
*self
, PyObject
*args
)
1297 if (!PyArg_ParseTuple(args
, "Oz:QueryValue", &obKey
, &subKey
))
1300 if (!PyHKEY_AsHKEY(obKey
, &hKey
, FALSE
))
1302 if ((rc
= RegQueryValue(hKey
, subKey
, NULL
, &bufSize
))
1304 return PyErr_SetFromWindowsErrWithFunction(rc
,
1306 retStr
= PyString_FromStringAndSize(NULL
, bufSize
);
1309 retBuf
= PyString_AS_STRING(retStr
);
1310 if ((rc
= RegQueryValue(hKey
, subKey
, retBuf
, &bufSize
))
1313 return PyErr_SetFromWindowsErrWithFunction(rc
,
1316 _PyString_Resize(&retStr
, strlen(retBuf
));
1321 PyQueryValueEx(PyObject
*self
, PyObject
*args
)
1334 if (!PyArg_ParseTuple(args
, "Oz:QueryValueEx", &obKey
, &valueName
))
1337 if (!PyHKEY_AsHKEY(obKey
, &hKey
, FALSE
))
1339 if ((rc
= RegQueryValueEx(hKey
, valueName
,
1343 return PyErr_SetFromWindowsErrWithFunction(rc
,
1345 retBuf
= (char *)PyMem_Malloc(bufSize
);
1347 return PyErr_NoMemory();
1348 if ((rc
= RegQueryValueEx(hKey
, valueName
, NULL
,
1349 &typ
, (BYTE
*)retBuf
, &bufSize
))
1352 return PyErr_SetFromWindowsErrWithFunction(rc
,
1355 obData
= Reg2Py(retBuf
, bufSize
, typ
);
1356 PyMem_Free((void *)retBuf
);
1359 result
= Py_BuildValue("Oi", obData
, typ
);
1366 PySaveKey(PyObject
*self
, PyObject
*args
)
1371 LPSECURITY_ATTRIBUTES pSA
= NULL
;
1374 if (!PyArg_ParseTuple(args
, "Os:SaveKey", &obKey
, &fileName
))
1376 if (!PyHKEY_AsHKEY(obKey
, &hKey
, FALSE
))
1378 /* One day we may get security into the core?
1379 if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE))
1382 Py_BEGIN_ALLOW_THREADS
1383 rc
= RegSaveKey(hKey
, fileName
, pSA
);
1384 Py_END_ALLOW_THREADS
1385 if (rc
!= ERROR_SUCCESS
)
1386 return PyErr_SetFromWindowsErrWithFunction(rc
, "RegSaveKey");
1392 PySetValue(PyObject
*self
, PyObject
*args
)
1403 if (!PyArg_ParseTuple(args
, "OOiO:SetValue",
1409 if (!PyHKEY_AsHKEY(obKey
, &hKey
, FALSE
))
1411 if (typ
!= REG_SZ
) {
1412 PyErr_SetString(PyExc_TypeError
,
1413 "Type must be _winreg.REG_SZ");
1416 /* XXX - need Unicode support */
1417 str
= PyString_AsString(obStrVal
);
1420 len
= PyString_Size(obStrVal
);
1421 if (obSubKey
== Py_None
)
1424 subKey
= PyString_AsString(obSubKey
);
1428 Py_BEGIN_ALLOW_THREADS
1429 rc
= RegSetValue(hKey
, subKey
, REG_SZ
, str
, len
+1);
1430 Py_END_ALLOW_THREADS
1431 if (rc
!= ERROR_SUCCESS
)
1432 return PyErr_SetFromWindowsErrWithFunction(rc
, "RegSetValue");
1438 PySetValueEx(PyObject
*self
, PyObject
*args
)
1451 if (!PyArg_ParseTuple(args
, "OzOiO:SetValueEx",
1458 if (!PyHKEY_AsHKEY(obKey
, &hKey
, FALSE
))
1460 if (!Py2Reg(value
, typ
, &data
, &len
))
1462 if (!PyErr_Occurred())
1463 PyErr_SetString(PyExc_ValueError
,
1464 "Could not convert the data to the specified type.");
1467 Py_BEGIN_ALLOW_THREADS
1468 rc
= RegSetValueEx(hKey
, valueName
, 0, typ
, data
, len
);
1469 Py_END_ALLOW_THREADS
1471 if (rc
!= ERROR_SUCCESS
)
1472 return PyErr_SetFromWindowsErrWithFunction(rc
,
1479 PyDisableReflectionKey(PyObject
*self
, PyObject
*args
)
1484 typedef LONG (WINAPI
*RDRKFunc
)(HKEY
);
1485 RDRKFunc pfn
= NULL
;
1488 if (!PyArg_ParseTuple(args
, "O:DisableReflectionKey", &obKey
))
1490 if (!PyHKEY_AsHKEY(obKey
, &hKey
, FALSE
))
1493 // Only available on 64bit platforms, so we must load it
1495 hMod
= GetModuleHandle("advapi32.dll");
1497 pfn
= (RDRKFunc
)GetProcAddress(hMod
,
1498 "RegDisableReflectionKey");
1500 PyErr_SetString(PyExc_NotImplementedError
,
1501 "not implemented on this platform");
1504 Py_BEGIN_ALLOW_THREADS
1506 Py_END_ALLOW_THREADS
1507 if (rc
!= ERROR_SUCCESS
)
1508 return PyErr_SetFromWindowsErrWithFunction(rc
,
1509 "RegDisableReflectionKey");
1515 PyEnableReflectionKey(PyObject
*self
, PyObject
*args
)
1520 typedef LONG (WINAPI
*RERKFunc
)(HKEY
);
1521 RERKFunc pfn
= NULL
;
1524 if (!PyArg_ParseTuple(args
, "O:EnableReflectionKey", &obKey
))
1526 if (!PyHKEY_AsHKEY(obKey
, &hKey
, FALSE
))
1529 // Only available on 64bit platforms, so we must load it
1531 hMod
= GetModuleHandle("advapi32.dll");
1533 pfn
= (RERKFunc
)GetProcAddress(hMod
,
1534 "RegEnableReflectionKey");
1536 PyErr_SetString(PyExc_NotImplementedError
,
1537 "not implemented on this platform");
1540 Py_BEGIN_ALLOW_THREADS
1542 Py_END_ALLOW_THREADS
1543 if (rc
!= ERROR_SUCCESS
)
1544 return PyErr_SetFromWindowsErrWithFunction(rc
,
1545 "RegEnableReflectionKey");
1551 PyQueryReflectionKey(PyObject
*self
, PyObject
*args
)
1556 typedef LONG (WINAPI
*RQRKFunc
)(HKEY
, BOOL
*);
1557 RQRKFunc pfn
= NULL
;
1561 if (!PyArg_ParseTuple(args
, "O:QueryReflectionKey", &obKey
))
1563 if (!PyHKEY_AsHKEY(obKey
, &hKey
, FALSE
))
1566 // Only available on 64bit platforms, so we must load it
1568 hMod
= GetModuleHandle("advapi32.dll");
1570 pfn
= (RQRKFunc
)GetProcAddress(hMod
,
1571 "RegQueryReflectionKey");
1573 PyErr_SetString(PyExc_NotImplementedError
,
1574 "not implemented on this platform");
1577 Py_BEGIN_ALLOW_THREADS
1578 rc
= (*pfn
)(hKey
, &result
);
1579 Py_END_ALLOW_THREADS
1580 if (rc
!= ERROR_SUCCESS
)
1581 return PyErr_SetFromWindowsErrWithFunction(rc
,
1582 "RegQueryReflectionKey");
1583 return PyBool_FromLong(rc
);
1586 static struct PyMethodDef winreg_methods
[] = {
1587 {"CloseKey", PyCloseKey
, METH_VARARGS
, CloseKey_doc
},
1588 {"ConnectRegistry", PyConnectRegistry
, METH_VARARGS
, ConnectRegistry_doc
},
1589 {"CreateKey", PyCreateKey
, METH_VARARGS
, CreateKey_doc
},
1590 {"DeleteKey", PyDeleteKey
, METH_VARARGS
, DeleteKey_doc
},
1591 {"DeleteValue", PyDeleteValue
, METH_VARARGS
, DeleteValue_doc
},
1592 {"DisableReflectionKey", PyDisableReflectionKey
, METH_VARARGS
, DisableReflectionKey_doc
},
1593 {"EnableReflectionKey", PyEnableReflectionKey
, METH_VARARGS
, EnableReflectionKey_doc
},
1594 {"EnumKey", PyEnumKey
, METH_VARARGS
, EnumKey_doc
},
1595 {"EnumValue", PyEnumValue
, METH_VARARGS
, EnumValue_doc
},
1596 {"ExpandEnvironmentStrings", PyExpandEnvironmentStrings
, METH_VARARGS
,
1597 ExpandEnvironmentStrings_doc
},
1598 {"FlushKey", PyFlushKey
, METH_VARARGS
, FlushKey_doc
},
1599 {"LoadKey", PyLoadKey
, METH_VARARGS
, LoadKey_doc
},
1600 {"OpenKey", PyOpenKey
, METH_VARARGS
, OpenKey_doc
},
1601 {"OpenKeyEx", PyOpenKey
, METH_VARARGS
, OpenKeyEx_doc
},
1602 {"QueryValue", PyQueryValue
, METH_VARARGS
, QueryValue_doc
},
1603 {"QueryValueEx", PyQueryValueEx
, METH_VARARGS
, QueryValueEx_doc
},
1604 {"QueryInfoKey", PyQueryInfoKey
, METH_VARARGS
, QueryInfoKey_doc
},
1605 {"QueryReflectionKey",PyQueryReflectionKey
,METH_VARARGS
, QueryReflectionKey_doc
},
1606 {"SaveKey", PySaveKey
, METH_VARARGS
, SaveKey_doc
},
1607 {"SetValue", PySetValue
, METH_VARARGS
, SetValue_doc
},
1608 {"SetValueEx", PySetValueEx
, METH_VARARGS
, SetValueEx_doc
},
1613 insint(PyObject
* d
, char * name
, long value
)
1615 PyObject
*v
= PyInt_FromLong(value
);
1616 if (!v
|| PyDict_SetItemString(d
, name
, v
))
1621 #define ADD_INT(val) insint(d, #val, val)
1624 inskey(PyObject
* d
, char * name
, HKEY key
)
1626 PyObject
*v
= PyLong_FromVoidPtr(key
);
1627 if (!v
|| PyDict_SetItemString(d
, name
, v
))
1632 #define ADD_KEY(val) inskey(d, #val, val)
1634 PyMODINIT_FUNC
init_winreg(void)
1637 m
= Py_InitModule3("_winreg", winreg_methods
, module_doc
);
1640 d
= PyModule_GetDict(m
);
1641 PyHKEY_Type
.ob_type
= &PyType_Type
;
1642 PyHKEY_Type
.tp_doc
= PyHKEY_doc
;
1643 Py_INCREF(&PyHKEY_Type
);
1644 if (PyDict_SetItemString(d
, "HKEYType",
1645 (PyObject
*)&PyHKEY_Type
) != 0)
1647 Py_INCREF(PyExc_WindowsError
);
1648 if (PyDict_SetItemString(d
, "error",
1649 PyExc_WindowsError
) != 0)
1652 /* Add the relevant constants */
1653 ADD_KEY(HKEY_CLASSES_ROOT
);
1654 ADD_KEY(HKEY_CURRENT_USER
);
1655 ADD_KEY(HKEY_LOCAL_MACHINE
);
1656 ADD_KEY(HKEY_USERS
);
1657 ADD_KEY(HKEY_PERFORMANCE_DATA
);
1658 #ifdef HKEY_CURRENT_CONFIG
1659 ADD_KEY(HKEY_CURRENT_CONFIG
);
1661 #ifdef HKEY_DYN_DATA
1662 ADD_KEY(HKEY_DYN_DATA
);
1664 ADD_INT(KEY_QUERY_VALUE
);
1665 ADD_INT(KEY_SET_VALUE
);
1666 ADD_INT(KEY_CREATE_SUB_KEY
);
1667 ADD_INT(KEY_ENUMERATE_SUB_KEYS
);
1668 ADD_INT(KEY_NOTIFY
);
1669 ADD_INT(KEY_CREATE_LINK
);
1672 ADD_INT(KEY_EXECUTE
);
1673 ADD_INT(KEY_ALL_ACCESS
);
1674 #ifdef KEY_WOW64_64KEY
1675 ADD_INT(KEY_WOW64_64KEY
);
1677 #ifdef KEY_WOW64_32KEY
1678 ADD_INT(KEY_WOW64_32KEY
);
1680 ADD_INT(REG_OPTION_RESERVED
);
1681 ADD_INT(REG_OPTION_NON_VOLATILE
);
1682 ADD_INT(REG_OPTION_VOLATILE
);
1683 ADD_INT(REG_OPTION_CREATE_LINK
);
1684 ADD_INT(REG_OPTION_BACKUP_RESTORE
);
1685 ADD_INT(REG_OPTION_OPEN_LINK
);
1686 ADD_INT(REG_LEGAL_OPTION
);
1687 ADD_INT(REG_CREATED_NEW_KEY
);
1688 ADD_INT(REG_OPENED_EXISTING_KEY
);
1689 ADD_INT(REG_WHOLE_HIVE_VOLATILE
);
1690 ADD_INT(REG_REFRESH_HIVE
);
1691 ADD_INT(REG_NO_LAZY_FLUSH
);
1692 ADD_INT(REG_NOTIFY_CHANGE_NAME
);
1693 ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES
);
1694 ADD_INT(REG_NOTIFY_CHANGE_LAST_SET
);
1695 ADD_INT(REG_NOTIFY_CHANGE_SECURITY
);
1696 ADD_INT(REG_LEGAL_CHANGE_FILTER
);
1699 ADD_INT(REG_EXPAND_SZ
);
1700 ADD_INT(REG_BINARY
);
1702 ADD_INT(REG_DWORD_LITTLE_ENDIAN
);
1703 ADD_INT(REG_DWORD_BIG_ENDIAN
);
1705 ADD_INT(REG_MULTI_SZ
);
1706 ADD_INT(REG_RESOURCE_LIST
);
1707 ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR
);
1708 ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST
);