blob: 0dcade8b3457de6a059c78aa8b670efd81a8952a [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"
Christian Heimes2380ac72008-01-09 00:17:24 +000049"ExpandEnvironmentStrings() - Expand the env strings in a REG_EXPAND_SZ string.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000050"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"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000071"to see what constants are used, and where.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +000072
73
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000074PyDoc_STRVAR(CloseKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +000075"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"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000080"closed when the hkey object is destroyed by Python.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +000081
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000082PyDoc_STRVAR(ConnectRegistry_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +000083"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"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000091"If the function fails, an EnvironmentError exception is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +000092
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000093PyDoc_STRVAR(CreateKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +000094"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"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000104"If the function fails, an exception is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000105
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000106PyDoc_STRVAR(DeleteKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000107"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"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000116"is removed. If the method fails, an EnvironmentError exception is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000117
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000118PyDoc_STRVAR(DeleteValue_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000119"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"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000122"value is a string that identifies the value to remove.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000123
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000124PyDoc_STRVAR(EnumKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000125"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"
Mark Hammondb422f952000-06-09 06:01:47 +0000131"It is typically called repeatedly until an EnvironmentError exception is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000132"raised, indicating no more values are available.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000133
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000134PyDoc_STRVAR(EnumValue_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000135"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"
Mark Hammondb422f952000-06-09 06:01:47 +0000140"It is typically called repeatedly, until an EnvironmentError exception\n"
141"is raised, indicating no more values.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000142"\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"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000147"data_type is an integer that identifies the type of the value data.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000148
Christian Heimes2380ac72008-01-09 00:17:24 +0000149PyDoc_STRVAR(ExpandEnvironmentStrings_doc,
150"string = ExpandEnvironmentStrings(string) - Expand environment vars.\n");
151
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000152PyDoc_STRVAR(FlushKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000153"FlushKey(key) - Writes all the attributes of a key to the registry.\n"
154"\n"
155"key is an already open key, or any one of the predefined HKEY_* constants.\n"
156"\n"
157"It is not necessary to call RegFlushKey to change a key.\n"
158"Registry changes are flushed to disk by the registry using its lazy flusher.\n"
159"Registry changes are also flushed to disk at system shutdown.\n"
160"Unlike CloseKey(), the FlushKey() method returns only when all the data has\n"
161"been written to the registry.\n"
162"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 +0000163"If you don't know whether a FlushKey() call is required, it probably isn't.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000164
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000165PyDoc_STRVAR(LoadKey_doc,
Mark Hammondb422f952000-06-09 06:01:47 +0000166"LoadKey(key, sub_key, file_name) - Creates a subkey under the specified key\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000167"and stores registration information from a specified file into that subkey.\n"
168"\n"
169"key is an already open key, or any one of the predefined HKEY_* constants.\n"
170"sub_key is a string that identifies the sub_key to load\n"
171"file_name is the name of the file to load registry data from.\n"
172" This file must have been created with the SaveKey() function.\n"
173" Under the file allocation table (FAT) file system, the filename may not\n"
174"have an extension.\n"
175"\n"
176"A call to LoadKey() fails if the calling process does not have the\n"
177"SE_RESTORE_PRIVILEGE privilege.\n"
178"\n"
179"If key is a handle returned by ConnectRegistry(), then the path specified\n"
180"in fileName is relative to the remote computer.\n"
181"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000182"The docs imply key must be in the HKEY_USER or HKEY_LOCAL_MACHINE tree");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000183
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000184PyDoc_STRVAR(OpenKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000185"key = OpenKey(key, sub_key, res = 0, sam = KEY_READ) - Opens the specified key.\n"
186"\n"
187"key is an already open key, or any one of the predefined HKEY_* constants.\n"
188"sub_key is a string that identifies the sub_key to open\n"
189"res is a reserved integer, and must be zero. Default is zero.\n"
190"sam is an integer that specifies an access mask that describes the desired\n"
191" security access for the key. Default is KEY_READ\n"
192"\n"
193"The result is a new handle to the specified key\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000194"If the function fails, an EnvironmentError exception is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000195
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000196PyDoc_STRVAR(OpenKeyEx_doc, "See OpenKey()");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000197
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000198PyDoc_STRVAR(QueryInfoKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000199"tuple = QueryInfoKey(key) - Returns information about a key.\n"
200"\n"
201"key is an already open key, or any one of the predefined HKEY_* constants.\n"
202"\n"
203"The result is a tuple of 3 items:"
204"An integer that identifies the number of sub keys this key has.\n"
205"An integer that identifies the number of values this key has.\n"
206"A long integer that identifies when the key was last modified (if available)\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000207" as 100's of nanoseconds since Jan 1, 1600.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000208
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000209PyDoc_STRVAR(QueryValue_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000210"string = QueryValue(key, sub_key) - retrieves the unnamed value for a key.\n"
211"\n"
212"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000213"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 +0000214" is associated. If this parameter is None or empty, the function retrieves\n"
215" the value set by the SetValue() method for the key identified by key."
216"\n"
217"Values in the registry have name, type, and data components. This method\n"
218"retrieves the data for a key's first value that has a NULL name.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000219"But the underlying API call doesn't return the type, Lame Lame Lame, DONT USE THIS!!!");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000220
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000221PyDoc_STRVAR(QueryValueEx_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000222"value,type_id = QueryValueEx(key, value_name) - Retrieves the type and data for a specified value name associated with an open registry key.\n"
223"\n"
224"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000225"value_name is a string indicating the value to query");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000226
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000227PyDoc_STRVAR(SaveKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000228"SaveKey(key, file_name) - Saves the specified key, and all its subkeys to the specified file.\n"
229"\n"
230"key is an already open key, or any one of the predefined HKEY_* constants.\n"
231"file_name is the name of the file to save registry data to.\n"
232" This file cannot already exist. If this filename includes an extension,\n"
233" it cannot be used on file allocation table (FAT) file systems by the\n"
234" LoadKey(), ReplaceKey() or RestoreKey() methods.\n"
235"\n"
236"If key represents a key on a remote computer, the path described by\n"
237"file_name is relative to the remote computer.\n"
238"The caller of this method must possess the SeBackupPrivilege security privilege.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000239"This function passes NULL for security_attributes to the API.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000240
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000241PyDoc_STRVAR(SetValue_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000242"SetValue(key, sub_key, type, value) - Associates a value with a specified key.\n"
243"\n"
244"key is an already open key, or any one of the predefined HKEY_* constants.\n"
245"sub_key is a string that names the subkey with which the value is associated.\n"
246"type is an integer that specifies the type of the data. Currently this\n"
247" must be REG_SZ, meaning only strings are supported.\n"
248"value is a string that specifies the new value.\n"
249"\n"
250"If the key specified by the sub_key parameter does not exist, the SetValue\n"
251"function creates it.\n"
252"\n"
253"Value lengths are limited by available memory. Long values (more than\n"
254"2048 bytes) should be stored as files with the filenames stored in \n"
255"the configuration registry. This helps the registry perform efficiently.\n"
256"\n"
257"The key identified by the key parameter must have been opened with\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000258"KEY_SET_VALUE access.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000259
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000260PyDoc_STRVAR(SetValueEx_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000261"SetValueEx(key, value_name, reserved, type, value) - Stores data in the value field of an open registry key.\n"
262"\n"
263"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Mark Hammondc9083b62003-01-15 23:38:15 +0000264"value_name is a string containing the name of the value to set, or None\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000265"type is an integer that specifies the type of the data. This should be one of:\n"
266" REG_BINARY -- Binary data in any form.\n"
267" REG_DWORD -- A 32-bit number.\n"
268" REG_DWORD_LITTLE_ENDIAN -- A 32-bit number in little-endian format.\n"
269" REG_DWORD_BIG_ENDIAN -- A 32-bit number in big-endian format.\n"
270" REG_EXPAND_SZ -- A null-terminated string that contains unexpanded references\n"
271" to environment variables (for example, %PATH%).\n"
272" REG_LINK -- A Unicode symbolic link.\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000273" REG_MULTI_SZ -- An sequence of null-terminated strings, terminated by\n"
274" two null characters. Note that Python handles this\n"
275" termination automatically.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000276" REG_NONE -- No defined value type.\n"
277" REG_RESOURCE_LIST -- A device-driver resource list.\n"
278" REG_SZ -- A null-terminated string.\n"
279"reserved can be anything - zero is always passed to the API.\n"
280"value is a string that specifies the new value.\n"
281"\n"
282"This method can also set additional value and type information for the\n"
283"specified key. The key identified by the key parameter must have been\n"
284"opened with KEY_SET_VALUE access.\n"
285"\n"
286"To open the key, use the CreateKeyEx() or OpenKeyEx() methods.\n"
287"\n"
288"Value lengths are limited by available memory. Long values (more than\n"
289"2048 bytes) should be stored as files with the filenames stored in \n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000290"the configuration registry. This helps the registry perform efficiently.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000291
292/* PyHKEY docstrings */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000293PyDoc_STRVAR(PyHKEY_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000294"PyHKEY Object - A Python object, representing a win32 registry key.\n"
295"\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000296"This object wraps a Windows HKEY object, automatically closing it when\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000297"the object is destroyed. To guarantee cleanup, you can call either\n"
298"the Close() method on the PyHKEY, or the CloseKey() method.\n"
299"\n"
300"All functions which accept a handle object also accept an integer - \n"
301"however, use of the handle object is encouraged.\n"
302"\n"
303"Functions:\n"
304"Close() - Closes the underlying handle.\n"
305"Detach() - Returns the integer Win32 handle, detaching it from the object\n"
306"\n"
307"Properties:\n"
308"handle - The integer Win32 handle.\n"
309"\n"
310"Operations:\n"
Jack Diederich4dafcc42006-11-28 19:15:13 +0000311"__bool__ - Handles with an open object return true, otherwise false.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000312"__int__ - Converting a handle to an integer returns the Win32 handle.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000313"__cmp__ - Handle objects are compared using the handle value.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000314
315
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000316PyDoc_STRVAR(PyHKEY_Close_doc,
Mark Hammondb422f952000-06-09 06:01:47 +0000317"key.Close() - Closes the underlying Windows handle.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000318"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000319"If the handle is already closed, no error is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000320
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000321PyDoc_STRVAR(PyHKEY_Detach_doc,
Mark Hammondb422f952000-06-09 06:01:47 +0000322"int = key.Detach() - Detaches the Windows handle from the handle object.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000323"\n"
324"The result is the value of the handle before it is detached. If the\n"
325"handle is already detached, this will return zero.\n"
326"\n"
327"After calling this function, the handle is effectively invalidated,\n"
328"but the handle is not closed. You would call this function when you\n"
329"need the underlying win32 handle to exist beyond the lifetime of the\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000330"handle object.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000331"On 64 bit windows, the result of this function is a long integer");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000332
333
334/************************************************************************
335
336 The PyHKEY object definition
337
338************************************************************************/
339typedef struct {
340 PyObject_VAR_HEAD
341 HKEY hkey;
342} PyHKEYObject;
343
344#define PyHKEY_Check(op) ((op)->ob_type == &PyHKEY_Type)
345
346static char *failMsg = "bad operand type";
347
348static PyObject *
349PyHKEY_unaryFailureFunc(PyObject *ob)
350{
351 PyErr_SetString(PyExc_TypeError, failMsg);
352 return NULL;
353}
354static PyObject *
355PyHKEY_binaryFailureFunc(PyObject *ob1, PyObject *ob2)
356{
357 PyErr_SetString(PyExc_TypeError, failMsg);
358 return NULL;
359}
360static PyObject *
361PyHKEY_ternaryFailureFunc(PyObject *ob1, PyObject *ob2, PyObject *ob3)
362{
363 PyErr_SetString(PyExc_TypeError, failMsg);
364 return NULL;
365}
366
367static void
368PyHKEY_deallocFunc(PyObject *ob)
369{
370 /* Can not call PyHKEY_Close, as the ob->tp_type
371 has already been cleared, thus causing the type
372 check to fail!
373 */
374 PyHKEYObject *obkey = (PyHKEYObject *)ob;
375 if (obkey->hkey)
376 RegCloseKey((HKEY)obkey->hkey);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000377 PyObject_DEL(ob);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000378}
379
380static int
Jack Diederich4dafcc42006-11-28 19:15:13 +0000381PyHKEY_boolFunc(PyObject *ob)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000382{
383 return ((PyHKEYObject *)ob)->hkey != 0;
384}
385
386static PyObject *
387PyHKEY_intFunc(PyObject *ob)
388{
389 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
390 return PyLong_FromVoidPtr(pyhkey->hkey);
391}
392
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000393static PyObject *
394PyHKEY_strFunc(PyObject *ob)
395{
396 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000397 return PyUnicode_FromFormat("<PyHKEY:%p>", pyhkey->hkey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000398}
399
400static int
401PyHKEY_compareFunc(PyObject *ob1, PyObject *ob2)
402{
403 PyHKEYObject *pyhkey1 = (PyHKEYObject *)ob1;
404 PyHKEYObject *pyhkey2 = (PyHKEYObject *)ob2;
405 return pyhkey1 == pyhkey2 ? 0 :
406 (pyhkey1 < pyhkey2 ? -1 : 1);
407}
408
409static long
410PyHKEY_hashFunc(PyObject *ob)
411{
412 /* Just use the address.
413 XXX - should we use the handle value?
414 */
Fred Drake13634cf2000-06-29 19:17:04 +0000415 return _Py_HashPointer(ob);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000416}
417
418
419static PyNumberMethods PyHKEY_NumberMethods =
420{
421 PyHKEY_binaryFailureFunc, /* nb_add */
422 PyHKEY_binaryFailureFunc, /* nb_subtract */
423 PyHKEY_binaryFailureFunc, /* nb_multiply */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000424 PyHKEY_binaryFailureFunc, /* nb_remainder */
425 PyHKEY_binaryFailureFunc, /* nb_divmod */
426 PyHKEY_ternaryFailureFunc, /* nb_power */
427 PyHKEY_unaryFailureFunc, /* nb_negative */
428 PyHKEY_unaryFailureFunc, /* nb_positive */
429 PyHKEY_unaryFailureFunc, /* nb_absolute */
Jack Diederich4dafcc42006-11-28 19:15:13 +0000430 PyHKEY_boolFunc, /* nb_bool */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000431 PyHKEY_unaryFailureFunc, /* nb_invert */
432 PyHKEY_binaryFailureFunc, /* nb_lshift */
433 PyHKEY_binaryFailureFunc, /* nb_rshift */
434 PyHKEY_binaryFailureFunc, /* nb_and */
435 PyHKEY_binaryFailureFunc, /* nb_xor */
436 PyHKEY_binaryFailureFunc, /* nb_or */
Neil Schemenauer16c70752007-09-21 20:19:23 +0000437 0, /* nb_reserved */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000438 PyHKEY_intFunc, /* nb_int */
439 PyHKEY_unaryFailureFunc, /* nb_long */
440 PyHKEY_unaryFailureFunc, /* nb_float */
441 PyHKEY_unaryFailureFunc, /* nb_oct */
442 PyHKEY_unaryFailureFunc, /* nb_hex */
443};
444
445
446/* fwd declare __getattr__ */
Tim Petersc3d12ac2005-12-24 06:03:06 +0000447static PyObject *PyHKEY_getattr(PyObject *self, const char *name);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000448
449/* The type itself */
450PyTypeObject PyHKEY_Type =
451{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000452 PyVarObject_HEAD_INIT(0, 0) /* fill in type at module init */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000453 "PyHKEY",
454 sizeof(PyHKEYObject),
455 0,
456 PyHKEY_deallocFunc, /* tp_dealloc */
Guido van Rossum346f1a82007-08-07 19:58:47 +0000457 0, /* tp_print */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000458 PyHKEY_getattr, /* tp_getattr */
459 0, /* tp_setattr */
460 PyHKEY_compareFunc, /* tp_compare */
461 0, /* tp_repr */
462 &PyHKEY_NumberMethods, /* tp_as_number */
463 0, /* tp_as_sequence */
464 0, /* tp_as_mapping */
465 PyHKEY_hashFunc, /* tp_hash */
466 0, /* tp_call */
467 PyHKEY_strFunc, /* tp_str */
468 0, /* tp_getattro */
469 0, /* tp_setattro */
470 0, /* tp_as_buffer */
471 0, /* tp_flags */
472 PyHKEY_doc, /* tp_doc */
473};
474
475#define OFF(e) offsetof(PyHKEYObject, e)
476
Neal Norwitz8dfc4a92007-08-11 06:39:53 +0000477static PyMemberDef PyHKEY_memberlist[] = {
478 {"handle", T_INT, OFF(hkey), READONLY},
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000479 {NULL} /* Sentinel */
480};
481
482/************************************************************************
483
484 The PyHKEY object methods
485
486************************************************************************/
487static PyObject *
488PyHKEY_CloseMethod(PyObject *self, PyObject *args)
489{
490 if (!PyArg_ParseTuple(args, ":Close"))
491 return NULL;
492 if (!PyHKEY_Close(self))
493 return NULL;
494 Py_INCREF(Py_None);
495 return Py_None;
496}
497
498static PyObject *
499PyHKEY_DetachMethod(PyObject *self, PyObject *args)
500{
501 void* ret;
502 PyHKEYObject *pThis = (PyHKEYObject *)self;
503 if (!PyArg_ParseTuple(args, ":Detach"))
504 return NULL;
505 ret = (void*)pThis->hkey;
506 pThis->hkey = 0;
507 return PyLong_FromVoidPtr(ret);
508}
509
Christian Heimes2380ac72008-01-09 00:17:24 +0000510static PyObject *
511PyHKEY_Enter(PyObject *self)
512{
513 Py_XINCREF(self);
514 return self;
515}
516
517static PyObject *
518PyHKEY_Exit(PyObject *self, PyObject *args)
519{
520 if (!PyHKEY_Close(self))
521 return NULL;
522 Py_RETURN_NONE;
523}
524
525
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000526static struct PyMethodDef PyHKEY_methods[] = {
Neal Norwitz031829d2002-03-31 14:37:44 +0000527 {"Close", PyHKEY_CloseMethod, METH_VARARGS, PyHKEY_Close_doc},
528 {"Detach", PyHKEY_DetachMethod, METH_VARARGS, PyHKEY_Detach_doc},
Christian Heimes2380ac72008-01-09 00:17:24 +0000529 {"__enter__", (PyCFunction)PyHKEY_Enter, METH_NOARGS, NULL},
530 {"__exit__", PyHKEY_Exit, METH_VARARGS, NULL},
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000531 {NULL}
532};
533
534/*static*/ PyObject *
Tim Petersc3d12ac2005-12-24 06:03:06 +0000535PyHKEY_getattr(PyObject *self, const char *name)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000536{
537 PyObject *res;
538
539 res = Py_FindMethod(PyHKEY_methods, self, name);
540 if (res != NULL)
541 return res;
542 PyErr_Clear();
543 if (strcmp(name, "handle") == 0)
544 return PyLong_FromVoidPtr(((PyHKEYObject *)self)->hkey);
Neal Norwitz8dfc4a92007-08-11 06:39:53 +0000545 PyErr_Format(PyExc_AttributeError,
546 "'%.50s' object has no attribute '%.400s'",
Christian Heimes90aa7642007-12-19 02:45:37 +0000547 Py_TYPE(self)->tp_name, name);
Neal Norwitz8dfc4a92007-08-11 06:39:53 +0000548 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000549}
550
551/************************************************************************
552 The public PyHKEY API (well, not public yet :-)
553************************************************************************/
554PyObject *
555PyHKEY_New(HKEY hInit)
556{
557 PyHKEYObject *key = PyObject_NEW(PyHKEYObject, &PyHKEY_Type);
558 if (key)
559 key->hkey = hInit;
560 return (PyObject *)key;
561}
562
563BOOL
564PyHKEY_Close(PyObject *ob_handle)
565{
566 LONG rc;
567 PyHKEYObject *key;
568
569 if (!PyHKEY_Check(ob_handle)) {
570 PyErr_SetString(PyExc_TypeError, "bad operand type");
571 return FALSE;
572 }
573 key = (PyHKEYObject *)ob_handle;
574 rc = key->hkey ? RegCloseKey((HKEY)key->hkey) : ERROR_SUCCESS;
575 key->hkey = 0;
576 if (rc != ERROR_SUCCESS)
577 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
578 return rc == ERROR_SUCCESS;
579}
580
581BOOL
582PyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK)
583{
584 if (ob == Py_None) {
585 if (!bNoneOK) {
586 PyErr_SetString(
587 PyExc_TypeError,
588 "None is not a valid HKEY in this context");
589 return FALSE;
590 }
591 *pHANDLE = (HKEY)0;
592 }
593 else if (PyHKEY_Check(ob)) {
594 PyHKEYObject *pH = (PyHKEYObject *)ob;
595 *pHANDLE = pH->hkey;
596 }
Neal Norwitz1fe5f382007-08-31 04:32:55 +0000597 else if (PyLong_Check(ob)) {
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000598 /* We also support integers */
599 PyErr_Clear();
600 *pHANDLE = (HKEY)PyLong_AsVoidPtr(ob);
601 if (PyErr_Occurred())
602 return FALSE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000603 }
604 else {
605 PyErr_SetString(
606 PyExc_TypeError,
607 "The object is not a PyHKEY object");
608 return FALSE;
609 }
610 return TRUE;
611}
612
613PyObject *
614PyHKEY_FromHKEY(HKEY h)
615{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000616 PyHKEYObject *op;
617
Guido van Rossume3a8e7e2002-08-19 19:26:42 +0000618 /* Inline PyObject_New */
Guido van Rossumb18618d2000-05-03 23:44:39 +0000619 op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject));
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000620 if (op == NULL)
621 return PyErr_NoMemory();
Guido van Rossumb18618d2000-05-03 23:44:39 +0000622 PyObject_INIT(op, &PyHKEY_Type);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000623 op->hkey = h;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000624 return (PyObject *)op;
625}
626
627
628/************************************************************************
629 The module methods
630************************************************************************/
631BOOL
632PyWinObject_CloseHKEY(PyObject *obHandle)
633{
634 BOOL ok;
635 if (PyHKEY_Check(obHandle)) {
636 ok = PyHKEY_Close(obHandle);
637 }
Fred Drake25e17262000-06-30 17:48:51 +0000638#if SIZEOF_LONG >= SIZEOF_HKEY
Christian Heimes217cfd12007-12-02 14:31:20 +0000639 else if (PyLong_Check(obHandle)) {
640 long rc = RegCloseKey((HKEY)PyLong_AsLong(obHandle));
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000641 ok = (rc == ERROR_SUCCESS);
642 if (!ok)
643 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
644 }
Fred Drake25e17262000-06-30 17:48:51 +0000645#else
646 else if (PyLong_Check(obHandle)) {
647 long rc = RegCloseKey((HKEY)PyLong_AsVoidPtr(obHandle));
648 ok = (rc == ERROR_SUCCESS);
649 if (!ok)
650 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
651 }
652#endif
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000653 else {
654 PyErr_SetString(
655 PyExc_TypeError,
656 "A handle must be a HKEY object or an integer");
657 return FALSE;
658 }
659 return ok;
660}
661
662
663/*
664 Private Helper functions for the registry interfaces
665
666** Note that fixupMultiSZ and countString have both had changes
667** made to support "incorrect strings". The registry specification
668** calls for strings to be terminated with 2 null bytes. It seems
Thomas Wouters7e474022000-07-16 12:04:32 +0000669** some commercial packages install strings which dont conform,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000670** causing this code to fail - however, "regedit" etc still work
671** with these strings (ie only we dont!).
672*/
673static void
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000674fixupMultiSZ(wchar_t **str, wchar_t *data, int len)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000675{
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000676 wchar_t *P;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000677 int i;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000678 wchar_t *Q;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000679
680 Q = data + len;
681 for (P = data, i = 0; P < Q && *P != '\0'; P++, i++) {
682 str[i] = P;
683 for(; *P != '\0'; P++)
684 ;
685 }
686}
687
688static int
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000689countStrings(wchar_t *data, int len)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000690{
691 int strings;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000692 wchar_t *P;
693 wchar_t *Q = data + len;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000694
695 for (P = data, strings = 0; P < Q && *P != '\0'; P++, strings++)
696 for (; P < Q && *P != '\0'; P++)
697 ;
698 return strings;
699}
700
701/* Convert PyObject into Registry data.
702 Allocates space as needed. */
703static BOOL
704Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
705{
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000706 switch (typ) {
707 case REG_DWORD:
Guido van Rossum7eaf8222007-06-18 17:58:50 +0000708 if (value != Py_None && !PyLong_Check(value))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000709 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000710 *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000711 if (*retDataBuf==NULL){
712 PyErr_NoMemory();
713 return FALSE;
714 }
715 *retDataSize = sizeof(DWORD);
716 if (value == Py_None) {
717 DWORD zero = 0;
718 memcpy(*retDataBuf, &zero, sizeof(DWORD));
719 }
Guido van Rossum7eaf8222007-06-18 17:58:50 +0000720 else {
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000721 DWORD d = PyLong_AsLong(value);
Guido van Rossum7eaf8222007-06-18 17:58:50 +0000722 memcpy(*retDataBuf, &d, sizeof(DWORD));
723 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000724 break;
725 case REG_SZ:
726 case REG_EXPAND_SZ:
727 {
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000728 if (value == Py_None)
729 *retDataSize = 1;
730 else {
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000731 if (!PyUnicode_Check(value))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000732 return FALSE;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000733
734 *retDataSize = 2 + PyUnicode_GET_DATA_SIZE(value);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000735 }
736 *retDataBuf = (BYTE *)PyMem_NEW(DWORD, *retDataSize);
737 if (*retDataBuf==NULL){
738 PyErr_NoMemory();
739 return FALSE;
740 }
741 if (value == Py_None)
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000742 wcscpy((wchar_t *)*retDataBuf, L"");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000743 else
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000744 wcscpy((wchar_t *)*retDataBuf,
745 PyUnicode_AS_UNICODE(value));
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000746 break;
747 }
748 case REG_MULTI_SZ:
749 {
750 DWORD size = 0;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000751 wchar_t *P;
752 int i,j;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000753
754 if (value == Py_None)
755 i = 0;
756 else {
757 if (!PyList_Check(value))
758 return FALSE;
759 i = PyList_Size(value);
760 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000761 for (j = 0; j < i; j++)
762 {
763 PyObject *t;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000764 t = PyList_GET_ITEM(value, j);
765 if (!PyUnicode_Check(t))
766 return FALSE;
767 size += 2 + PyUnicode_GET_DATA_SIZE(t);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000768 }
769
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000770 *retDataSize = size + 2;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000771 *retDataBuf = (BYTE *)PyMem_NEW(char,
772 *retDataSize);
773 if (*retDataBuf==NULL){
774 PyErr_NoMemory();
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000775 return FALSE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000776 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000777 P = (wchar_t *)*retDataBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000778
779 for (j = 0; j < i; j++)
780 {
781 PyObject *t;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000782 t = PyList_GET_ITEM(value, j);
783 wcscpy(P, PyUnicode_AS_UNICODE(t));
784 P += 1 + wcslen(
785 PyUnicode_AS_UNICODE(t));
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000786 }
787 /* And doubly-terminate the list... */
788 *P = '\0';
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000789 break;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000790 }
791 case REG_BINARY:
792 /* ALSO handle ALL unknown data types here. Even if we can't
793 support it natively, we should handle the bits. */
794 default:
795 if (value == Py_None)
796 *retDataSize = 0;
797 else {
Thomas Heller39763a12007-09-24 14:43:56 +0000798 Py_buffer view;
Neal Norwitz1385b892007-08-26 23:07:13 +0000799
800 if (!PyObject_CheckBuffer(value)) {
Tim Peters313fcd42006-02-19 04:05:39 +0000801 PyErr_Format(PyExc_TypeError,
Mark Hammond4e80bb52000-07-28 03:44:41 +0000802 "Objects of type '%s' can not "
Tim Peters313fcd42006-02-19 04:05:39 +0000803 "be used as binary registry values",
Mark Hammond4e80bb52000-07-28 03:44:41 +0000804 value->ob_type->tp_name);
805 return FALSE;
806 }
Neal Norwitz1385b892007-08-26 23:07:13 +0000807
808 if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0)
809 return FALSE;
810
811 *retDataBuf = (BYTE *)PyMem_NEW(char, view.len);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000812 if (*retDataBuf==NULL){
Neal Norwitz1385b892007-08-26 23:07:13 +0000813 PyObject_ReleaseBuffer(value, &view);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000814 PyErr_NoMemory();
815 return FALSE;
816 }
Neal Norwitz1385b892007-08-26 23:07:13 +0000817 *retDataSize = view.len;
818 memcpy(*retDataBuf, view.buf, view.len);
819 PyObject_ReleaseBuffer(value, &view);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000820 }
821 break;
822 }
823 return TRUE;
824}
825
826/* Convert Registry data into PyObject*/
827static PyObject *
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000828Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000829{
830 PyObject *obData;
831
832 switch (typ) {
833 case REG_DWORD:
834 if (retDataSize == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +0000835 obData = PyLong_FromLong(0);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000836 else
Christian Heimes217cfd12007-12-02 14:31:20 +0000837 obData = PyLong_FromLong(*(int *)retDataBuf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000838 break;
839 case REG_SZ:
840 case REG_EXPAND_SZ:
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000841 {
842 /* the buffer may or may not have a trailing NULL */
843 wchar_t *data = (wchar_t *)retDataBuf;
844 int len = retDataSize / 2;
845 if (retDataSize && data[len-1] == '\0')
846 retDataSize -= 2;
847 if (retDataSize <= 0)
848 data = L"";
849 obData = PyUnicode_FromUnicode(data, retDataSize/2);
850 break;
851 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000852 case REG_MULTI_SZ:
853 if (retDataSize == 0)
854 obData = PyList_New(0);
855 else
856 {
857 int index = 0;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000858 wchar_t *data = (wchar_t *)retDataBuf;
859 int len = retDataSize / 2;
860 int s = countStrings(data, len);
861 wchar_t **str = (wchar_t **)malloc(sizeof(wchar_t *)*s);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000862 if (str == NULL)
863 return PyErr_NoMemory();
864
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000865 fixupMultiSZ(str, data, len);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000866 obData = PyList_New(s);
Fred Drake25e17262000-06-30 17:48:51 +0000867 if (obData == NULL)
868 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000869 for (index = 0; index < s; index++)
870 {
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000871 size_t len = wcslen(str[index]);
Fred Drake25e17262000-06-30 17:48:51 +0000872 if (len > INT_MAX) {
873 PyErr_SetString(PyExc_OverflowError,
874 "registry string is too long for a Python string");
875 Py_DECREF(obData);
876 return NULL;
877 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000878 PyList_SetItem(obData,
879 index,
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000880 PyUnicode_FromUnicode(str[index], len));
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000881 }
882 free(str);
883
884 break;
885 }
886 case REG_BINARY:
887 /* ALSO handle ALL unknown data types here. Even if we can't
888 support it natively, we should handle the bits. */
889 default:
890 if (retDataSize == 0) {
891 Py_INCREF(Py_None);
892 obData = Py_None;
893 }
894 else
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000895 obData = PyBytes_FromStringAndSize(
896 (char *)retDataBuf, retDataSize);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000897 break;
898 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000899 return obData;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000900}
901
902/* The Python methods */
903
904static PyObject *
905PyCloseKey(PyObject *self, PyObject *args)
906{
907 PyObject *obKey;
908 if (!PyArg_ParseTuple(args, "O:CloseKey", &obKey))
909 return NULL;
910 if (!PyHKEY_Close(obKey))
911 return NULL;
912 Py_INCREF(Py_None);
913 return Py_None;
914}
915
916static PyObject *
917PyConnectRegistry(PyObject *self, PyObject *args)
918{
919 HKEY hKey;
920 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000921 wchar_t *szCompName = NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000922 HKEY retKey;
923 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000924 if (!PyArg_ParseTuple(args, "ZO:ConnectRegistry", &szCompName, &obKey))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000925 return NULL;
926 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
927 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000928 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000929 rc = RegConnectRegistryW(szCompName, hKey, &retKey);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000930 Py_END_ALLOW_THREADS
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000931 if (rc != ERROR_SUCCESS)
932 return PyErr_SetFromWindowsErrWithFunction(rc,
933 "ConnectRegistry");
934 return PyHKEY_FromHKEY(retKey);
935}
936
937static PyObject *
938PyCreateKey(PyObject *self, PyObject *args)
939{
940 HKEY hKey;
941 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000942 wchar_t *subKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000943 HKEY retKey;
944 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000945 if (!PyArg_ParseTuple(args, "OZ:CreateKey", &obKey, &subKey))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000946 return NULL;
947 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
948 return NULL;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000949 rc = RegCreateKeyW(hKey, subKey, &retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000950 if (rc != ERROR_SUCCESS)
951 return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey");
952 return PyHKEY_FromHKEY(retKey);
953}
954
955static PyObject *
956PyDeleteKey(PyObject *self, PyObject *args)
957{
958 HKEY hKey;
959 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000960 wchar_t *subKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000961 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000962 if (!PyArg_ParseTuple(args, "Ou:DeleteKey", &obKey, &subKey))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000963 return NULL;
964 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
965 return NULL;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000966 rc = RegDeleteKeyW(hKey, subKey );
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000967 if (rc != ERROR_SUCCESS)
968 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey");
969 Py_INCREF(Py_None);
970 return Py_None;
971}
972
973static PyObject *
974PyDeleteValue(PyObject *self, PyObject *args)
975{
976 HKEY hKey;
977 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000978 wchar_t *subKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000979 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000980 if (!PyArg_ParseTuple(args, "OZ:DeleteValue", &obKey, &subKey))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000981 return NULL;
982 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
983 return NULL;
984 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000985 rc = RegDeleteValueW(hKey, subKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000986 Py_END_ALLOW_THREADS
987 if (rc !=ERROR_SUCCESS)
988 return PyErr_SetFromWindowsErrWithFunction(rc,
989 "RegDeleteValue");
990 Py_INCREF(Py_None);
991 return Py_None;
992}
993
994static PyObject *
995PyEnumKey(PyObject *self, PyObject *args)
996{
997 HKEY hKey;
998 PyObject *obKey;
999 int index;
1000 long rc;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001001 PyObject *retStr;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001002 wchar_t tmpbuf[256]; /* max key name length is 255 */
Georg Brandlb2699b22006-02-18 23:44:24 +00001003 DWORD len = sizeof(tmpbuf); /* includes NULL terminator */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001004
1005 if (!PyArg_ParseTuple(args, "Oi:EnumKey", &obKey, &index))
1006 return NULL;
1007 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1008 return NULL;
Tim Peters313fcd42006-02-19 04:05:39 +00001009
Georg Brandl9a928e72006-02-18 23:35:11 +00001010 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001011 rc = RegEnumKeyExW(hKey, index, tmpbuf, &len, NULL, NULL, NULL, NULL);
Georg Brandl9a928e72006-02-18 23:35:11 +00001012 Py_END_ALLOW_THREADS
1013 if (rc != ERROR_SUCCESS)
1014 return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx");
Tim Peters313fcd42006-02-19 04:05:39 +00001015
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001016 retStr = PyUnicode_FromUnicode(tmpbuf, len);
Georg Brandl9a928e72006-02-18 23:35:11 +00001017 return retStr; /* can be NULL */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001018}
1019
1020static PyObject *
1021PyEnumValue(PyObject *self, PyObject *args)
1022{
1023 HKEY hKey;
1024 PyObject *obKey;
1025 int index;
1026 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001027 wchar_t *retValueBuf;
1028 BYTE *retDataBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001029 DWORD retValueSize;
1030 DWORD retDataSize;
1031 DWORD typ;
1032 PyObject *obData;
1033 PyObject *retVal;
1034
1035 if (!PyArg_ParseTuple(args, "Oi:EnumValue", &obKey, &index))
1036 return NULL;
1037 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1038 return NULL;
1039
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001040 if ((rc = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, NULL, NULL,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001041 NULL,
1042 &retValueSize, &retDataSize, NULL, NULL))
1043 != ERROR_SUCCESS)
1044 return PyErr_SetFromWindowsErrWithFunction(rc,
1045 "RegQueryInfoKey");
1046 ++retValueSize; /* include null terminators */
1047 ++retDataSize;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001048 retValueBuf = (wchar_t *)PyMem_Malloc(sizeof(wchar_t) * retValueSize);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001049 if (retValueBuf == NULL)
1050 return PyErr_NoMemory();
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001051 retDataBuf = (BYTE *)PyMem_Malloc(retDataSize);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001052 if (retDataBuf == NULL) {
1053 PyMem_Free(retValueBuf);
1054 return PyErr_NoMemory();
1055 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001056
1057 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001058 rc = RegEnumValueW(hKey,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001059 index,
1060 retValueBuf,
1061 &retValueSize,
1062 NULL,
1063 &typ,
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001064 retDataBuf,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001065 &retDataSize);
1066 Py_END_ALLOW_THREADS
1067
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001068 if (rc != ERROR_SUCCESS) {
1069 retVal = PyErr_SetFromWindowsErrWithFunction(rc,
1070 "PyRegEnumValue");
1071 goto fail;
1072 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001073 obData = Reg2Py(retDataBuf, retDataSize, typ);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001074 if (obData == NULL) {
1075 retVal = NULL;
1076 goto fail;
1077 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001078 retVal = Py_BuildValue("uOi", retValueBuf, obData, typ);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001079 Py_DECREF(obData);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001080 fail:
1081 PyMem_Free(retValueBuf);
1082 PyMem_Free(retDataBuf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001083 return retVal;
1084}
1085
1086static PyObject *
Christian Heimes2380ac72008-01-09 00:17:24 +00001087PyExpandEnvironmentStrings(PyObject *self, PyObject *args)
1088{
1089 Py_UNICODE *retValue = NULL;
1090 Py_UNICODE *src;
1091 DWORD retValueSize;
1092 DWORD rc;
1093 PyObject *o;
1094
1095 if (!PyArg_ParseTuple(args, "u:ExpandEnvironmentStrings", &src))
1096 return NULL;
1097
1098 retValueSize = ExpandEnvironmentStringsW(src, retValue, 0);
1099 if (retValueSize == 0) {
1100 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1101 "ExpandEnvironmentStrings");
1102 }
1103 retValue = (Py_UNICODE *)PyMem_Malloc(retValueSize * sizeof(Py_UNICODE));
1104 if (retValue == NULL) {
1105 return PyErr_NoMemory();
1106 }
1107
1108 rc = ExpandEnvironmentStringsW(src, retValue, retValueSize);
1109 if (rc == 0) {
1110 PyMem_Free(retValue);
1111 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1112 "ExpandEnvironmentStrings");
1113 }
1114 o = PyUnicode_FromUnicode(retValue, wcslen(retValue));
1115 PyMem_Free(retValue);
1116 return o;
1117}
1118
1119static PyObject *
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001120PyFlushKey(PyObject *self, PyObject *args)
1121{
1122 HKEY hKey;
1123 PyObject *obKey;
1124 long rc;
1125 if (!PyArg_ParseTuple(args, "O:FlushKey", &obKey))
1126 return NULL;
1127 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1128 return NULL;
1129 Py_BEGIN_ALLOW_THREADS
1130 rc = RegFlushKey(hKey);
1131 Py_END_ALLOW_THREADS
1132 if (rc != ERROR_SUCCESS)
1133 return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey");
1134 Py_INCREF(Py_None);
1135 return Py_None;
1136}
1137static PyObject *
1138PyLoadKey(PyObject *self, PyObject *args)
1139{
1140 HKEY hKey;
1141 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001142 wchar_t *subKey;
1143 wchar_t *fileName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001144
1145 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001146 if (!PyArg_ParseTuple(args, "Ouu:LoadKey", &obKey, &subKey, &fileName))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001147 return NULL;
1148 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1149 return NULL;
1150 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001151 rc = RegLoadKeyW(hKey, subKey, fileName );
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001152 Py_END_ALLOW_THREADS
1153 if (rc != ERROR_SUCCESS)
1154 return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey");
1155 Py_INCREF(Py_None);
1156 return Py_None;
1157}
1158
1159static PyObject *
1160PyOpenKey(PyObject *self, PyObject *args)
1161{
1162 HKEY hKey;
1163 PyObject *obKey;
1164
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001165 wchar_t *subKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001166 int res = 0;
1167 HKEY retKey;
1168 long rc;
1169 REGSAM sam = KEY_READ;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001170 if (!PyArg_ParseTuple(args, "OZ|ii:OpenKey", &obKey, &subKey,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001171 &res, &sam))
1172 return NULL;
1173 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1174 return NULL;
1175
1176 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001177 rc = RegOpenKeyExW(hKey, subKey, res, sam, &retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001178 Py_END_ALLOW_THREADS
1179 if (rc != ERROR_SUCCESS)
1180 return PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx");
1181 return PyHKEY_FromHKEY(retKey);
1182}
1183
1184
1185static PyObject *
1186PyQueryInfoKey(PyObject *self, PyObject *args)
1187{
1188 HKEY hKey;
1189 PyObject *obKey;
1190 long rc;
1191 DWORD nSubKeys, nValues;
1192 FILETIME ft;
1193 LARGE_INTEGER li;
1194 PyObject *l;
1195 PyObject *ret;
1196 if (!PyArg_ParseTuple(args, "O:QueryInfoKey", &obKey))
1197 return NULL;
1198 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1199 return NULL;
1200 if ((rc = RegQueryInfoKey(hKey, NULL, NULL, 0, &nSubKeys, NULL, NULL,
1201 &nValues, NULL, NULL, NULL, &ft))
1202 != ERROR_SUCCESS)
1203 return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey");
1204 li.LowPart = ft.dwLowDateTime;
1205 li.HighPart = ft.dwHighDateTime;
1206 l = PyLong_FromLongLong(li.QuadPart);
1207 if (l == NULL)
1208 return NULL;
1209 ret = Py_BuildValue("iiO", nSubKeys, nValues, l);
1210 Py_DECREF(l);
1211 return ret;
1212}
1213
1214static PyObject *
1215PyQueryValue(PyObject *self, PyObject *args)
1216{
1217 HKEY hKey;
1218 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001219 wchar_t *subKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001220 long rc;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001221 PyObject *retStr;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001222 wchar_t *retBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001223 long bufSize = 0;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001224
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001225 if (!PyArg_ParseTuple(args, "OZ:QueryValue", &obKey, &subKey))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001226 return NULL;
1227
1228 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1229 return NULL;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001230 if ((rc = RegQueryValueW(hKey, subKey, NULL, &bufSize))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001231 != ERROR_SUCCESS)
1232 return PyErr_SetFromWindowsErrWithFunction(rc,
1233 "RegQueryValue");
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001234 retBuf = (wchar_t *)PyMem_Malloc(bufSize);
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001235 if (retBuf == NULL)
1236 return PyErr_NoMemory();
1237
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001238 if ((rc = RegQueryValueW(hKey, subKey, retBuf, &bufSize))
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001239 != ERROR_SUCCESS) {
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001240 PyMem_Free(retBuf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001241 return PyErr_SetFromWindowsErrWithFunction(rc,
1242 "RegQueryValue");
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001243 }
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001244
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001245 retStr = PyUnicode_FromUnicode(retBuf, wcslen(retBuf));
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001246 PyMem_Free(retBuf);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001247 return retStr;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001248}
1249
1250static PyObject *
1251PyQueryValueEx(PyObject *self, PyObject *args)
1252{
1253 HKEY hKey;
1254 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001255 wchar_t *valueName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001256
1257 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001258 BYTE *retBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001259 DWORD bufSize = 0;
1260 DWORD typ;
1261 PyObject *obData;
1262 PyObject *result;
1263
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001264 if (!PyArg_ParseTuple(args, "OZ:QueryValueEx", &obKey, &valueName))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001265 return NULL;
1266
1267 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1268 return NULL;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001269 if ((rc = RegQueryValueExW(hKey, valueName,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001270 NULL, NULL, NULL,
1271 &bufSize))
1272 != ERROR_SUCCESS)
1273 return PyErr_SetFromWindowsErrWithFunction(rc,
1274 "RegQueryValueEx");
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001275 retBuf = (BYTE *)PyMem_Malloc(bufSize);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001276 if (retBuf == NULL)
1277 return PyErr_NoMemory();
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001278 if ((rc = RegQueryValueExW(hKey, valueName, NULL,
1279 &typ, retBuf, &bufSize))
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001280 != ERROR_SUCCESS) {
1281 PyMem_Free(retBuf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001282 return PyErr_SetFromWindowsErrWithFunction(rc,
1283 "RegQueryValueEx");
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001284 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001285 obData = Reg2Py(retBuf, bufSize, typ);
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001286 PyMem_Free(retBuf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001287 if (obData == NULL)
1288 return NULL;
1289 result = Py_BuildValue("Oi", obData, typ);
1290 Py_DECREF(obData);
1291 return result;
1292}
1293
1294
1295static PyObject *
1296PySaveKey(PyObject *self, PyObject *args)
1297{
1298 HKEY hKey;
1299 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001300 wchar_t *fileName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001301 LPSECURITY_ATTRIBUTES pSA = NULL;
1302
1303 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001304 if (!PyArg_ParseTuple(args, "Ou:SaveKey", &obKey, &fileName))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001305 return NULL;
1306 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1307 return NULL;
1308/* One day we may get security into the core?
1309 if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE))
1310 return NULL;
1311*/
1312 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001313 rc = RegSaveKeyW(hKey, fileName, pSA );
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001314 Py_END_ALLOW_THREADS
1315 if (rc != ERROR_SUCCESS)
1316 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey");
1317 Py_INCREF(Py_None);
1318 return Py_None;
1319}
1320
1321static PyObject *
1322PySetValue(PyObject *self, PyObject *args)
1323{
1324 HKEY hKey;
1325 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001326 wchar_t *subKey;
1327 wchar_t *str;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001328 DWORD typ;
1329 DWORD len;
1330 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001331 if (!PyArg_ParseTuple(args, "OZiu#:SetValue",
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001332 &obKey,
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001333 &subKey,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001334 &typ,
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001335 &str,
1336 &len))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001337 return NULL;
1338 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1339 return NULL;
1340 if (typ != REG_SZ) {
1341 PyErr_SetString(PyExc_TypeError,
Thomas Hellere1d18f52002-12-20 20:13:35 +00001342 "Type must be _winreg.REG_SZ");
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001343 return NULL;
1344 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001345
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001346 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001347 rc = RegSetValueW(hKey, subKey, REG_SZ, str, len+1);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001348 Py_END_ALLOW_THREADS
1349 if (rc != ERROR_SUCCESS)
1350 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue");
1351 Py_INCREF(Py_None);
1352 return Py_None;
1353}
1354
1355static PyObject *
1356PySetValueEx(PyObject *self, PyObject *args)
1357{
1358 HKEY hKey;
1359 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001360 Py_UNICODE *valueName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001361 PyObject *obRes;
1362 PyObject *value;
1363 BYTE *data;
1364 DWORD len;
1365 DWORD typ;
1366
1367 LONG rc;
1368
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001369 if (!PyArg_ParseTuple(args, "OZOiO:SetValueEx",
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001370 &obKey,
1371 &valueName,
1372 &obRes,
1373 &typ,
1374 &value))
1375 return NULL;
1376 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1377 return NULL;
1378 if (!Py2Reg(value, typ, &data, &len))
1379 {
1380 if (!PyErr_Occurred())
1381 PyErr_SetString(PyExc_ValueError,
1382 "Could not convert the data to the specified type.");
1383 return NULL;
1384 }
1385 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001386 rc = RegSetValueExW(hKey, valueName, 0, typ, data, len);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001387 Py_END_ALLOW_THREADS
Guido van Rossumb18618d2000-05-03 23:44:39 +00001388 PyMem_DEL(data);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001389 if (rc != ERROR_SUCCESS)
1390 return PyErr_SetFromWindowsErrWithFunction(rc,
1391 "RegSetValueEx");
1392 Py_INCREF(Py_None);
1393 return Py_None;
1394}
1395
1396static struct PyMethodDef winreg_methods[] = {
Neal Norwitz031829d2002-03-31 14:37:44 +00001397 {"CloseKey", PyCloseKey, METH_VARARGS, CloseKey_doc},
1398 {"ConnectRegistry", PyConnectRegistry, METH_VARARGS, ConnectRegistry_doc},
1399 {"CreateKey", PyCreateKey, METH_VARARGS, CreateKey_doc},
1400 {"DeleteKey", PyDeleteKey, METH_VARARGS, DeleteKey_doc},
1401 {"DeleteValue", PyDeleteValue, METH_VARARGS, DeleteValue_doc},
1402 {"EnumKey", PyEnumKey, METH_VARARGS, EnumKey_doc},
1403 {"EnumValue", PyEnumValue, METH_VARARGS, EnumValue_doc},
Christian Heimes2380ac72008-01-09 00:17:24 +00001404 {"ExpandEnvironmentStrings", PyExpandEnvironmentStrings, METH_VARARGS,
1405 ExpandEnvironmentStrings_doc },
Neal Norwitz031829d2002-03-31 14:37:44 +00001406 {"FlushKey", PyFlushKey, METH_VARARGS, FlushKey_doc},
1407 {"LoadKey", PyLoadKey, METH_VARARGS, LoadKey_doc},
1408 {"OpenKey", PyOpenKey, METH_VARARGS, OpenKey_doc},
1409 {"OpenKeyEx", PyOpenKey, METH_VARARGS, OpenKeyEx_doc},
1410 {"QueryValue", PyQueryValue, METH_VARARGS, QueryValue_doc},
1411 {"QueryValueEx", PyQueryValueEx, METH_VARARGS, QueryValueEx_doc},
1412 {"QueryInfoKey", PyQueryInfoKey, METH_VARARGS, QueryInfoKey_doc},
1413 {"SaveKey", PySaveKey, METH_VARARGS, SaveKey_doc},
1414 {"SetValue", PySetValue, METH_VARARGS, SetValue_doc},
1415 {"SetValueEx", PySetValueEx, METH_VARARGS, SetValueEx_doc},
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001416 NULL,
1417};
1418
1419static void
1420insint(PyObject * d, char * name, long value)
1421{
Christian Heimes217cfd12007-12-02 14:31:20 +00001422 PyObject *v = PyLong_FromLong(value);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001423 if (!v || PyDict_SetItemString(d, name, v))
1424 PyErr_Clear();
1425 Py_XDECREF(v);
1426}
1427
1428#define ADD_INT(val) insint(d, #val, val)
1429
1430static void
1431inskey(PyObject * d, char * name, HKEY key)
1432{
1433 PyObject *v = PyLong_FromVoidPtr(key);
1434 if (!v || PyDict_SetItemString(d, name, v))
1435 PyErr_Clear();
1436 Py_XDECREF(v);
1437}
1438
1439#define ADD_KEY(val) inskey(d, #val, val)
1440
Mark Hammond8235ea12002-07-19 06:55:41 +00001441PyMODINIT_FUNC init_winreg(void)
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001442{
1443 PyObject *m, *d;
Fred Drake270e19b2000-06-29 16:14:14 +00001444 m = Py_InitModule3("_winreg", winreg_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001445 if (m == NULL)
1446 return;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001447 d = PyModule_GetDict(m);
Christian Heimes90aa7642007-12-19 02:45:37 +00001448 Py_TYPE(&PyHKEY_Type) = &PyType_Type;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001449 PyHKEY_Type.tp_doc = PyHKEY_doc;
1450 Py_INCREF(&PyHKEY_Type);
1451 if (PyDict_SetItemString(d, "HKEYType",
1452 (PyObject *)&PyHKEY_Type) != 0)
1453 return;
1454 Py_INCREF(PyExc_WindowsError);
1455 if (PyDict_SetItemString(d, "error",
1456 PyExc_WindowsError) != 0)
1457 return;
1458
1459 /* Add the relevant constants */
1460 ADD_KEY(HKEY_CLASSES_ROOT);
1461 ADD_KEY(HKEY_CURRENT_USER);
1462 ADD_KEY(HKEY_LOCAL_MACHINE);
1463 ADD_KEY(HKEY_USERS);
1464 ADD_KEY(HKEY_PERFORMANCE_DATA);
1465#ifdef HKEY_CURRENT_CONFIG
1466 ADD_KEY(HKEY_CURRENT_CONFIG);
1467#endif
1468#ifdef HKEY_DYN_DATA
1469 ADD_KEY(HKEY_DYN_DATA);
1470#endif
1471 ADD_INT(KEY_QUERY_VALUE);
1472 ADD_INT(KEY_SET_VALUE);
1473 ADD_INT(KEY_CREATE_SUB_KEY);
1474 ADD_INT(KEY_ENUMERATE_SUB_KEYS);
1475 ADD_INT(KEY_NOTIFY);
1476 ADD_INT(KEY_CREATE_LINK);
1477 ADD_INT(KEY_READ);
1478 ADD_INT(KEY_WRITE);
1479 ADD_INT(KEY_EXECUTE);
1480 ADD_INT(KEY_ALL_ACCESS);
1481 ADD_INT(REG_OPTION_RESERVED);
1482 ADD_INT(REG_OPTION_NON_VOLATILE);
1483 ADD_INT(REG_OPTION_VOLATILE);
1484 ADD_INT(REG_OPTION_CREATE_LINK);
1485 ADD_INT(REG_OPTION_BACKUP_RESTORE);
1486 ADD_INT(REG_OPTION_OPEN_LINK);
1487 ADD_INT(REG_LEGAL_OPTION);
1488 ADD_INT(REG_CREATED_NEW_KEY);
1489 ADD_INT(REG_OPENED_EXISTING_KEY);
1490 ADD_INT(REG_WHOLE_HIVE_VOLATILE);
1491 ADD_INT(REG_REFRESH_HIVE);
1492 ADD_INT(REG_NO_LAZY_FLUSH);
1493 ADD_INT(REG_NOTIFY_CHANGE_NAME);
1494 ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES);
1495 ADD_INT(REG_NOTIFY_CHANGE_LAST_SET);
1496 ADD_INT(REG_NOTIFY_CHANGE_SECURITY);
1497 ADD_INT(REG_LEGAL_CHANGE_FILTER);
1498 ADD_INT(REG_NONE);
1499 ADD_INT(REG_SZ);
1500 ADD_INT(REG_EXPAND_SZ);
1501 ADD_INT(REG_BINARY);
1502 ADD_INT(REG_DWORD);
1503 ADD_INT(REG_DWORD_LITTLE_ENDIAN);
1504 ADD_INT(REG_DWORD_BIG_ENDIAN);
1505 ADD_INT(REG_LINK);
1506 ADD_INT(REG_MULTI_SZ);
1507 ADD_INT(REG_RESOURCE_LIST);
1508 ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
1509 ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
1510}
1511
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001512