blob: 571209fdff5b42aaac8efa8a9009906120932d9f [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
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
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"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000329"__cmp__ - 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 {
356 PyObject_VAR_HEAD
357 HKEY hkey;
358} 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{
367 PyErr_SetString(PyExc_TypeError, failMsg);
368 return NULL;
369}
370static PyObject *
371PyHKEY_binaryFailureFunc(PyObject *ob1, PyObject *ob2)
372{
373 PyErr_SetString(PyExc_TypeError, failMsg);
374 return NULL;
375}
376static PyObject *
377PyHKEY_ternaryFailureFunc(PyObject *ob1, PyObject *ob2, PyObject *ob3)
378{
379 PyErr_SetString(PyExc_TypeError, failMsg);
380 return NULL;
381}
382
383static void
384PyHKEY_deallocFunc(PyObject *ob)
385{
386 /* 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);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000393 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{
399 return ((PyHKEYObject *)ob)->hkey != 0;
400}
401
402static PyObject *
403PyHKEY_intFunc(PyObject *ob)
404{
405 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
406 return PyLong_FromVoidPtr(pyhkey->hkey);
407}
408
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000409static PyObject *
410PyHKEY_strFunc(PyObject *ob)
411{
412 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000413 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{
419 PyHKEYObject *pyhkey1 = (PyHKEYObject *)ob1;
420 PyHKEYObject *pyhkey2 = (PyHKEYObject *)ob2;
421 return pyhkey1 == pyhkey2 ? 0 :
422 (pyhkey1 < pyhkey2 ? -1 : 1);
423}
424
425static long
426PyHKEY_hashFunc(PyObject *ob)
427{
428 /* Just use the address.
429 XXX - should we use the handle value?
430 */
Fred Drake13634cf2000-06-29 19:17:04 +0000431 return _Py_HashPointer(ob);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000432}
433
434
435static PyNumberMethods PyHKEY_NumberMethods =
436{
437 PyHKEY_binaryFailureFunc, /* nb_add */
438 PyHKEY_binaryFailureFunc, /* nb_subtract */
439 PyHKEY_binaryFailureFunc, /* nb_multiply */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000440 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 */
Jack Diederich4dafcc42006-11-28 19:15:13 +0000446 PyHKEY_boolFunc, /* nb_bool */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000447 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 */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000453 PyHKEY_intFunc, /* nb_int */
454 PyHKEY_unaryFailureFunc, /* nb_long */
455 PyHKEY_unaryFailureFunc, /* nb_float */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000456};
457
458
459/* fwd declare __getattr__ */
Tim Petersc3d12ac2005-12-24 06:03:06 +0000460static PyObject *PyHKEY_getattr(PyObject *self, const char *name);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000461
462/* The type itself */
463PyTypeObject PyHKEY_Type =
464{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000465 PyVarObject_HEAD_INIT(0, 0) /* fill in type at module init */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000466 "PyHKEY",
467 sizeof(PyHKEYObject),
468 0,
469 PyHKEY_deallocFunc, /* tp_dealloc */
Guido van Rossum346f1a82007-08-07 19:58:47 +0000470 0, /* tp_print */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000471 PyHKEY_getattr, /* tp_getattr */
472 0, /* tp_setattr */
473 PyHKEY_compareFunc, /* tp_compare */
474 0, /* tp_repr */
475 &PyHKEY_NumberMethods, /* tp_as_number */
476 0, /* tp_as_sequence */
477 0, /* tp_as_mapping */
478 PyHKEY_hashFunc, /* tp_hash */
479 0, /* tp_call */
480 PyHKEY_strFunc, /* tp_str */
481 0, /* tp_getattro */
482 0, /* tp_setattro */
483 0, /* tp_as_buffer */
484 0, /* tp_flags */
485 PyHKEY_doc, /* tp_doc */
486};
487
488#define OFF(e) offsetof(PyHKEYObject, e)
489
Neal Norwitz8dfc4a92007-08-11 06:39:53 +0000490static PyMemberDef PyHKEY_memberlist[] = {
491 {"handle", T_INT, OFF(hkey), READONLY},
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000492 {NULL} /* Sentinel */
493};
494
495/************************************************************************
496
497 The PyHKEY object methods
498
499************************************************************************/
500static PyObject *
501PyHKEY_CloseMethod(PyObject *self, PyObject *args)
502{
503 if (!PyArg_ParseTuple(args, ":Close"))
504 return NULL;
505 if (!PyHKEY_Close(self))
506 return NULL;
507 Py_INCREF(Py_None);
508 return Py_None;
509}
510
511static PyObject *
512PyHKEY_DetachMethod(PyObject *self, PyObject *args)
513{
514 void* ret;
515 PyHKEYObject *pThis = (PyHKEYObject *)self;
516 if (!PyArg_ParseTuple(args, ":Detach"))
517 return NULL;
518 ret = (void*)pThis->hkey;
519 pThis->hkey = 0;
520 return PyLong_FromVoidPtr(ret);
521}
522
Christian Heimes2380ac72008-01-09 00:17:24 +0000523static PyObject *
524PyHKEY_Enter(PyObject *self)
525{
526 Py_XINCREF(self);
527 return self;
528}
529
530static PyObject *
531PyHKEY_Exit(PyObject *self, PyObject *args)
532{
533 if (!PyHKEY_Close(self))
534 return NULL;
535 Py_RETURN_NONE;
536}
537
538
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000539static struct PyMethodDef PyHKEY_methods[] = {
Neal Norwitz031829d2002-03-31 14:37:44 +0000540 {"Close", PyHKEY_CloseMethod, METH_VARARGS, PyHKEY_Close_doc},
541 {"Detach", PyHKEY_DetachMethod, METH_VARARGS, PyHKEY_Detach_doc},
Christian Heimes2380ac72008-01-09 00:17:24 +0000542 {"__enter__", (PyCFunction)PyHKEY_Enter, METH_NOARGS, NULL},
543 {"__exit__", PyHKEY_Exit, METH_VARARGS, NULL},
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000544 {NULL}
545};
546
547/*static*/ PyObject *
Tim Petersc3d12ac2005-12-24 06:03:06 +0000548PyHKEY_getattr(PyObject *self, const char *name)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000549{
550 PyObject *res;
551
552 res = Py_FindMethod(PyHKEY_methods, self, name);
553 if (res != NULL)
554 return res;
555 PyErr_Clear();
556 if (strcmp(name, "handle") == 0)
557 return PyLong_FromVoidPtr(((PyHKEYObject *)self)->hkey);
Neal Norwitz8dfc4a92007-08-11 06:39:53 +0000558 PyErr_Format(PyExc_AttributeError,
559 "'%.50s' object has no attribute '%.400s'",
Christian Heimes90aa7642007-12-19 02:45:37 +0000560 Py_TYPE(self)->tp_name, name);
Neal Norwitz8dfc4a92007-08-11 06:39:53 +0000561 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000562}
563
564/************************************************************************
565 The public PyHKEY API (well, not public yet :-)
566************************************************************************/
567PyObject *
568PyHKEY_New(HKEY hInit)
569{
570 PyHKEYObject *key = PyObject_NEW(PyHKEYObject, &PyHKEY_Type);
571 if (key)
572 key->hkey = hInit;
573 return (PyObject *)key;
574}
575
576BOOL
577PyHKEY_Close(PyObject *ob_handle)
578{
579 LONG rc;
580 PyHKEYObject *key;
581
582 if (!PyHKEY_Check(ob_handle)) {
583 PyErr_SetString(PyExc_TypeError, "bad operand type");
584 return FALSE;
585 }
586 key = (PyHKEYObject *)ob_handle;
587 rc = key->hkey ? RegCloseKey((HKEY)key->hkey) : ERROR_SUCCESS;
588 key->hkey = 0;
589 if (rc != ERROR_SUCCESS)
590 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
591 return rc == ERROR_SUCCESS;
592}
593
594BOOL
595PyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK)
596{
597 if (ob == Py_None) {
598 if (!bNoneOK) {
599 PyErr_SetString(
600 PyExc_TypeError,
601 "None is not a valid HKEY in this context");
602 return FALSE;
603 }
604 *pHANDLE = (HKEY)0;
605 }
606 else if (PyHKEY_Check(ob)) {
607 PyHKEYObject *pH = (PyHKEYObject *)ob;
608 *pHANDLE = pH->hkey;
609 }
Neal Norwitz1fe5f382007-08-31 04:32:55 +0000610 else if (PyLong_Check(ob)) {
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000611 /* We also support integers */
612 PyErr_Clear();
613 *pHANDLE = (HKEY)PyLong_AsVoidPtr(ob);
614 if (PyErr_Occurred())
615 return FALSE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000616 }
617 else {
618 PyErr_SetString(
619 PyExc_TypeError,
620 "The object is not a PyHKEY object");
621 return FALSE;
622 }
623 return TRUE;
624}
625
626PyObject *
627PyHKEY_FromHKEY(HKEY h)
628{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000629 PyHKEYObject *op;
630
Guido van Rossume3a8e7e2002-08-19 19:26:42 +0000631 /* Inline PyObject_New */
Guido van Rossumb18618d2000-05-03 23:44:39 +0000632 op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject));
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000633 if (op == NULL)
634 return PyErr_NoMemory();
Guido van Rossumb18618d2000-05-03 23:44:39 +0000635 PyObject_INIT(op, &PyHKEY_Type);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000636 op->hkey = h;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000637 return (PyObject *)op;
638}
639
640
641/************************************************************************
642 The module methods
643************************************************************************/
644BOOL
645PyWinObject_CloseHKEY(PyObject *obHandle)
646{
647 BOOL ok;
648 if (PyHKEY_Check(obHandle)) {
649 ok = PyHKEY_Close(obHandle);
650 }
Fred Drake25e17262000-06-30 17:48:51 +0000651#if SIZEOF_LONG >= SIZEOF_HKEY
Christian Heimes217cfd12007-12-02 14:31:20 +0000652 else if (PyLong_Check(obHandle)) {
653 long rc = RegCloseKey((HKEY)PyLong_AsLong(obHandle));
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000654 ok = (rc == ERROR_SUCCESS);
655 if (!ok)
656 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
657 }
Fred Drake25e17262000-06-30 17:48:51 +0000658#else
659 else if (PyLong_Check(obHandle)) {
660 long rc = RegCloseKey((HKEY)PyLong_AsVoidPtr(obHandle));
661 ok = (rc == ERROR_SUCCESS);
662 if (!ok)
663 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
664 }
665#endif
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000666 else {
667 PyErr_SetString(
668 PyExc_TypeError,
669 "A handle must be a HKEY object or an integer");
670 return FALSE;
671 }
672 return ok;
673}
674
675
676/*
677 Private Helper functions for the registry interfaces
678
679** Note that fixupMultiSZ and countString have both had changes
680** made to support "incorrect strings". The registry specification
681** calls for strings to be terminated with 2 null bytes. It seems
Thomas Wouters7e474022000-07-16 12:04:32 +0000682** some commercial packages install strings which dont conform,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000683** causing this code to fail - however, "regedit" etc still work
684** with these strings (ie only we dont!).
685*/
686static void
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000687fixupMultiSZ(wchar_t **str, wchar_t *data, int len)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000688{
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000689 wchar_t *P;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000690 int i;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000691 wchar_t *Q;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000692
693 Q = data + len;
694 for (P = data, i = 0; P < Q && *P != '\0'; P++, i++) {
695 str[i] = P;
696 for(; *P != '\0'; P++)
697 ;
698 }
699}
700
701static int
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000702countStrings(wchar_t *data, int len)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000703{
704 int strings;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000705 wchar_t *P;
706 wchar_t *Q = data + len;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000707
708 for (P = data, strings = 0; P < Q && *P != '\0'; P++, strings++)
709 for (; P < Q && *P != '\0'; P++)
710 ;
711 return strings;
712}
713
714/* Convert PyObject into Registry data.
715 Allocates space as needed. */
716static BOOL
717Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
718{
Christian Heimescc47b052008-03-25 14:56:36 +0000719 Py_ssize_t i,j;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000720 switch (typ) {
721 case REG_DWORD:
Guido van Rossum7eaf8222007-06-18 17:58:50 +0000722 if (value != Py_None && !PyLong_Check(value))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000723 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000724 *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000725 if (*retDataBuf==NULL){
726 PyErr_NoMemory();
727 return FALSE;
728 }
729 *retDataSize = sizeof(DWORD);
730 if (value == Py_None) {
731 DWORD zero = 0;
732 memcpy(*retDataBuf, &zero, sizeof(DWORD));
733 }
Guido van Rossum7eaf8222007-06-18 17:58:50 +0000734 else {
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000735 DWORD d = PyLong_AsLong(value);
Guido van Rossum7eaf8222007-06-18 17:58:50 +0000736 memcpy(*retDataBuf, &d, sizeof(DWORD));
737 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000738 break;
739 case REG_SZ:
740 case REG_EXPAND_SZ:
741 {
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000742 if (value == Py_None)
743 *retDataSize = 1;
744 else {
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000745 if (!PyUnicode_Check(value))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000746 return FALSE;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000747
748 *retDataSize = 2 + PyUnicode_GET_DATA_SIZE(value);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000749 }
750 *retDataBuf = (BYTE *)PyMem_NEW(DWORD, *retDataSize);
751 if (*retDataBuf==NULL){
752 PyErr_NoMemory();
753 return FALSE;
754 }
755 if (value == Py_None)
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000756 wcscpy((wchar_t *)*retDataBuf, L"");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000757 else
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000758 wcscpy((wchar_t *)*retDataBuf,
759 PyUnicode_AS_UNICODE(value));
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000760 break;
761 }
762 case REG_MULTI_SZ:
763 {
764 DWORD size = 0;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000765 wchar_t *P;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000766
767 if (value == Py_None)
768 i = 0;
769 else {
770 if (!PyList_Check(value))
771 return FALSE;
772 i = PyList_Size(value);
773 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000774 for (j = 0; j < i; j++)
775 {
776 PyObject *t;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000777 t = PyList_GET_ITEM(value, j);
778 if (!PyUnicode_Check(t))
779 return FALSE;
780 size += 2 + PyUnicode_GET_DATA_SIZE(t);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000781 }
782
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000783 *retDataSize = size + 2;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000784 *retDataBuf = (BYTE *)PyMem_NEW(char,
785 *retDataSize);
786 if (*retDataBuf==NULL){
787 PyErr_NoMemory();
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000788 return FALSE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000789 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000790 P = (wchar_t *)*retDataBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000791
792 for (j = 0; j < i; j++)
793 {
794 PyObject *t;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000795 t = PyList_GET_ITEM(value, j);
796 wcscpy(P, PyUnicode_AS_UNICODE(t));
797 P += 1 + wcslen(
798 PyUnicode_AS_UNICODE(t));
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000799 }
800 /* And doubly-terminate the list... */
801 *P = '\0';
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000802 break;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000803 }
804 case REG_BINARY:
805 /* ALSO handle ALL unknown data types here. Even if we can't
806 support it natively, we should handle the bits. */
807 default:
808 if (value == Py_None)
809 *retDataSize = 0;
810 else {
Thomas Heller39763a12007-09-24 14:43:56 +0000811 Py_buffer view;
Neal Norwitz1385b892007-08-26 23:07:13 +0000812
813 if (!PyObject_CheckBuffer(value)) {
Tim Peters313fcd42006-02-19 04:05:39 +0000814 PyErr_Format(PyExc_TypeError,
Mark Hammond4e80bb52000-07-28 03:44:41 +0000815 "Objects of type '%s' can not "
Tim Peters313fcd42006-02-19 04:05:39 +0000816 "be used as binary registry values",
Mark Hammond4e80bb52000-07-28 03:44:41 +0000817 value->ob_type->tp_name);
818 return FALSE;
819 }
Neal Norwitz1385b892007-08-26 23:07:13 +0000820
821 if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0)
822 return FALSE;
823
824 *retDataBuf = (BYTE *)PyMem_NEW(char, view.len);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000825 if (*retDataBuf==NULL){
Neal Norwitz1385b892007-08-26 23:07:13 +0000826 PyObject_ReleaseBuffer(value, &view);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000827 PyErr_NoMemory();
828 return FALSE;
829 }
Neal Norwitz1385b892007-08-26 23:07:13 +0000830 *retDataSize = view.len;
831 memcpy(*retDataBuf, view.buf, view.len);
832 PyObject_ReleaseBuffer(value, &view);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000833 }
834 break;
835 }
836 return TRUE;
837}
838
839/* Convert Registry data into PyObject*/
840static PyObject *
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000841Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000842{
843 PyObject *obData;
844
845 switch (typ) {
846 case REG_DWORD:
847 if (retDataSize == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +0000848 obData = PyLong_FromLong(0);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000849 else
Christian Heimes217cfd12007-12-02 14:31:20 +0000850 obData = PyLong_FromLong(*(int *)retDataBuf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000851 break;
852 case REG_SZ:
853 case REG_EXPAND_SZ:
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000854 {
855 /* the buffer may or may not have a trailing NULL */
856 wchar_t *data = (wchar_t *)retDataBuf;
857 int len = retDataSize / 2;
858 if (retDataSize && data[len-1] == '\0')
859 retDataSize -= 2;
860 if (retDataSize <= 0)
861 data = L"";
862 obData = PyUnicode_FromUnicode(data, retDataSize/2);
863 break;
864 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000865 case REG_MULTI_SZ:
866 if (retDataSize == 0)
867 obData = PyList_New(0);
868 else
869 {
870 int index = 0;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000871 wchar_t *data = (wchar_t *)retDataBuf;
872 int len = retDataSize / 2;
873 int s = countStrings(data, len);
874 wchar_t **str = (wchar_t **)malloc(sizeof(wchar_t *)*s);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000875 if (str == NULL)
876 return PyErr_NoMemory();
877
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000878 fixupMultiSZ(str, data, len);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000879 obData = PyList_New(s);
Fred Drake25e17262000-06-30 17:48:51 +0000880 if (obData == NULL)
881 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000882 for (index = 0; index < s; index++)
883 {
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000884 size_t len = wcslen(str[index]);
Fred Drake25e17262000-06-30 17:48:51 +0000885 if (len > INT_MAX) {
886 PyErr_SetString(PyExc_OverflowError,
887 "registry string is too long for a Python string");
888 Py_DECREF(obData);
889 return NULL;
890 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000891 PyList_SetItem(obData,
892 index,
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000893 PyUnicode_FromUnicode(str[index], len));
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000894 }
895 free(str);
896
897 break;
898 }
899 case REG_BINARY:
900 /* ALSO handle ALL unknown data types here. Even if we can't
901 support it natively, we should handle the bits. */
902 default:
903 if (retDataSize == 0) {
904 Py_INCREF(Py_None);
905 obData = Py_None;
906 }
907 else
Christian Heimes9c4756e2008-05-26 13:22:05 +0000908 obData = PyByteArray_FromStringAndSize(
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000909 (char *)retDataBuf, retDataSize);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000910 break;
911 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000912 return obData;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000913}
914
915/* The Python methods */
916
917static PyObject *
918PyCloseKey(PyObject *self, PyObject *args)
919{
920 PyObject *obKey;
921 if (!PyArg_ParseTuple(args, "O:CloseKey", &obKey))
922 return NULL;
923 if (!PyHKEY_Close(obKey))
924 return NULL;
925 Py_INCREF(Py_None);
926 return Py_None;
927}
928
929static PyObject *
930PyConnectRegistry(PyObject *self, PyObject *args)
931{
932 HKEY hKey;
933 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000934 wchar_t *szCompName = NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000935 HKEY retKey;
936 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000937 if (!PyArg_ParseTuple(args, "ZO:ConnectRegistry", &szCompName, &obKey))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000938 return NULL;
939 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
940 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000941 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000942 rc = RegConnectRegistryW(szCompName, hKey, &retKey);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000943 Py_END_ALLOW_THREADS
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000944 if (rc != ERROR_SUCCESS)
945 return PyErr_SetFromWindowsErrWithFunction(rc,
946 "ConnectRegistry");
947 return PyHKEY_FromHKEY(retKey);
948}
949
950static PyObject *
951PyCreateKey(PyObject *self, PyObject *args)
952{
953 HKEY hKey;
954 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000955 wchar_t *subKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000956 HKEY retKey;
957 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000958 if (!PyArg_ParseTuple(args, "OZ:CreateKey", &obKey, &subKey))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000959 return NULL;
960 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
961 return NULL;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000962 rc = RegCreateKeyW(hKey, subKey, &retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000963 if (rc != ERROR_SUCCESS)
964 return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey");
965 return PyHKEY_FromHKEY(retKey);
966}
967
968static PyObject *
969PyDeleteKey(PyObject *self, PyObject *args)
970{
971 HKEY hKey;
972 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000973 wchar_t *subKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000974 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000975 if (!PyArg_ParseTuple(args, "Ou:DeleteKey", &obKey, &subKey))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000976 return NULL;
977 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
978 return NULL;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000979 rc = RegDeleteKeyW(hKey, subKey );
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000980 if (rc != ERROR_SUCCESS)
981 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey");
982 Py_INCREF(Py_None);
983 return Py_None;
984}
985
986static PyObject *
987PyDeleteValue(PyObject *self, PyObject *args)
988{
989 HKEY hKey;
990 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000991 wchar_t *subKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000992 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000993 if (!PyArg_ParseTuple(args, "OZ:DeleteValue", &obKey, &subKey))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000994 return NULL;
995 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
996 return NULL;
997 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000998 rc = RegDeleteValueW(hKey, subKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000999 Py_END_ALLOW_THREADS
1000 if (rc !=ERROR_SUCCESS)
1001 return PyErr_SetFromWindowsErrWithFunction(rc,
1002 "RegDeleteValue");
1003 Py_INCREF(Py_None);
1004 return Py_None;
1005}
1006
1007static PyObject *
1008PyEnumKey(PyObject *self, PyObject *args)
1009{
1010 HKEY hKey;
1011 PyObject *obKey;
1012 int index;
1013 long rc;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001014 PyObject *retStr;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001015 wchar_t tmpbuf[256]; /* max key name length is 255 */
Georg Brandlb2699b22006-02-18 23:44:24 +00001016 DWORD len = sizeof(tmpbuf); /* includes NULL terminator */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001017
1018 if (!PyArg_ParseTuple(args, "Oi:EnumKey", &obKey, &index))
1019 return NULL;
1020 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1021 return NULL;
Tim Peters313fcd42006-02-19 04:05:39 +00001022
Georg Brandl9a928e72006-02-18 23:35:11 +00001023 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001024 rc = RegEnumKeyExW(hKey, index, tmpbuf, &len, NULL, NULL, NULL, NULL);
Georg Brandl9a928e72006-02-18 23:35:11 +00001025 Py_END_ALLOW_THREADS
1026 if (rc != ERROR_SUCCESS)
1027 return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx");
Tim Peters313fcd42006-02-19 04:05:39 +00001028
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001029 retStr = PyUnicode_FromUnicode(tmpbuf, len);
Georg Brandl9a928e72006-02-18 23:35:11 +00001030 return retStr; /* can be NULL */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001031}
1032
1033static PyObject *
1034PyEnumValue(PyObject *self, PyObject *args)
1035{
1036 HKEY hKey;
1037 PyObject *obKey;
1038 int index;
1039 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001040 wchar_t *retValueBuf;
1041 BYTE *retDataBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001042 DWORD retValueSize;
1043 DWORD retDataSize;
1044 DWORD typ;
1045 PyObject *obData;
1046 PyObject *retVal;
1047
1048 if (!PyArg_ParseTuple(args, "Oi:EnumValue", &obKey, &index))
1049 return NULL;
1050 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1051 return NULL;
1052
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001053 if ((rc = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, NULL, NULL,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001054 NULL,
1055 &retValueSize, &retDataSize, NULL, NULL))
1056 != ERROR_SUCCESS)
1057 return PyErr_SetFromWindowsErrWithFunction(rc,
1058 "RegQueryInfoKey");
1059 ++retValueSize; /* include null terminators */
1060 ++retDataSize;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001061 retValueBuf = (wchar_t *)PyMem_Malloc(sizeof(wchar_t) * retValueSize);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001062 if (retValueBuf == NULL)
1063 return PyErr_NoMemory();
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001064 retDataBuf = (BYTE *)PyMem_Malloc(retDataSize);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001065 if (retDataBuf == NULL) {
1066 PyMem_Free(retValueBuf);
1067 return PyErr_NoMemory();
1068 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001069
1070 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001071 rc = RegEnumValueW(hKey,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001072 index,
1073 retValueBuf,
1074 &retValueSize,
1075 NULL,
1076 &typ,
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001077 retDataBuf,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001078 &retDataSize);
1079 Py_END_ALLOW_THREADS
1080
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001081 if (rc != ERROR_SUCCESS) {
1082 retVal = PyErr_SetFromWindowsErrWithFunction(rc,
1083 "PyRegEnumValue");
1084 goto fail;
1085 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001086 obData = Reg2Py(retDataBuf, retDataSize, typ);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001087 if (obData == NULL) {
1088 retVal = NULL;
1089 goto fail;
1090 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001091 retVal = Py_BuildValue("uOi", retValueBuf, obData, typ);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001092 Py_DECREF(obData);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001093 fail:
1094 PyMem_Free(retValueBuf);
1095 PyMem_Free(retDataBuf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001096 return retVal;
1097}
1098
1099static PyObject *
Christian Heimes2380ac72008-01-09 00:17:24 +00001100PyExpandEnvironmentStrings(PyObject *self, PyObject *args)
1101{
1102 Py_UNICODE *retValue = NULL;
1103 Py_UNICODE *src;
1104 DWORD retValueSize;
1105 DWORD rc;
1106 PyObject *o;
1107
1108 if (!PyArg_ParseTuple(args, "u:ExpandEnvironmentStrings", &src))
1109 return NULL;
1110
1111 retValueSize = ExpandEnvironmentStringsW(src, retValue, 0);
1112 if (retValueSize == 0) {
1113 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1114 "ExpandEnvironmentStrings");
1115 }
1116 retValue = (Py_UNICODE *)PyMem_Malloc(retValueSize * sizeof(Py_UNICODE));
1117 if (retValue == NULL) {
1118 return PyErr_NoMemory();
1119 }
1120
1121 rc = ExpandEnvironmentStringsW(src, retValue, retValueSize);
1122 if (rc == 0) {
1123 PyMem_Free(retValue);
1124 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1125 "ExpandEnvironmentStrings");
1126 }
1127 o = PyUnicode_FromUnicode(retValue, wcslen(retValue));
1128 PyMem_Free(retValue);
1129 return o;
1130}
1131
1132static PyObject *
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001133PyFlushKey(PyObject *self, PyObject *args)
1134{
1135 HKEY hKey;
1136 PyObject *obKey;
1137 long rc;
1138 if (!PyArg_ParseTuple(args, "O:FlushKey", &obKey))
1139 return NULL;
1140 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1141 return NULL;
1142 Py_BEGIN_ALLOW_THREADS
1143 rc = RegFlushKey(hKey);
1144 Py_END_ALLOW_THREADS
1145 if (rc != ERROR_SUCCESS)
1146 return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey");
1147 Py_INCREF(Py_None);
1148 return Py_None;
1149}
1150static PyObject *
1151PyLoadKey(PyObject *self, PyObject *args)
1152{
1153 HKEY hKey;
1154 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001155 wchar_t *subKey;
1156 wchar_t *fileName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001157
1158 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001159 if (!PyArg_ParseTuple(args, "Ouu:LoadKey", &obKey, &subKey, &fileName))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001160 return NULL;
1161 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1162 return NULL;
1163 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001164 rc = RegLoadKeyW(hKey, subKey, fileName );
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001165 Py_END_ALLOW_THREADS
1166 if (rc != ERROR_SUCCESS)
1167 return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey");
1168 Py_INCREF(Py_None);
1169 return Py_None;
1170}
1171
1172static PyObject *
1173PyOpenKey(PyObject *self, PyObject *args)
1174{
1175 HKEY hKey;
1176 PyObject *obKey;
1177
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001178 wchar_t *subKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001179 int res = 0;
1180 HKEY retKey;
1181 long rc;
1182 REGSAM sam = KEY_READ;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001183 if (!PyArg_ParseTuple(args, "OZ|ii:OpenKey", &obKey, &subKey,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001184 &res, &sam))
1185 return NULL;
1186 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1187 return NULL;
1188
1189 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001190 rc = RegOpenKeyExW(hKey, subKey, res, sam, &retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001191 Py_END_ALLOW_THREADS
1192 if (rc != ERROR_SUCCESS)
1193 return PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx");
1194 return PyHKEY_FromHKEY(retKey);
1195}
1196
1197
1198static PyObject *
1199PyQueryInfoKey(PyObject *self, PyObject *args)
1200{
1201 HKEY hKey;
1202 PyObject *obKey;
1203 long rc;
1204 DWORD nSubKeys, nValues;
1205 FILETIME ft;
1206 LARGE_INTEGER li;
1207 PyObject *l;
1208 PyObject *ret;
1209 if (!PyArg_ParseTuple(args, "O:QueryInfoKey", &obKey))
1210 return NULL;
1211 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1212 return NULL;
1213 if ((rc = RegQueryInfoKey(hKey, NULL, NULL, 0, &nSubKeys, NULL, NULL,
1214 &nValues, NULL, NULL, NULL, &ft))
1215 != ERROR_SUCCESS)
1216 return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey");
1217 li.LowPart = ft.dwLowDateTime;
1218 li.HighPart = ft.dwHighDateTime;
1219 l = PyLong_FromLongLong(li.QuadPart);
1220 if (l == NULL)
1221 return NULL;
1222 ret = Py_BuildValue("iiO", nSubKeys, nValues, l);
1223 Py_DECREF(l);
1224 return ret;
1225}
1226
1227static PyObject *
1228PyQueryValue(PyObject *self, PyObject *args)
1229{
1230 HKEY hKey;
1231 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001232 wchar_t *subKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001233 long rc;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001234 PyObject *retStr;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001235 wchar_t *retBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001236 long bufSize = 0;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001237
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001238 if (!PyArg_ParseTuple(args, "OZ:QueryValue", &obKey, &subKey))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001239 return NULL;
1240
1241 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1242 return NULL;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001243 if ((rc = RegQueryValueW(hKey, subKey, NULL, &bufSize))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001244 != ERROR_SUCCESS)
1245 return PyErr_SetFromWindowsErrWithFunction(rc,
1246 "RegQueryValue");
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001247 retBuf = (wchar_t *)PyMem_Malloc(bufSize);
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001248 if (retBuf == NULL)
1249 return PyErr_NoMemory();
1250
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001251 if ((rc = RegQueryValueW(hKey, subKey, retBuf, &bufSize))
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001252 != ERROR_SUCCESS) {
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001253 PyMem_Free(retBuf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001254 return PyErr_SetFromWindowsErrWithFunction(rc,
1255 "RegQueryValue");
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001256 }
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001257
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001258 retStr = PyUnicode_FromUnicode(retBuf, wcslen(retBuf));
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001259 PyMem_Free(retBuf);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001260 return retStr;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001261}
1262
1263static PyObject *
1264PyQueryValueEx(PyObject *self, PyObject *args)
1265{
1266 HKEY hKey;
1267 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001268 wchar_t *valueName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001269
1270 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001271 BYTE *retBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001272 DWORD bufSize = 0;
1273 DWORD typ;
1274 PyObject *obData;
1275 PyObject *result;
1276
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001277 if (!PyArg_ParseTuple(args, "OZ:QueryValueEx", &obKey, &valueName))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001278 return NULL;
1279
1280 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1281 return NULL;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001282 if ((rc = RegQueryValueExW(hKey, valueName,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001283 NULL, NULL, NULL,
1284 &bufSize))
1285 != ERROR_SUCCESS)
1286 return PyErr_SetFromWindowsErrWithFunction(rc,
1287 "RegQueryValueEx");
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001288 retBuf = (BYTE *)PyMem_Malloc(bufSize);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001289 if (retBuf == NULL)
1290 return PyErr_NoMemory();
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001291 if ((rc = RegQueryValueExW(hKey, valueName, NULL,
1292 &typ, retBuf, &bufSize))
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001293 != ERROR_SUCCESS) {
1294 PyMem_Free(retBuf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001295 return PyErr_SetFromWindowsErrWithFunction(rc,
1296 "RegQueryValueEx");
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001297 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001298 obData = Reg2Py(retBuf, bufSize, typ);
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001299 PyMem_Free(retBuf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001300 if (obData == NULL)
1301 return NULL;
1302 result = Py_BuildValue("Oi", obData, typ);
1303 Py_DECREF(obData);
1304 return result;
1305}
1306
1307
1308static PyObject *
1309PySaveKey(PyObject *self, PyObject *args)
1310{
1311 HKEY hKey;
1312 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001313 wchar_t *fileName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001314 LPSECURITY_ATTRIBUTES pSA = NULL;
1315
1316 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001317 if (!PyArg_ParseTuple(args, "Ou:SaveKey", &obKey, &fileName))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001318 return NULL;
1319 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1320 return NULL;
1321/* One day we may get security into the core?
1322 if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE))
1323 return NULL;
1324*/
1325 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001326 rc = RegSaveKeyW(hKey, fileName, pSA );
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001327 Py_END_ALLOW_THREADS
1328 if (rc != ERROR_SUCCESS)
1329 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey");
1330 Py_INCREF(Py_None);
1331 return Py_None;
1332}
1333
1334static PyObject *
1335PySetValue(PyObject *self, PyObject *args)
1336{
1337 HKEY hKey;
1338 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001339 wchar_t *subKey;
1340 wchar_t *str;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001341 DWORD typ;
1342 DWORD len;
1343 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001344 if (!PyArg_ParseTuple(args, "OZiu#:SetValue",
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001345 &obKey,
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001346 &subKey,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001347 &typ,
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001348 &str,
1349 &len))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001350 return NULL;
1351 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1352 return NULL;
1353 if (typ != REG_SZ) {
1354 PyErr_SetString(PyExc_TypeError,
Georg Brandl38feaf02008-05-25 07:45:51 +00001355 "Type must be winreg.REG_SZ");
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001356 return NULL;
1357 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001358
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001359 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001360 rc = RegSetValueW(hKey, subKey, REG_SZ, str, len+1);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001361 Py_END_ALLOW_THREADS
1362 if (rc != ERROR_SUCCESS)
1363 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue");
1364 Py_INCREF(Py_None);
1365 return Py_None;
1366}
1367
1368static PyObject *
1369PySetValueEx(PyObject *self, PyObject *args)
1370{
1371 HKEY hKey;
1372 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001373 Py_UNICODE *valueName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001374 PyObject *obRes;
1375 PyObject *value;
1376 BYTE *data;
1377 DWORD len;
1378 DWORD typ;
1379
1380 LONG rc;
1381
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001382 if (!PyArg_ParseTuple(args, "OZOiO:SetValueEx",
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001383 &obKey,
1384 &valueName,
1385 &obRes,
1386 &typ,
1387 &value))
1388 return NULL;
1389 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1390 return NULL;
1391 if (!Py2Reg(value, typ, &data, &len))
1392 {
1393 if (!PyErr_Occurred())
1394 PyErr_SetString(PyExc_ValueError,
1395 "Could not convert the data to the specified type.");
1396 return NULL;
1397 }
1398 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001399 rc = RegSetValueExW(hKey, valueName, 0, typ, data, len);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001400 Py_END_ALLOW_THREADS
Guido van Rossumb18618d2000-05-03 23:44:39 +00001401 PyMem_DEL(data);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001402 if (rc != ERROR_SUCCESS)
1403 return PyErr_SetFromWindowsErrWithFunction(rc,
1404 "RegSetValueEx");
1405 Py_INCREF(Py_None);
1406 return Py_None;
1407}
1408
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001409static PyObject *
1410PyDisableReflectionKey(PyObject *self, PyObject *args)
1411{
1412 HKEY hKey;
1413 PyObject *obKey;
1414 HMODULE hMod;
1415 typedef LONG (WINAPI *RDRKFunc)(HKEY);
1416 RDRKFunc pfn = NULL;
1417 LONG rc;
1418
1419 if (!PyArg_ParseTuple(args, "O:DisableReflectionKey", &obKey))
1420 return NULL;
1421 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1422 return NULL;
1423
1424 // Only available on 64bit platforms, so we must load it
1425 // dynamically.
1426 hMod = GetModuleHandle("advapi32.dll");
1427 if (hMod)
1428 pfn = (RDRKFunc)GetProcAddress(hMod,
1429 "RegDisableReflectionKey");
1430 if (!pfn) {
1431 PyErr_SetString(PyExc_NotImplementedError,
1432 "not implemented on this platform");
1433 return NULL;
1434 }
1435 Py_BEGIN_ALLOW_THREADS
1436 rc = (*pfn)(hKey);
1437 Py_END_ALLOW_THREADS
1438 if (rc != ERROR_SUCCESS)
1439 return PyErr_SetFromWindowsErrWithFunction(rc,
1440 "RegDisableReflectionKey");
1441 Py_INCREF(Py_None);
1442 return Py_None;
1443}
1444
1445static PyObject *
1446PyEnableReflectionKey(PyObject *self, PyObject *args)
1447{
1448 HKEY hKey;
1449 PyObject *obKey;
1450 HMODULE hMod;
1451 typedef LONG (WINAPI *RERKFunc)(HKEY);
1452 RERKFunc pfn = NULL;
1453 LONG rc;
1454
1455 if (!PyArg_ParseTuple(args, "O:EnableReflectionKey", &obKey))
1456 return NULL;
1457 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1458 return NULL;
1459
1460 // Only available on 64bit platforms, so we must load it
1461 // dynamically.
1462 hMod = GetModuleHandle("advapi32.dll");
1463 if (hMod)
1464 pfn = (RERKFunc)GetProcAddress(hMod,
1465 "RegEnableReflectionKey");
1466 if (!pfn) {
1467 PyErr_SetString(PyExc_NotImplementedError,
1468 "not implemented on this platform");
1469 return NULL;
1470 }
1471 Py_BEGIN_ALLOW_THREADS
1472 rc = (*pfn)(hKey);
1473 Py_END_ALLOW_THREADS
1474 if (rc != ERROR_SUCCESS)
1475 return PyErr_SetFromWindowsErrWithFunction(rc,
1476 "RegEnableReflectionKey");
1477 Py_INCREF(Py_None);
1478 return Py_None;
1479}
1480
1481static PyObject *
1482PyQueryReflectionKey(PyObject *self, PyObject *args)
1483{
1484 HKEY hKey;
1485 PyObject *obKey;
1486 HMODULE hMod;
1487 typedef LONG (WINAPI *RQRKFunc)(HKEY, BOOL *);
1488 RQRKFunc pfn = NULL;
1489 BOOL result;
1490 LONG rc;
1491
1492 if (!PyArg_ParseTuple(args, "O:QueryReflectionKey", &obKey))
1493 return NULL;
1494 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1495 return NULL;
1496
1497 // Only available on 64bit platforms, so we must load it
1498 // dynamically.
1499 hMod = GetModuleHandle("advapi32.dll");
1500 if (hMod)
1501 pfn = (RQRKFunc)GetProcAddress(hMod,
1502 "RegQueryReflectionKey");
1503 if (!pfn) {
1504 PyErr_SetString(PyExc_NotImplementedError,
1505 "not implemented on this platform");
1506 return NULL;
1507 }
1508 Py_BEGIN_ALLOW_THREADS
1509 rc = (*pfn)(hKey, &result);
1510 Py_END_ALLOW_THREADS
1511 if (rc != ERROR_SUCCESS)
1512 return PyErr_SetFromWindowsErrWithFunction(rc,
1513 "RegQueryReflectionKey");
1514 return PyBool_FromLong(rc);
1515}
1516
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001517static struct PyMethodDef winreg_methods[] = {
Neal Norwitz031829d2002-03-31 14:37:44 +00001518 {"CloseKey", PyCloseKey, METH_VARARGS, CloseKey_doc},
1519 {"ConnectRegistry", PyConnectRegistry, METH_VARARGS, ConnectRegistry_doc},
1520 {"CreateKey", PyCreateKey, METH_VARARGS, CreateKey_doc},
1521 {"DeleteKey", PyDeleteKey, METH_VARARGS, DeleteKey_doc},
1522 {"DeleteValue", PyDeleteValue, METH_VARARGS, DeleteValue_doc},
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001523 {"DisableReflectionKey", PyDisableReflectionKey, METH_VARARGS, DisableReflectionKey_doc},
1524 {"EnableReflectionKey", PyEnableReflectionKey, METH_VARARGS, EnableReflectionKey_doc},
Neal Norwitz031829d2002-03-31 14:37:44 +00001525 {"EnumKey", PyEnumKey, METH_VARARGS, EnumKey_doc},
1526 {"EnumValue", PyEnumValue, METH_VARARGS, EnumValue_doc},
Christian Heimes2380ac72008-01-09 00:17:24 +00001527 {"ExpandEnvironmentStrings", PyExpandEnvironmentStrings, METH_VARARGS,
1528 ExpandEnvironmentStrings_doc },
Neal Norwitz031829d2002-03-31 14:37:44 +00001529 {"FlushKey", PyFlushKey, METH_VARARGS, FlushKey_doc},
1530 {"LoadKey", PyLoadKey, METH_VARARGS, LoadKey_doc},
1531 {"OpenKey", PyOpenKey, METH_VARARGS, OpenKey_doc},
1532 {"OpenKeyEx", PyOpenKey, METH_VARARGS, OpenKeyEx_doc},
1533 {"QueryValue", PyQueryValue, METH_VARARGS, QueryValue_doc},
1534 {"QueryValueEx", PyQueryValueEx, METH_VARARGS, QueryValueEx_doc},
1535 {"QueryInfoKey", PyQueryInfoKey, METH_VARARGS, QueryInfoKey_doc},
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001536 {"QueryReflectionKey",PyQueryReflectionKey,METH_VARARGS, QueryReflectionKey_doc},
Neal Norwitz031829d2002-03-31 14:37:44 +00001537 {"SaveKey", PySaveKey, METH_VARARGS, SaveKey_doc},
1538 {"SetValue", PySetValue, METH_VARARGS, SetValue_doc},
1539 {"SetValueEx", PySetValueEx, METH_VARARGS, SetValueEx_doc},
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001540 NULL,
1541};
1542
1543static void
1544insint(PyObject * d, char * name, long value)
1545{
Christian Heimes217cfd12007-12-02 14:31:20 +00001546 PyObject *v = PyLong_FromLong(value);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001547 if (!v || PyDict_SetItemString(d, name, v))
1548 PyErr_Clear();
1549 Py_XDECREF(v);
1550}
1551
1552#define ADD_INT(val) insint(d, #val, val)
1553
1554static void
1555inskey(PyObject * d, char * name, HKEY key)
1556{
1557 PyObject *v = PyLong_FromVoidPtr(key);
1558 if (!v || PyDict_SetItemString(d, name, v))
1559 PyErr_Clear();
1560 Py_XDECREF(v);
1561}
1562
1563#define ADD_KEY(val) inskey(d, #val, val)
1564
Martin v. Löwis1a214512008-06-11 05:26:20 +00001565
1566static struct PyModuleDef winregmodule = {
1567 PyModuleDef_HEAD_INIT,
1568 "winreg",
1569 module_doc,
1570 -1,
1571 winreg_methods,
1572 NULL,
1573 NULL,
1574 NULL,
1575 NULL
1576};
1577
1578PyMODINIT_FUNC PyInit_winreg(void)
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001579{
1580 PyObject *m, *d;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001581 m = PyModule_Create(&winregmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001582 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001583 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001584 d = PyModule_GetDict(m);
Christian Heimes90aa7642007-12-19 02:45:37 +00001585 Py_TYPE(&PyHKEY_Type) = &PyType_Type;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001586 PyHKEY_Type.tp_doc = PyHKEY_doc;
1587 Py_INCREF(&PyHKEY_Type);
1588 if (PyDict_SetItemString(d, "HKEYType",
1589 (PyObject *)&PyHKEY_Type) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001590 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001591 Py_INCREF(PyExc_WindowsError);
1592 if (PyDict_SetItemString(d, "error",
1593 PyExc_WindowsError) != 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001594 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001595
1596 /* Add the relevant constants */
1597 ADD_KEY(HKEY_CLASSES_ROOT);
1598 ADD_KEY(HKEY_CURRENT_USER);
1599 ADD_KEY(HKEY_LOCAL_MACHINE);
1600 ADD_KEY(HKEY_USERS);
1601 ADD_KEY(HKEY_PERFORMANCE_DATA);
1602#ifdef HKEY_CURRENT_CONFIG
1603 ADD_KEY(HKEY_CURRENT_CONFIG);
1604#endif
1605#ifdef HKEY_DYN_DATA
1606 ADD_KEY(HKEY_DYN_DATA);
1607#endif
1608 ADD_INT(KEY_QUERY_VALUE);
1609 ADD_INT(KEY_SET_VALUE);
1610 ADD_INT(KEY_CREATE_SUB_KEY);
1611 ADD_INT(KEY_ENUMERATE_SUB_KEYS);
1612 ADD_INT(KEY_NOTIFY);
1613 ADD_INT(KEY_CREATE_LINK);
1614 ADD_INT(KEY_READ);
1615 ADD_INT(KEY_WRITE);
1616 ADD_INT(KEY_EXECUTE);
1617 ADD_INT(KEY_ALL_ACCESS);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001618#ifdef KEY_WOW64_64KEY
1619 ADD_INT(KEY_WOW64_64KEY);
1620#endif
1621#ifdef KEY_WOW64_32KEY
1622 ADD_INT(KEY_WOW64_32KEY);
1623#endif
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001624 ADD_INT(REG_OPTION_RESERVED);
1625 ADD_INT(REG_OPTION_NON_VOLATILE);
1626 ADD_INT(REG_OPTION_VOLATILE);
1627 ADD_INT(REG_OPTION_CREATE_LINK);
1628 ADD_INT(REG_OPTION_BACKUP_RESTORE);
1629 ADD_INT(REG_OPTION_OPEN_LINK);
1630 ADD_INT(REG_LEGAL_OPTION);
1631 ADD_INT(REG_CREATED_NEW_KEY);
1632 ADD_INT(REG_OPENED_EXISTING_KEY);
1633 ADD_INT(REG_WHOLE_HIVE_VOLATILE);
1634 ADD_INT(REG_REFRESH_HIVE);
1635 ADD_INT(REG_NO_LAZY_FLUSH);
1636 ADD_INT(REG_NOTIFY_CHANGE_NAME);
1637 ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES);
1638 ADD_INT(REG_NOTIFY_CHANGE_LAST_SET);
1639 ADD_INT(REG_NOTIFY_CHANGE_SECURITY);
1640 ADD_INT(REG_LEGAL_CHANGE_FILTER);
1641 ADD_INT(REG_NONE);
1642 ADD_INT(REG_SZ);
1643 ADD_INT(REG_EXPAND_SZ);
1644 ADD_INT(REG_BINARY);
1645 ADD_INT(REG_DWORD);
1646 ADD_INT(REG_DWORD_LITTLE_ENDIAN);
1647 ADD_INT(REG_DWORD_BIG_ENDIAN);
1648 ADD_INT(REG_LINK);
1649 ADD_INT(REG_MULTI_SZ);
1650 ADD_INT(REG_RESOURCE_LIST);
1651 ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
1652 ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
Martin v. Löwis1a214512008-06-11 05:26:20 +00001653 return m;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001654}
1655
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001656