source: vendor/python/2.5/PC/_winreg.c

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

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