blob: 5c5023d84973726e6e9e7301ebbc4cebe5d4846d [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{
Christian Heimescc47b052008-03-25 14:56:36 +0000706 Py_ssize_t i,j;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000707 switch (typ) {
708 case REG_DWORD:
Guido van Rossum7eaf8222007-06-18 17:58:50 +0000709 if (value != Py_None && !PyLong_Check(value))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000710 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000711 *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000712 if (*retDataBuf==NULL){
713 PyErr_NoMemory();
714 return FALSE;
715 }
716 *retDataSize = sizeof(DWORD);
717 if (value == Py_None) {
718 DWORD zero = 0;
719 memcpy(*retDataBuf, &zero, sizeof(DWORD));
720 }
Guido van Rossum7eaf8222007-06-18 17:58:50 +0000721 else {
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000722 DWORD d = PyLong_AsLong(value);
Guido van Rossum7eaf8222007-06-18 17:58:50 +0000723 memcpy(*retDataBuf, &d, sizeof(DWORD));
724 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000725 break;
726 case REG_SZ:
727 case REG_EXPAND_SZ:
728 {
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000729 if (value == Py_None)
730 *retDataSize = 1;
731 else {
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000732 if (!PyUnicode_Check(value))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000733 return FALSE;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000734
735 *retDataSize = 2 + PyUnicode_GET_DATA_SIZE(value);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000736 }
737 *retDataBuf = (BYTE *)PyMem_NEW(DWORD, *retDataSize);
738 if (*retDataBuf==NULL){
739 PyErr_NoMemory();
740 return FALSE;
741 }
742 if (value == Py_None)
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000743 wcscpy((wchar_t *)*retDataBuf, L"");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000744 else
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000745 wcscpy((wchar_t *)*retDataBuf,
746 PyUnicode_AS_UNICODE(value));
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000747 break;
748 }
749 case REG_MULTI_SZ:
750 {
751 DWORD size = 0;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000752 wchar_t *P;
753 int i,j;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000754
755 if (value == Py_None)
756 i = 0;
757 else {
758 if (!PyList_Check(value))
759 return FALSE;
760 i = PyList_Size(value);
761 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000762 for (j = 0; j < i; j++)
763 {
764 PyObject *t;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000765 t = PyList_GET_ITEM(value, j);
766 if (!PyUnicode_Check(t))
767 return FALSE;
768 size += 2 + PyUnicode_GET_DATA_SIZE(t);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000769 }
770
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000771 *retDataSize = size + 2;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000772 *retDataBuf = (BYTE *)PyMem_NEW(char,
773 *retDataSize);
774 if (*retDataBuf==NULL){
775 PyErr_NoMemory();
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000776 return FALSE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000777 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000778 P = (wchar_t *)*retDataBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000779
780 for (j = 0; j < i; j++)
781 {
782 PyObject *t;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000783 t = PyList_GET_ITEM(value, j);
784 wcscpy(P, PyUnicode_AS_UNICODE(t));
785 P += 1 + wcslen(
786 PyUnicode_AS_UNICODE(t));
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000787 }
788 /* And doubly-terminate the list... */
789 *P = '\0';
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000790 break;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000791 }
792 case REG_BINARY:
793 /* ALSO handle ALL unknown data types here. Even if we can't
794 support it natively, we should handle the bits. */
795 default:
796 if (value == Py_None)
797 *retDataSize = 0;
798 else {
Thomas Heller39763a12007-09-24 14:43:56 +0000799 Py_buffer view;
Neal Norwitz1385b892007-08-26 23:07:13 +0000800
801 if (!PyObject_CheckBuffer(value)) {
Tim Peters313fcd42006-02-19 04:05:39 +0000802 PyErr_Format(PyExc_TypeError,
Mark Hammond4e80bb52000-07-28 03:44:41 +0000803 "Objects of type '%s' can not "
Tim Peters313fcd42006-02-19 04:05:39 +0000804 "be used as binary registry values",
Mark Hammond4e80bb52000-07-28 03:44:41 +0000805 value->ob_type->tp_name);
806 return FALSE;
807 }
Neal Norwitz1385b892007-08-26 23:07:13 +0000808
809 if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0)
810 return FALSE;
811
812 *retDataBuf = (BYTE *)PyMem_NEW(char, view.len);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000813 if (*retDataBuf==NULL){
Neal Norwitz1385b892007-08-26 23:07:13 +0000814 PyObject_ReleaseBuffer(value, &view);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000815 PyErr_NoMemory();
816 return FALSE;
817 }
Neal Norwitz1385b892007-08-26 23:07:13 +0000818 *retDataSize = view.len;
819 memcpy(*retDataBuf, view.buf, view.len);
820 PyObject_ReleaseBuffer(value, &view);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000821 }
822 break;
823 }
824 return TRUE;
825}
826
827/* Convert Registry data into PyObject*/
828static PyObject *
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000829Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000830{
831 PyObject *obData;
832
833 switch (typ) {
834 case REG_DWORD:
835 if (retDataSize == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +0000836 obData = PyLong_FromLong(0);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000837 else
Christian Heimes217cfd12007-12-02 14:31:20 +0000838 obData = PyLong_FromLong(*(int *)retDataBuf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000839 break;
840 case REG_SZ:
841 case REG_EXPAND_SZ:
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000842 {
843 /* the buffer may or may not have a trailing NULL */
844 wchar_t *data = (wchar_t *)retDataBuf;
845 int len = retDataSize / 2;
846 if (retDataSize && data[len-1] == '\0')
847 retDataSize -= 2;
848 if (retDataSize <= 0)
849 data = L"";
850 obData = PyUnicode_FromUnicode(data, retDataSize/2);
851 break;
852 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000853 case REG_MULTI_SZ:
854 if (retDataSize == 0)
855 obData = PyList_New(0);
856 else
857 {
858 int index = 0;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000859 wchar_t *data = (wchar_t *)retDataBuf;
860 int len = retDataSize / 2;
861 int s = countStrings(data, len);
862 wchar_t **str = (wchar_t **)malloc(sizeof(wchar_t *)*s);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000863 if (str == NULL)
864 return PyErr_NoMemory();
865
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000866 fixupMultiSZ(str, data, len);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000867 obData = PyList_New(s);
Fred Drake25e17262000-06-30 17:48:51 +0000868 if (obData == NULL)
869 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000870 for (index = 0; index < s; index++)
871 {
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000872 size_t len = wcslen(str[index]);
Fred Drake25e17262000-06-30 17:48:51 +0000873 if (len > INT_MAX) {
874 PyErr_SetString(PyExc_OverflowError,
875 "registry string is too long for a Python string");
876 Py_DECREF(obData);
877 return NULL;
878 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000879 PyList_SetItem(obData,
880 index,
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000881 PyUnicode_FromUnicode(str[index], len));
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000882 }
883 free(str);
884
885 break;
886 }
887 case REG_BINARY:
888 /* ALSO handle ALL unknown data types here. Even if we can't
889 support it natively, we should handle the bits. */
890 default:
891 if (retDataSize == 0) {
892 Py_INCREF(Py_None);
893 obData = Py_None;
894 }
895 else
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000896 obData = PyBytes_FromStringAndSize(
897 (char *)retDataBuf, retDataSize);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000898 break;
899 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000900 return obData;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000901}
902
903/* The Python methods */
904
905static PyObject *
906PyCloseKey(PyObject *self, PyObject *args)
907{
908 PyObject *obKey;
909 if (!PyArg_ParseTuple(args, "O:CloseKey", &obKey))
910 return NULL;
911 if (!PyHKEY_Close(obKey))
912 return NULL;
913 Py_INCREF(Py_None);
914 return Py_None;
915}
916
917static PyObject *
918PyConnectRegistry(PyObject *self, PyObject *args)
919{
920 HKEY hKey;
921 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000922 wchar_t *szCompName = NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000923 HKEY retKey;
924 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000925 if (!PyArg_ParseTuple(args, "ZO:ConnectRegistry", &szCompName, &obKey))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000926 return NULL;
927 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
928 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000929 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000930 rc = RegConnectRegistryW(szCompName, hKey, &retKey);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000931 Py_END_ALLOW_THREADS
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000932 if (rc != ERROR_SUCCESS)
933 return PyErr_SetFromWindowsErrWithFunction(rc,
934 "ConnectRegistry");
935 return PyHKEY_FromHKEY(retKey);
936}
937
938static PyObject *
939PyCreateKey(PyObject *self, PyObject *args)
940{
941 HKEY hKey;
942 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000943 wchar_t *subKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000944 HKEY retKey;
945 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000946 if (!PyArg_ParseTuple(args, "OZ:CreateKey", &obKey, &subKey))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000947 return NULL;
948 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
949 return NULL;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000950 rc = RegCreateKeyW(hKey, subKey, &retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000951 if (rc != ERROR_SUCCESS)
952 return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey");
953 return PyHKEY_FromHKEY(retKey);
954}
955
956static PyObject *
957PyDeleteKey(PyObject *self, PyObject *args)
958{
959 HKEY hKey;
960 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000961 wchar_t *subKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000962 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000963 if (!PyArg_ParseTuple(args, "Ou:DeleteKey", &obKey, &subKey))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000964 return NULL;
965 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
966 return NULL;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000967 rc = RegDeleteKeyW(hKey, subKey );
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000968 if (rc != ERROR_SUCCESS)
969 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey");
970 Py_INCREF(Py_None);
971 return Py_None;
972}
973
974static PyObject *
975PyDeleteValue(PyObject *self, PyObject *args)
976{
977 HKEY hKey;
978 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000979 wchar_t *subKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000980 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000981 if (!PyArg_ParseTuple(args, "OZ:DeleteValue", &obKey, &subKey))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000982 return NULL;
983 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
984 return NULL;
985 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000986 rc = RegDeleteValueW(hKey, subKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000987 Py_END_ALLOW_THREADS
988 if (rc !=ERROR_SUCCESS)
989 return PyErr_SetFromWindowsErrWithFunction(rc,
990 "RegDeleteValue");
991 Py_INCREF(Py_None);
992 return Py_None;
993}
994
995static PyObject *
996PyEnumKey(PyObject *self, PyObject *args)
997{
998 HKEY hKey;
999 PyObject *obKey;
1000 int index;
1001 long rc;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001002 PyObject *retStr;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001003 wchar_t tmpbuf[256]; /* max key name length is 255 */
Georg Brandlb2699b22006-02-18 23:44:24 +00001004 DWORD len = sizeof(tmpbuf); /* includes NULL terminator */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001005
1006 if (!PyArg_ParseTuple(args, "Oi:EnumKey", &obKey, &index))
1007 return NULL;
1008 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1009 return NULL;
Tim Peters313fcd42006-02-19 04:05:39 +00001010
Georg Brandl9a928e72006-02-18 23:35:11 +00001011 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001012 rc = RegEnumKeyExW(hKey, index, tmpbuf, &len, NULL, NULL, NULL, NULL);
Georg Brandl9a928e72006-02-18 23:35:11 +00001013 Py_END_ALLOW_THREADS
1014 if (rc != ERROR_SUCCESS)
1015 return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx");
Tim Peters313fcd42006-02-19 04:05:39 +00001016
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001017 retStr = PyUnicode_FromUnicode(tmpbuf, len);
Georg Brandl9a928e72006-02-18 23:35:11 +00001018 return retStr; /* can be NULL */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001019}
1020
1021static PyObject *
1022PyEnumValue(PyObject *self, PyObject *args)
1023{
1024 HKEY hKey;
1025 PyObject *obKey;
1026 int index;
1027 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001028 wchar_t *retValueBuf;
1029 BYTE *retDataBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001030 DWORD retValueSize;
1031 DWORD retDataSize;
1032 DWORD typ;
1033 PyObject *obData;
1034 PyObject *retVal;
1035
1036 if (!PyArg_ParseTuple(args, "Oi:EnumValue", &obKey, &index))
1037 return NULL;
1038 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1039 return NULL;
1040
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001041 if ((rc = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, NULL, NULL,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001042 NULL,
1043 &retValueSize, &retDataSize, NULL, NULL))
1044 != ERROR_SUCCESS)
1045 return PyErr_SetFromWindowsErrWithFunction(rc,
1046 "RegQueryInfoKey");
1047 ++retValueSize; /* include null terminators */
1048 ++retDataSize;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001049 retValueBuf = (wchar_t *)PyMem_Malloc(sizeof(wchar_t) * retValueSize);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001050 if (retValueBuf == NULL)
1051 return PyErr_NoMemory();
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001052 retDataBuf = (BYTE *)PyMem_Malloc(retDataSize);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001053 if (retDataBuf == NULL) {
1054 PyMem_Free(retValueBuf);
1055 return PyErr_NoMemory();
1056 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001057
1058 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001059 rc = RegEnumValueW(hKey,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001060 index,
1061 retValueBuf,
1062 &retValueSize,
1063 NULL,
1064 &typ,
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001065 retDataBuf,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001066 &retDataSize);
1067 Py_END_ALLOW_THREADS
1068
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001069 if (rc != ERROR_SUCCESS) {
1070 retVal = PyErr_SetFromWindowsErrWithFunction(rc,
1071 "PyRegEnumValue");
1072 goto fail;
1073 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001074 obData = Reg2Py(retDataBuf, retDataSize, typ);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001075 if (obData == NULL) {
1076 retVal = NULL;
1077 goto fail;
1078 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001079 retVal = Py_BuildValue("uOi", retValueBuf, obData, typ);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001080 Py_DECREF(obData);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001081 fail:
1082 PyMem_Free(retValueBuf);
1083 PyMem_Free(retDataBuf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001084 return retVal;
1085}
1086
1087static PyObject *
Christian Heimes2380ac72008-01-09 00:17:24 +00001088PyExpandEnvironmentStrings(PyObject *self, PyObject *args)
1089{
1090 Py_UNICODE *retValue = NULL;
1091 Py_UNICODE *src;
1092 DWORD retValueSize;
1093 DWORD rc;
1094 PyObject *o;
1095
1096 if (!PyArg_ParseTuple(args, "u:ExpandEnvironmentStrings", &src))
1097 return NULL;
1098
1099 retValueSize = ExpandEnvironmentStringsW(src, retValue, 0);
1100 if (retValueSize == 0) {
1101 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1102 "ExpandEnvironmentStrings");
1103 }
1104 retValue = (Py_UNICODE *)PyMem_Malloc(retValueSize * sizeof(Py_UNICODE));
1105 if (retValue == NULL) {
1106 return PyErr_NoMemory();
1107 }
1108
1109 rc = ExpandEnvironmentStringsW(src, retValue, retValueSize);
1110 if (rc == 0) {
1111 PyMem_Free(retValue);
1112 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1113 "ExpandEnvironmentStrings");
1114 }
1115 o = PyUnicode_FromUnicode(retValue, wcslen(retValue));
1116 PyMem_Free(retValue);
1117 return o;
1118}
1119
1120static PyObject *
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001121PyFlushKey(PyObject *self, PyObject *args)
1122{
1123 HKEY hKey;
1124 PyObject *obKey;
1125 long rc;
1126 if (!PyArg_ParseTuple(args, "O:FlushKey", &obKey))
1127 return NULL;
1128 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1129 return NULL;
1130 Py_BEGIN_ALLOW_THREADS
1131 rc = RegFlushKey(hKey);
1132 Py_END_ALLOW_THREADS
1133 if (rc != ERROR_SUCCESS)
1134 return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey");
1135 Py_INCREF(Py_None);
1136 return Py_None;
1137}
1138static PyObject *
1139PyLoadKey(PyObject *self, PyObject *args)
1140{
1141 HKEY hKey;
1142 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001143 wchar_t *subKey;
1144 wchar_t *fileName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001145
1146 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001147 if (!PyArg_ParseTuple(args, "Ouu:LoadKey", &obKey, &subKey, &fileName))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001148 return NULL;
1149 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1150 return NULL;
1151 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001152 rc = RegLoadKeyW(hKey, subKey, fileName );
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001153 Py_END_ALLOW_THREADS
1154 if (rc != ERROR_SUCCESS)
1155 return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey");
1156 Py_INCREF(Py_None);
1157 return Py_None;
1158}
1159
1160static PyObject *
1161PyOpenKey(PyObject *self, PyObject *args)
1162{
1163 HKEY hKey;
1164 PyObject *obKey;
1165
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001166 wchar_t *subKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001167 int res = 0;
1168 HKEY retKey;
1169 long rc;
1170 REGSAM sam = KEY_READ;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001171 if (!PyArg_ParseTuple(args, "OZ|ii:OpenKey", &obKey, &subKey,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001172 &res, &sam))
1173 return NULL;
1174 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1175 return NULL;
1176
1177 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001178 rc = RegOpenKeyExW(hKey, subKey, res, sam, &retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001179 Py_END_ALLOW_THREADS
1180 if (rc != ERROR_SUCCESS)
1181 return PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx");
1182 return PyHKEY_FromHKEY(retKey);
1183}
1184
1185
1186static PyObject *
1187PyQueryInfoKey(PyObject *self, PyObject *args)
1188{
1189 HKEY hKey;
1190 PyObject *obKey;
1191 long rc;
1192 DWORD nSubKeys, nValues;
1193 FILETIME ft;
1194 LARGE_INTEGER li;
1195 PyObject *l;
1196 PyObject *ret;
1197 if (!PyArg_ParseTuple(args, "O:QueryInfoKey", &obKey))
1198 return NULL;
1199 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1200 return NULL;
1201 if ((rc = RegQueryInfoKey(hKey, NULL, NULL, 0, &nSubKeys, NULL, NULL,
1202 &nValues, NULL, NULL, NULL, &ft))
1203 != ERROR_SUCCESS)
1204 return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey");
1205 li.LowPart = ft.dwLowDateTime;
1206 li.HighPart = ft.dwHighDateTime;
1207 l = PyLong_FromLongLong(li.QuadPart);
1208 if (l == NULL)
1209 return NULL;
1210 ret = Py_BuildValue("iiO", nSubKeys, nValues, l);
1211 Py_DECREF(l);
1212 return ret;
1213}
1214
1215static PyObject *
1216PyQueryValue(PyObject *self, PyObject *args)
1217{
1218 HKEY hKey;
1219 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001220 wchar_t *subKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001221 long rc;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001222 PyObject *retStr;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001223 wchar_t *retBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001224 long bufSize = 0;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001225
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001226 if (!PyArg_ParseTuple(args, "OZ:QueryValue", &obKey, &subKey))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001227 return NULL;
1228
1229 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1230 return NULL;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001231 if ((rc = RegQueryValueW(hKey, subKey, NULL, &bufSize))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001232 != ERROR_SUCCESS)
1233 return PyErr_SetFromWindowsErrWithFunction(rc,
1234 "RegQueryValue");
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001235 retBuf = (wchar_t *)PyMem_Malloc(bufSize);
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001236 if (retBuf == NULL)
1237 return PyErr_NoMemory();
1238
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001239 if ((rc = RegQueryValueW(hKey, subKey, retBuf, &bufSize))
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001240 != ERROR_SUCCESS) {
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001241 PyMem_Free(retBuf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001242 return PyErr_SetFromWindowsErrWithFunction(rc,
1243 "RegQueryValue");
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001244 }
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001245
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001246 retStr = PyUnicode_FromUnicode(retBuf, wcslen(retBuf));
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001247 PyMem_Free(retBuf);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001248 return retStr;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001249}
1250
1251static PyObject *
1252PyQueryValueEx(PyObject *self, PyObject *args)
1253{
1254 HKEY hKey;
1255 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001256 wchar_t *valueName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001257
1258 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001259 BYTE *retBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001260 DWORD bufSize = 0;
1261 DWORD typ;
1262 PyObject *obData;
1263 PyObject *result;
1264
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001265 if (!PyArg_ParseTuple(args, "OZ:QueryValueEx", &obKey, &valueName))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001266 return NULL;
1267
1268 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1269 return NULL;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001270 if ((rc = RegQueryValueExW(hKey, valueName,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001271 NULL, NULL, NULL,
1272 &bufSize))
1273 != ERROR_SUCCESS)
1274 return PyErr_SetFromWindowsErrWithFunction(rc,
1275 "RegQueryValueEx");
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001276 retBuf = (BYTE *)PyMem_Malloc(bufSize);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001277 if (retBuf == NULL)
1278 return PyErr_NoMemory();
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001279 if ((rc = RegQueryValueExW(hKey, valueName, NULL,
1280 &typ, retBuf, &bufSize))
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001281 != ERROR_SUCCESS) {
1282 PyMem_Free(retBuf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001283 return PyErr_SetFromWindowsErrWithFunction(rc,
1284 "RegQueryValueEx");
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001285 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001286 obData = Reg2Py(retBuf, bufSize, typ);
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001287 PyMem_Free(retBuf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001288 if (obData == NULL)
1289 return NULL;
1290 result = Py_BuildValue("Oi", obData, typ);
1291 Py_DECREF(obData);
1292 return result;
1293}
1294
1295
1296static PyObject *
1297PySaveKey(PyObject *self, PyObject *args)
1298{
1299 HKEY hKey;
1300 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001301 wchar_t *fileName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001302 LPSECURITY_ATTRIBUTES pSA = NULL;
1303
1304 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001305 if (!PyArg_ParseTuple(args, "Ou:SaveKey", &obKey, &fileName))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001306 return NULL;
1307 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1308 return NULL;
1309/* One day we may get security into the core?
1310 if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE))
1311 return NULL;
1312*/
1313 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001314 rc = RegSaveKeyW(hKey, fileName, pSA );
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001315 Py_END_ALLOW_THREADS
1316 if (rc != ERROR_SUCCESS)
1317 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey");
1318 Py_INCREF(Py_None);
1319 return Py_None;
1320}
1321
1322static PyObject *
1323PySetValue(PyObject *self, PyObject *args)
1324{
1325 HKEY hKey;
1326 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001327 wchar_t *subKey;
1328 wchar_t *str;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001329 DWORD typ;
1330 DWORD len;
1331 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001332 if (!PyArg_ParseTuple(args, "OZiu#:SetValue",
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001333 &obKey,
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001334 &subKey,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001335 &typ,
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001336 &str,
1337 &len))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001338 return NULL;
1339 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1340 return NULL;
1341 if (typ != REG_SZ) {
1342 PyErr_SetString(PyExc_TypeError,
Thomas Hellere1d18f52002-12-20 20:13:35 +00001343 "Type must be _winreg.REG_SZ");
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001344 return NULL;
1345 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001346
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001347 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001348 rc = RegSetValueW(hKey, subKey, REG_SZ, str, len+1);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001349 Py_END_ALLOW_THREADS
1350 if (rc != ERROR_SUCCESS)
1351 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue");
1352 Py_INCREF(Py_None);
1353 return Py_None;
1354}
1355
1356static PyObject *
1357PySetValueEx(PyObject *self, PyObject *args)
1358{
1359 HKEY hKey;
1360 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001361 Py_UNICODE *valueName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001362 PyObject *obRes;
1363 PyObject *value;
1364 BYTE *data;
1365 DWORD len;
1366 DWORD typ;
1367
1368 LONG rc;
1369
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001370 if (!PyArg_ParseTuple(args, "OZOiO:SetValueEx",
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001371 &obKey,
1372 &valueName,
1373 &obRes,
1374 &typ,
1375 &value))
1376 return NULL;
1377 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1378 return NULL;
1379 if (!Py2Reg(value, typ, &data, &len))
1380 {
1381 if (!PyErr_Occurred())
1382 PyErr_SetString(PyExc_ValueError,
1383 "Could not convert the data to the specified type.");
1384 return NULL;
1385 }
1386 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001387 rc = RegSetValueExW(hKey, valueName, 0, typ, data, len);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001388 Py_END_ALLOW_THREADS
Guido van Rossumb18618d2000-05-03 23:44:39 +00001389 PyMem_DEL(data);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001390 if (rc != ERROR_SUCCESS)
1391 return PyErr_SetFromWindowsErrWithFunction(rc,
1392 "RegSetValueEx");
1393 Py_INCREF(Py_None);
1394 return Py_None;
1395}
1396
1397static struct PyMethodDef winreg_methods[] = {
Neal Norwitz031829d2002-03-31 14:37:44 +00001398 {"CloseKey", PyCloseKey, METH_VARARGS, CloseKey_doc},
1399 {"ConnectRegistry", PyConnectRegistry, METH_VARARGS, ConnectRegistry_doc},
1400 {"CreateKey", PyCreateKey, METH_VARARGS, CreateKey_doc},
1401 {"DeleteKey", PyDeleteKey, METH_VARARGS, DeleteKey_doc},
1402 {"DeleteValue", PyDeleteValue, METH_VARARGS, DeleteValue_doc},
1403 {"EnumKey", PyEnumKey, METH_VARARGS, EnumKey_doc},
1404 {"EnumValue", PyEnumValue, METH_VARARGS, EnumValue_doc},
Christian Heimes2380ac72008-01-09 00:17:24 +00001405 {"ExpandEnvironmentStrings", PyExpandEnvironmentStrings, METH_VARARGS,
1406 ExpandEnvironmentStrings_doc },
Neal Norwitz031829d2002-03-31 14:37:44 +00001407 {"FlushKey", PyFlushKey, METH_VARARGS, FlushKey_doc},
1408 {"LoadKey", PyLoadKey, METH_VARARGS, LoadKey_doc},
1409 {"OpenKey", PyOpenKey, METH_VARARGS, OpenKey_doc},
1410 {"OpenKeyEx", PyOpenKey, METH_VARARGS, OpenKeyEx_doc},
1411 {"QueryValue", PyQueryValue, METH_VARARGS, QueryValue_doc},
1412 {"QueryValueEx", PyQueryValueEx, METH_VARARGS, QueryValueEx_doc},
1413 {"QueryInfoKey", PyQueryInfoKey, METH_VARARGS, QueryInfoKey_doc},
1414 {"SaveKey", PySaveKey, METH_VARARGS, SaveKey_doc},
1415 {"SetValue", PySetValue, METH_VARARGS, SetValue_doc},
1416 {"SetValueEx", PySetValueEx, METH_VARARGS, SetValueEx_doc},
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001417 NULL,
1418};
1419
1420static void
1421insint(PyObject * d, char * name, long value)
1422{
Christian Heimes217cfd12007-12-02 14:31:20 +00001423 PyObject *v = PyLong_FromLong(value);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001424 if (!v || PyDict_SetItemString(d, name, v))
1425 PyErr_Clear();
1426 Py_XDECREF(v);
1427}
1428
1429#define ADD_INT(val) insint(d, #val, val)
1430
1431static void
1432inskey(PyObject * d, char * name, HKEY key)
1433{
1434 PyObject *v = PyLong_FromVoidPtr(key);
1435 if (!v || PyDict_SetItemString(d, name, v))
1436 PyErr_Clear();
1437 Py_XDECREF(v);
1438}
1439
1440#define ADD_KEY(val) inskey(d, #val, val)
1441
Mark Hammond8235ea12002-07-19 06:55:41 +00001442PyMODINIT_FUNC init_winreg(void)
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001443{
1444 PyObject *m, *d;
Fred Drake270e19b2000-06-29 16:14:14 +00001445 m = Py_InitModule3("_winreg", winreg_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001446 if (m == NULL)
1447 return;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001448 d = PyModule_GetDict(m);
Christian Heimes90aa7642007-12-19 02:45:37 +00001449 Py_TYPE(&PyHKEY_Type) = &PyType_Type;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001450 PyHKEY_Type.tp_doc = PyHKEY_doc;
1451 Py_INCREF(&PyHKEY_Type);
1452 if (PyDict_SetItemString(d, "HKEYType",
1453 (PyObject *)&PyHKEY_Type) != 0)
1454 return;
1455 Py_INCREF(PyExc_WindowsError);
1456 if (PyDict_SetItemString(d, "error",
1457 PyExc_WindowsError) != 0)
1458 return;
1459
1460 /* Add the relevant constants */
1461 ADD_KEY(HKEY_CLASSES_ROOT);
1462 ADD_KEY(HKEY_CURRENT_USER);
1463 ADD_KEY(HKEY_LOCAL_MACHINE);
1464 ADD_KEY(HKEY_USERS);
1465 ADD_KEY(HKEY_PERFORMANCE_DATA);
1466#ifdef HKEY_CURRENT_CONFIG
1467 ADD_KEY(HKEY_CURRENT_CONFIG);
1468#endif
1469#ifdef HKEY_DYN_DATA
1470 ADD_KEY(HKEY_DYN_DATA);
1471#endif
1472 ADD_INT(KEY_QUERY_VALUE);
1473 ADD_INT(KEY_SET_VALUE);
1474 ADD_INT(KEY_CREATE_SUB_KEY);
1475 ADD_INT(KEY_ENUMERATE_SUB_KEYS);
1476 ADD_INT(KEY_NOTIFY);
1477 ADD_INT(KEY_CREATE_LINK);
1478 ADD_INT(KEY_READ);
1479 ADD_INT(KEY_WRITE);
1480 ADD_INT(KEY_EXECUTE);
1481 ADD_INT(KEY_ALL_ACCESS);
1482 ADD_INT(REG_OPTION_RESERVED);
1483 ADD_INT(REG_OPTION_NON_VOLATILE);
1484 ADD_INT(REG_OPTION_VOLATILE);
1485 ADD_INT(REG_OPTION_CREATE_LINK);
1486 ADD_INT(REG_OPTION_BACKUP_RESTORE);
1487 ADD_INT(REG_OPTION_OPEN_LINK);
1488 ADD_INT(REG_LEGAL_OPTION);
1489 ADD_INT(REG_CREATED_NEW_KEY);
1490 ADD_INT(REG_OPENED_EXISTING_KEY);
1491 ADD_INT(REG_WHOLE_HIVE_VOLATILE);
1492 ADD_INT(REG_REFRESH_HIVE);
1493 ADD_INT(REG_NO_LAZY_FLUSH);
1494 ADD_INT(REG_NOTIFY_CHANGE_NAME);
1495 ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES);
1496 ADD_INT(REG_NOTIFY_CHANGE_LAST_SET);
1497 ADD_INT(REG_NOTIFY_CHANGE_SECURITY);
1498 ADD_INT(REG_LEGAL_CHANGE_FILTER);
1499 ADD_INT(REG_NONE);
1500 ADD_INT(REG_SZ);
1501 ADD_INT(REG_EXPAND_SZ);
1502 ADD_INT(REG_BINARY);
1503 ADD_INT(REG_DWORD);
1504 ADD_INT(REG_DWORD_LITTLE_ENDIAN);
1505 ADD_INT(REG_DWORD_BIG_ENDIAN);
1506 ADD_INT(REG_LINK);
1507 ADD_INT(REG_MULTI_SZ);
1508 ADD_INT(REG_RESOURCE_LIST);
1509 ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
1510 ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
1511}
1512
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001513