blob: 3dccc3db319f295f896627589f30b7b85d76befb [file] [log] [blame]
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001/*
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 */
37static char 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
74static char 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
82static char 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 exception is raised.";
92
93static char 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
106static char 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, and exception is raised.";
117
118static char 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
124static char 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 exception is raised, indicating\n"
132"no more values are available.\n";
133
134static char 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 exception is raised,\n"
141"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
149static char 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.\n";
161
162static char LoadKey_doc[] =
163"RegLoadKey(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
181static char 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 exception is raised.\n";
192
193static char OpenKeyEx_doc[] =
194"See OpenKey()";
195
196static char QueryInfoKey_doc[] =
197"tuple = QueryInfoKey(key) - Returns information about a key.\n"
198"\n"
199"key is an already open key, or any one of the predefined HKEY_* constants.\n"
200"\n"
201"The result is a tuple of 3 items:"
202"An integer that identifies the number of sub keys this key has.\n"
203"An integer that identifies the number of values this key has.\n"
204"A long integer that identifies when the key was last modified (if available)\n"
205" as 100's of nanoseconds since Jan 1, 1600.\n";
206
207static char QueryValue_doc[] =
208"string = QueryValue(key, sub_key) - retrieves the unnamed value for a key.\n"
209"\n"
210"key is an already open key, or any one of the predefined HKEY_* constants.\n"
211"sub_key is a string that holds The name of the subkey with which the value\n"
212" is associated. If this parameter is None or empty, the function retrieves\n"
213" the value set by the SetValue() method for the key identified by key."
214"\n"
215"Values in the registry have name, type, and data components. This method\n"
216"retrieves the data for a key's first value that has a NULL name.\n"
217"But the underlying API call doesn't return the type, Lame Lame Lame, DONT USE THIS!!!";
218
219static char QueryValueEx_doc[] =
220"value,type_id = QueryValueEx(key, value_name) - Retrieves the type and data for a specified value name associated with an open registry key.\n"
221"\n"
222"key is an already open key, or any one of the predefined HKEY_* constants.\n"
223"value_name is a string indicating the value to query";
224
225static char SaveKey_doc[] =
226"SaveKey(key, file_name) - Saves the specified key, and all its subkeys to the specified file.\n"
227"\n"
228"key is an already open key, or any one of the predefined HKEY_* constants.\n"
229"file_name is the name of the file to save registry data to.\n"
230" This file cannot already exist. If this filename includes an extension,\n"
231" it cannot be used on file allocation table (FAT) file systems by the\n"
232" LoadKey(), ReplaceKey() or RestoreKey() methods.\n"
233"\n"
234"If key represents a key on a remote computer, the path described by\n"
235"file_name is relative to the remote computer.\n"
236"The caller of this method must possess the SeBackupPrivilege security privilege.\n"
237"This function passes NULL for security_attributes to the API.";
238
239static char SetValue_doc[] =
240"SetValue(key, sub_key, type, value) - Associates a value with a specified key.\n"
241"\n"
242"key is an already open key, or any one of the predefined HKEY_* constants.\n"
243"sub_key is a string that names the subkey with which the value is associated.\n"
244"type is an integer that specifies the type of the data. Currently this\n"
245" must be REG_SZ, meaning only strings are supported.\n"
246"value is a string that specifies the new value.\n"
247"\n"
248"If the key specified by the sub_key parameter does not exist, the SetValue\n"
249"function creates it.\n"
250"\n"
251"Value lengths are limited by available memory. Long values (more than\n"
252"2048 bytes) should be stored as files with the filenames stored in \n"
253"the configuration registry. This helps the registry perform efficiently.\n"
254"\n"
255"The key identified by the key parameter must have been opened with\n"
256"KEY_SET_VALUE access.";
257
258static char SetValueEx_doc[] =
259"SetValueEx(key, value_name, reserved, type, value) - Stores data in the value field of an open registry key.\n"
260"\n"
261"key is an already open key, or any one of the predefined HKEY_* constants.\n"
262"sub_key is a string that names the subkey with which the value is associated.\n"
263"type is an integer that specifies the type of the data. This should be one of:\n"
264" REG_BINARY -- Binary data in any form.\n"
265" REG_DWORD -- A 32-bit number.\n"
266" REG_DWORD_LITTLE_ENDIAN -- A 32-bit number in little-endian format.\n"
267" REG_DWORD_BIG_ENDIAN -- A 32-bit number in big-endian format.\n"
268" REG_EXPAND_SZ -- A null-terminated string that contains unexpanded references\n"
269" to environment variables (for example, %PATH%).\n"
270" REG_LINK -- A Unicode symbolic link.\n"
271" REG_MULTI_SZ -- An array of null-terminated strings, terminated by\n"
272" two null characters.\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.\n";
288
289/* PyHKEY docstrings */
290static char PyHKEY_doc[] =
291"PyHKEY Object - A Python object, representing a win32 registry key.\n"
292"\n"
293"This object wraps a win32 HANDLE 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.\n";
311
312
313static char PyHKEY_Close_doc[] =
314"key.Close() - Closes the underlying Win32 handle.\n"
315"\n"
316"If the handle is already closed, no error is raised.";
317
318static char PyHKEY_Detach_doc[] =
319"int = key.Detach() - Detaches the Win32 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.";
328
329
330/************************************************************************
331
332 The PyHKEY object definition
333
334************************************************************************/
335typedef struct {
336 PyObject_VAR_HEAD
337 HKEY hkey;
338} PyHKEYObject;
339
340#define PyHKEY_Check(op) ((op)->ob_type == &PyHKEY_Type)
341
342static char *failMsg = "bad operand type";
343
344static PyObject *
345PyHKEY_unaryFailureFunc(PyObject *ob)
346{
347 PyErr_SetString(PyExc_TypeError, failMsg);
348 return NULL;
349}
350static PyObject *
351PyHKEY_binaryFailureFunc(PyObject *ob1, PyObject *ob2)
352{
353 PyErr_SetString(PyExc_TypeError, failMsg);
354 return NULL;
355}
356static PyObject *
357PyHKEY_ternaryFailureFunc(PyObject *ob1, PyObject *ob2, PyObject *ob3)
358{
359 PyErr_SetString(PyExc_TypeError, failMsg);
360 return NULL;
361}
362
363static void
364PyHKEY_deallocFunc(PyObject *ob)
365{
366 /* Can not call PyHKEY_Close, as the ob->tp_type
367 has already been cleared, thus causing the type
368 check to fail!
369 */
370 PyHKEYObject *obkey = (PyHKEYObject *)ob;
371 if (obkey->hkey)
372 RegCloseKey((HKEY)obkey->hkey);
373 PyMem_DEL(ob);
374}
375
376static int
377PyHKEY_nonzeroFunc(PyObject *ob)
378{
379 return ((PyHKEYObject *)ob)->hkey != 0;
380}
381
382static PyObject *
383PyHKEY_intFunc(PyObject *ob)
384{
385 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
386 return PyLong_FromVoidPtr(pyhkey->hkey);
387}
388
389static int
390PyHKEY_printFunc(PyObject *ob, FILE *fp, int flags)
391{
392 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
393 char resBuf[160];
394 wsprintf(resBuf, "<PyHKEY at %p (%p)>",
395 ob, pyhkey->hkey);
396 fputs(resBuf, fp);
397 return 0;
398}
399
400static PyObject *
401PyHKEY_strFunc(PyObject *ob)
402{
403 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
404 char resBuf[160];
405 wsprintf(resBuf, "<PyHKEY:%p>", pyhkey->hkey);
406 return PyString_FromString(resBuf);
407}
408
409static int
410PyHKEY_compareFunc(PyObject *ob1, PyObject *ob2)
411{
412 PyHKEYObject *pyhkey1 = (PyHKEYObject *)ob1;
413 PyHKEYObject *pyhkey2 = (PyHKEYObject *)ob2;
414 return pyhkey1 == pyhkey2 ? 0 :
415 (pyhkey1 < pyhkey2 ? -1 : 1);
416}
417
418static long
419PyHKEY_hashFunc(PyObject *ob)
420{
421 /* Just use the address.
422 XXX - should we use the handle value?
423 */
424 return (long)ob;
425}
426
427
428static PyNumberMethods PyHKEY_NumberMethods =
429{
430 PyHKEY_binaryFailureFunc, /* nb_add */
431 PyHKEY_binaryFailureFunc, /* nb_subtract */
432 PyHKEY_binaryFailureFunc, /* nb_multiply */
433 PyHKEY_binaryFailureFunc, /* nb_divide */
434 PyHKEY_binaryFailureFunc, /* nb_remainder */
435 PyHKEY_binaryFailureFunc, /* nb_divmod */
436 PyHKEY_ternaryFailureFunc, /* nb_power */
437 PyHKEY_unaryFailureFunc, /* nb_negative */
438 PyHKEY_unaryFailureFunc, /* nb_positive */
439 PyHKEY_unaryFailureFunc, /* nb_absolute */
440 PyHKEY_nonzeroFunc, /* nb_nonzero */
441 PyHKEY_unaryFailureFunc, /* nb_invert */
442 PyHKEY_binaryFailureFunc, /* nb_lshift */
443 PyHKEY_binaryFailureFunc, /* nb_rshift */
444 PyHKEY_binaryFailureFunc, /* nb_and */
445 PyHKEY_binaryFailureFunc, /* nb_xor */
446 PyHKEY_binaryFailureFunc, /* nb_or */
447 0, /* nb_coerce (allowed to be zero) */
448 PyHKEY_intFunc, /* nb_int */
449 PyHKEY_unaryFailureFunc, /* nb_long */
450 PyHKEY_unaryFailureFunc, /* nb_float */
451 PyHKEY_unaryFailureFunc, /* nb_oct */
452 PyHKEY_unaryFailureFunc, /* nb_hex */
453};
454
455
456/* fwd declare __getattr__ */
457static PyObject *PyHKEY_getattr(PyObject *self, char *name);
458
459/* The type itself */
460PyTypeObject PyHKEY_Type =
461{
462 PyObject_HEAD_INIT(0) /* fill in type at module init */
463 0,
464 "PyHKEY",
465 sizeof(PyHKEYObject),
466 0,
467 PyHKEY_deallocFunc, /* tp_dealloc */
468 PyHKEY_printFunc, /* tp_print */
469 PyHKEY_getattr, /* tp_getattr */
470 0, /* tp_setattr */
471 PyHKEY_compareFunc, /* tp_compare */
472 0, /* tp_repr */
473 &PyHKEY_NumberMethods, /* tp_as_number */
474 0, /* tp_as_sequence */
475 0, /* tp_as_mapping */
476 PyHKEY_hashFunc, /* tp_hash */
477 0, /* tp_call */
478 PyHKEY_strFunc, /* tp_str */
479 0, /* tp_getattro */
480 0, /* tp_setattro */
481 0, /* tp_as_buffer */
482 0, /* tp_flags */
483 PyHKEY_doc, /* tp_doc */
484};
485
486#define OFF(e) offsetof(PyHKEYObject, e)
487
488static struct memberlist PyHKEY_memberlist[] = {
489 {"handle", T_INT, OFF(hkey)},
490 {NULL} /* Sentinel */
491};
492
493/************************************************************************
494
495 The PyHKEY object methods
496
497************************************************************************/
498static PyObject *
499PyHKEY_CloseMethod(PyObject *self, PyObject *args)
500{
501 if (!PyArg_ParseTuple(args, ":Close"))
502 return NULL;
503 if (!PyHKEY_Close(self))
504 return NULL;
505 Py_INCREF(Py_None);
506 return Py_None;
507}
508
509static PyObject *
510PyHKEY_DetachMethod(PyObject *self, PyObject *args)
511{
512 void* ret;
513 PyHKEYObject *pThis = (PyHKEYObject *)self;
514 if (!PyArg_ParseTuple(args, ":Detach"))
515 return NULL;
516 ret = (void*)pThis->hkey;
517 pThis->hkey = 0;
518 return PyLong_FromVoidPtr(ret);
519}
520
521static struct PyMethodDef PyHKEY_methods[] = {
522 {"Close", PyHKEY_CloseMethod, 1, PyHKEY_Close_doc},
523 {"Detach", PyHKEY_DetachMethod, 1, PyHKEY_Detach_doc},
524 {NULL}
525};
526
527/*static*/ PyObject *
528PyHKEY_getattr(PyObject *self, char *name)
529{
530 PyObject *res;
531
532 res = Py_FindMethod(PyHKEY_methods, self, name);
533 if (res != NULL)
534 return res;
535 PyErr_Clear();
536 if (strcmp(name, "handle") == 0)
537 return PyLong_FromVoidPtr(((PyHKEYObject *)self)->hkey);
538 return PyMember_Get((char *)self, PyHKEY_memberlist, name);
539}
540
541/************************************************************************
542 The public PyHKEY API (well, not public yet :-)
543************************************************************************/
544PyObject *
545PyHKEY_New(HKEY hInit)
546{
547 PyHKEYObject *key = PyObject_NEW(PyHKEYObject, &PyHKEY_Type);
548 if (key)
549 key->hkey = hInit;
550 return (PyObject *)key;
551}
552
553BOOL
554PyHKEY_Close(PyObject *ob_handle)
555{
556 LONG rc;
557 PyHKEYObject *key;
558
559 if (!PyHKEY_Check(ob_handle)) {
560 PyErr_SetString(PyExc_TypeError, "bad operand type");
561 return FALSE;
562 }
563 key = (PyHKEYObject *)ob_handle;
564 rc = key->hkey ? RegCloseKey((HKEY)key->hkey) : ERROR_SUCCESS;
565 key->hkey = 0;
566 if (rc != ERROR_SUCCESS)
567 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
568 return rc == ERROR_SUCCESS;
569}
570
571BOOL
572PyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK)
573{
574 if (ob == Py_None) {
575 if (!bNoneOK) {
576 PyErr_SetString(
577 PyExc_TypeError,
578 "None is not a valid HKEY in this context");
579 return FALSE;
580 }
581 *pHANDLE = (HKEY)0;
582 }
583 else if (PyHKEY_Check(ob)) {
584 PyHKEYObject *pH = (PyHKEYObject *)ob;
585 *pHANDLE = pH->hkey;
586 }
587 else if (PyInt_Check(ob) || PyLong_Check(ob)) {
588 /* We also support integers */
589 PyErr_Clear();
590 *pHANDLE = (HKEY)PyLong_AsVoidPtr(ob);
591 if (PyErr_Occurred())
592 return FALSE;
593 *pHANDLE = (HKEY)PyInt_AsLong(ob);
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 = (PyHKEYObject *) malloc(sizeof(PyHKEYObject));
608 if (op == NULL)
609 return PyErr_NoMemory();
610 op->ob_type = &PyHKEY_Type;
611 op->hkey = h;
612 _Py_NewReference((PyObject *)op);
613 return (PyObject *)op;
614}
615
616
617/************************************************************************
618 The module methods
619************************************************************************/
620BOOL
621PyWinObject_CloseHKEY(PyObject *obHandle)
622{
623 BOOL ok;
624 if (PyHKEY_Check(obHandle)) {
625 ok = PyHKEY_Close(obHandle);
626 }
627 else if (PyInt_Check(obHandle)) {
628 long rc = RegCloseKey((HKEY)PyInt_AsLong(obHandle));
629 ok = (rc == ERROR_SUCCESS);
630 if (!ok)
631 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
632 }
633 else {
634 PyErr_SetString(
635 PyExc_TypeError,
636 "A handle must be a HKEY object or an integer");
637 return FALSE;
638 }
639 return ok;
640}
641
642
643/*
644 Private Helper functions for the registry interfaces
645
646** Note that fixupMultiSZ and countString have both had changes
647** made to support "incorrect strings". The registry specification
648** calls for strings to be terminated with 2 null bytes. It seems
649** some commercial packages install strings whcich dont conform,
650** causing this code to fail - however, "regedit" etc still work
651** with these strings (ie only we dont!).
652*/
653static void
654fixupMultiSZ(char **str, char *data, int len)
655{
656 char *P;
657 int i;
658 char *Q;
659
660 Q = data + len;
661 for (P = data, i = 0; P < Q && *P != '\0'; P++, i++) {
662 str[i] = P;
663 for(; *P != '\0'; P++)
664 ;
665 }
666}
667
668static int
669countStrings(char *data, int len)
670{
671 int strings;
672 char *P;
673 char *Q = data + len;
674
675 for (P = data, strings = 0; P < Q && *P != '\0'; P++, strings++)
676 for (; P < Q && *P != '\0'; P++)
677 ;
678 return strings;
679}
680
681/* Convert PyObject into Registry data.
682 Allocates space as needed. */
683static BOOL
684Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
685{
686 int i,j;
687 switch (typ) {
688 case REG_DWORD:
689 if (value != Py_None && !PyInt_Check(value))
690 return FALSE;
691 *retDataBuf = (BYTE *)PyMem_NEW(DWORD, sizeof(DWORD));
692 if (*retDataBuf==NULL){
693 PyErr_NoMemory();
694 return FALSE;
695 }
696 *retDataSize = sizeof(DWORD);
697 if (value == Py_None) {
698 DWORD zero = 0;
699 memcpy(*retDataBuf, &zero, sizeof(DWORD));
700 }
701 else
702 memcpy(*retDataBuf,
703 &PyInt_AS_LONG((PyIntObject *)value),
704 sizeof(DWORD));
705 break;
706 case REG_SZ:
707 case REG_EXPAND_SZ:
708 {
709 int need_decref = 0;
710 if (value == Py_None)
711 *retDataSize = 1;
712 else {
713 if (PyUnicode_Check(value)) {
714 value = PyUnicode_AsEncodedString(
715 value,
716 "mbcs",
717 NULL);
718 if (value==NULL)
719 return FALSE;
720 need_decref = 1;
721 }
722 if (!PyString_Check(value))
723 return FALSE;
724 *retDataSize = 1 + strlen(
725 PyString_AS_STRING(
726 (PyStringObject *)value));
727 }
728 *retDataBuf = (BYTE *)PyMem_NEW(DWORD, *retDataSize);
729 if (*retDataBuf==NULL){
730 PyErr_NoMemory();
731 return FALSE;
732 }
733 if (value == Py_None)
734 strcpy((char *)*retDataBuf, "");
735 else
736 strcpy((char *)*retDataBuf,
737 PyString_AS_STRING(
738 (PyStringObject *)value));
739 if (need_decref)
740 Py_DECREF(value);
741 break;
742 }
743 case REG_MULTI_SZ:
744 {
745 DWORD size = 0;
746 char *P;
747 PyObject **obs = NULL;
748
749 if (value == Py_None)
750 i = 0;
751 else {
752 if (!PyList_Check(value))
753 return FALSE;
754 i = PyList_Size(value);
755 }
756 obs = malloc(sizeof(PyObject *) * i);
757 memset(obs, 0, sizeof(PyObject *) * i);
758 for (j = 0; j < i; j++)
759 {
760 PyObject *t;
761 t = PyList_GET_ITEM(
762 (PyListObject *)value,j);
763 if (PyString_Check(t)) {
764 obs[j] = t;
765 Py_INCREF(t);
766 } else if (PyUnicode_Check(t)) {
767 obs[j] = PyUnicode_AsEncodedString(
768 t,
769 "mbcs",
770 NULL);
771 if (obs[j]==NULL)
772 goto reg_multi_fail;
773 } else
774 goto reg_multi_fail;
775 size += 1 + strlen(
776 PyString_AS_STRING(
777 (PyStringObject *)obs[j]));
778 }
779
780 *retDataSize = size + 1;
781 *retDataBuf = (BYTE *)PyMem_NEW(char,
782 *retDataSize);
783 if (*retDataBuf==NULL){
784 PyErr_NoMemory();
785 goto reg_multi_fail;
786 }
787 P = (char *)*retDataBuf;
788
789 for (j = 0; j < i; j++)
790 {
791 PyObject *t;
792 t = obs[j];
793 strcpy(P,
794 PyString_AS_STRING(
795 (PyStringObject *)t));
796 P += 1 + strlen(
797 PyString_AS_STRING(
798 (PyStringObject *)t));
799 Py_DECREF(obs[j]);
800 }
801 /* And doubly-terminate the list... */
802 *P = '\0';
803 free(obs);
804 break;
805 reg_multi_fail:
806 if (obs) {
807 for (j = 0; j < i; j++)
808 Py_XDECREF(obs[j]);
809
810 free(obs);
811 }
812 return FALSE;
813 }
814 case REG_BINARY:
815 /* ALSO handle ALL unknown data types here. Even if we can't
816 support it natively, we should handle the bits. */
817 default:
818 if (value == Py_None)
819 *retDataSize = 0;
820 else {
821 if (!PyString_Check(value))
822 return 0;
823 *retDataSize = PyString_Size(value);
824 *retDataBuf = (BYTE *)PyMem_NEW(char,
825 *retDataSize);
826 if (*retDataBuf==NULL){
827 PyErr_NoMemory();
828 return FALSE;
829 }
830 memcpy(*retDataBuf,
831 PyString_AS_STRING(
832 (PyStringObject *)value),
833 *retDataSize);
834 }
835 break;
836 }
837 return TRUE;
838}
839
840/* Convert Registry data into PyObject*/
841static PyObject *
842Reg2Py(char *retDataBuf, DWORD retDataSize, DWORD typ)
843{
844 PyObject *obData;
845
846 switch (typ) {
847 case REG_DWORD:
848 if (retDataSize == 0)
849 obData = Py_BuildValue("i", 0);
850 else
851 obData = Py_BuildValue("i",
852 *(int *)retDataBuf);
853 break;
854 case REG_SZ:
855 case REG_EXPAND_SZ:
856 /* retDataBuf may or may not have a trailing NULL in
857 the buffer. */
858 if (retDataSize && retDataBuf[retDataSize-1] == '\0')
859 --retDataSize;
860 if (retDataSize ==0)
861 retDataBuf = "";
862 obData = PyUnicode_DecodeMBCS(retDataBuf,
863 retDataSize,
864 NULL);
865 break;
866 case REG_MULTI_SZ:
867 if (retDataSize == 0)
868 obData = PyList_New(0);
869 else
870 {
871 int index = 0;
872 int s = countStrings(retDataBuf, retDataSize);
873 char **str = (char **)malloc(sizeof(char *)*s);
874 if (str == NULL)
875 return PyErr_NoMemory();
876
877 fixupMultiSZ(str, retDataBuf, retDataSize);
878 obData = PyList_New(s);
879 for (index = 0; index < s; index++)
880 {
881 PyList_SetItem(obData,
882 index,
883 PyUnicode_DecodeMBCS(
884 (const char *)str[index],
885 _mbstrlen(str[index]),
886 NULL)
887 );
888 }
889 free(str);
890
891 break;
892 }
893 case REG_BINARY:
894 /* ALSO handle ALL unknown data types here. Even if we can't
895 support it natively, we should handle the bits. */
896 default:
897 if (retDataSize == 0) {
898 Py_INCREF(Py_None);
899 obData = Py_None;
900 }
901 else
902 obData = Py_BuildValue("s#",
903 (char *)retDataBuf,
904 retDataSize);
905 break;
906 }
907 if (obData == NULL)
908 return NULL;
909 else
910 return obData;
911}
912
913/* The Python methods */
914
915static PyObject *
916PyCloseKey(PyObject *self, PyObject *args)
917{
918 PyObject *obKey;
919 if (!PyArg_ParseTuple(args, "O:CloseKey", &obKey))
920 return NULL;
921 if (!PyHKEY_Close(obKey))
922 return NULL;
923 Py_INCREF(Py_None);
924 return Py_None;
925}
926
927static PyObject *
928PyConnectRegistry(PyObject *self, PyObject *args)
929{
930 HKEY hKey;
931 PyObject *obKey;
932 char *szCompName = NULL;
933 HKEY retKey;
934 long rc;
935 if (!PyArg_ParseTuple(args, "zO:ConnectRegistry", &szCompName, &obKey))
936 return NULL;
937 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
938 return NULL;
939 rc = RegConnectRegistry(szCompName, hKey, &retKey);
940 if (rc != ERROR_SUCCESS)
941 return PyErr_SetFromWindowsErrWithFunction(rc,
942 "ConnectRegistry");
943 return PyHKEY_FromHKEY(retKey);
944}
945
946static PyObject *
947PyCreateKey(PyObject *self, PyObject *args)
948{
949 HKEY hKey;
950 PyObject *obKey;
951 char *subKey;
952 HKEY retKey;
953 long rc;
954 if (!PyArg_ParseTuple(args, "Oz:CreateKey", &obKey, &subKey))
955 return NULL;
956 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
957 return NULL;
958 rc = RegCreateKey(hKey, subKey, &retKey);
959 if (rc != ERROR_SUCCESS)
960 return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey");
961 return PyHKEY_FromHKEY(retKey);
962}
963
964static PyObject *
965PyDeleteKey(PyObject *self, PyObject *args)
966{
967 HKEY hKey;
968 PyObject *obKey;
969 char *subKey;
970 long rc;
971 if (!PyArg_ParseTuple(args, "Os:DeleteKey", &obKey, &subKey))
972 return NULL;
973 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
974 return NULL;
975 rc = RegDeleteKey(hKey, subKey );
976 if (rc != ERROR_SUCCESS)
977 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey");
978 Py_INCREF(Py_None);
979 return Py_None;
980}
981
982static PyObject *
983PyDeleteValue(PyObject *self, PyObject *args)
984{
985 HKEY hKey;
986 PyObject *obKey;
987 char *subKey;
988 long rc;
989 if (!PyArg_ParseTuple(args, "Oz:DeleteValue", &obKey, &subKey))
990 return NULL;
991 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
992 return NULL;
993 Py_BEGIN_ALLOW_THREADS
994 rc = RegDeleteValue(hKey, subKey);
995 Py_END_ALLOW_THREADS
996 if (rc !=ERROR_SUCCESS)
997 return PyErr_SetFromWindowsErrWithFunction(rc,
998 "RegDeleteValue");
999 Py_INCREF(Py_None);
1000 return Py_None;
1001}
1002
1003static PyObject *
1004PyEnumKey(PyObject *self, PyObject *args)
1005{
1006 HKEY hKey;
1007 PyObject *obKey;
1008 int index;
1009 long rc;
1010 char *retBuf;
1011 DWORD len;
1012
1013 if (!PyArg_ParseTuple(args, "Oi:EnumKey", &obKey, &index))
1014 return NULL;
1015 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1016 return NULL;
1017
1018 if ((rc = RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, &len,
1019 NULL, NULL, NULL, NULL, NULL, NULL))
1020 != ERROR_SUCCESS)
1021 return PyErr_SetFromWindowsErrWithFunction(rc,
1022 "RegQueryInfoKey");
1023 ++len; /* include null terminator */
1024 retBuf = (char *)alloca(len);
1025
1026 if ((rc = RegEnumKey(hKey, index, retBuf, len)) != ERROR_SUCCESS)
1027 return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKey");
1028 return Py_BuildValue("s", retBuf);
1029}
1030
1031static PyObject *
1032PyEnumValue(PyObject *self, PyObject *args)
1033{
1034 HKEY hKey;
1035 PyObject *obKey;
1036 int index;
1037 long rc;
1038 char *retValueBuf;
1039 char *retDataBuf;
1040 DWORD retValueSize;
1041 DWORD retDataSize;
1042 DWORD typ;
1043 PyObject *obData;
1044 PyObject *retVal;
1045
1046 if (!PyArg_ParseTuple(args, "Oi:EnumValue", &obKey, &index))
1047 return NULL;
1048 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1049 return NULL;
1050
1051 if ((rc = RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, NULL, NULL,
1052 NULL,
1053 &retValueSize, &retDataSize, NULL, NULL))
1054 != ERROR_SUCCESS)
1055 return PyErr_SetFromWindowsErrWithFunction(rc,
1056 "RegQueryInfoKey");
1057 ++retValueSize; /* include null terminators */
1058 ++retDataSize;
1059 retValueBuf = (char *)alloca(retValueSize);
1060 retDataBuf = (char *)alloca(retDataSize);
1061
1062 Py_BEGIN_ALLOW_THREADS
1063 rc = RegEnumValue(hKey,
1064 index,
1065 retValueBuf,
1066 &retValueSize,
1067 NULL,
1068 &typ,
1069 (BYTE *)retDataBuf,
1070 &retDataSize);
1071 Py_END_ALLOW_THREADS
1072
1073 if (rc != ERROR_SUCCESS)
1074 return PyErr_SetFromWindowsErrWithFunction(rc,
1075 "PyRegEnumValue");
1076 obData = Reg2Py(retDataBuf, retDataSize, typ);
1077 if (obData == NULL)
1078 return NULL;
1079 retVal = Py_BuildValue("sOi", retValueBuf, obData, typ);
1080 Py_DECREF(obData);
1081 return retVal;
1082}
1083
1084static PyObject *
1085PyFlushKey(PyObject *self, PyObject *args)
1086{
1087 HKEY hKey;
1088 PyObject *obKey;
1089 long rc;
1090 if (!PyArg_ParseTuple(args, "O:FlushKey", &obKey))
1091 return NULL;
1092 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1093 return NULL;
1094 Py_BEGIN_ALLOW_THREADS
1095 rc = RegFlushKey(hKey);
1096 Py_END_ALLOW_THREADS
1097 if (rc != ERROR_SUCCESS)
1098 return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey");
1099 Py_INCREF(Py_None);
1100 return Py_None;
1101}
1102static PyObject *
1103PyLoadKey(PyObject *self, PyObject *args)
1104{
1105 HKEY hKey;
1106 PyObject *obKey;
1107 char *subKey;
1108 char *fileName;
1109
1110 long rc;
1111 if (!PyArg_ParseTuple(args, "Oss:LoadKey", &obKey, &subKey, &fileName))
1112 return NULL;
1113 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1114 return NULL;
1115 Py_BEGIN_ALLOW_THREADS
1116 rc = RegLoadKey(hKey, subKey, fileName );
1117 Py_END_ALLOW_THREADS
1118 if (rc != ERROR_SUCCESS)
1119 return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey");
1120 Py_INCREF(Py_None);
1121 return Py_None;
1122}
1123
1124static PyObject *
1125PyOpenKey(PyObject *self, PyObject *args)
1126{
1127 HKEY hKey;
1128 PyObject *obKey;
1129
1130 char *subKey;
1131 int res = 0;
1132 HKEY retKey;
1133 long rc;
1134 REGSAM sam = KEY_READ;
1135 if (!PyArg_ParseTuple(args, "Oz|ii:OpenKey", &obKey, &subKey,
1136 &res, &sam))
1137 return NULL;
1138 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1139 return NULL;
1140
1141 Py_BEGIN_ALLOW_THREADS
1142 rc = RegOpenKeyEx(hKey, subKey, res, sam, &retKey);
1143 Py_END_ALLOW_THREADS
1144 if (rc != ERROR_SUCCESS)
1145 return PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx");
1146 return PyHKEY_FromHKEY(retKey);
1147}
1148
1149
1150static PyObject *
1151PyQueryInfoKey(PyObject *self, PyObject *args)
1152{
1153 HKEY hKey;
1154 PyObject *obKey;
1155 long rc;
1156 DWORD nSubKeys, nValues;
1157 FILETIME ft;
1158 LARGE_INTEGER li;
1159 PyObject *l;
1160 PyObject *ret;
1161 if (!PyArg_ParseTuple(args, "O:QueryInfoKey", &obKey))
1162 return NULL;
1163 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1164 return NULL;
1165 if ((rc = RegQueryInfoKey(hKey, NULL, NULL, 0, &nSubKeys, NULL, NULL,
1166 &nValues, NULL, NULL, NULL, &ft))
1167 != ERROR_SUCCESS)
1168 return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey");
1169 li.LowPart = ft.dwLowDateTime;
1170 li.HighPart = ft.dwHighDateTime;
1171 l = PyLong_FromLongLong(li.QuadPart);
1172 if (l == NULL)
1173 return NULL;
1174 ret = Py_BuildValue("iiO", nSubKeys, nValues, l);
1175 Py_DECREF(l);
1176 return ret;
1177}
1178
1179static PyObject *
1180PyQueryValue(PyObject *self, PyObject *args)
1181{
1182 HKEY hKey;
1183 PyObject *obKey;
1184 char *subKey;
1185
1186 long rc;
1187 char *retBuf;
1188 long bufSize = 0;
1189 if (!PyArg_ParseTuple(args, "Oz:QueryValue", &obKey, &subKey))
1190 return NULL;
1191
1192 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1193 return NULL;
1194 if ((rc = RegQueryValue(hKey, subKey, NULL, &bufSize))
1195 != ERROR_SUCCESS)
1196 return PyErr_SetFromWindowsErrWithFunction(rc,
1197 "RegQueryValue");
1198 retBuf = (char *)alloca(bufSize);
1199 if ((rc = RegQueryValue(hKey, subKey, retBuf, &bufSize))
1200 != ERROR_SUCCESS)
1201 return PyErr_SetFromWindowsErrWithFunction(rc,
1202 "RegQueryValue");
1203 return Py_BuildValue("s", retBuf);
1204}
1205
1206static PyObject *
1207PyQueryValueEx(PyObject *self, PyObject *args)
1208{
1209 HKEY hKey;
1210 PyObject *obKey;
1211 char *valueName;
1212
1213 long rc;
1214 char *retBuf;
1215 DWORD bufSize = 0;
1216 DWORD typ;
1217 PyObject *obData;
1218 PyObject *result;
1219
1220 if (!PyArg_ParseTuple(args, "Oz:QueryValueEx", &obKey, &valueName))
1221 return NULL;
1222
1223 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1224 return NULL;
1225 if ((rc = RegQueryValueEx(hKey, valueName,
1226 NULL, NULL, NULL,
1227 &bufSize))
1228 != ERROR_SUCCESS)
1229 return PyErr_SetFromWindowsErrWithFunction(rc,
1230 "RegQueryValueEx");
1231 retBuf = (char *)alloca(bufSize);
1232 if ((rc = RegQueryValueEx(hKey, valueName, NULL,
1233 &typ, (BYTE *)retBuf, &bufSize))
1234 != ERROR_SUCCESS)
1235 return PyErr_SetFromWindowsErrWithFunction(rc,
1236 "RegQueryValueEx");
1237 obData = Reg2Py(retBuf, bufSize, typ);
1238 if (obData == NULL)
1239 return NULL;
1240 result = Py_BuildValue("Oi", obData, typ);
1241 Py_DECREF(obData);
1242 return result;
1243}
1244
1245
1246static PyObject *
1247PySaveKey(PyObject *self, PyObject *args)
1248{
1249 HKEY hKey;
1250 PyObject *obKey;
1251 char *fileName;
1252 LPSECURITY_ATTRIBUTES pSA = NULL;
1253
1254 long rc;
1255 if (!PyArg_ParseTuple(args, "Os:SaveKey", &obKey, &fileName))
1256 return NULL;
1257 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1258 return NULL;
1259/* One day we may get security into the core?
1260 if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE))
1261 return NULL;
1262*/
1263 Py_BEGIN_ALLOW_THREADS
1264 rc = RegSaveKey(hKey, fileName, pSA );
1265 Py_END_ALLOW_THREADS
1266 if (rc != ERROR_SUCCESS)
1267 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey");
1268 Py_INCREF(Py_None);
1269 return Py_None;
1270}
1271
1272static PyObject *
1273PySetValue(PyObject *self, PyObject *args)
1274{
1275 HKEY hKey;
1276 PyObject *obKey;
1277 char *subKey;
1278 char *str;
1279 DWORD typ;
1280 DWORD len;
1281 long rc;
1282 PyObject *obStrVal;
1283 PyObject *obSubKey;
1284 if (!PyArg_ParseTuple(args, "OOiO:SetValue",
1285 &obKey,
1286 &obSubKey,
1287 &typ,
1288 &obStrVal))
1289 return NULL;
1290 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1291 return NULL;
1292 if (typ != REG_SZ) {
1293 PyErr_SetString(PyExc_TypeError,
1294 "Type must be win32con.REG_SZ");
1295 return NULL;
1296 }
1297 /* XXX - need Unicode support */
1298 str = PyString_AsString(obStrVal);
1299 if (str == NULL)
1300 return NULL;
1301 len = PyString_Size(obStrVal);
1302 if (obSubKey == Py_None)
1303 subKey = NULL;
1304 else {
1305 subKey = PyString_AsString(obSubKey);
1306 if (subKey == NULL)
1307 return NULL;
1308 }
1309 Py_BEGIN_ALLOW_THREADS
1310 rc = RegSetValue(hKey, subKey, REG_SZ, str, len+1);
1311 Py_END_ALLOW_THREADS
1312 if (rc != ERROR_SUCCESS)
1313 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue");
1314 Py_INCREF(Py_None);
1315 return Py_None;
1316}
1317
1318static PyObject *
1319PySetValueEx(PyObject *self, PyObject *args)
1320{
1321 HKEY hKey;
1322 PyObject *obKey;
1323 char *valueName;
1324 PyObject *obRes;
1325 PyObject *value;
1326 BYTE *data;
1327 DWORD len;
1328 DWORD typ;
1329
1330 LONG rc;
1331
1332 if (!PyArg_ParseTuple(args, "OzOiO:SetValueEx",
1333 &obKey,
1334 &valueName,
1335 &obRes,
1336 &typ,
1337 &value))
1338 return NULL;
1339 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1340 return NULL;
1341 if (!Py2Reg(value, typ, &data, &len))
1342 {
1343 if (!PyErr_Occurred())
1344 PyErr_SetString(PyExc_ValueError,
1345 "Could not convert the data to the specified type.");
1346 return NULL;
1347 }
1348 Py_BEGIN_ALLOW_THREADS
1349 rc = RegSetValueEx(hKey, valueName, 0, typ, data, len);
1350 Py_END_ALLOW_THREADS
1351 PyMem_Free(data);
1352 if (rc != ERROR_SUCCESS)
1353 return PyErr_SetFromWindowsErrWithFunction(rc,
1354 "RegSetValueEx");
1355 Py_INCREF(Py_None);
1356 return Py_None;
1357}
1358
1359static struct PyMethodDef winreg_methods[] = {
1360 {"CloseKey", PyCloseKey, 1, CloseKey_doc},
1361 {"ConnectRegistry", PyConnectRegistry, 1, ConnectRegistry_doc},
1362 {"CreateKey", PyCreateKey, 1, CreateKey_doc},
1363 {"DeleteKey", PyDeleteKey, 1, DeleteKey_doc},
1364 {"DeleteValue", PyDeleteValue, 1, DeleteValue_doc},
1365 {"EnumKey", PyEnumKey, 1, EnumKey_doc},
1366 {"EnumValue", PyEnumValue, 1, EnumValue_doc},
1367 {"FlushKey", PyFlushKey, 1, FlushKey_doc},
1368 {"LoadKey", PyLoadKey, 1, LoadKey_doc},
1369 {"OpenKey", PyOpenKey, 1, OpenKey_doc},
1370 {"OpenKeyEx", PyOpenKey, 1, OpenKeyEx_doc},
1371 {"QueryValue", PyQueryValue, 1, QueryValue_doc},
1372 {"QueryValueEx", PyQueryValueEx, 1, QueryValueEx_doc},
1373 {"QueryInfoKey", PyQueryInfoKey, 1, QueryInfoKey_doc},
1374 {"SaveKey", PySaveKey, 1, SaveKey_doc},
1375 {"SetValue", PySetValue, 1, SetValue_doc},
1376 {"SetValueEx", PySetValueEx, 1, SetValueEx_doc},
1377 NULL,
1378};
1379
1380static void
1381insint(PyObject * d, char * name, long value)
1382{
1383 PyObject *v = PyInt_FromLong(value);
1384 if (!v || PyDict_SetItemString(d, name, v))
1385 PyErr_Clear();
1386 Py_XDECREF(v);
1387}
1388
1389#define ADD_INT(val) insint(d, #val, val)
1390
1391static void
1392inskey(PyObject * d, char * name, HKEY key)
1393{
1394 PyObject *v = PyLong_FromVoidPtr(key);
1395 if (!v || PyDict_SetItemString(d, name, v))
1396 PyErr_Clear();
1397 Py_XDECREF(v);
1398}
1399
1400#define ADD_KEY(val) inskey(d, #val, val)
1401
1402__declspec(dllexport) void initwinreg(void)
1403{
1404 PyObject *m, *d;
1405 m = Py_InitModule3("winreg", winreg_methods, module_doc);
1406 d = PyModule_GetDict(m);
1407 PyHKEY_Type.ob_type = &PyType_Type;
1408 PyHKEY_Type.tp_doc = PyHKEY_doc;
1409 Py_INCREF(&PyHKEY_Type);
1410 if (PyDict_SetItemString(d, "HKEYType",
1411 (PyObject *)&PyHKEY_Type) != 0)
1412 return;
1413 Py_INCREF(PyExc_WindowsError);
1414 if (PyDict_SetItemString(d, "error",
1415 PyExc_WindowsError) != 0)
1416 return;
1417
1418 /* Add the relevant constants */
1419 ADD_KEY(HKEY_CLASSES_ROOT);
1420 ADD_KEY(HKEY_CURRENT_USER);
1421 ADD_KEY(HKEY_LOCAL_MACHINE);
1422 ADD_KEY(HKEY_USERS);
1423 ADD_KEY(HKEY_PERFORMANCE_DATA);
1424#ifdef HKEY_CURRENT_CONFIG
1425 ADD_KEY(HKEY_CURRENT_CONFIG);
1426#endif
1427#ifdef HKEY_DYN_DATA
1428 ADD_KEY(HKEY_DYN_DATA);
1429#endif
1430 ADD_INT(KEY_QUERY_VALUE);
1431 ADD_INT(KEY_SET_VALUE);
1432 ADD_INT(KEY_CREATE_SUB_KEY);
1433 ADD_INT(KEY_ENUMERATE_SUB_KEYS);
1434 ADD_INT(KEY_NOTIFY);
1435 ADD_INT(KEY_CREATE_LINK);
1436 ADD_INT(KEY_READ);
1437 ADD_INT(KEY_WRITE);
1438 ADD_INT(KEY_EXECUTE);
1439 ADD_INT(KEY_ALL_ACCESS);
1440 ADD_INT(REG_OPTION_RESERVED);
1441 ADD_INT(REG_OPTION_NON_VOLATILE);
1442 ADD_INT(REG_OPTION_VOLATILE);
1443 ADD_INT(REG_OPTION_CREATE_LINK);
1444 ADD_INT(REG_OPTION_BACKUP_RESTORE);
1445 ADD_INT(REG_OPTION_OPEN_LINK);
1446 ADD_INT(REG_LEGAL_OPTION);
1447 ADD_INT(REG_CREATED_NEW_KEY);
1448 ADD_INT(REG_OPENED_EXISTING_KEY);
1449 ADD_INT(REG_WHOLE_HIVE_VOLATILE);
1450 ADD_INT(REG_REFRESH_HIVE);
1451 ADD_INT(REG_NO_LAZY_FLUSH);
1452 ADD_INT(REG_NOTIFY_CHANGE_NAME);
1453 ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES);
1454 ADD_INT(REG_NOTIFY_CHANGE_LAST_SET);
1455 ADD_INT(REG_NOTIFY_CHANGE_SECURITY);
1456 ADD_INT(REG_LEGAL_CHANGE_FILTER);
1457 ADD_INT(REG_NONE);
1458 ADD_INT(REG_SZ);
1459 ADD_INT(REG_EXPAND_SZ);
1460 ADD_INT(REG_BINARY);
1461 ADD_INT(REG_DWORD);
1462 ADD_INT(REG_DWORD_LITTLE_ENDIAN);
1463 ADD_INT(REG_DWORD_BIG_ENDIAN);
1464 ADD_INT(REG_LINK);
1465 ADD_INT(REG_MULTI_SZ);
1466 ADD_INT(REG_RESOURCE_LIST);
1467 ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
1468 ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
1469}
1470