blob: 5e5f2e55befdf8ce43b5bc7fd07df44aaaafcab5 [file] [log] [blame]
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001/*
Fred Drake270e19b2000-06-29 16:14:14 +00002 _winreg.c
Guido van Rossum9f3712c2000-03-28 20:37:15 +00003
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
Guido van Rossum9f3712c2000-03-28 20:37:15 +000015#include "Python.h"
16#include "structmember.h"
Guido van Rossume7ba4952007-06-06 23:52:48 +000017#include "windows.h"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000018
19static BOOL PyHKEY_AsHKEY(PyObject *ob, HKEY *pRes, BOOL bNoneOK);
20static PyObject *PyHKEY_FromHKEY(HKEY h);
21static BOOL PyHKEY_Close(PyObject *obHandle);
22
23static char errNotAHandle[] = "Object is not a handle";
24
25/* The win32api module reports the function name that failed,
26 but this concept is not in the Python core.
27 Hopefully it will one day, and in the meantime I dont
28 want to lose this info...
29*/
30#define PyErr_SetFromWindowsErrWithFunction(rc, fnname) \
31 PyErr_SetFromWindowsErr(rc)
32
33/* Forward declares */
34
35/* Doc strings */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000036PyDoc_STRVAR(module_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +000037"This module provides access to the Windows registry API.\n"
38"\n"
39"Functions:\n"
40"\n"
41"CloseKey() - Closes a registry key.\n"
42"ConnectRegistry() - Establishes a connection to a predefined registry handle\n"
43" on another computer.\n"
44"CreateKey() - Creates the specified key, or opens it if it already exists.\n"
45"DeleteKey() - Deletes the specified key.\n"
46"DeleteValue() - Removes a named value from the specified registry key.\n"
47"EnumKey() - Enumerates subkeys of the specified open registry key.\n"
48"EnumValue() - Enumerates values of the specified open registry key.\n"
49"FlushKey() - Writes all the attributes of the specified key to the registry.\n"
50"LoadKey() - Creates a subkey under HKEY_USER or HKEY_LOCAL_MACHINE and stores\n"
51" registration information from a specified file into that subkey.\n"
52"OpenKey() - Alias for <om win32api.RegOpenKeyEx>\n"
53"OpenKeyEx() - Opens the specified key.\n"
54"QueryValue() - Retrieves the value associated with the unnamed value for a\n"
55" specified key in the registry.\n"
56"QueryValueEx() - Retrieves the type and data for a specified value name\n"
57" associated with an open registry key.\n"
58"QueryInfoKey() - Returns information about the specified key.\n"
59"SaveKey() - Saves the specified key, and all its subkeys a file.\n"
60"SetValue() - Associates a value with a specified key.\n"
61"SetValueEx() - Stores data in the value field of an open registry key.\n"
62"\n"
63"Special objects:\n"
64"\n"
65"HKEYType -- type object for HKEY objects\n"
66"error -- exception raised for Win32 errors\n"
67"\n"
68"Integer constants:\n"
69"Many constants are defined - see the documentation for each function\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000070"to see what constants are used, and where.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +000071
72
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000073PyDoc_STRVAR(CloseKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +000074"CloseKey(hkey) - Closes a previously opened registry key.\n"
75"\n"
76"The hkey argument specifies a previously opened key.\n"
77"\n"
78"Note that if the key is not closed using this method, it will be\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000079"closed when the hkey object is destroyed by Python.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +000080
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000081PyDoc_STRVAR(ConnectRegistry_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +000082"key = ConnectRegistry(computer_name, key) - "
83"Establishes a connection to a predefined registry handle on another computer.\n"
84"\n"
85"computer_name is the name of the remote computer, of the form \\\\computername.\n"
86" If None, the local computer is used.\n"
87"key is the predefined handle to connect to.\n"
88"\n"
89"The return value is the handle of the opened key.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000090"If the function fails, an EnvironmentError exception is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +000091
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000092PyDoc_STRVAR(CreateKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +000093"key = CreateKey(key, sub_key) - Creates or opens the specified key.\n"
94"\n"
95"key is an already open key, or one of the predefined HKEY_* constants\n"
96"sub_key is a string that names the key this method opens or creates.\n"
97" If key is one of the predefined keys, sub_key may be None. In that case,\n"
98" the handle returned is the same key handle passed in to the function.\n"
99"\n"
100"If the key already exists, this function opens the existing key\n"
101"\n"
102"The return value is the handle of the opened key.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000103"If the function fails, an exception is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000104
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000105PyDoc_STRVAR(DeleteKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000106"DeleteKey(key, sub_key) - Deletes the specified key.\n"
107"\n"
108"key is an already open key, or any one of the predefined HKEY_* constants.\n"
109"sub_key is a string that must be a subkey of the key identified by the key parameter.\n"
110" This value must not be None, and the key may not have subkeys.\n"
111"\n"
112"This method can not delete keys with subkeys.\n"
113"\n"
114"If the method succeeds, the entire key, including all of its values,\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000115"is removed. If the method fails, an EnvironmentError exception is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000116
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000117PyDoc_STRVAR(DeleteValue_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000118"DeleteValue(key, value) - Removes a named value from a registry key.\n"
119"\n"
120"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000121"value is a string that identifies the value to remove.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000122
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000123PyDoc_STRVAR(EnumKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000124"string = EnumKey(key, index) - Enumerates subkeys of an open registry key.\n"
125"\n"
126"key is an already open key, or any one of the predefined HKEY_* constants.\n"
127"index is an integer that identifies the index of the key to retrieve.\n"
128"\n"
129"The function retrieves the name of one subkey each time it is called.\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000130"It is typically called repeatedly until an EnvironmentError exception is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000131"raised, indicating no more values are available.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000132
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000133PyDoc_STRVAR(EnumValue_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000134"tuple = EnumValue(key, index) - Enumerates values of an open registry key.\n"
135"key is an already open key, or any one of the predefined HKEY_* constants.\n"
136"index is an integer that identifies the index of the value to retrieve.\n"
137"\n"
138"The function retrieves the name of one subkey each time it is called.\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000139"It is typically called repeatedly, until an EnvironmentError exception\n"
140"is raised, indicating no more values.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000141"\n"
142"The result is a tuple of 3 items:\n"
143"value_name is a string that identifies the value.\n"
144"value_data is an object that holds the value data, and whose type depends\n"
145" on the underlying registry type.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000146"data_type is an integer that identifies the type of the value data.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000147
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000148PyDoc_STRVAR(FlushKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000149"FlushKey(key) - Writes all the attributes of a key to the registry.\n"
150"\n"
151"key is an already open key, or any one of the predefined HKEY_* constants.\n"
152"\n"
153"It is not necessary to call RegFlushKey to change a key.\n"
154"Registry changes are flushed to disk by the registry using its lazy flusher.\n"
155"Registry changes are also flushed to disk at system shutdown.\n"
156"Unlike CloseKey(), the FlushKey() method returns only when all the data has\n"
157"been written to the registry.\n"
158"An application should only call FlushKey() if it requires absolute certainty that registry changes are on disk.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000159"If you don't know whether a FlushKey() call is required, it probably isn't.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000160
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000161PyDoc_STRVAR(LoadKey_doc,
Mark Hammondb422f952000-06-09 06:01:47 +0000162"LoadKey(key, sub_key, file_name) - Creates a subkey under the specified key\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000163"and stores registration information from a specified file into that subkey.\n"
164"\n"
165"key is an already open key, or any one of the predefined HKEY_* constants.\n"
166"sub_key is a string that identifies the sub_key to load\n"
167"file_name is the name of the file to load registry data from.\n"
168" This file must have been created with the SaveKey() function.\n"
169" Under the file allocation table (FAT) file system, the filename may not\n"
170"have an extension.\n"
171"\n"
172"A call to LoadKey() fails if the calling process does not have the\n"
173"SE_RESTORE_PRIVILEGE privilege.\n"
174"\n"
175"If key is a handle returned by ConnectRegistry(), then the path specified\n"
176"in fileName is relative to the remote computer.\n"
177"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000178"The docs imply key must be in the HKEY_USER or HKEY_LOCAL_MACHINE tree");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000179
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000180PyDoc_STRVAR(OpenKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000181"key = OpenKey(key, sub_key, res = 0, sam = KEY_READ) - Opens the specified key.\n"
182"\n"
183"key is an already open key, or any one of the predefined HKEY_* constants.\n"
184"sub_key is a string that identifies the sub_key to open\n"
185"res is a reserved integer, and must be zero. Default is zero.\n"
186"sam is an integer that specifies an access mask that describes the desired\n"
187" security access for the key. Default is KEY_READ\n"
188"\n"
189"The result is a new handle to the specified key\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000190"If the function fails, an EnvironmentError exception is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000191
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000192PyDoc_STRVAR(OpenKeyEx_doc, "See OpenKey()");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000193
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000194PyDoc_STRVAR(QueryInfoKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000195"tuple = QueryInfoKey(key) - Returns information about a key.\n"
196"\n"
197"key is an already open key, or any one of the predefined HKEY_* constants.\n"
198"\n"
199"The result is a tuple of 3 items:"
200"An integer that identifies the number of sub keys this key has.\n"
201"An integer that identifies the number of values this key has.\n"
202"A long integer that identifies when the key was last modified (if available)\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000203" as 100's of nanoseconds since Jan 1, 1600.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000204
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000205PyDoc_STRVAR(QueryValue_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000206"string = QueryValue(key, sub_key) - retrieves the unnamed value for a key.\n"
207"\n"
208"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000209"sub_key is a string that holds the name of the subkey with which the value\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000210" is associated. If this parameter is None or empty, the function retrieves\n"
211" the value set by the SetValue() method for the key identified by key."
212"\n"
213"Values in the registry have name, type, and data components. This method\n"
214"retrieves the data for a key's first value that has a NULL name.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000215"But the underlying API call doesn't return the type, Lame Lame Lame, DONT USE THIS!!!");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000216
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000217PyDoc_STRVAR(QueryValueEx_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000218"value,type_id = QueryValueEx(key, value_name) - Retrieves the type and data for a specified value name associated with an open registry key.\n"
219"\n"
220"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000221"value_name is a string indicating the value to query");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000222
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000223PyDoc_STRVAR(SaveKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000224"SaveKey(key, file_name) - Saves the specified key, and all its subkeys to the specified file.\n"
225"\n"
226"key is an already open key, or any one of the predefined HKEY_* constants.\n"
227"file_name is the name of the file to save registry data to.\n"
228" This file cannot already exist. If this filename includes an extension,\n"
229" it cannot be used on file allocation table (FAT) file systems by the\n"
230" LoadKey(), ReplaceKey() or RestoreKey() methods.\n"
231"\n"
232"If key represents a key on a remote computer, the path described by\n"
233"file_name is relative to the remote computer.\n"
234"The caller of this method must possess the SeBackupPrivilege security privilege.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000235"This function passes NULL for security_attributes to the API.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000236
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000237PyDoc_STRVAR(SetValue_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000238"SetValue(key, sub_key, type, value) - Associates a value with a specified key.\n"
239"\n"
240"key is an already open key, or any one of the predefined HKEY_* constants.\n"
241"sub_key is a string that names the subkey with which the value is associated.\n"
242"type is an integer that specifies the type of the data. Currently this\n"
243" must be REG_SZ, meaning only strings are supported.\n"
244"value is a string that specifies the new value.\n"
245"\n"
246"If the key specified by the sub_key parameter does not exist, the SetValue\n"
247"function creates it.\n"
248"\n"
249"Value lengths are limited by available memory. Long values (more than\n"
250"2048 bytes) should be stored as files with the filenames stored in \n"
251"the configuration registry. This helps the registry perform efficiently.\n"
252"\n"
253"The key identified by the key parameter must have been opened with\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000254"KEY_SET_VALUE access.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000255
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000256PyDoc_STRVAR(SetValueEx_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000257"SetValueEx(key, value_name, reserved, type, value) - Stores data in the value field of an open registry key.\n"
258"\n"
259"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Mark Hammondc9083b62003-01-15 23:38:15 +0000260"value_name is a string containing the name of the value to set, or None\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000261"type is an integer that specifies the type of the data. This should be one of:\n"
262" REG_BINARY -- Binary data in any form.\n"
263" REG_DWORD -- A 32-bit number.\n"
264" REG_DWORD_LITTLE_ENDIAN -- A 32-bit number in little-endian format.\n"
265" REG_DWORD_BIG_ENDIAN -- A 32-bit number in big-endian format.\n"
266" REG_EXPAND_SZ -- A null-terminated string that contains unexpanded references\n"
267" to environment variables (for example, %PATH%).\n"
268" REG_LINK -- A Unicode symbolic link.\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000269" REG_MULTI_SZ -- An sequence of null-terminated strings, terminated by\n"
270" two null characters. Note that Python handles this\n"
271" termination automatically.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000272" REG_NONE -- No defined value type.\n"
273" REG_RESOURCE_LIST -- A device-driver resource list.\n"
274" REG_SZ -- A null-terminated string.\n"
275"reserved can be anything - zero is always passed to the API.\n"
276"value is a string that specifies the new value.\n"
277"\n"
278"This method can also set additional value and type information for the\n"
279"specified key. The key identified by the key parameter must have been\n"
280"opened with KEY_SET_VALUE access.\n"
281"\n"
282"To open the key, use the CreateKeyEx() or OpenKeyEx() methods.\n"
283"\n"
284"Value lengths are limited by available memory. Long values (more than\n"
285"2048 bytes) should be stored as files with the filenames stored in \n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000286"the configuration registry. This helps the registry perform efficiently.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000287
288/* PyHKEY docstrings */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000289PyDoc_STRVAR(PyHKEY_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000290"PyHKEY Object - A Python object, representing a win32 registry key.\n"
291"\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000292"This object wraps a Windows HKEY object, automatically closing it when\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000293"the object is destroyed. To guarantee cleanup, you can call either\n"
294"the Close() method on the PyHKEY, or the CloseKey() method.\n"
295"\n"
296"All functions which accept a handle object also accept an integer - \n"
297"however, use of the handle object is encouraged.\n"
298"\n"
299"Functions:\n"
300"Close() - Closes the underlying handle.\n"
301"Detach() - Returns the integer Win32 handle, detaching it from the object\n"
302"\n"
303"Properties:\n"
304"handle - The integer Win32 handle.\n"
305"\n"
306"Operations:\n"
Jack Diederich4dafcc42006-11-28 19:15:13 +0000307"__bool__ - Handles with an open object return true, otherwise false.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000308"__int__ - Converting a handle to an integer returns the Win32 handle.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000309"__cmp__ - Handle objects are compared using the handle value.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000310
311
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000312PyDoc_STRVAR(PyHKEY_Close_doc,
Mark Hammondb422f952000-06-09 06:01:47 +0000313"key.Close() - Closes the underlying Windows handle.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000314"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000315"If the handle is already closed, no error is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000316
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000317PyDoc_STRVAR(PyHKEY_Detach_doc,
Mark Hammondb422f952000-06-09 06:01:47 +0000318"int = key.Detach() - Detaches the Windows handle from the handle object.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000319"\n"
320"The result is the value of the handle before it is detached. If the\n"
321"handle is already detached, this will return zero.\n"
322"\n"
323"After calling this function, the handle is effectively invalidated,\n"
324"but the handle is not closed. You would call this function when you\n"
325"need the underlying win32 handle to exist beyond the lifetime of the\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000326"handle object.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000327"On 64 bit windows, the result of this function is a long integer");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000328
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);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000373 PyObject_DEL(ob);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000374}
375
376static int
Jack Diederich4dafcc42006-11-28 19:15:13 +0000377PyHKEY_boolFunc(PyObject *ob)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000378{
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
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000389static PyObject *
390PyHKEY_strFunc(PyObject *ob)
391{
392 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000393 return PyUnicode_FromFormat("<PyHKEY:%p>", pyhkey->hkey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000394}
395
396static int
397PyHKEY_compareFunc(PyObject *ob1, PyObject *ob2)
398{
399 PyHKEYObject *pyhkey1 = (PyHKEYObject *)ob1;
400 PyHKEYObject *pyhkey2 = (PyHKEYObject *)ob2;
401 return pyhkey1 == pyhkey2 ? 0 :
402 (pyhkey1 < pyhkey2 ? -1 : 1);
403}
404
405static long
406PyHKEY_hashFunc(PyObject *ob)
407{
408 /* Just use the address.
409 XXX - should we use the handle value?
410 */
Fred Drake13634cf2000-06-29 19:17:04 +0000411 return _Py_HashPointer(ob);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000412}
413
414
415static PyNumberMethods PyHKEY_NumberMethods =
416{
417 PyHKEY_binaryFailureFunc, /* nb_add */
418 PyHKEY_binaryFailureFunc, /* nb_subtract */
419 PyHKEY_binaryFailureFunc, /* nb_multiply */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000420 PyHKEY_binaryFailureFunc, /* nb_remainder */
421 PyHKEY_binaryFailureFunc, /* nb_divmod */
422 PyHKEY_ternaryFailureFunc, /* nb_power */
423 PyHKEY_unaryFailureFunc, /* nb_negative */
424 PyHKEY_unaryFailureFunc, /* nb_positive */
425 PyHKEY_unaryFailureFunc, /* nb_absolute */
Jack Diederich4dafcc42006-11-28 19:15:13 +0000426 PyHKEY_boolFunc, /* nb_bool */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000427 PyHKEY_unaryFailureFunc, /* nb_invert */
428 PyHKEY_binaryFailureFunc, /* nb_lshift */
429 PyHKEY_binaryFailureFunc, /* nb_rshift */
430 PyHKEY_binaryFailureFunc, /* nb_and */
431 PyHKEY_binaryFailureFunc, /* nb_xor */
432 PyHKEY_binaryFailureFunc, /* nb_or */
Neil Schemenauer16c70752007-09-21 20:19:23 +0000433 0, /* nb_reserved */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000434 PyHKEY_intFunc, /* nb_int */
435 PyHKEY_unaryFailureFunc, /* nb_long */
436 PyHKEY_unaryFailureFunc, /* nb_float */
437 PyHKEY_unaryFailureFunc, /* nb_oct */
438 PyHKEY_unaryFailureFunc, /* nb_hex */
439};
440
441
442/* fwd declare __getattr__ */
Tim Petersc3d12ac2005-12-24 06:03:06 +0000443static PyObject *PyHKEY_getattr(PyObject *self, const char *name);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000444
445/* The type itself */
446PyTypeObject PyHKEY_Type =
447{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000448 PyVarObject_HEAD_INIT(0, 0) /* fill in type at module init */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000449 "PyHKEY",
450 sizeof(PyHKEYObject),
451 0,
452 PyHKEY_deallocFunc, /* tp_dealloc */
Guido van Rossum346f1a82007-08-07 19:58:47 +0000453 0, /* tp_print */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000454 PyHKEY_getattr, /* tp_getattr */
455 0, /* tp_setattr */
456 PyHKEY_compareFunc, /* tp_compare */
457 0, /* tp_repr */
458 &PyHKEY_NumberMethods, /* tp_as_number */
459 0, /* tp_as_sequence */
460 0, /* tp_as_mapping */
461 PyHKEY_hashFunc, /* tp_hash */
462 0, /* tp_call */
463 PyHKEY_strFunc, /* tp_str */
464 0, /* tp_getattro */
465 0, /* tp_setattro */
466 0, /* tp_as_buffer */
467 0, /* tp_flags */
468 PyHKEY_doc, /* tp_doc */
469};
470
471#define OFF(e) offsetof(PyHKEYObject, e)
472
Neal Norwitz8dfc4a92007-08-11 06:39:53 +0000473static PyMemberDef PyHKEY_memberlist[] = {
474 {"handle", T_INT, OFF(hkey), READONLY},
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000475 {NULL} /* Sentinel */
476};
477
478/************************************************************************
479
480 The PyHKEY object methods
481
482************************************************************************/
483static PyObject *
484PyHKEY_CloseMethod(PyObject *self, PyObject *args)
485{
486 if (!PyArg_ParseTuple(args, ":Close"))
487 return NULL;
488 if (!PyHKEY_Close(self))
489 return NULL;
490 Py_INCREF(Py_None);
491 return Py_None;
492}
493
494static PyObject *
495PyHKEY_DetachMethod(PyObject *self, PyObject *args)
496{
497 void* ret;
498 PyHKEYObject *pThis = (PyHKEYObject *)self;
499 if (!PyArg_ParseTuple(args, ":Detach"))
500 return NULL;
501 ret = (void*)pThis->hkey;
502 pThis->hkey = 0;
503 return PyLong_FromVoidPtr(ret);
504}
505
506static struct PyMethodDef PyHKEY_methods[] = {
Neal Norwitz031829d2002-03-31 14:37:44 +0000507 {"Close", PyHKEY_CloseMethod, METH_VARARGS, PyHKEY_Close_doc},
508 {"Detach", PyHKEY_DetachMethod, METH_VARARGS, PyHKEY_Detach_doc},
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000509 {NULL}
510};
511
512/*static*/ PyObject *
Tim Petersc3d12ac2005-12-24 06:03:06 +0000513PyHKEY_getattr(PyObject *self, const char *name)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000514{
515 PyObject *res;
516
517 res = Py_FindMethod(PyHKEY_methods, self, name);
518 if (res != NULL)
519 return res;
520 PyErr_Clear();
521 if (strcmp(name, "handle") == 0)
522 return PyLong_FromVoidPtr(((PyHKEYObject *)self)->hkey);
Neal Norwitz8dfc4a92007-08-11 06:39:53 +0000523 PyErr_Format(PyExc_AttributeError,
524 "'%.50s' object has no attribute '%.400s'",
525 Py_Type(self)->tp_name, name);
526 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000527}
528
529/************************************************************************
530 The public PyHKEY API (well, not public yet :-)
531************************************************************************/
532PyObject *
533PyHKEY_New(HKEY hInit)
534{
535 PyHKEYObject *key = PyObject_NEW(PyHKEYObject, &PyHKEY_Type);
536 if (key)
537 key->hkey = hInit;
538 return (PyObject *)key;
539}
540
541BOOL
542PyHKEY_Close(PyObject *ob_handle)
543{
544 LONG rc;
545 PyHKEYObject *key;
546
547 if (!PyHKEY_Check(ob_handle)) {
548 PyErr_SetString(PyExc_TypeError, "bad operand type");
549 return FALSE;
550 }
551 key = (PyHKEYObject *)ob_handle;
552 rc = key->hkey ? RegCloseKey((HKEY)key->hkey) : ERROR_SUCCESS;
553 key->hkey = 0;
554 if (rc != ERROR_SUCCESS)
555 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
556 return rc == ERROR_SUCCESS;
557}
558
559BOOL
560PyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK)
561{
562 if (ob == Py_None) {
563 if (!bNoneOK) {
564 PyErr_SetString(
565 PyExc_TypeError,
566 "None is not a valid HKEY in this context");
567 return FALSE;
568 }
569 *pHANDLE = (HKEY)0;
570 }
571 else if (PyHKEY_Check(ob)) {
572 PyHKEYObject *pH = (PyHKEYObject *)ob;
573 *pHANDLE = pH->hkey;
574 }
Neal Norwitz1fe5f382007-08-31 04:32:55 +0000575 else if (PyLong_Check(ob)) {
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000576 /* We also support integers */
577 PyErr_Clear();
578 *pHANDLE = (HKEY)PyLong_AsVoidPtr(ob);
579 if (PyErr_Occurred())
580 return FALSE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000581 }
582 else {
583 PyErr_SetString(
584 PyExc_TypeError,
585 "The object is not a PyHKEY object");
586 return FALSE;
587 }
588 return TRUE;
589}
590
591PyObject *
592PyHKEY_FromHKEY(HKEY h)
593{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000594 PyHKEYObject *op;
595
Guido van Rossume3a8e7e2002-08-19 19:26:42 +0000596 /* Inline PyObject_New */
Guido van Rossumb18618d2000-05-03 23:44:39 +0000597 op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject));
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000598 if (op == NULL)
599 return PyErr_NoMemory();
Guido van Rossumb18618d2000-05-03 23:44:39 +0000600 PyObject_INIT(op, &PyHKEY_Type);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000601 op->hkey = h;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000602 return (PyObject *)op;
603}
604
605
606/************************************************************************
607 The module methods
608************************************************************************/
609BOOL
610PyWinObject_CloseHKEY(PyObject *obHandle)
611{
612 BOOL ok;
613 if (PyHKEY_Check(obHandle)) {
614 ok = PyHKEY_Close(obHandle);
615 }
Fred Drake25e17262000-06-30 17:48:51 +0000616#if SIZEOF_LONG >= SIZEOF_HKEY
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000617 else if (PyInt_Check(obHandle)) {
618 long rc = RegCloseKey((HKEY)PyInt_AsLong(obHandle));
619 ok = (rc == ERROR_SUCCESS);
620 if (!ok)
621 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
622 }
Fred Drake25e17262000-06-30 17:48:51 +0000623#else
624 else if (PyLong_Check(obHandle)) {
625 long rc = RegCloseKey((HKEY)PyLong_AsVoidPtr(obHandle));
626 ok = (rc == ERROR_SUCCESS);
627 if (!ok)
628 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
629 }
630#endif
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000631 else {
632 PyErr_SetString(
633 PyExc_TypeError,
634 "A handle must be a HKEY object or an integer");
635 return FALSE;
636 }
637 return ok;
638}
639
640
641/*
642 Private Helper functions for the registry interfaces
643
644** Note that fixupMultiSZ and countString have both had changes
645** made to support "incorrect strings". The registry specification
646** calls for strings to be terminated with 2 null bytes. It seems
Thomas Wouters7e474022000-07-16 12:04:32 +0000647** some commercial packages install strings which dont conform,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000648** causing this code to fail - however, "regedit" etc still work
649** with these strings (ie only we dont!).
650*/
651static void
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000652fixupMultiSZ(wchar_t **str, wchar_t *data, int len)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000653{
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000654 wchar_t *P;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000655 int i;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000656 wchar_t *Q;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000657
658 Q = data + len;
659 for (P = data, i = 0; P < Q && *P != '\0'; P++, i++) {
660 str[i] = P;
661 for(; *P != '\0'; P++)
662 ;
663 }
664}
665
666static int
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000667countStrings(wchar_t *data, int len)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000668{
669 int strings;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000670 wchar_t *P;
671 wchar_t *Q = data + len;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000672
673 for (P = data, strings = 0; P < Q && *P != '\0'; P++, strings++)
674 for (; P < Q && *P != '\0'; P++)
675 ;
676 return strings;
677}
678
679/* Convert PyObject into Registry data.
680 Allocates space as needed. */
681static BOOL
682Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
683{
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000684 switch (typ) {
685 case REG_DWORD:
Guido van Rossum7eaf8222007-06-18 17:58:50 +0000686 if (value != Py_None && !PyLong_Check(value))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000687 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000688 *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000689 if (*retDataBuf==NULL){
690 PyErr_NoMemory();
691 return FALSE;
692 }
693 *retDataSize = sizeof(DWORD);
694 if (value == Py_None) {
695 DWORD zero = 0;
696 memcpy(*retDataBuf, &zero, sizeof(DWORD));
697 }
Guido van Rossum7eaf8222007-06-18 17:58:50 +0000698 else {
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000699 DWORD d = PyLong_AsLong(value);
Guido van Rossum7eaf8222007-06-18 17:58:50 +0000700 memcpy(*retDataBuf, &d, sizeof(DWORD));
701 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000702 break;
703 case REG_SZ:
704 case REG_EXPAND_SZ:
705 {
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000706 if (value == Py_None)
707 *retDataSize = 1;
708 else {
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000709 if (!PyUnicode_Check(value))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000710 return FALSE;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000711
712 *retDataSize = 2 + PyUnicode_GET_DATA_SIZE(value);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000713 }
714 *retDataBuf = (BYTE *)PyMem_NEW(DWORD, *retDataSize);
715 if (*retDataBuf==NULL){
716 PyErr_NoMemory();
717 return FALSE;
718 }
719 if (value == Py_None)
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000720 wcscpy((wchar_t *)*retDataBuf, L"");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000721 else
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000722 wcscpy((wchar_t *)*retDataBuf,
723 PyUnicode_AS_UNICODE(value));
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000724 break;
725 }
726 case REG_MULTI_SZ:
727 {
728 DWORD size = 0;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000729 wchar_t *P;
730 int i,j;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000731
732 if (value == Py_None)
733 i = 0;
734 else {
735 if (!PyList_Check(value))
736 return FALSE;
737 i = PyList_Size(value);
738 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000739 for (j = 0; j < i; j++)
740 {
741 PyObject *t;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000742 t = PyList_GET_ITEM(value, j);
743 if (!PyUnicode_Check(t))
744 return FALSE;
745 size += 2 + PyUnicode_GET_DATA_SIZE(t);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000746 }
747
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000748 *retDataSize = size + 2;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000749 *retDataBuf = (BYTE *)PyMem_NEW(char,
750 *retDataSize);
751 if (*retDataBuf==NULL){
752 PyErr_NoMemory();
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000753 return FALSE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000754 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000755 P = (wchar_t *)*retDataBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000756
757 for (j = 0; j < i; j++)
758 {
759 PyObject *t;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000760 t = PyList_GET_ITEM(value, j);
761 wcscpy(P, PyUnicode_AS_UNICODE(t));
762 P += 1 + wcslen(
763 PyUnicode_AS_UNICODE(t));
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000764 }
765 /* And doubly-terminate the list... */
766 *P = '\0';
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000767 break;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000768 }
769 case REG_BINARY:
770 /* ALSO handle ALL unknown data types here. Even if we can't
771 support it natively, we should handle the bits. */
772 default:
773 if (value == Py_None)
774 *retDataSize = 0;
775 else {
Neal Norwitz1385b892007-08-26 23:07:13 +0000776 PyBuffer view;
777
778 if (!PyObject_CheckBuffer(value)) {
Tim Peters313fcd42006-02-19 04:05:39 +0000779 PyErr_Format(PyExc_TypeError,
Mark Hammond4e80bb52000-07-28 03:44:41 +0000780 "Objects of type '%s' can not "
Tim Peters313fcd42006-02-19 04:05:39 +0000781 "be used as binary registry values",
Mark Hammond4e80bb52000-07-28 03:44:41 +0000782 value->ob_type->tp_name);
783 return FALSE;
784 }
Neal Norwitz1385b892007-08-26 23:07:13 +0000785
786 if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0)
787 return FALSE;
788
789 *retDataBuf = (BYTE *)PyMem_NEW(char, view.len);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000790 if (*retDataBuf==NULL){
Neal Norwitz1385b892007-08-26 23:07:13 +0000791 PyObject_ReleaseBuffer(value, &view);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000792 PyErr_NoMemory();
793 return FALSE;
794 }
Neal Norwitz1385b892007-08-26 23:07:13 +0000795 *retDataSize = view.len;
796 memcpy(*retDataBuf, view.buf, view.len);
797 PyObject_ReleaseBuffer(value, &view);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000798 }
799 break;
800 }
801 return TRUE;
802}
803
804/* Convert Registry data into PyObject*/
805static PyObject *
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000806Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000807{
808 PyObject *obData;
809
810 switch (typ) {
811 case REG_DWORD:
812 if (retDataSize == 0)
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000813 obData = PyInt_FromLong(0);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000814 else
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000815 obData = PyInt_FromLong(*(int *)retDataBuf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000816 break;
817 case REG_SZ:
818 case REG_EXPAND_SZ:
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000819 {
820 /* the buffer may or may not have a trailing NULL */
821 wchar_t *data = (wchar_t *)retDataBuf;
822 int len = retDataSize / 2;
823 if (retDataSize && data[len-1] == '\0')
824 retDataSize -= 2;
825 if (retDataSize <= 0)
826 data = L"";
827 obData = PyUnicode_FromUnicode(data, retDataSize/2);
828 break;
829 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000830 case REG_MULTI_SZ:
831 if (retDataSize == 0)
832 obData = PyList_New(0);
833 else
834 {
835 int index = 0;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000836 wchar_t *data = (wchar_t *)retDataBuf;
837 int len = retDataSize / 2;
838 int s = countStrings(data, len);
839 wchar_t **str = (wchar_t **)malloc(sizeof(wchar_t *)*s);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000840 if (str == NULL)
841 return PyErr_NoMemory();
842
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000843 fixupMultiSZ(str, data, len);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000844 obData = PyList_New(s);
Fred Drake25e17262000-06-30 17:48:51 +0000845 if (obData == NULL)
846 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000847 for (index = 0; index < s; index++)
848 {
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000849 size_t len = wcslen(str[index]);
Fred Drake25e17262000-06-30 17:48:51 +0000850 if (len > INT_MAX) {
851 PyErr_SetString(PyExc_OverflowError,
852 "registry string is too long for a Python string");
853 Py_DECREF(obData);
854 return NULL;
855 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000856 PyList_SetItem(obData,
857 index,
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000858 PyUnicode_FromUnicode(str[index], len));
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000859 }
860 free(str);
861
862 break;
863 }
864 case REG_BINARY:
865 /* ALSO handle ALL unknown data types here. Even if we can't
866 support it natively, we should handle the bits. */
867 default:
868 if (retDataSize == 0) {
869 Py_INCREF(Py_None);
870 obData = Py_None;
871 }
872 else
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000873 obData = PyBytes_FromStringAndSize(
874 (char *)retDataBuf, retDataSize);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000875 break;
876 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000877 return obData;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000878}
879
880/* The Python methods */
881
882static PyObject *
883PyCloseKey(PyObject *self, PyObject *args)
884{
885 PyObject *obKey;
886 if (!PyArg_ParseTuple(args, "O:CloseKey", &obKey))
887 return NULL;
888 if (!PyHKEY_Close(obKey))
889 return NULL;
890 Py_INCREF(Py_None);
891 return Py_None;
892}
893
894static PyObject *
895PyConnectRegistry(PyObject *self, PyObject *args)
896{
897 HKEY hKey;
898 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000899 wchar_t *szCompName = NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000900 HKEY retKey;
901 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000902 if (!PyArg_ParseTuple(args, "ZO:ConnectRegistry", &szCompName, &obKey))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000903 return NULL;
904 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
905 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000906 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000907 rc = RegConnectRegistryW(szCompName, hKey, &retKey);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000908 Py_END_ALLOW_THREADS
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000909 if (rc != ERROR_SUCCESS)
910 return PyErr_SetFromWindowsErrWithFunction(rc,
911 "ConnectRegistry");
912 return PyHKEY_FromHKEY(retKey);
913}
914
915static PyObject *
916PyCreateKey(PyObject *self, PyObject *args)
917{
918 HKEY hKey;
919 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000920 wchar_t *subKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000921 HKEY retKey;
922 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000923 if (!PyArg_ParseTuple(args, "OZ:CreateKey", &obKey, &subKey))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000924 return NULL;
925 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
926 return NULL;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000927 rc = RegCreateKeyW(hKey, subKey, &retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000928 if (rc != ERROR_SUCCESS)
929 return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey");
930 return PyHKEY_FromHKEY(retKey);
931}
932
933static PyObject *
934PyDeleteKey(PyObject *self, PyObject *args)
935{
936 HKEY hKey;
937 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000938 wchar_t *subKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000939 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000940 if (!PyArg_ParseTuple(args, "Ou:DeleteKey", &obKey, &subKey))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000941 return NULL;
942 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
943 return NULL;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000944 rc = RegDeleteKeyW(hKey, subKey );
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000945 if (rc != ERROR_SUCCESS)
946 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey");
947 Py_INCREF(Py_None);
948 return Py_None;
949}
950
951static PyObject *
952PyDeleteValue(PyObject *self, PyObject *args)
953{
954 HKEY hKey;
955 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000956 wchar_t *subKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000957 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000958 if (!PyArg_ParseTuple(args, "OZ:DeleteValue", &obKey, &subKey))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000959 return NULL;
960 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
961 return NULL;
962 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000963 rc = RegDeleteValueW(hKey, subKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000964 Py_END_ALLOW_THREADS
965 if (rc !=ERROR_SUCCESS)
966 return PyErr_SetFromWindowsErrWithFunction(rc,
967 "RegDeleteValue");
968 Py_INCREF(Py_None);
969 return Py_None;
970}
971
972static PyObject *
973PyEnumKey(PyObject *self, PyObject *args)
974{
975 HKEY hKey;
976 PyObject *obKey;
977 int index;
978 long rc;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +0000979 PyObject *retStr;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000980 wchar_t tmpbuf[256]; /* max key name length is 255 */
Georg Brandlb2699b22006-02-18 23:44:24 +0000981 DWORD len = sizeof(tmpbuf); /* includes NULL terminator */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000982
983 if (!PyArg_ParseTuple(args, "Oi:EnumKey", &obKey, &index))
984 return NULL;
985 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
986 return NULL;
Tim Peters313fcd42006-02-19 04:05:39 +0000987
Georg Brandl9a928e72006-02-18 23:35:11 +0000988 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000989 rc = RegEnumKeyExW(hKey, index, tmpbuf, &len, NULL, NULL, NULL, NULL);
Georg Brandl9a928e72006-02-18 23:35:11 +0000990 Py_END_ALLOW_THREADS
991 if (rc != ERROR_SUCCESS)
992 return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx");
Tim Peters313fcd42006-02-19 04:05:39 +0000993
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000994 retStr = PyUnicode_FromUnicode(tmpbuf, len);
Georg Brandl9a928e72006-02-18 23:35:11 +0000995 return retStr; /* can be NULL */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000996}
997
998static PyObject *
999PyEnumValue(PyObject *self, PyObject *args)
1000{
1001 HKEY hKey;
1002 PyObject *obKey;
1003 int index;
1004 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001005 wchar_t *retValueBuf;
1006 BYTE *retDataBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001007 DWORD retValueSize;
1008 DWORD retDataSize;
1009 DWORD typ;
1010 PyObject *obData;
1011 PyObject *retVal;
1012
1013 if (!PyArg_ParseTuple(args, "Oi:EnumValue", &obKey, &index))
1014 return NULL;
1015 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1016 return NULL;
1017
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001018 if ((rc = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, NULL, NULL,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001019 NULL,
1020 &retValueSize, &retDataSize, NULL, NULL))
1021 != ERROR_SUCCESS)
1022 return PyErr_SetFromWindowsErrWithFunction(rc,
1023 "RegQueryInfoKey");
1024 ++retValueSize; /* include null terminators */
1025 ++retDataSize;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001026 retValueBuf = (wchar_t *)PyMem_Malloc(sizeof(wchar_t) * retValueSize);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001027 if (retValueBuf == NULL)
1028 return PyErr_NoMemory();
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001029 retDataBuf = (BYTE *)PyMem_Malloc(retDataSize);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001030 if (retDataBuf == NULL) {
1031 PyMem_Free(retValueBuf);
1032 return PyErr_NoMemory();
1033 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001034
1035 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001036 rc = RegEnumValueW(hKey,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001037 index,
1038 retValueBuf,
1039 &retValueSize,
1040 NULL,
1041 &typ,
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001042 retDataBuf,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001043 &retDataSize);
1044 Py_END_ALLOW_THREADS
1045
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001046 if (rc != ERROR_SUCCESS) {
1047 retVal = PyErr_SetFromWindowsErrWithFunction(rc,
1048 "PyRegEnumValue");
1049 goto fail;
1050 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001051 obData = Reg2Py(retDataBuf, retDataSize, typ);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001052 if (obData == NULL) {
1053 retVal = NULL;
1054 goto fail;
1055 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001056 retVal = Py_BuildValue("uOi", retValueBuf, obData, typ);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001057 Py_DECREF(obData);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001058 fail:
1059 PyMem_Free(retValueBuf);
1060 PyMem_Free(retDataBuf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001061 return retVal;
1062}
1063
1064static PyObject *
1065PyFlushKey(PyObject *self, PyObject *args)
1066{
1067 HKEY hKey;
1068 PyObject *obKey;
1069 long rc;
1070 if (!PyArg_ParseTuple(args, "O:FlushKey", &obKey))
1071 return NULL;
1072 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1073 return NULL;
1074 Py_BEGIN_ALLOW_THREADS
1075 rc = RegFlushKey(hKey);
1076 Py_END_ALLOW_THREADS
1077 if (rc != ERROR_SUCCESS)
1078 return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey");
1079 Py_INCREF(Py_None);
1080 return Py_None;
1081}
1082static PyObject *
1083PyLoadKey(PyObject *self, PyObject *args)
1084{
1085 HKEY hKey;
1086 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001087 wchar_t *subKey;
1088 wchar_t *fileName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001089
1090 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001091 if (!PyArg_ParseTuple(args, "Ouu:LoadKey", &obKey, &subKey, &fileName))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001092 return NULL;
1093 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1094 return NULL;
1095 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001096 rc = RegLoadKeyW(hKey, subKey, fileName );
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001097 Py_END_ALLOW_THREADS
1098 if (rc != ERROR_SUCCESS)
1099 return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey");
1100 Py_INCREF(Py_None);
1101 return Py_None;
1102}
1103
1104static PyObject *
1105PyOpenKey(PyObject *self, PyObject *args)
1106{
1107 HKEY hKey;
1108 PyObject *obKey;
1109
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001110 wchar_t *subKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001111 int res = 0;
1112 HKEY retKey;
1113 long rc;
1114 REGSAM sam = KEY_READ;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001115 if (!PyArg_ParseTuple(args, "OZ|ii:OpenKey", &obKey, &subKey,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001116 &res, &sam))
1117 return NULL;
1118 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1119 return NULL;
1120
1121 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001122 rc = RegOpenKeyExW(hKey, subKey, res, sam, &retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001123 Py_END_ALLOW_THREADS
1124 if (rc != ERROR_SUCCESS)
1125 return PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx");
1126 return PyHKEY_FromHKEY(retKey);
1127}
1128
1129
1130static PyObject *
1131PyQueryInfoKey(PyObject *self, PyObject *args)
1132{
1133 HKEY hKey;
1134 PyObject *obKey;
1135 long rc;
1136 DWORD nSubKeys, nValues;
1137 FILETIME ft;
1138 LARGE_INTEGER li;
1139 PyObject *l;
1140 PyObject *ret;
1141 if (!PyArg_ParseTuple(args, "O:QueryInfoKey", &obKey))
1142 return NULL;
1143 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1144 return NULL;
1145 if ((rc = RegQueryInfoKey(hKey, NULL, NULL, 0, &nSubKeys, NULL, NULL,
1146 &nValues, NULL, NULL, NULL, &ft))
1147 != ERROR_SUCCESS)
1148 return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey");
1149 li.LowPart = ft.dwLowDateTime;
1150 li.HighPart = ft.dwHighDateTime;
1151 l = PyLong_FromLongLong(li.QuadPart);
1152 if (l == NULL)
1153 return NULL;
1154 ret = Py_BuildValue("iiO", nSubKeys, nValues, l);
1155 Py_DECREF(l);
1156 return ret;
1157}
1158
1159static PyObject *
1160PyQueryValue(PyObject *self, PyObject *args)
1161{
1162 HKEY hKey;
1163 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001164 wchar_t *subKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001165 long rc;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001166 PyObject *retStr;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001167 wchar_t *retBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001168 long bufSize = 0;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001169
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001170 if (!PyArg_ParseTuple(args, "OZ:QueryValue", &obKey, &subKey))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001171 return NULL;
1172
1173 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1174 return NULL;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001175 if ((rc = RegQueryValueW(hKey, subKey, NULL, &bufSize))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001176 != ERROR_SUCCESS)
1177 return PyErr_SetFromWindowsErrWithFunction(rc,
1178 "RegQueryValue");
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001179 retBuf = (wchar_t *)PyMem_Malloc(bufSize);
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001180 if (retBuf == NULL)
1181 return PyErr_NoMemory();
1182
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001183 if ((rc = RegQueryValueW(hKey, subKey, retBuf, &bufSize))
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001184 != ERROR_SUCCESS) {
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001185 PyMem_Free(retBuf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001186 return PyErr_SetFromWindowsErrWithFunction(rc,
1187 "RegQueryValue");
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001188 }
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001189
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001190 retStr = PyUnicode_FromUnicode(retBuf, wcslen(retBuf));
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001191 PyMem_Free(retBuf);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001192 return retStr;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001193}
1194
1195static PyObject *
1196PyQueryValueEx(PyObject *self, PyObject *args)
1197{
1198 HKEY hKey;
1199 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001200 wchar_t *valueName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001201
1202 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001203 BYTE *retBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001204 DWORD bufSize = 0;
1205 DWORD typ;
1206 PyObject *obData;
1207 PyObject *result;
1208
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001209 if (!PyArg_ParseTuple(args, "OZ:QueryValueEx", &obKey, &valueName))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001210 return NULL;
1211
1212 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1213 return NULL;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001214 if ((rc = RegQueryValueExW(hKey, valueName,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001215 NULL, NULL, NULL,
1216 &bufSize))
1217 != ERROR_SUCCESS)
1218 return PyErr_SetFromWindowsErrWithFunction(rc,
1219 "RegQueryValueEx");
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001220 retBuf = (BYTE *)PyMem_Malloc(bufSize);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001221 if (retBuf == NULL)
1222 return PyErr_NoMemory();
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001223 if ((rc = RegQueryValueExW(hKey, valueName, NULL,
1224 &typ, retBuf, &bufSize))
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001225 != ERROR_SUCCESS) {
1226 PyMem_Free(retBuf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001227 return PyErr_SetFromWindowsErrWithFunction(rc,
1228 "RegQueryValueEx");
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001229 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001230 obData = Reg2Py(retBuf, bufSize, typ);
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001231 PyMem_Free(retBuf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001232 if (obData == NULL)
1233 return NULL;
1234 result = Py_BuildValue("Oi", obData, typ);
1235 Py_DECREF(obData);
1236 return result;
1237}
1238
1239
1240static PyObject *
1241PySaveKey(PyObject *self, PyObject *args)
1242{
1243 HKEY hKey;
1244 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001245 wchar_t *fileName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001246 LPSECURITY_ATTRIBUTES pSA = NULL;
1247
1248 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001249 if (!PyArg_ParseTuple(args, "Ou:SaveKey", &obKey, &fileName))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001250 return NULL;
1251 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1252 return NULL;
1253/* One day we may get security into the core?
1254 if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE))
1255 return NULL;
1256*/
1257 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001258 rc = RegSaveKeyW(hKey, fileName, pSA );
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001259 Py_END_ALLOW_THREADS
1260 if (rc != ERROR_SUCCESS)
1261 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey");
1262 Py_INCREF(Py_None);
1263 return Py_None;
1264}
1265
1266static PyObject *
1267PySetValue(PyObject *self, PyObject *args)
1268{
1269 HKEY hKey;
1270 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001271 wchar_t *subKey;
1272 wchar_t *str;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001273 DWORD typ;
1274 DWORD len;
1275 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001276 if (!PyArg_ParseTuple(args, "OZiu#:SetValue",
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001277 &obKey,
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001278 &subKey,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001279 &typ,
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001280 &str,
1281 &len))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001282 return NULL;
1283 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1284 return NULL;
1285 if (typ != REG_SZ) {
1286 PyErr_SetString(PyExc_TypeError,
Thomas Hellere1d18f52002-12-20 20:13:35 +00001287 "Type must be _winreg.REG_SZ");
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001288 return NULL;
1289 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001290
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001291 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001292 rc = RegSetValueW(hKey, subKey, REG_SZ, str, len+1);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001293 Py_END_ALLOW_THREADS
1294 if (rc != ERROR_SUCCESS)
1295 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue");
1296 Py_INCREF(Py_None);
1297 return Py_None;
1298}
1299
1300static PyObject *
1301PySetValueEx(PyObject *self, PyObject *args)
1302{
1303 HKEY hKey;
1304 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001305 Py_UNICODE *valueName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001306 PyObject *obRes;
1307 PyObject *value;
1308 BYTE *data;
1309 DWORD len;
1310 DWORD typ;
1311
1312 LONG rc;
1313
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001314 if (!PyArg_ParseTuple(args, "OZOiO:SetValueEx",
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001315 &obKey,
1316 &valueName,
1317 &obRes,
1318 &typ,
1319 &value))
1320 return NULL;
1321 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1322 return NULL;
1323 if (!Py2Reg(value, typ, &data, &len))
1324 {
1325 if (!PyErr_Occurred())
1326 PyErr_SetString(PyExc_ValueError,
1327 "Could not convert the data to the specified type.");
1328 return NULL;
1329 }
1330 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001331 rc = RegSetValueExW(hKey, valueName, 0, typ, data, len);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001332 Py_END_ALLOW_THREADS
Guido van Rossumb18618d2000-05-03 23:44:39 +00001333 PyMem_DEL(data);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001334 if (rc != ERROR_SUCCESS)
1335 return PyErr_SetFromWindowsErrWithFunction(rc,
1336 "RegSetValueEx");
1337 Py_INCREF(Py_None);
1338 return Py_None;
1339}
1340
1341static struct PyMethodDef winreg_methods[] = {
Neal Norwitz031829d2002-03-31 14:37:44 +00001342 {"CloseKey", PyCloseKey, METH_VARARGS, CloseKey_doc},
1343 {"ConnectRegistry", PyConnectRegistry, METH_VARARGS, ConnectRegistry_doc},
1344 {"CreateKey", PyCreateKey, METH_VARARGS, CreateKey_doc},
1345 {"DeleteKey", PyDeleteKey, METH_VARARGS, DeleteKey_doc},
1346 {"DeleteValue", PyDeleteValue, METH_VARARGS, DeleteValue_doc},
1347 {"EnumKey", PyEnumKey, METH_VARARGS, EnumKey_doc},
1348 {"EnumValue", PyEnumValue, METH_VARARGS, EnumValue_doc},
1349 {"FlushKey", PyFlushKey, METH_VARARGS, FlushKey_doc},
1350 {"LoadKey", PyLoadKey, METH_VARARGS, LoadKey_doc},
1351 {"OpenKey", PyOpenKey, METH_VARARGS, OpenKey_doc},
1352 {"OpenKeyEx", PyOpenKey, METH_VARARGS, OpenKeyEx_doc},
1353 {"QueryValue", PyQueryValue, METH_VARARGS, QueryValue_doc},
1354 {"QueryValueEx", PyQueryValueEx, METH_VARARGS, QueryValueEx_doc},
1355 {"QueryInfoKey", PyQueryInfoKey, METH_VARARGS, QueryInfoKey_doc},
1356 {"SaveKey", PySaveKey, METH_VARARGS, SaveKey_doc},
1357 {"SetValue", PySetValue, METH_VARARGS, SetValue_doc},
1358 {"SetValueEx", PySetValueEx, METH_VARARGS, SetValueEx_doc},
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001359 NULL,
1360};
1361
1362static void
1363insint(PyObject * d, char * name, long value)
1364{
1365 PyObject *v = PyInt_FromLong(value);
1366 if (!v || PyDict_SetItemString(d, name, v))
1367 PyErr_Clear();
1368 Py_XDECREF(v);
1369}
1370
1371#define ADD_INT(val) insint(d, #val, val)
1372
1373static void
1374inskey(PyObject * d, char * name, HKEY key)
1375{
1376 PyObject *v = PyLong_FromVoidPtr(key);
1377 if (!v || PyDict_SetItemString(d, name, v))
1378 PyErr_Clear();
1379 Py_XDECREF(v);
1380}
1381
1382#define ADD_KEY(val) inskey(d, #val, val)
1383
Mark Hammond8235ea12002-07-19 06:55:41 +00001384PyMODINIT_FUNC init_winreg(void)
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001385{
1386 PyObject *m, *d;
Fred Drake270e19b2000-06-29 16:14:14 +00001387 m = Py_InitModule3("_winreg", winreg_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001388 if (m == NULL)
1389 return;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001390 d = PyModule_GetDict(m);
Martin v. Löwis95c95ce2007-07-22 14:41:55 +00001391 Py_Type(&PyHKEY_Type) = &PyType_Type;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001392 PyHKEY_Type.tp_doc = PyHKEY_doc;
1393 Py_INCREF(&PyHKEY_Type);
1394 if (PyDict_SetItemString(d, "HKEYType",
1395 (PyObject *)&PyHKEY_Type) != 0)
1396 return;
1397 Py_INCREF(PyExc_WindowsError);
1398 if (PyDict_SetItemString(d, "error",
1399 PyExc_WindowsError) != 0)
1400 return;
1401
1402 /* Add the relevant constants */
1403 ADD_KEY(HKEY_CLASSES_ROOT);
1404 ADD_KEY(HKEY_CURRENT_USER);
1405 ADD_KEY(HKEY_LOCAL_MACHINE);
1406 ADD_KEY(HKEY_USERS);
1407 ADD_KEY(HKEY_PERFORMANCE_DATA);
1408#ifdef HKEY_CURRENT_CONFIG
1409 ADD_KEY(HKEY_CURRENT_CONFIG);
1410#endif
1411#ifdef HKEY_DYN_DATA
1412 ADD_KEY(HKEY_DYN_DATA);
1413#endif
1414 ADD_INT(KEY_QUERY_VALUE);
1415 ADD_INT(KEY_SET_VALUE);
1416 ADD_INT(KEY_CREATE_SUB_KEY);
1417 ADD_INT(KEY_ENUMERATE_SUB_KEYS);
1418 ADD_INT(KEY_NOTIFY);
1419 ADD_INT(KEY_CREATE_LINK);
1420 ADD_INT(KEY_READ);
1421 ADD_INT(KEY_WRITE);
1422 ADD_INT(KEY_EXECUTE);
1423 ADD_INT(KEY_ALL_ACCESS);
1424 ADD_INT(REG_OPTION_RESERVED);
1425 ADD_INT(REG_OPTION_NON_VOLATILE);
1426 ADD_INT(REG_OPTION_VOLATILE);
1427 ADD_INT(REG_OPTION_CREATE_LINK);
1428 ADD_INT(REG_OPTION_BACKUP_RESTORE);
1429 ADD_INT(REG_OPTION_OPEN_LINK);
1430 ADD_INT(REG_LEGAL_OPTION);
1431 ADD_INT(REG_CREATED_NEW_KEY);
1432 ADD_INT(REG_OPENED_EXISTING_KEY);
1433 ADD_INT(REG_WHOLE_HIVE_VOLATILE);
1434 ADD_INT(REG_REFRESH_HIVE);
1435 ADD_INT(REG_NO_LAZY_FLUSH);
1436 ADD_INT(REG_NOTIFY_CHANGE_NAME);
1437 ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES);
1438 ADD_INT(REG_NOTIFY_CHANGE_LAST_SET);
1439 ADD_INT(REG_NOTIFY_CHANGE_SECURITY);
1440 ADD_INT(REG_LEGAL_CHANGE_FILTER);
1441 ADD_INT(REG_NONE);
1442 ADD_INT(REG_SZ);
1443 ADD_INT(REG_EXPAND_SZ);
1444 ADD_INT(REG_BINARY);
1445 ADD_INT(REG_DWORD);
1446 ADD_INT(REG_DWORD_LITTLE_ENDIAN);
1447 ADD_INT(REG_DWORD_BIG_ENDIAN);
1448 ADD_INT(REG_LINK);
1449 ADD_INT(REG_MULTI_SZ);
1450 ADD_INT(REG_RESOURCE_LIST);
1451 ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
1452 ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
1453}
1454
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001455