blob: 52d230e7b985cf55774b2e4ef67bd6e98b4741c2 [file] [log] [blame]
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001/*
Georg Brandl38feaf02008-05-25 07:45:51 +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
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00007 module circa 1995.
Guido van Rossum9f3712c2000-03-28 20:37:15 +00008 * 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) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000031 PyErr_SetFromWindowsErr(rc)
Guido van Rossum9f3712c2000-03-28 20:37:15 +000032
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
Martin v. Löwisd218dc12008-04-07 03:17:54 +0000292PyDoc_STRVAR(DisableReflectionKey_doc,
Christian Heimes5e696852008-04-09 08:37:03 +0000293"Disables registry reflection for 32-bit processes running on a 64-bit\n"
Martin v. Löwisd218dc12008-04-07 03:17:54 +0000294"Operating System. Will generally raise NotImplemented if executed on\n"
Christian Heimes5e696852008-04-09 08:37:03 +0000295"a 32-bit Operating System.\n"
Martin v. Löwisd218dc12008-04-07 03:17:54 +0000296"If the key is not on the reflection list, the function succeeds but has no effect.\n"
297"Disabling reflection for a key does not affect reflection of any subkeys.");
298
299PyDoc_STRVAR(EnableReflectionKey_doc,
300"Restores registry reflection for the specified disabled key.\n"
Christian Heimes5e696852008-04-09 08:37:03 +0000301"Will generally raise NotImplemented if executed on a 32-bit Operating System.\n"
Martin v. Löwisd218dc12008-04-07 03:17:54 +0000302"Restoring reflection for a key does not affect reflection of any subkeys.");
303
304PyDoc_STRVAR(QueryReflectionKey_doc,
305"bool = QueryReflectionKey(hkey) - Determines the reflection state for the specified key.\n"
Christian Heimes5e696852008-04-09 08:37:03 +0000306"Will generally raise NotImplemented if executed on a 32-bit Operating System.\n");
Martin v. Löwisd218dc12008-04-07 03:17:54 +0000307
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000308/* PyHKEY docstrings */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000309PyDoc_STRVAR(PyHKEY_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000310"PyHKEY Object - A Python object, representing a win32 registry key.\n"
311"\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000312"This object wraps a Windows HKEY object, automatically closing it when\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000313"the object is destroyed. To guarantee cleanup, you can call either\n"
314"the Close() method on the PyHKEY, or the CloseKey() method.\n"
315"\n"
316"All functions which accept a handle object also accept an integer - \n"
317"however, use of the handle object is encouraged.\n"
318"\n"
319"Functions:\n"
320"Close() - Closes the underlying handle.\n"
321"Detach() - Returns the integer Win32 handle, detaching it from the object\n"
322"\n"
323"Properties:\n"
324"handle - The integer Win32 handle.\n"
325"\n"
326"Operations:\n"
Jack Diederich4dafcc42006-11-28 19:15:13 +0000327"__bool__ - Handles with an open object return true, otherwise false.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000328"__int__ - Converting a handle to an integer returns the Win32 handle.\n"
Mark Dickinson211c6252009-02-01 10:28:51 +0000329"rich comparison - Handle objects are compared using the handle value.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000330
331
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000332PyDoc_STRVAR(PyHKEY_Close_doc,
Mark Hammondb422f952000-06-09 06:01:47 +0000333"key.Close() - Closes the underlying Windows handle.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000334"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000335"If the handle is already closed, no error is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000336
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000337PyDoc_STRVAR(PyHKEY_Detach_doc,
Mark Hammondb422f952000-06-09 06:01:47 +0000338"int = key.Detach() - Detaches the Windows handle from the handle object.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000339"\n"
340"The result is the value of the handle before it is detached. If the\n"
341"handle is already detached, this will return zero.\n"
342"\n"
343"After calling this function, the handle is effectively invalidated,\n"
344"but the handle is not closed. You would call this function when you\n"
345"need the underlying win32 handle to exist beyond the lifetime of the\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000346"handle object.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000347"On 64 bit windows, the result of this function is a long integer");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000348
349
350/************************************************************************
351
352 The PyHKEY object definition
353
354************************************************************************/
355typedef struct {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000356 PyObject_VAR_HEAD
357 HKEY hkey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000358} PyHKEYObject;
359
360#define PyHKEY_Check(op) ((op)->ob_type == &PyHKEY_Type)
361
362static char *failMsg = "bad operand type";
363
364static PyObject *
365PyHKEY_unaryFailureFunc(PyObject *ob)
366{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000367 PyErr_SetString(PyExc_TypeError, failMsg);
368 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000369}
370static PyObject *
371PyHKEY_binaryFailureFunc(PyObject *ob1, PyObject *ob2)
372{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000373 PyErr_SetString(PyExc_TypeError, failMsg);
374 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000375}
376static PyObject *
377PyHKEY_ternaryFailureFunc(PyObject *ob1, PyObject *ob2, PyObject *ob3)
378{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000379 PyErr_SetString(PyExc_TypeError, failMsg);
380 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000381}
382
383static void
384PyHKEY_deallocFunc(PyObject *ob)
385{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000386 /* Can not call PyHKEY_Close, as the ob->tp_type
387 has already been cleared, thus causing the type
388 check to fail!
389 */
390 PyHKEYObject *obkey = (PyHKEYObject *)ob;
391 if (obkey->hkey)
392 RegCloseKey((HKEY)obkey->hkey);
393 PyObject_DEL(ob);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000394}
395
396static int
Jack Diederich4dafcc42006-11-28 19:15:13 +0000397PyHKEY_boolFunc(PyObject *ob)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000398{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000399 return ((PyHKEYObject *)ob)->hkey != 0;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000400}
401
402static PyObject *
403PyHKEY_intFunc(PyObject *ob)
404{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000405 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
406 return PyLong_FromVoidPtr(pyhkey->hkey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000407}
408
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000409static PyObject *
410PyHKEY_strFunc(PyObject *ob)
411{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000412 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
413 return PyUnicode_FromFormat("<PyHKEY:%p>", pyhkey->hkey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000414}
415
416static int
417PyHKEY_compareFunc(PyObject *ob1, PyObject *ob2)
418{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000419 PyHKEYObject *pyhkey1 = (PyHKEYObject *)ob1;
420 PyHKEYObject *pyhkey2 = (PyHKEYObject *)ob2;
421 return pyhkey1 == pyhkey2 ? 0 :
422 (pyhkey1 < pyhkey2 ? -1 : 1);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000423}
424
425static long
426PyHKEY_hashFunc(PyObject *ob)
427{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000428 /* Just use the address.
429 XXX - should we use the handle value?
430 */
431 return _Py_HashPointer(ob);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000432}
433
434
435static PyNumberMethods PyHKEY_NumberMethods =
436{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000437 PyHKEY_binaryFailureFunc, /* nb_add */
438 PyHKEY_binaryFailureFunc, /* nb_subtract */
439 PyHKEY_binaryFailureFunc, /* nb_multiply */
440 PyHKEY_binaryFailureFunc, /* nb_remainder */
441 PyHKEY_binaryFailureFunc, /* nb_divmod */
442 PyHKEY_ternaryFailureFunc, /* nb_power */
443 PyHKEY_unaryFailureFunc, /* nb_negative */
444 PyHKEY_unaryFailureFunc, /* nb_positive */
445 PyHKEY_unaryFailureFunc, /* nb_absolute */
446 PyHKEY_boolFunc, /* nb_bool */
447 PyHKEY_unaryFailureFunc, /* nb_invert */
448 PyHKEY_binaryFailureFunc, /* nb_lshift */
449 PyHKEY_binaryFailureFunc, /* nb_rshift */
450 PyHKEY_binaryFailureFunc, /* nb_and */
451 PyHKEY_binaryFailureFunc, /* nb_xor */
452 PyHKEY_binaryFailureFunc, /* nb_or */
453 PyHKEY_intFunc, /* nb_int */
454 0, /* nb_reserved */
455 PyHKEY_unaryFailureFunc, /* nb_float */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000456};
457
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000458static PyObject *PyHKEY_CloseMethod(PyObject *self, PyObject *args);
459static PyObject *PyHKEY_DetachMethod(PyObject *self, PyObject *args);
460static PyObject *PyHKEY_Enter(PyObject *self);
461static PyObject *PyHKEY_Exit(PyObject *self, PyObject *args);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000462
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000463static struct PyMethodDef PyHKEY_methods[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000464 {"Close", PyHKEY_CloseMethod, METH_VARARGS, PyHKEY_Close_doc},
465 {"Detach", PyHKEY_DetachMethod, METH_VARARGS, PyHKEY_Detach_doc},
466 {"__enter__", (PyCFunction)PyHKEY_Enter, METH_NOARGS, NULL},
467 {"__exit__", PyHKEY_Exit, METH_VARARGS, NULL},
468 {NULL}
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000469};
470
471#define OFF(e) offsetof(PyHKEYObject, e)
472static PyMemberDef PyHKEY_memberlist[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000473 {"handle", T_INT, OFF(hkey), READONLY},
474 {NULL} /* Sentinel */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000475};
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000476
477/* The type itself */
478PyTypeObject PyHKEY_Type =
479{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000480 PyVarObject_HEAD_INIT(0, 0) /* fill in type at module init */
481 "PyHKEY",
482 sizeof(PyHKEYObject),
483 0,
484 PyHKEY_deallocFunc, /* tp_dealloc */
485 0, /* tp_print */
486 0, /* tp_getattr */
487 0, /* tp_setattr */
488 0, /* tp_reserved */
489 0, /* tp_repr */
490 &PyHKEY_NumberMethods, /* tp_as_number */
491 0, /* tp_as_sequence */
492 0, /* tp_as_mapping */
493 PyHKEY_hashFunc, /* tp_hash */
494 0, /* tp_call */
495 PyHKEY_strFunc, /* tp_str */
496 0, /* tp_getattro */
497 0, /* tp_setattro */
498 0, /* tp_as_buffer */
499 0, /* tp_flags */
500 PyHKEY_doc, /* tp_doc */
501 0, /*tp_traverse*/
502 0, /*tp_clear*/
503 0, /*tp_richcompare*/
504 0, /*tp_weaklistoffset*/
505 0, /*tp_iter*/
506 0, /*tp_iternext*/
507 PyHKEY_methods, /*tp_methods*/
508 PyHKEY_memberlist, /*tp_members*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000509};
510
511/************************************************************************
512
513 The PyHKEY object methods
514
515************************************************************************/
516static PyObject *
517PyHKEY_CloseMethod(PyObject *self, PyObject *args)
518{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000519 if (!PyArg_ParseTuple(args, ":Close"))
520 return NULL;
521 if (!PyHKEY_Close(self))
522 return NULL;
523 Py_INCREF(Py_None);
524 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000525}
526
527static PyObject *
528PyHKEY_DetachMethod(PyObject *self, PyObject *args)
529{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000530 void* ret;
531 PyHKEYObject *pThis = (PyHKEYObject *)self;
532 if (!PyArg_ParseTuple(args, ":Detach"))
533 return NULL;
534 ret = (void*)pThis->hkey;
535 pThis->hkey = 0;
536 return PyLong_FromVoidPtr(ret);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000537}
538
Christian Heimes2380ac72008-01-09 00:17:24 +0000539static PyObject *
540PyHKEY_Enter(PyObject *self)
541{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000542 Py_XINCREF(self);
543 return self;
Christian Heimes2380ac72008-01-09 00:17:24 +0000544}
545
546static PyObject *
547PyHKEY_Exit(PyObject *self, PyObject *args)
548{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000549 if (!PyHKEY_Close(self))
550 return NULL;
551 Py_RETURN_NONE;
Christian Heimes2380ac72008-01-09 00:17:24 +0000552}
553
554
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000555/************************************************************************
556 The public PyHKEY API (well, not public yet :-)
557************************************************************************/
558PyObject *
559PyHKEY_New(HKEY hInit)
560{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000561 PyHKEYObject *key = PyObject_NEW(PyHKEYObject, &PyHKEY_Type);
562 if (key)
563 key->hkey = hInit;
564 return (PyObject *)key;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000565}
566
567BOOL
568PyHKEY_Close(PyObject *ob_handle)
569{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000570 LONG rc;
571 PyHKEYObject *key;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000572
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000573 if (!PyHKEY_Check(ob_handle)) {
574 PyErr_SetString(PyExc_TypeError, "bad operand type");
575 return FALSE;
576 }
577 key = (PyHKEYObject *)ob_handle;
578 rc = key->hkey ? RegCloseKey((HKEY)key->hkey) : ERROR_SUCCESS;
579 key->hkey = 0;
580 if (rc != ERROR_SUCCESS)
581 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
582 return rc == ERROR_SUCCESS;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000583}
584
585BOOL
586PyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK)
587{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000588 if (ob == Py_None) {
589 if (!bNoneOK) {
590 PyErr_SetString(
591 PyExc_TypeError,
592 "None is not a valid HKEY in this context");
593 return FALSE;
594 }
595 *pHANDLE = (HKEY)0;
596 }
597 else if (PyHKEY_Check(ob)) {
598 PyHKEYObject *pH = (PyHKEYObject *)ob;
599 *pHANDLE = pH->hkey;
600 }
601 else if (PyLong_Check(ob)) {
602 /* We also support integers */
603 PyErr_Clear();
604 *pHANDLE = (HKEY)PyLong_AsVoidPtr(ob);
605 if (PyErr_Occurred())
606 return FALSE;
607 }
608 else {
609 PyErr_SetString(
610 PyExc_TypeError,
611 "The object is not a PyHKEY object");
612 return FALSE;
613 }
614 return TRUE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000615}
616
617PyObject *
618PyHKEY_FromHKEY(HKEY h)
619{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000620 PyHKEYObject *op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000621
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000622 /* Inline PyObject_New */
623 op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject));
624 if (op == NULL)
625 return PyErr_NoMemory();
626 PyObject_INIT(op, &PyHKEY_Type);
627 op->hkey = h;
628 return (PyObject *)op;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000629}
630
631
632/************************************************************************
633 The module methods
634************************************************************************/
635BOOL
636PyWinObject_CloseHKEY(PyObject *obHandle)
637{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000638 BOOL ok;
639 if (PyHKEY_Check(obHandle)) {
640 ok = PyHKEY_Close(obHandle);
641 }
Fred Drake25e17262000-06-30 17:48:51 +0000642#if SIZEOF_LONG >= SIZEOF_HKEY
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000643 else if (PyLong_Check(obHandle)) {
644 long rc = RegCloseKey((HKEY)PyLong_AsLong(obHandle));
645 ok = (rc == ERROR_SUCCESS);
646 if (!ok)
647 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
648 }
Fred Drake25e17262000-06-30 17:48:51 +0000649#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000650 else if (PyLong_Check(obHandle)) {
651 long rc = RegCloseKey((HKEY)PyLong_AsVoidPtr(obHandle));
652 ok = (rc == ERROR_SUCCESS);
653 if (!ok)
654 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
655 }
Fred Drake25e17262000-06-30 17:48:51 +0000656#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000657 else {
658 PyErr_SetString(
659 PyExc_TypeError,
660 "A handle must be a HKEY object or an integer");
661 return FALSE;
662 }
663 return ok;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000664}
665
666
667/*
668 Private Helper functions for the registry interfaces
669
670** Note that fixupMultiSZ and countString have both had changes
671** made to support "incorrect strings". The registry specification
672** calls for strings to be terminated with 2 null bytes. It seems
Thomas Wouters7e474022000-07-16 12:04:32 +0000673** some commercial packages install strings which dont conform,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000674** causing this code to fail - however, "regedit" etc still work
675** with these strings (ie only we dont!).
676*/
677static void
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000678fixupMultiSZ(wchar_t **str, wchar_t *data, int len)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000679{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000680 wchar_t *P;
681 int i;
682 wchar_t *Q;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000683
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000684 Q = data + len;
685 for (P = data, i = 0; P < Q && *P != '\0'; P++, i++) {
686 str[i] = P;
687 for(; *P != '\0'; P++)
688 ;
689 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000690}
691
692static int
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000693countStrings(wchar_t *data, int len)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000694{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000695 int strings;
696 wchar_t *P;
697 wchar_t *Q = data + len;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000698
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000699 for (P = data, strings = 0; P < Q && *P != '\0'; P++, strings++)
700 for (; P < Q && *P != '\0'; P++)
701 ;
702 return strings;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000703}
704
705/* Convert PyObject into Registry data.
706 Allocates space as needed. */
707static BOOL
708Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
709{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000710 Py_ssize_t i,j;
711 switch (typ) {
712 case REG_DWORD:
713 if (value != Py_None && !PyLong_Check(value))
714 return FALSE;
715 *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1);
716 if (*retDataBuf==NULL){
717 PyErr_NoMemory();
718 return FALSE;
719 }
720 *retDataSize = sizeof(DWORD);
721 if (value == Py_None) {
722 DWORD zero = 0;
723 memcpy(*retDataBuf, &zero, sizeof(DWORD));
724 }
725 else {
726 DWORD d = PyLong_AsLong(value);
727 memcpy(*retDataBuf, &d, sizeof(DWORD));
728 }
729 break;
730 case REG_SZ:
731 case REG_EXPAND_SZ:
732 {
733 if (value == Py_None)
734 *retDataSize = 1;
735 else {
736 if (!PyUnicode_Check(value))
737 return FALSE;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000738
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000739 *retDataSize = 2 + PyUnicode_GET_DATA_SIZE(value);
740 }
741 *retDataBuf = (BYTE *)PyMem_NEW(DWORD, *retDataSize);
742 if (*retDataBuf==NULL){
743 PyErr_NoMemory();
744 return FALSE;
745 }
746 if (value == Py_None)
747 wcscpy((wchar_t *)*retDataBuf, L"");
748 else
749 wcscpy((wchar_t *)*retDataBuf,
750 PyUnicode_AS_UNICODE(value));
751 break;
752 }
753 case REG_MULTI_SZ:
754 {
755 DWORD size = 0;
756 wchar_t *P;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000757
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000758 if (value == Py_None)
759 i = 0;
760 else {
761 if (!PyList_Check(value))
762 return FALSE;
763 i = PyList_Size(value);
764 }
765 for (j = 0; j < i; j++)
766 {
767 PyObject *t;
768 t = PyList_GET_ITEM(value, j);
769 if (!PyUnicode_Check(t))
770 return FALSE;
771 size += 2 + PyUnicode_GET_DATA_SIZE(t);
772 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000773
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000774 *retDataSize = size + 2;
775 *retDataBuf = (BYTE *)PyMem_NEW(char,
776 *retDataSize);
777 if (*retDataBuf==NULL){
778 PyErr_NoMemory();
779 return FALSE;
780 }
781 P = (wchar_t *)*retDataBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000782
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000783 for (j = 0; j < i; j++)
784 {
785 PyObject *t;
786 t = PyList_GET_ITEM(value, j);
787 wcscpy(P, PyUnicode_AS_UNICODE(t));
788 P += 1 + wcslen(
789 PyUnicode_AS_UNICODE(t));
790 }
791 /* And doubly-terminate the list... */
792 *P = '\0';
793 break;
794 }
795 case REG_BINARY:
796 /* ALSO handle ALL unknown data types here. Even if we can't
797 support it natively, we should handle the bits. */
798 default:
799 if (value == Py_None)
800 *retDataSize = 0;
801 else {
802 Py_buffer view;
Neal Norwitz1385b892007-08-26 23:07:13 +0000803
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000804 if (!PyObject_CheckBuffer(value)) {
805 PyErr_Format(PyExc_TypeError,
806 "Objects of type '%s' can not "
807 "be used as binary registry values",
808 value->ob_type->tp_name);
809 return FALSE;
810 }
Neal Norwitz1385b892007-08-26 23:07:13 +0000811
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000812 if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0)
813 return FALSE;
Neal Norwitz1385b892007-08-26 23:07:13 +0000814
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000815 *retDataBuf = (BYTE *)PyMem_NEW(char, view.len);
816 if (*retDataBuf==NULL){
817 PyBuffer_Release(&view);
818 PyErr_NoMemory();
819 return FALSE;
820 }
821 *retDataSize = view.len;
822 memcpy(*retDataBuf, view.buf, view.len);
823 PyBuffer_Release(&view);
824 }
825 break;
826 }
827 return TRUE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000828}
829
830/* Convert Registry data into PyObject*/
831static PyObject *
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000832Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000833{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000834 PyObject *obData;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000835
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000836 switch (typ) {
837 case REG_DWORD:
838 if (retDataSize == 0)
839 obData = PyLong_FromLong(0);
840 else
841 obData = PyLong_FromLong(*(int *)retDataBuf);
842 break;
843 case REG_SZ:
844 case REG_EXPAND_SZ:
845 {
846 /* the buffer may or may not have a trailing NULL */
847 wchar_t *data = (wchar_t *)retDataBuf;
848 int len = retDataSize / 2;
849 if (retDataSize && data[len-1] == '\0')
850 retDataSize -= 2;
851 if (retDataSize <= 0)
852 data = L"";
853 obData = PyUnicode_FromUnicode(data, retDataSize/2);
854 break;
855 }
856 case REG_MULTI_SZ:
857 if (retDataSize == 0)
858 obData = PyList_New(0);
859 else
860 {
861 int index = 0;
862 wchar_t *data = (wchar_t *)retDataBuf;
863 int len = retDataSize / 2;
864 int s = countStrings(data, len);
865 wchar_t **str = (wchar_t **)malloc(sizeof(wchar_t *)*s);
866 if (str == NULL)
867 return PyErr_NoMemory();
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000868
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000869 fixupMultiSZ(str, data, len);
870 obData = PyList_New(s);
871 if (obData == NULL)
872 return NULL;
873 for (index = 0; index < s; index++)
874 {
875 size_t len = wcslen(str[index]);
876 if (len > INT_MAX) {
877 PyErr_SetString(PyExc_OverflowError,
878 "registry string is too long for a Python string");
879 Py_DECREF(obData);
880 return NULL;
881 }
882 PyList_SetItem(obData,
883 index,
884 PyUnicode_FromUnicode(str[index], len));
885 }
886 free(str);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000887
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000888 break;
889 }
890 case REG_BINARY:
891 /* ALSO handle ALL unknown data types here. Even if we can't
892 support it natively, we should handle the bits. */
893 default:
894 if (retDataSize == 0) {
895 Py_INCREF(Py_None);
896 obData = Py_None;
897 }
898 else
899 obData = PyBytes_FromStringAndSize(
900 (char *)retDataBuf, retDataSize);
901 break;
902 }
903 return obData;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000904}
905
906/* The Python methods */
907
908static PyObject *
909PyCloseKey(PyObject *self, PyObject *args)
910{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000911 PyObject *obKey;
912 if (!PyArg_ParseTuple(args, "O:CloseKey", &obKey))
913 return NULL;
914 if (!PyHKEY_Close(obKey))
915 return NULL;
916 Py_INCREF(Py_None);
917 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000918}
919
920static PyObject *
921PyConnectRegistry(PyObject *self, PyObject *args)
922{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000923 HKEY hKey;
924 PyObject *obKey;
925 wchar_t *szCompName = NULL;
926 HKEY retKey;
927 long rc;
928 if (!PyArg_ParseTuple(args, "ZO:ConnectRegistry", &szCompName, &obKey))
929 return NULL;
930 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
931 return NULL;
932 Py_BEGIN_ALLOW_THREADS
933 rc = RegConnectRegistryW(szCompName, hKey, &retKey);
934 Py_END_ALLOW_THREADS
935 if (rc != ERROR_SUCCESS)
936 return PyErr_SetFromWindowsErrWithFunction(rc,
937 "ConnectRegistry");
938 return PyHKEY_FromHKEY(retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000939}
940
941static PyObject *
942PyCreateKey(PyObject *self, PyObject *args)
943{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000944 HKEY hKey;
945 PyObject *obKey;
946 wchar_t *subKey;
947 HKEY retKey;
948 long rc;
949 if (!PyArg_ParseTuple(args, "OZ:CreateKey", &obKey, &subKey))
950 return NULL;
951 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
952 return NULL;
953 rc = RegCreateKeyW(hKey, subKey, &retKey);
954 if (rc != ERROR_SUCCESS)
955 return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey");
956 return PyHKEY_FromHKEY(retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000957}
958
959static PyObject *
960PyDeleteKey(PyObject *self, PyObject *args)
961{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000962 HKEY hKey;
963 PyObject *obKey;
964 wchar_t *subKey;
965 long rc;
966 if (!PyArg_ParseTuple(args, "Ou:DeleteKey", &obKey, &subKey))
967 return NULL;
968 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
969 return NULL;
970 rc = RegDeleteKeyW(hKey, subKey );
971 if (rc != ERROR_SUCCESS)
972 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey");
973 Py_INCREF(Py_None);
974 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000975}
976
977static PyObject *
978PyDeleteValue(PyObject *self, PyObject *args)
979{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000980 HKEY hKey;
981 PyObject *obKey;
982 wchar_t *subKey;
983 long rc;
984 if (!PyArg_ParseTuple(args, "OZ:DeleteValue", &obKey, &subKey))
985 return NULL;
986 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
987 return NULL;
988 Py_BEGIN_ALLOW_THREADS
989 rc = RegDeleteValueW(hKey, subKey);
990 Py_END_ALLOW_THREADS
991 if (rc !=ERROR_SUCCESS)
992 return PyErr_SetFromWindowsErrWithFunction(rc,
993 "RegDeleteValue");
994 Py_INCREF(Py_None);
995 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000996}
997
998static PyObject *
999PyEnumKey(PyObject *self, PyObject *args)
1000{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001001 HKEY hKey;
1002 PyObject *obKey;
1003 int index;
1004 long rc;
1005 PyObject *retStr;
Brian Curtin9c9d8782010-05-26 18:09:32 +00001006
1007 /* The Windows docs claim that the max key name length is 255
1008 * characters, plus a terminating nul character. However,
1009 * empirical testing demonstrates that it is possible to
1010 * create a 256 character key that is missing the terminating
1011 * nul. RegEnumKeyEx requires a 257 character buffer to
1012 * retrieve such a key name. */
1013 wchar_t tmpbuf[257];
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001014 DWORD len = sizeof(tmpbuf); /* includes NULL terminator */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001015
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001016 if (!PyArg_ParseTuple(args, "Oi:EnumKey", &obKey, &index))
1017 return NULL;
1018 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1019 return NULL;
Tim Peters313fcd42006-02-19 04:05:39 +00001020
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001021 Py_BEGIN_ALLOW_THREADS
1022 rc = RegEnumKeyExW(hKey, index, tmpbuf, &len, NULL, NULL, NULL, NULL);
1023 Py_END_ALLOW_THREADS
1024 if (rc != ERROR_SUCCESS)
1025 return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx");
Tim Peters313fcd42006-02-19 04:05:39 +00001026
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001027 retStr = PyUnicode_FromUnicode(tmpbuf, len);
1028 return retStr; /* can be NULL */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001029}
1030
1031static PyObject *
1032PyEnumValue(PyObject *self, PyObject *args)
1033{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001034 HKEY hKey;
1035 PyObject *obKey;
1036 int index;
1037 long rc;
1038 wchar_t *retValueBuf;
Brian Curtin7f99f092010-06-08 21:00:35 +00001039 wchar_t *tmpBuf;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001040 BYTE *retDataBuf;
Brian Curtin9c9d8782010-05-26 18:09:32 +00001041 DWORD retValueSize, bufValueSize;
1042 DWORD retDataSize, bufDataSize;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001043 DWORD typ;
1044 PyObject *obData;
1045 PyObject *retVal;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001046
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001047 if (!PyArg_ParseTuple(args, "Oi:EnumValue", &obKey, &index))
1048 return NULL;
1049 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1050 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001051
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001052 if ((rc = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, NULL, NULL,
1053 NULL,
1054 &retValueSize, &retDataSize, NULL, NULL))
1055 != ERROR_SUCCESS)
1056 return PyErr_SetFromWindowsErrWithFunction(rc,
1057 "RegQueryInfoKey");
1058 ++retValueSize; /* include null terminators */
1059 ++retDataSize;
Brian Curtin9c9d8782010-05-26 18:09:32 +00001060 bufDataSize = retDataSize;
1061 bufValueSize = retValueSize;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001062 retValueBuf = (wchar_t *)PyMem_Malloc(sizeof(wchar_t) * retValueSize);
1063 if (retValueBuf == NULL)
1064 return PyErr_NoMemory();
1065 retDataBuf = (BYTE *)PyMem_Malloc(retDataSize);
1066 if (retDataBuf == NULL) {
1067 PyMem_Free(retValueBuf);
1068 return PyErr_NoMemory();
1069 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001070
Brian Curtin9c9d8782010-05-26 18:09:32 +00001071 while (1) {
Brian Curtin9c9d8782010-05-26 18:09:32 +00001072 Py_BEGIN_ALLOW_THREADS
1073 rc = RegEnumValueW(hKey,
1074 index,
1075 retValueBuf,
1076 &retValueSize,
1077 NULL,
1078 &typ,
1079 (BYTE *)retDataBuf,
1080 &retDataSize);
1081 Py_END_ALLOW_THREADS
1082
1083 if (rc != ERROR_MORE_DATA)
1084 break;
1085
1086 bufDataSize *= 2;
Brian Curtin7f99f092010-06-08 21:00:35 +00001087 tmpBuf = (wchar_t *)PyMem_Realloc(retDataBuf, bufDataSize);
1088 if (tmpBuf == NULL) {
Brian Curtin9c9d8782010-05-26 18:09:32 +00001089 PyErr_NoMemory();
1090 retVal = NULL;
1091 goto fail;
1092 }
Brian Curtin7f99f092010-06-08 21:00:35 +00001093 retDataBuf = tmpBuf;
Brian Curtin9c9d8782010-05-26 18:09:32 +00001094 retDataSize = bufDataSize;
1095 retValueSize = bufValueSize;
1096 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001097
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001098 if (rc != ERROR_SUCCESS) {
1099 retVal = PyErr_SetFromWindowsErrWithFunction(rc,
1100 "PyRegEnumValue");
1101 goto fail;
1102 }
1103 obData = Reg2Py(retDataBuf, retDataSize, typ);
1104 if (obData == NULL) {
1105 retVal = NULL;
1106 goto fail;
1107 }
1108 retVal = Py_BuildValue("uOi", retValueBuf, obData, typ);
1109 Py_DECREF(obData);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001110 fail:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001111 PyMem_Free(retValueBuf);
1112 PyMem_Free(retDataBuf);
1113 return retVal;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001114}
1115
1116static PyObject *
Christian Heimes2380ac72008-01-09 00:17:24 +00001117PyExpandEnvironmentStrings(PyObject *self, PyObject *args)
1118{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001119 Py_UNICODE *retValue = NULL;
1120 Py_UNICODE *src;
1121 DWORD retValueSize;
1122 DWORD rc;
1123 PyObject *o;
Christian Heimes2380ac72008-01-09 00:17:24 +00001124
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001125 if (!PyArg_ParseTuple(args, "u:ExpandEnvironmentStrings", &src))
1126 return NULL;
Christian Heimes2380ac72008-01-09 00:17:24 +00001127
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001128 retValueSize = ExpandEnvironmentStringsW(src, retValue, 0);
1129 if (retValueSize == 0) {
1130 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1131 "ExpandEnvironmentStrings");
1132 }
1133 retValue = (Py_UNICODE *)PyMem_Malloc(retValueSize * sizeof(Py_UNICODE));
1134 if (retValue == NULL) {
1135 return PyErr_NoMemory();
1136 }
Christian Heimes2380ac72008-01-09 00:17:24 +00001137
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001138 rc = ExpandEnvironmentStringsW(src, retValue, retValueSize);
1139 if (rc == 0) {
1140 PyMem_Free(retValue);
1141 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1142 "ExpandEnvironmentStrings");
1143 }
1144 o = PyUnicode_FromUnicode(retValue, wcslen(retValue));
1145 PyMem_Free(retValue);
1146 return o;
Christian Heimes2380ac72008-01-09 00:17:24 +00001147}
1148
1149static PyObject *
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001150PyFlushKey(PyObject *self, PyObject *args)
1151{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001152 HKEY hKey;
1153 PyObject *obKey;
1154 long rc;
1155 if (!PyArg_ParseTuple(args, "O:FlushKey", &obKey))
1156 return NULL;
1157 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1158 return NULL;
1159 Py_BEGIN_ALLOW_THREADS
1160 rc = RegFlushKey(hKey);
1161 Py_END_ALLOW_THREADS
1162 if (rc != ERROR_SUCCESS)
1163 return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey");
1164 Py_INCREF(Py_None);
1165 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001166}
1167static PyObject *
1168PyLoadKey(PyObject *self, PyObject *args)
1169{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001170 HKEY hKey;
1171 PyObject *obKey;
1172 wchar_t *subKey;
1173 wchar_t *fileName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001174
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001175 long rc;
1176 if (!PyArg_ParseTuple(args, "Ouu:LoadKey", &obKey, &subKey, &fileName))
1177 return NULL;
1178 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1179 return NULL;
1180 Py_BEGIN_ALLOW_THREADS
1181 rc = RegLoadKeyW(hKey, subKey, fileName );
1182 Py_END_ALLOW_THREADS
1183 if (rc != ERROR_SUCCESS)
1184 return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey");
1185 Py_INCREF(Py_None);
1186 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001187}
1188
1189static PyObject *
1190PyOpenKey(PyObject *self, PyObject *args)
1191{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001192 HKEY hKey;
1193 PyObject *obKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001194
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001195 wchar_t *subKey;
1196 int res = 0;
1197 HKEY retKey;
1198 long rc;
1199 REGSAM sam = KEY_READ;
1200 if (!PyArg_ParseTuple(args, "OZ|ii:OpenKey", &obKey, &subKey,
1201 &res, &sam))
1202 return NULL;
1203 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1204 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001205
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001206 Py_BEGIN_ALLOW_THREADS
1207 rc = RegOpenKeyExW(hKey, subKey, res, sam, &retKey);
1208 Py_END_ALLOW_THREADS
1209 if (rc != ERROR_SUCCESS)
1210 return PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx");
1211 return PyHKEY_FromHKEY(retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001212}
1213
1214
1215static PyObject *
1216PyQueryInfoKey(PyObject *self, PyObject *args)
1217{
1218 HKEY hKey;
1219 PyObject *obKey;
1220 long rc;
1221 DWORD nSubKeys, nValues;
1222 FILETIME ft;
1223 LARGE_INTEGER li;
1224 PyObject *l;
1225 PyObject *ret;
1226 if (!PyArg_ParseTuple(args, "O:QueryInfoKey", &obKey))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001227 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001228 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001229 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001230 if ((rc = RegQueryInfoKey(hKey, NULL, NULL, 0, &nSubKeys, NULL, NULL,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001231 &nValues, NULL, NULL, NULL, &ft))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001232 != ERROR_SUCCESS)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001233 return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey");
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001234 li.LowPart = ft.dwLowDateTime;
1235 li.HighPart = ft.dwHighDateTime;
1236 l = PyLong_FromLongLong(li.QuadPart);
1237 if (l == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001238 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001239 ret = Py_BuildValue("iiO", nSubKeys, nValues, l);
1240 Py_DECREF(l);
1241 return ret;
1242}
1243
1244static PyObject *
1245PyQueryValue(PyObject *self, PyObject *args)
1246{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001247 HKEY hKey;
1248 PyObject *obKey;
1249 wchar_t *subKey;
1250 long rc;
1251 PyObject *retStr;
1252 wchar_t *retBuf;
Brian Curtin9c9d8782010-05-26 18:09:32 +00001253 DWORD bufSize = 0;
1254 DWORD retSize = 0;
1255 wchar_t *tmp;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001256
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001257 if (!PyArg_ParseTuple(args, "OZ:QueryValue", &obKey, &subKey))
1258 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001259
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001260 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1261 return NULL;
Brian Curtin9c9d8782010-05-26 18:09:32 +00001262
1263 rc = RegQueryValueW(hKey, subKey, NULL, &retSize);
1264 if (rc == ERROR_MORE_DATA)
1265 retSize = 256;
1266 else if (rc != ERROR_SUCCESS)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001267 return PyErr_SetFromWindowsErrWithFunction(rc,
1268 "RegQueryValue");
Brian Curtin9c9d8782010-05-26 18:09:32 +00001269
1270 bufSize = retSize;
1271 retBuf = (wchar_t *) PyMem_Malloc(bufSize);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001272 if (retBuf == NULL)
1273 return PyErr_NoMemory();
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001274
Brian Curtin9c9d8782010-05-26 18:09:32 +00001275 while (1) {
1276 retSize = bufSize;
1277 rc = RegQueryValueW(hKey, subKey, retBuf, &retSize);
1278 if (rc != ERROR_MORE_DATA)
1279 break;
1280
1281 bufSize *= 2;
1282 tmp = (wchar_t *) PyMem_Realloc(retBuf, bufSize);
1283 if (tmp == NULL) {
1284 PyMem_Free(retBuf);
1285 return PyErr_NoMemory();
1286 }
1287 retBuf = tmp;
1288 }
1289
1290 if (rc != ERROR_SUCCESS) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001291 PyMem_Free(retBuf);
1292 return PyErr_SetFromWindowsErrWithFunction(rc,
1293 "RegQueryValue");
1294 }
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001295
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001296 retStr = PyUnicode_FromUnicode(retBuf, wcslen(retBuf));
1297 PyMem_Free(retBuf);
1298 return retStr;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001299}
1300
1301static PyObject *
1302PyQueryValueEx(PyObject *self, PyObject *args)
1303{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001304 HKEY hKey;
1305 PyObject *obKey;
1306 wchar_t *valueName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001307
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001308 long rc;
Brian Curtin9c9d8782010-05-26 18:09:32 +00001309 BYTE *retBuf, *tmp;
1310 DWORD bufSize = 0, retSize;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001311 DWORD typ;
1312 PyObject *obData;
1313 PyObject *result;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001314
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001315 if (!PyArg_ParseTuple(args, "OZ:QueryValueEx", &obKey, &valueName))
1316 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001317
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001318 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1319 return NULL;
Brian Curtin9c9d8782010-05-26 18:09:32 +00001320
1321 rc = RegQueryValueExW(hKey, valueName, NULL, NULL, NULL, &bufSize);
1322 if (rc == ERROR_MORE_DATA)
1323 bufSize = 256;
1324 else if (rc != ERROR_SUCCESS)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001325 return PyErr_SetFromWindowsErrWithFunction(rc,
1326 "RegQueryValueEx");
1327 retBuf = (BYTE *)PyMem_Malloc(bufSize);
1328 if (retBuf == NULL)
1329 return PyErr_NoMemory();
Brian Curtin9c9d8782010-05-26 18:09:32 +00001330
1331 while (1) {
1332 retSize = bufSize;
1333 rc = RegQueryValueExW(hKey, valueName, NULL, &typ,
1334 (BYTE *)retBuf, &retSize);
1335 if (rc != ERROR_MORE_DATA)
1336 break;
1337
1338 bufSize *= 2;
1339 tmp = (char *) PyMem_Realloc(retBuf, bufSize);
1340 if (tmp == NULL) {
1341 PyMem_Free(retBuf);
1342 return PyErr_NoMemory();
1343 }
1344 retBuf = tmp;
1345 }
1346
1347 if (rc != ERROR_SUCCESS) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001348 PyMem_Free(retBuf);
1349 return PyErr_SetFromWindowsErrWithFunction(rc,
1350 "RegQueryValueEx");
1351 }
1352 obData = Reg2Py(retBuf, bufSize, typ);
1353 PyMem_Free(retBuf);
1354 if (obData == NULL)
1355 return NULL;
1356 result = Py_BuildValue("Oi", obData, typ);
1357 Py_DECREF(obData);
1358 return result;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001359}
1360
1361
1362static PyObject *
1363PySaveKey(PyObject *self, PyObject *args)
1364{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001365 HKEY hKey;
1366 PyObject *obKey;
1367 wchar_t *fileName;
1368 LPSECURITY_ATTRIBUTES pSA = NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001369
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001370 long rc;
1371 if (!PyArg_ParseTuple(args, "Ou:SaveKey", &obKey, &fileName))
1372 return NULL;
1373 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1374 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001375/* One day we may get security into the core?
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001376 if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE))
1377 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001378*/
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001379 Py_BEGIN_ALLOW_THREADS
1380 rc = RegSaveKeyW(hKey, fileName, pSA );
1381 Py_END_ALLOW_THREADS
1382 if (rc != ERROR_SUCCESS)
1383 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey");
1384 Py_INCREF(Py_None);
1385 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001386}
1387
1388static PyObject *
1389PySetValue(PyObject *self, PyObject *args)
1390{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001391 HKEY hKey;
1392 PyObject *obKey;
1393 wchar_t *subKey;
1394 wchar_t *str;
1395 DWORD typ;
1396 DWORD len;
1397 long rc;
1398 if (!PyArg_ParseTuple(args, "OZiu#:SetValue",
1399 &obKey,
1400 &subKey,
1401 &typ,
1402 &str,
1403 &len))
1404 return NULL;
1405 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1406 return NULL;
1407 if (typ != REG_SZ) {
1408 PyErr_SetString(PyExc_TypeError,
1409 "Type must be winreg.REG_SZ");
1410 return NULL;
1411 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001412
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001413 Py_BEGIN_ALLOW_THREADS
1414 rc = RegSetValueW(hKey, subKey, REG_SZ, str, len+1);
1415 Py_END_ALLOW_THREADS
1416 if (rc != ERROR_SUCCESS)
1417 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue");
1418 Py_INCREF(Py_None);
1419 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001420}
1421
1422static PyObject *
1423PySetValueEx(PyObject *self, PyObject *args)
1424{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001425 HKEY hKey;
1426 PyObject *obKey;
1427 Py_UNICODE *valueName;
1428 PyObject *obRes;
1429 PyObject *value;
1430 BYTE *data;
1431 DWORD len;
1432 DWORD typ;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001433
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001434 LONG rc;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001435
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001436 if (!PyArg_ParseTuple(args, "OZOiO:SetValueEx",
1437 &obKey,
1438 &valueName,
1439 &obRes,
1440 &typ,
1441 &value))
1442 return NULL;
1443 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1444 return NULL;
1445 if (!Py2Reg(value, typ, &data, &len))
1446 {
1447 if (!PyErr_Occurred())
1448 PyErr_SetString(PyExc_ValueError,
1449 "Could not convert the data to the specified type.");
1450 return NULL;
1451 }
1452 Py_BEGIN_ALLOW_THREADS
1453 rc = RegSetValueExW(hKey, valueName, 0, typ, data, len);
1454 Py_END_ALLOW_THREADS
1455 PyMem_DEL(data);
1456 if (rc != ERROR_SUCCESS)
1457 return PyErr_SetFromWindowsErrWithFunction(rc,
1458 "RegSetValueEx");
1459 Py_INCREF(Py_None);
1460 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001461}
1462
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001463static PyObject *
1464PyDisableReflectionKey(PyObject *self, PyObject *args)
1465{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001466 HKEY hKey;
1467 PyObject *obKey;
1468 HMODULE hMod;
1469 typedef LONG (WINAPI *RDRKFunc)(HKEY);
1470 RDRKFunc pfn = NULL;
1471 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001472
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001473 if (!PyArg_ParseTuple(args, "O:DisableReflectionKey", &obKey))
1474 return NULL;
1475 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1476 return NULL;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001477
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001478 // Only available on 64bit platforms, so we must load it
1479 // dynamically.
1480 hMod = GetModuleHandle("advapi32.dll");
1481 if (hMod)
1482 pfn = (RDRKFunc)GetProcAddress(hMod,
1483 "RegDisableReflectionKey");
1484 if (!pfn) {
1485 PyErr_SetString(PyExc_NotImplementedError,
1486 "not implemented on this platform");
1487 return NULL;
1488 }
1489 Py_BEGIN_ALLOW_THREADS
1490 rc = (*pfn)(hKey);
1491 Py_END_ALLOW_THREADS
1492 if (rc != ERROR_SUCCESS)
1493 return PyErr_SetFromWindowsErrWithFunction(rc,
1494 "RegDisableReflectionKey");
1495 Py_INCREF(Py_None);
1496 return Py_None;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001497}
1498
1499static PyObject *
1500PyEnableReflectionKey(PyObject *self, PyObject *args)
1501{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001502 HKEY hKey;
1503 PyObject *obKey;
1504 HMODULE hMod;
1505 typedef LONG (WINAPI *RERKFunc)(HKEY);
1506 RERKFunc pfn = NULL;
1507 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001508
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001509 if (!PyArg_ParseTuple(args, "O:EnableReflectionKey", &obKey))
1510 return NULL;
1511 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1512 return NULL;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001513
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001514 // Only available on 64bit platforms, so we must load it
1515 // dynamically.
1516 hMod = GetModuleHandle("advapi32.dll");
1517 if (hMod)
1518 pfn = (RERKFunc)GetProcAddress(hMod,
1519 "RegEnableReflectionKey");
1520 if (!pfn) {
1521 PyErr_SetString(PyExc_NotImplementedError,
1522 "not implemented on this platform");
1523 return NULL;
1524 }
1525 Py_BEGIN_ALLOW_THREADS
1526 rc = (*pfn)(hKey);
1527 Py_END_ALLOW_THREADS
1528 if (rc != ERROR_SUCCESS)
1529 return PyErr_SetFromWindowsErrWithFunction(rc,
1530 "RegEnableReflectionKey");
1531 Py_INCREF(Py_None);
1532 return Py_None;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001533}
1534
1535static PyObject *
1536PyQueryReflectionKey(PyObject *self, PyObject *args)
1537{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001538 HKEY hKey;
1539 PyObject *obKey;
1540 HMODULE hMod;
1541 typedef LONG (WINAPI *RQRKFunc)(HKEY, BOOL *);
1542 RQRKFunc pfn = NULL;
1543 BOOL result;
1544 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001545
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001546 if (!PyArg_ParseTuple(args, "O:QueryReflectionKey", &obKey))
1547 return NULL;
1548 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1549 return NULL;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001550
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001551 // Only available on 64bit platforms, so we must load it
1552 // dynamically.
1553 hMod = GetModuleHandle("advapi32.dll");
1554 if (hMod)
1555 pfn = (RQRKFunc)GetProcAddress(hMod,
1556 "RegQueryReflectionKey");
1557 if (!pfn) {
1558 PyErr_SetString(PyExc_NotImplementedError,
1559 "not implemented on this platform");
1560 return NULL;
1561 }
1562 Py_BEGIN_ALLOW_THREADS
1563 rc = (*pfn)(hKey, &result);
1564 Py_END_ALLOW_THREADS
1565 if (rc != ERROR_SUCCESS)
1566 return PyErr_SetFromWindowsErrWithFunction(rc,
1567 "RegQueryReflectionKey");
1568 return PyBool_FromLong(rc);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001569}
1570
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001571static struct PyMethodDef winreg_methods[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001572 {"CloseKey", PyCloseKey, METH_VARARGS, CloseKey_doc},
1573 {"ConnectRegistry", PyConnectRegistry, METH_VARARGS, ConnectRegistry_doc},
1574 {"CreateKey", PyCreateKey, METH_VARARGS, CreateKey_doc},
1575 {"DeleteKey", PyDeleteKey, METH_VARARGS, DeleteKey_doc},
1576 {"DeleteValue", PyDeleteValue, METH_VARARGS, DeleteValue_doc},
1577 {"DisableReflectionKey", PyDisableReflectionKey, METH_VARARGS, DisableReflectionKey_doc},
1578 {"EnableReflectionKey", PyEnableReflectionKey, METH_VARARGS, EnableReflectionKey_doc},
1579 {"EnumKey", PyEnumKey, METH_VARARGS, EnumKey_doc},
1580 {"EnumValue", PyEnumValue, METH_VARARGS, EnumValue_doc},
1581 {"ExpandEnvironmentStrings", PyExpandEnvironmentStrings, METH_VARARGS,
1582 ExpandEnvironmentStrings_doc },
1583 {"FlushKey", PyFlushKey, METH_VARARGS, FlushKey_doc},
1584 {"LoadKey", PyLoadKey, METH_VARARGS, LoadKey_doc},
1585 {"OpenKey", PyOpenKey, METH_VARARGS, OpenKey_doc},
1586 {"OpenKeyEx", PyOpenKey, METH_VARARGS, OpenKeyEx_doc},
1587 {"QueryValue", PyQueryValue, METH_VARARGS, QueryValue_doc},
1588 {"QueryValueEx", PyQueryValueEx, METH_VARARGS, QueryValueEx_doc},
1589 {"QueryInfoKey", PyQueryInfoKey, METH_VARARGS, QueryInfoKey_doc},
1590 {"QueryReflectionKey",PyQueryReflectionKey,METH_VARARGS, QueryReflectionKey_doc},
1591 {"SaveKey", PySaveKey, METH_VARARGS, SaveKey_doc},
1592 {"SetValue", PySetValue, METH_VARARGS, SetValue_doc},
1593 {"SetValueEx", PySetValueEx, METH_VARARGS, SetValueEx_doc},
1594 NULL,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001595};
1596
1597static void
1598insint(PyObject * d, char * name, long value)
1599{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001600 PyObject *v = PyLong_FromLong(value);
1601 if (!v || PyDict_SetItemString(d, name, v))
1602 PyErr_Clear();
1603 Py_XDECREF(v);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001604}
1605
1606#define ADD_INT(val) insint(d, #val, val)
1607
1608static void
1609inskey(PyObject * d, char * name, HKEY key)
1610{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001611 PyObject *v = PyLong_FromVoidPtr(key);
1612 if (!v || PyDict_SetItemString(d, name, v))
1613 PyErr_Clear();
1614 Py_XDECREF(v);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001615}
1616
1617#define ADD_KEY(val) inskey(d, #val, val)
1618
Martin v. Löwis1a214512008-06-11 05:26:20 +00001619
1620static struct PyModuleDef winregmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001621 PyModuleDef_HEAD_INIT,
1622 "winreg",
1623 module_doc,
1624 -1,
1625 winreg_methods,
1626 NULL,
1627 NULL,
1628 NULL,
1629 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001630};
1631
1632PyMODINIT_FUNC PyInit_winreg(void)
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001633{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001634 PyObject *m, *d;
1635 m = PyModule_Create(&winregmodule);
1636 if (m == NULL)
1637 return NULL;
1638 d = PyModule_GetDict(m);
1639 PyHKEY_Type.tp_doc = PyHKEY_doc;
1640 if (PyType_Ready(&PyHKEY_Type) < 0)
1641 return NULL;
1642 Py_INCREF(&PyHKEY_Type);
1643 if (PyDict_SetItemString(d, "HKEYType",
1644 (PyObject *)&PyHKEY_Type) != 0)
1645 return NULL;
1646 Py_INCREF(PyExc_WindowsError);
1647 if (PyDict_SetItemString(d, "error",
1648 PyExc_WindowsError) != 0)
1649 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001650
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001651 /* Add the relevant constants */
1652 ADD_KEY(HKEY_CLASSES_ROOT);
1653 ADD_KEY(HKEY_CURRENT_USER);
1654 ADD_KEY(HKEY_LOCAL_MACHINE);
1655 ADD_KEY(HKEY_USERS);
1656 ADD_KEY(HKEY_PERFORMANCE_DATA);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001657#ifdef HKEY_CURRENT_CONFIG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001658 ADD_KEY(HKEY_CURRENT_CONFIG);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001659#endif
1660#ifdef HKEY_DYN_DATA
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001661 ADD_KEY(HKEY_DYN_DATA);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001662#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001663 ADD_INT(KEY_QUERY_VALUE);
1664 ADD_INT(KEY_SET_VALUE);
1665 ADD_INT(KEY_CREATE_SUB_KEY);
1666 ADD_INT(KEY_ENUMERATE_SUB_KEYS);
1667 ADD_INT(KEY_NOTIFY);
1668 ADD_INT(KEY_CREATE_LINK);
1669 ADD_INT(KEY_READ);
1670 ADD_INT(KEY_WRITE);
1671 ADD_INT(KEY_EXECUTE);
1672 ADD_INT(KEY_ALL_ACCESS);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001673#ifdef KEY_WOW64_64KEY
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001674 ADD_INT(KEY_WOW64_64KEY);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001675#endif
1676#ifdef KEY_WOW64_32KEY
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001677 ADD_INT(KEY_WOW64_32KEY);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001678#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001679 ADD_INT(REG_OPTION_RESERVED);
1680 ADD_INT(REG_OPTION_NON_VOLATILE);
1681 ADD_INT(REG_OPTION_VOLATILE);
1682 ADD_INT(REG_OPTION_CREATE_LINK);
1683 ADD_INT(REG_OPTION_BACKUP_RESTORE);
1684 ADD_INT(REG_OPTION_OPEN_LINK);
1685 ADD_INT(REG_LEGAL_OPTION);
1686 ADD_INT(REG_CREATED_NEW_KEY);
1687 ADD_INT(REG_OPENED_EXISTING_KEY);
1688 ADD_INT(REG_WHOLE_HIVE_VOLATILE);
1689 ADD_INT(REG_REFRESH_HIVE);
1690 ADD_INT(REG_NO_LAZY_FLUSH);
1691 ADD_INT(REG_NOTIFY_CHANGE_NAME);
1692 ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES);
1693 ADD_INT(REG_NOTIFY_CHANGE_LAST_SET);
1694 ADD_INT(REG_NOTIFY_CHANGE_SECURITY);
1695 ADD_INT(REG_LEGAL_CHANGE_FILTER);
1696 ADD_INT(REG_NONE);
1697 ADD_INT(REG_SZ);
1698 ADD_INT(REG_EXPAND_SZ);
1699 ADD_INT(REG_BINARY);
1700 ADD_INT(REG_DWORD);
1701 ADD_INT(REG_DWORD_LITTLE_ENDIAN);
1702 ADD_INT(REG_DWORD_BIG_ENDIAN);
1703 ADD_INT(REG_LINK);
1704 ADD_INT(REG_MULTI_SZ);
1705 ADD_INT(REG_RESOURCE_LIST);
1706 ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
1707 ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
1708 return m;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001709}
1710
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001711