blob: 1b141d3f473447d4e15aeeb229b82a2a292e0a83 [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 */
Neil Schemenauer16c70752007-09-21 20:19:23 +0000453 0, /* nb_reserved */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000454 PyHKEY_intFunc, /* nb_int */
455 PyHKEY_unaryFailureFunc, /* nb_long */
456 PyHKEY_unaryFailureFunc, /* nb_float */
457 PyHKEY_unaryFailureFunc, /* nb_oct */
458 PyHKEY_unaryFailureFunc, /* nb_hex */
459};
460
461
462/* fwd declare __getattr__ */
Tim Petersc3d12ac2005-12-24 06:03:06 +0000463static PyObject *PyHKEY_getattr(PyObject *self, const char *name);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000464
465/* The type itself */
466PyTypeObject PyHKEY_Type =
467{
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000468 PyVarObject_HEAD_INIT(0, 0) /* fill in type at module init */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000469 "PyHKEY",
470 sizeof(PyHKEYObject),
471 0,
472 PyHKEY_deallocFunc, /* tp_dealloc */
Guido van Rossum346f1a82007-08-07 19:58:47 +0000473 0, /* tp_print */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000474 PyHKEY_getattr, /* tp_getattr */
475 0, /* tp_setattr */
476 PyHKEY_compareFunc, /* tp_compare */
477 0, /* tp_repr */
478 &PyHKEY_NumberMethods, /* tp_as_number */
479 0, /* tp_as_sequence */
480 0, /* tp_as_mapping */
481 PyHKEY_hashFunc, /* tp_hash */
482 0, /* tp_call */
483 PyHKEY_strFunc, /* tp_str */
484 0, /* tp_getattro */
485 0, /* tp_setattro */
486 0, /* tp_as_buffer */
487 0, /* tp_flags */
488 PyHKEY_doc, /* tp_doc */
489};
490
491#define OFF(e) offsetof(PyHKEYObject, e)
492
Neal Norwitz8dfc4a92007-08-11 06:39:53 +0000493static PyMemberDef PyHKEY_memberlist[] = {
494 {"handle", T_INT, OFF(hkey), READONLY},
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000495 {NULL} /* Sentinel */
496};
497
498/************************************************************************
499
500 The PyHKEY object methods
501
502************************************************************************/
503static PyObject *
504PyHKEY_CloseMethod(PyObject *self, PyObject *args)
505{
506 if (!PyArg_ParseTuple(args, ":Close"))
507 return NULL;
508 if (!PyHKEY_Close(self))
509 return NULL;
510 Py_INCREF(Py_None);
511 return Py_None;
512}
513
514static PyObject *
515PyHKEY_DetachMethod(PyObject *self, PyObject *args)
516{
517 void* ret;
518 PyHKEYObject *pThis = (PyHKEYObject *)self;
519 if (!PyArg_ParseTuple(args, ":Detach"))
520 return NULL;
521 ret = (void*)pThis->hkey;
522 pThis->hkey = 0;
523 return PyLong_FromVoidPtr(ret);
524}
525
Christian Heimes2380ac72008-01-09 00:17:24 +0000526static PyObject *
527PyHKEY_Enter(PyObject *self)
528{
529 Py_XINCREF(self);
530 return self;
531}
532
533static PyObject *
534PyHKEY_Exit(PyObject *self, PyObject *args)
535{
536 if (!PyHKEY_Close(self))
537 return NULL;
538 Py_RETURN_NONE;
539}
540
541
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000542static struct PyMethodDef PyHKEY_methods[] = {
Neal Norwitz031829d2002-03-31 14:37:44 +0000543 {"Close", PyHKEY_CloseMethod, METH_VARARGS, PyHKEY_Close_doc},
544 {"Detach", PyHKEY_DetachMethod, METH_VARARGS, PyHKEY_Detach_doc},
Christian Heimes2380ac72008-01-09 00:17:24 +0000545 {"__enter__", (PyCFunction)PyHKEY_Enter, METH_NOARGS, NULL},
546 {"__exit__", PyHKEY_Exit, METH_VARARGS, NULL},
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000547 {NULL}
548};
549
550/*static*/ PyObject *
Tim Petersc3d12ac2005-12-24 06:03:06 +0000551PyHKEY_getattr(PyObject *self, const char *name)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000552{
553 PyObject *res;
554
555 res = Py_FindMethod(PyHKEY_methods, self, name);
556 if (res != NULL)
557 return res;
558 PyErr_Clear();
559 if (strcmp(name, "handle") == 0)
560 return PyLong_FromVoidPtr(((PyHKEYObject *)self)->hkey);
Neal Norwitz8dfc4a92007-08-11 06:39:53 +0000561 PyErr_Format(PyExc_AttributeError,
562 "'%.50s' object has no attribute '%.400s'",
Christian Heimes90aa7642007-12-19 02:45:37 +0000563 Py_TYPE(self)->tp_name, name);
Neal Norwitz8dfc4a92007-08-11 06:39:53 +0000564 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000565}
566
567/************************************************************************
568 The public PyHKEY API (well, not public yet :-)
569************************************************************************/
570PyObject *
571PyHKEY_New(HKEY hInit)
572{
573 PyHKEYObject *key = PyObject_NEW(PyHKEYObject, &PyHKEY_Type);
574 if (key)
575 key->hkey = hInit;
576 return (PyObject *)key;
577}
578
579BOOL
580PyHKEY_Close(PyObject *ob_handle)
581{
582 LONG rc;
583 PyHKEYObject *key;
584
585 if (!PyHKEY_Check(ob_handle)) {
586 PyErr_SetString(PyExc_TypeError, "bad operand type");
587 return FALSE;
588 }
589 key = (PyHKEYObject *)ob_handle;
590 rc = key->hkey ? RegCloseKey((HKEY)key->hkey) : ERROR_SUCCESS;
591 key->hkey = 0;
592 if (rc != ERROR_SUCCESS)
593 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
594 return rc == ERROR_SUCCESS;
595}
596
597BOOL
598PyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK)
599{
600 if (ob == Py_None) {
601 if (!bNoneOK) {
602 PyErr_SetString(
603 PyExc_TypeError,
604 "None is not a valid HKEY in this context");
605 return FALSE;
606 }
607 *pHANDLE = (HKEY)0;
608 }
609 else if (PyHKEY_Check(ob)) {
610 PyHKEYObject *pH = (PyHKEYObject *)ob;
611 *pHANDLE = pH->hkey;
612 }
Neal Norwitz1fe5f382007-08-31 04:32:55 +0000613 else if (PyLong_Check(ob)) {
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000614 /* We also support integers */
615 PyErr_Clear();
616 *pHANDLE = (HKEY)PyLong_AsVoidPtr(ob);
617 if (PyErr_Occurred())
618 return FALSE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000619 }
620 else {
621 PyErr_SetString(
622 PyExc_TypeError,
623 "The object is not a PyHKEY object");
624 return FALSE;
625 }
626 return TRUE;
627}
628
629PyObject *
630PyHKEY_FromHKEY(HKEY h)
631{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000632 PyHKEYObject *op;
633
Guido van Rossume3a8e7e2002-08-19 19:26:42 +0000634 /* Inline PyObject_New */
Guido van Rossumb18618d2000-05-03 23:44:39 +0000635 op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject));
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000636 if (op == NULL)
637 return PyErr_NoMemory();
Guido van Rossumb18618d2000-05-03 23:44:39 +0000638 PyObject_INIT(op, &PyHKEY_Type);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000639 op->hkey = h;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000640 return (PyObject *)op;
641}
642
643
644/************************************************************************
645 The module methods
646************************************************************************/
647BOOL
648PyWinObject_CloseHKEY(PyObject *obHandle)
649{
650 BOOL ok;
651 if (PyHKEY_Check(obHandle)) {
652 ok = PyHKEY_Close(obHandle);
653 }
Fred Drake25e17262000-06-30 17:48:51 +0000654#if SIZEOF_LONG >= SIZEOF_HKEY
Christian Heimes217cfd12007-12-02 14:31:20 +0000655 else if (PyLong_Check(obHandle)) {
656 long rc = RegCloseKey((HKEY)PyLong_AsLong(obHandle));
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000657 ok = (rc == ERROR_SUCCESS);
658 if (!ok)
659 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
660 }
Fred Drake25e17262000-06-30 17:48:51 +0000661#else
662 else if (PyLong_Check(obHandle)) {
663 long rc = RegCloseKey((HKEY)PyLong_AsVoidPtr(obHandle));
664 ok = (rc == ERROR_SUCCESS);
665 if (!ok)
666 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
667 }
668#endif
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000669 else {
670 PyErr_SetString(
671 PyExc_TypeError,
672 "A handle must be a HKEY object or an integer");
673 return FALSE;
674 }
675 return ok;
676}
677
678
679/*
680 Private Helper functions for the registry interfaces
681
682** Note that fixupMultiSZ and countString have both had changes
683** made to support "incorrect strings". The registry specification
684** calls for strings to be terminated with 2 null bytes. It seems
Thomas Wouters7e474022000-07-16 12:04:32 +0000685** some commercial packages install strings which dont conform,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000686** causing this code to fail - however, "regedit" etc still work
687** with these strings (ie only we dont!).
688*/
689static void
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000690fixupMultiSZ(wchar_t **str, wchar_t *data, int len)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000691{
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000692 wchar_t *P;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000693 int i;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000694 wchar_t *Q;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000695
696 Q = data + len;
697 for (P = data, i = 0; P < Q && *P != '\0'; P++, i++) {
698 str[i] = P;
699 for(; *P != '\0'; P++)
700 ;
701 }
702}
703
704static int
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000705countStrings(wchar_t *data, int len)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000706{
707 int strings;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000708 wchar_t *P;
709 wchar_t *Q = data + len;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000710
711 for (P = data, strings = 0; P < Q && *P != '\0'; P++, strings++)
712 for (; P < Q && *P != '\0'; P++)
713 ;
714 return strings;
715}
716
717/* Convert PyObject into Registry data.
718 Allocates space as needed. */
719static BOOL
720Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
721{
Christian Heimescc47b052008-03-25 14:56:36 +0000722 Py_ssize_t i,j;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000723 switch (typ) {
724 case REG_DWORD:
Guido van Rossum7eaf8222007-06-18 17:58:50 +0000725 if (value != Py_None && !PyLong_Check(value))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000726 return FALSE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000727 *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000728 if (*retDataBuf==NULL){
729 PyErr_NoMemory();
730 return FALSE;
731 }
732 *retDataSize = sizeof(DWORD);
733 if (value == Py_None) {
734 DWORD zero = 0;
735 memcpy(*retDataBuf, &zero, sizeof(DWORD));
736 }
Guido van Rossum7eaf8222007-06-18 17:58:50 +0000737 else {
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000738 DWORD d = PyLong_AsLong(value);
Guido van Rossum7eaf8222007-06-18 17:58:50 +0000739 memcpy(*retDataBuf, &d, sizeof(DWORD));
740 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000741 break;
742 case REG_SZ:
743 case REG_EXPAND_SZ:
744 {
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000745 if (value == Py_None)
746 *retDataSize = 1;
747 else {
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000748 if (!PyUnicode_Check(value))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000749 return FALSE;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000750
751 *retDataSize = 2 + PyUnicode_GET_DATA_SIZE(value);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000752 }
753 *retDataBuf = (BYTE *)PyMem_NEW(DWORD, *retDataSize);
754 if (*retDataBuf==NULL){
755 PyErr_NoMemory();
756 return FALSE;
757 }
758 if (value == Py_None)
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000759 wcscpy((wchar_t *)*retDataBuf, L"");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000760 else
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000761 wcscpy((wchar_t *)*retDataBuf,
762 PyUnicode_AS_UNICODE(value));
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000763 break;
764 }
765 case REG_MULTI_SZ:
766 {
767 DWORD size = 0;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000768 wchar_t *P;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000769
770 if (value == Py_None)
771 i = 0;
772 else {
773 if (!PyList_Check(value))
774 return FALSE;
775 i = PyList_Size(value);
776 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000777 for (j = 0; j < i; j++)
778 {
779 PyObject *t;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000780 t = PyList_GET_ITEM(value, j);
781 if (!PyUnicode_Check(t))
782 return FALSE;
783 size += 2 + PyUnicode_GET_DATA_SIZE(t);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000784 }
785
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000786 *retDataSize = size + 2;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000787 *retDataBuf = (BYTE *)PyMem_NEW(char,
788 *retDataSize);
789 if (*retDataBuf==NULL){
790 PyErr_NoMemory();
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000791 return FALSE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000792 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000793 P = (wchar_t *)*retDataBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000794
795 for (j = 0; j < i; j++)
796 {
797 PyObject *t;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000798 t = PyList_GET_ITEM(value, j);
799 wcscpy(P, PyUnicode_AS_UNICODE(t));
800 P += 1 + wcslen(
801 PyUnicode_AS_UNICODE(t));
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000802 }
803 /* And doubly-terminate the list... */
804 *P = '\0';
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000805 break;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000806 }
807 case REG_BINARY:
808 /* ALSO handle ALL unknown data types here. Even if we can't
809 support it natively, we should handle the bits. */
810 default:
811 if (value == Py_None)
812 *retDataSize = 0;
813 else {
Thomas Heller39763a12007-09-24 14:43:56 +0000814 Py_buffer view;
Neal Norwitz1385b892007-08-26 23:07:13 +0000815
816 if (!PyObject_CheckBuffer(value)) {
Tim Peters313fcd42006-02-19 04:05:39 +0000817 PyErr_Format(PyExc_TypeError,
Mark Hammond4e80bb52000-07-28 03:44:41 +0000818 "Objects of type '%s' can not "
Tim Peters313fcd42006-02-19 04:05:39 +0000819 "be used as binary registry values",
Mark Hammond4e80bb52000-07-28 03:44:41 +0000820 value->ob_type->tp_name);
821 return FALSE;
822 }
Neal Norwitz1385b892007-08-26 23:07:13 +0000823
824 if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0)
825 return FALSE;
826
827 *retDataBuf = (BYTE *)PyMem_NEW(char, view.len);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000828 if (*retDataBuf==NULL){
Neal Norwitz1385b892007-08-26 23:07:13 +0000829 PyObject_ReleaseBuffer(value, &view);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000830 PyErr_NoMemory();
831 return FALSE;
832 }
Neal Norwitz1385b892007-08-26 23:07:13 +0000833 *retDataSize = view.len;
834 memcpy(*retDataBuf, view.buf, view.len);
835 PyObject_ReleaseBuffer(value, &view);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000836 }
837 break;
838 }
839 return TRUE;
840}
841
842/* Convert Registry data into PyObject*/
843static PyObject *
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000844Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000845{
846 PyObject *obData;
847
848 switch (typ) {
849 case REG_DWORD:
850 if (retDataSize == 0)
Christian Heimes217cfd12007-12-02 14:31:20 +0000851 obData = PyLong_FromLong(0);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000852 else
Christian Heimes217cfd12007-12-02 14:31:20 +0000853 obData = PyLong_FromLong(*(int *)retDataBuf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000854 break;
855 case REG_SZ:
856 case REG_EXPAND_SZ:
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000857 {
858 /* the buffer may or may not have a trailing NULL */
859 wchar_t *data = (wchar_t *)retDataBuf;
860 int len = retDataSize / 2;
861 if (retDataSize && data[len-1] == '\0')
862 retDataSize -= 2;
863 if (retDataSize <= 0)
864 data = L"";
865 obData = PyUnicode_FromUnicode(data, retDataSize/2);
866 break;
867 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000868 case REG_MULTI_SZ:
869 if (retDataSize == 0)
870 obData = PyList_New(0);
871 else
872 {
873 int index = 0;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000874 wchar_t *data = (wchar_t *)retDataBuf;
875 int len = retDataSize / 2;
876 int s = countStrings(data, len);
877 wchar_t **str = (wchar_t **)malloc(sizeof(wchar_t *)*s);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000878 if (str == NULL)
879 return PyErr_NoMemory();
880
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000881 fixupMultiSZ(str, data, len);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000882 obData = PyList_New(s);
Fred Drake25e17262000-06-30 17:48:51 +0000883 if (obData == NULL)
884 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000885 for (index = 0; index < s; index++)
886 {
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000887 size_t len = wcslen(str[index]);
Fred Drake25e17262000-06-30 17:48:51 +0000888 if (len > INT_MAX) {
889 PyErr_SetString(PyExc_OverflowError,
890 "registry string is too long for a Python string");
891 Py_DECREF(obData);
892 return NULL;
893 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000894 PyList_SetItem(obData,
895 index,
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000896 PyUnicode_FromUnicode(str[index], len));
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000897 }
898 free(str);
899
900 break;
901 }
902 case REG_BINARY:
903 /* ALSO handle ALL unknown data types here. Even if we can't
904 support it natively, we should handle the bits. */
905 default:
906 if (retDataSize == 0) {
907 Py_INCREF(Py_None);
908 obData = Py_None;
909 }
910 else
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000911 obData = PyBytes_FromStringAndSize(
912 (char *)retDataBuf, retDataSize);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000913 break;
914 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000915 return obData;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000916}
917
918/* The Python methods */
919
920static PyObject *
921PyCloseKey(PyObject *self, PyObject *args)
922{
923 PyObject *obKey;
924 if (!PyArg_ParseTuple(args, "O:CloseKey", &obKey))
925 return NULL;
926 if (!PyHKEY_Close(obKey))
927 return NULL;
928 Py_INCREF(Py_None);
929 return Py_None;
930}
931
932static PyObject *
933PyConnectRegistry(PyObject *self, PyObject *args)
934{
935 HKEY hKey;
936 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000937 wchar_t *szCompName = NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000938 HKEY retKey;
939 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000940 if (!PyArg_ParseTuple(args, "ZO:ConnectRegistry", &szCompName, &obKey))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000941 return NULL;
942 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
943 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000944 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000945 rc = RegConnectRegistryW(szCompName, hKey, &retKey);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000946 Py_END_ALLOW_THREADS
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000947 if (rc != ERROR_SUCCESS)
948 return PyErr_SetFromWindowsErrWithFunction(rc,
949 "ConnectRegistry");
950 return PyHKEY_FromHKEY(retKey);
951}
952
953static PyObject *
954PyCreateKey(PyObject *self, PyObject *args)
955{
956 HKEY hKey;
957 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000958 wchar_t *subKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000959 HKEY retKey;
960 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000961 if (!PyArg_ParseTuple(args, "OZ:CreateKey", &obKey, &subKey))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000962 return NULL;
963 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
964 return NULL;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000965 rc = RegCreateKeyW(hKey, subKey, &retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000966 if (rc != ERROR_SUCCESS)
967 return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey");
968 return PyHKEY_FromHKEY(retKey);
969}
970
971static PyObject *
972PyDeleteKey(PyObject *self, PyObject *args)
973{
974 HKEY hKey;
975 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000976 wchar_t *subKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000977 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000978 if (!PyArg_ParseTuple(args, "Ou:DeleteKey", &obKey, &subKey))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000979 return NULL;
980 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
981 return NULL;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000982 rc = RegDeleteKeyW(hKey, subKey );
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000983 if (rc != ERROR_SUCCESS)
984 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey");
985 Py_INCREF(Py_None);
986 return Py_None;
987}
988
989static PyObject *
990PyDeleteValue(PyObject *self, PyObject *args)
991{
992 HKEY hKey;
993 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000994 wchar_t *subKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000995 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000996 if (!PyArg_ParseTuple(args, "OZ:DeleteValue", &obKey, &subKey))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000997 return NULL;
998 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
999 return NULL;
1000 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001001 rc = RegDeleteValueW(hKey, subKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001002 Py_END_ALLOW_THREADS
1003 if (rc !=ERROR_SUCCESS)
1004 return PyErr_SetFromWindowsErrWithFunction(rc,
1005 "RegDeleteValue");
1006 Py_INCREF(Py_None);
1007 return Py_None;
1008}
1009
1010static PyObject *
1011PyEnumKey(PyObject *self, PyObject *args)
1012{
1013 HKEY hKey;
1014 PyObject *obKey;
1015 int index;
1016 long rc;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001017 PyObject *retStr;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001018 wchar_t tmpbuf[256]; /* max key name length is 255 */
Georg Brandlb2699b22006-02-18 23:44:24 +00001019 DWORD len = sizeof(tmpbuf); /* includes NULL terminator */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001020
1021 if (!PyArg_ParseTuple(args, "Oi:EnumKey", &obKey, &index))
1022 return NULL;
1023 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1024 return NULL;
Tim Peters313fcd42006-02-19 04:05:39 +00001025
Georg Brandl9a928e72006-02-18 23:35:11 +00001026 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001027 rc = RegEnumKeyExW(hKey, index, tmpbuf, &len, NULL, NULL, NULL, NULL);
Georg Brandl9a928e72006-02-18 23:35:11 +00001028 Py_END_ALLOW_THREADS
1029 if (rc != ERROR_SUCCESS)
1030 return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx");
Tim Peters313fcd42006-02-19 04:05:39 +00001031
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001032 retStr = PyUnicode_FromUnicode(tmpbuf, len);
Georg Brandl9a928e72006-02-18 23:35:11 +00001033 return retStr; /* can be NULL */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001034}
1035
1036static PyObject *
1037PyEnumValue(PyObject *self, PyObject *args)
1038{
1039 HKEY hKey;
1040 PyObject *obKey;
1041 int index;
1042 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001043 wchar_t *retValueBuf;
1044 BYTE *retDataBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001045 DWORD retValueSize;
1046 DWORD retDataSize;
1047 DWORD typ;
1048 PyObject *obData;
1049 PyObject *retVal;
1050
1051 if (!PyArg_ParseTuple(args, "Oi:EnumValue", &obKey, &index))
1052 return NULL;
1053 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1054 return NULL;
1055
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001056 if ((rc = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, NULL, NULL,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001057 NULL,
1058 &retValueSize, &retDataSize, NULL, NULL))
1059 != ERROR_SUCCESS)
1060 return PyErr_SetFromWindowsErrWithFunction(rc,
1061 "RegQueryInfoKey");
1062 ++retValueSize; /* include null terminators */
1063 ++retDataSize;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001064 retValueBuf = (wchar_t *)PyMem_Malloc(sizeof(wchar_t) * retValueSize);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001065 if (retValueBuf == NULL)
1066 return PyErr_NoMemory();
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001067 retDataBuf = (BYTE *)PyMem_Malloc(retDataSize);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001068 if (retDataBuf == NULL) {
1069 PyMem_Free(retValueBuf);
1070 return PyErr_NoMemory();
1071 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001072
1073 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001074 rc = RegEnumValueW(hKey,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001075 index,
1076 retValueBuf,
1077 &retValueSize,
1078 NULL,
1079 &typ,
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001080 retDataBuf,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001081 &retDataSize);
1082 Py_END_ALLOW_THREADS
1083
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001084 if (rc != ERROR_SUCCESS) {
1085 retVal = PyErr_SetFromWindowsErrWithFunction(rc,
1086 "PyRegEnumValue");
1087 goto fail;
1088 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001089 obData = Reg2Py(retDataBuf, retDataSize, typ);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001090 if (obData == NULL) {
1091 retVal = NULL;
1092 goto fail;
1093 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001094 retVal = Py_BuildValue("uOi", retValueBuf, obData, typ);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001095 Py_DECREF(obData);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001096 fail:
1097 PyMem_Free(retValueBuf);
1098 PyMem_Free(retDataBuf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001099 return retVal;
1100}
1101
1102static PyObject *
Christian Heimes2380ac72008-01-09 00:17:24 +00001103PyExpandEnvironmentStrings(PyObject *self, PyObject *args)
1104{
1105 Py_UNICODE *retValue = NULL;
1106 Py_UNICODE *src;
1107 DWORD retValueSize;
1108 DWORD rc;
1109 PyObject *o;
1110
1111 if (!PyArg_ParseTuple(args, "u:ExpandEnvironmentStrings", &src))
1112 return NULL;
1113
1114 retValueSize = ExpandEnvironmentStringsW(src, retValue, 0);
1115 if (retValueSize == 0) {
1116 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1117 "ExpandEnvironmentStrings");
1118 }
1119 retValue = (Py_UNICODE *)PyMem_Malloc(retValueSize * sizeof(Py_UNICODE));
1120 if (retValue == NULL) {
1121 return PyErr_NoMemory();
1122 }
1123
1124 rc = ExpandEnvironmentStringsW(src, retValue, retValueSize);
1125 if (rc == 0) {
1126 PyMem_Free(retValue);
1127 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1128 "ExpandEnvironmentStrings");
1129 }
1130 o = PyUnicode_FromUnicode(retValue, wcslen(retValue));
1131 PyMem_Free(retValue);
1132 return o;
1133}
1134
1135static PyObject *
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001136PyFlushKey(PyObject *self, PyObject *args)
1137{
1138 HKEY hKey;
1139 PyObject *obKey;
1140 long rc;
1141 if (!PyArg_ParseTuple(args, "O:FlushKey", &obKey))
1142 return NULL;
1143 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1144 return NULL;
1145 Py_BEGIN_ALLOW_THREADS
1146 rc = RegFlushKey(hKey);
1147 Py_END_ALLOW_THREADS
1148 if (rc != ERROR_SUCCESS)
1149 return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey");
1150 Py_INCREF(Py_None);
1151 return Py_None;
1152}
1153static PyObject *
1154PyLoadKey(PyObject *self, PyObject *args)
1155{
1156 HKEY hKey;
1157 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001158 wchar_t *subKey;
1159 wchar_t *fileName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001160
1161 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001162 if (!PyArg_ParseTuple(args, "Ouu:LoadKey", &obKey, &subKey, &fileName))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001163 return NULL;
1164 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1165 return NULL;
1166 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001167 rc = RegLoadKeyW(hKey, subKey, fileName );
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001168 Py_END_ALLOW_THREADS
1169 if (rc != ERROR_SUCCESS)
1170 return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey");
1171 Py_INCREF(Py_None);
1172 return Py_None;
1173}
1174
1175static PyObject *
1176PyOpenKey(PyObject *self, PyObject *args)
1177{
1178 HKEY hKey;
1179 PyObject *obKey;
1180
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001181 wchar_t *subKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001182 int res = 0;
1183 HKEY retKey;
1184 long rc;
1185 REGSAM sam = KEY_READ;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001186 if (!PyArg_ParseTuple(args, "OZ|ii:OpenKey", &obKey, &subKey,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001187 &res, &sam))
1188 return NULL;
1189 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1190 return NULL;
1191
1192 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001193 rc = RegOpenKeyExW(hKey, subKey, res, sam, &retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001194 Py_END_ALLOW_THREADS
1195 if (rc != ERROR_SUCCESS)
1196 return PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx");
1197 return PyHKEY_FromHKEY(retKey);
1198}
1199
1200
1201static PyObject *
1202PyQueryInfoKey(PyObject *self, PyObject *args)
1203{
1204 HKEY hKey;
1205 PyObject *obKey;
1206 long rc;
1207 DWORD nSubKeys, nValues;
1208 FILETIME ft;
1209 LARGE_INTEGER li;
1210 PyObject *l;
1211 PyObject *ret;
1212 if (!PyArg_ParseTuple(args, "O:QueryInfoKey", &obKey))
1213 return NULL;
1214 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1215 return NULL;
1216 if ((rc = RegQueryInfoKey(hKey, NULL, NULL, 0, &nSubKeys, NULL, NULL,
1217 &nValues, NULL, NULL, NULL, &ft))
1218 != ERROR_SUCCESS)
1219 return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey");
1220 li.LowPart = ft.dwLowDateTime;
1221 li.HighPart = ft.dwHighDateTime;
1222 l = PyLong_FromLongLong(li.QuadPart);
1223 if (l == NULL)
1224 return NULL;
1225 ret = Py_BuildValue("iiO", nSubKeys, nValues, l);
1226 Py_DECREF(l);
1227 return ret;
1228}
1229
1230static PyObject *
1231PyQueryValue(PyObject *self, PyObject *args)
1232{
1233 HKEY hKey;
1234 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001235 wchar_t *subKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001236 long rc;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001237 PyObject *retStr;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001238 wchar_t *retBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001239 long bufSize = 0;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001240
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001241 if (!PyArg_ParseTuple(args, "OZ:QueryValue", &obKey, &subKey))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001242 return NULL;
1243
1244 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1245 return NULL;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001246 if ((rc = RegQueryValueW(hKey, subKey, NULL, &bufSize))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001247 != ERROR_SUCCESS)
1248 return PyErr_SetFromWindowsErrWithFunction(rc,
1249 "RegQueryValue");
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001250 retBuf = (wchar_t *)PyMem_Malloc(bufSize);
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001251 if (retBuf == NULL)
1252 return PyErr_NoMemory();
1253
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001254 if ((rc = RegQueryValueW(hKey, subKey, retBuf, &bufSize))
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001255 != ERROR_SUCCESS) {
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001256 PyMem_Free(retBuf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001257 return PyErr_SetFromWindowsErrWithFunction(rc,
1258 "RegQueryValue");
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001259 }
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001260
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001261 retStr = PyUnicode_FromUnicode(retBuf, wcslen(retBuf));
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001262 PyMem_Free(retBuf);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001263 return retStr;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001264}
1265
1266static PyObject *
1267PyQueryValueEx(PyObject *self, PyObject *args)
1268{
1269 HKEY hKey;
1270 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001271 wchar_t *valueName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001272
1273 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001274 BYTE *retBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001275 DWORD bufSize = 0;
1276 DWORD typ;
1277 PyObject *obData;
1278 PyObject *result;
1279
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001280 if (!PyArg_ParseTuple(args, "OZ:QueryValueEx", &obKey, &valueName))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001281 return NULL;
1282
1283 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1284 return NULL;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001285 if ((rc = RegQueryValueExW(hKey, valueName,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001286 NULL, NULL, NULL,
1287 &bufSize))
1288 != ERROR_SUCCESS)
1289 return PyErr_SetFromWindowsErrWithFunction(rc,
1290 "RegQueryValueEx");
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001291 retBuf = (BYTE *)PyMem_Malloc(bufSize);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001292 if (retBuf == NULL)
1293 return PyErr_NoMemory();
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001294 if ((rc = RegQueryValueExW(hKey, valueName, NULL,
1295 &typ, retBuf, &bufSize))
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001296 != ERROR_SUCCESS) {
1297 PyMem_Free(retBuf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001298 return PyErr_SetFromWindowsErrWithFunction(rc,
1299 "RegQueryValueEx");
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001300 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001301 obData = Reg2Py(retBuf, bufSize, typ);
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001302 PyMem_Free(retBuf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001303 if (obData == NULL)
1304 return NULL;
1305 result = Py_BuildValue("Oi", obData, typ);
1306 Py_DECREF(obData);
1307 return result;
1308}
1309
1310
1311static PyObject *
1312PySaveKey(PyObject *self, PyObject *args)
1313{
1314 HKEY hKey;
1315 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001316 wchar_t *fileName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001317 LPSECURITY_ATTRIBUTES pSA = NULL;
1318
1319 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001320 if (!PyArg_ParseTuple(args, "Ou:SaveKey", &obKey, &fileName))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001321 return NULL;
1322 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1323 return NULL;
1324/* One day we may get security into the core?
1325 if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE))
1326 return NULL;
1327*/
1328 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001329 rc = RegSaveKeyW(hKey, fileName, pSA );
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001330 Py_END_ALLOW_THREADS
1331 if (rc != ERROR_SUCCESS)
1332 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey");
1333 Py_INCREF(Py_None);
1334 return Py_None;
1335}
1336
1337static PyObject *
1338PySetValue(PyObject *self, PyObject *args)
1339{
1340 HKEY hKey;
1341 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001342 wchar_t *subKey;
1343 wchar_t *str;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001344 DWORD typ;
1345 DWORD len;
1346 long rc;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001347 if (!PyArg_ParseTuple(args, "OZiu#:SetValue",
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001348 &obKey,
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001349 &subKey,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001350 &typ,
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001351 &str,
1352 &len))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001353 return NULL;
1354 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1355 return NULL;
1356 if (typ != REG_SZ) {
1357 PyErr_SetString(PyExc_TypeError,
Georg Brandl38feaf02008-05-25 07:45:51 +00001358 "Type must be winreg.REG_SZ");
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001359 return NULL;
1360 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001361
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001362 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001363 rc = RegSetValueW(hKey, subKey, REG_SZ, str, len+1);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001364 Py_END_ALLOW_THREADS
1365 if (rc != ERROR_SUCCESS)
1366 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue");
1367 Py_INCREF(Py_None);
1368 return Py_None;
1369}
1370
1371static PyObject *
1372PySetValueEx(PyObject *self, PyObject *args)
1373{
1374 HKEY hKey;
1375 PyObject *obKey;
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001376 Py_UNICODE *valueName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001377 PyObject *obRes;
1378 PyObject *value;
1379 BYTE *data;
1380 DWORD len;
1381 DWORD typ;
1382
1383 LONG rc;
1384
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001385 if (!PyArg_ParseTuple(args, "OZOiO:SetValueEx",
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001386 &obKey,
1387 &valueName,
1388 &obRes,
1389 &typ,
1390 &value))
1391 return NULL;
1392 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1393 return NULL;
1394 if (!Py2Reg(value, typ, &data, &len))
1395 {
1396 if (!PyErr_Occurred())
1397 PyErr_SetString(PyExc_ValueError,
1398 "Could not convert the data to the specified type.");
1399 return NULL;
1400 }
1401 Py_BEGIN_ALLOW_THREADS
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001402 rc = RegSetValueExW(hKey, valueName, 0, typ, data, len);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001403 Py_END_ALLOW_THREADS
Guido van Rossumb18618d2000-05-03 23:44:39 +00001404 PyMem_DEL(data);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001405 if (rc != ERROR_SUCCESS)
1406 return PyErr_SetFromWindowsErrWithFunction(rc,
1407 "RegSetValueEx");
1408 Py_INCREF(Py_None);
1409 return Py_None;
1410}
1411
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001412static PyObject *
1413PyDisableReflectionKey(PyObject *self, PyObject *args)
1414{
1415 HKEY hKey;
1416 PyObject *obKey;
1417 HMODULE hMod;
1418 typedef LONG (WINAPI *RDRKFunc)(HKEY);
1419 RDRKFunc pfn = NULL;
1420 LONG rc;
1421
1422 if (!PyArg_ParseTuple(args, "O:DisableReflectionKey", &obKey))
1423 return NULL;
1424 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1425 return NULL;
1426
1427 // Only available on 64bit platforms, so we must load it
1428 // dynamically.
1429 hMod = GetModuleHandle("advapi32.dll");
1430 if (hMod)
1431 pfn = (RDRKFunc)GetProcAddress(hMod,
1432 "RegDisableReflectionKey");
1433 if (!pfn) {
1434 PyErr_SetString(PyExc_NotImplementedError,
1435 "not implemented on this platform");
1436 return NULL;
1437 }
1438 Py_BEGIN_ALLOW_THREADS
1439 rc = (*pfn)(hKey);
1440 Py_END_ALLOW_THREADS
1441 if (rc != ERROR_SUCCESS)
1442 return PyErr_SetFromWindowsErrWithFunction(rc,
1443 "RegDisableReflectionKey");
1444 Py_INCREF(Py_None);
1445 return Py_None;
1446}
1447
1448static PyObject *
1449PyEnableReflectionKey(PyObject *self, PyObject *args)
1450{
1451 HKEY hKey;
1452 PyObject *obKey;
1453 HMODULE hMod;
1454 typedef LONG (WINAPI *RERKFunc)(HKEY);
1455 RERKFunc pfn = NULL;
1456 LONG rc;
1457
1458 if (!PyArg_ParseTuple(args, "O:EnableReflectionKey", &obKey))
1459 return NULL;
1460 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1461 return NULL;
1462
1463 // Only available on 64bit platforms, so we must load it
1464 // dynamically.
1465 hMod = GetModuleHandle("advapi32.dll");
1466 if (hMod)
1467 pfn = (RERKFunc)GetProcAddress(hMod,
1468 "RegEnableReflectionKey");
1469 if (!pfn) {
1470 PyErr_SetString(PyExc_NotImplementedError,
1471 "not implemented on this platform");
1472 return NULL;
1473 }
1474 Py_BEGIN_ALLOW_THREADS
1475 rc = (*pfn)(hKey);
1476 Py_END_ALLOW_THREADS
1477 if (rc != ERROR_SUCCESS)
1478 return PyErr_SetFromWindowsErrWithFunction(rc,
1479 "RegEnableReflectionKey");
1480 Py_INCREF(Py_None);
1481 return Py_None;
1482}
1483
1484static PyObject *
1485PyQueryReflectionKey(PyObject *self, PyObject *args)
1486{
1487 HKEY hKey;
1488 PyObject *obKey;
1489 HMODULE hMod;
1490 typedef LONG (WINAPI *RQRKFunc)(HKEY, BOOL *);
1491 RQRKFunc pfn = NULL;
1492 BOOL result;
1493 LONG rc;
1494
1495 if (!PyArg_ParseTuple(args, "O:QueryReflectionKey", &obKey))
1496 return NULL;
1497 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1498 return NULL;
1499
1500 // Only available on 64bit platforms, so we must load it
1501 // dynamically.
1502 hMod = GetModuleHandle("advapi32.dll");
1503 if (hMod)
1504 pfn = (RQRKFunc)GetProcAddress(hMod,
1505 "RegQueryReflectionKey");
1506 if (!pfn) {
1507 PyErr_SetString(PyExc_NotImplementedError,
1508 "not implemented on this platform");
1509 return NULL;
1510 }
1511 Py_BEGIN_ALLOW_THREADS
1512 rc = (*pfn)(hKey, &result);
1513 Py_END_ALLOW_THREADS
1514 if (rc != ERROR_SUCCESS)
1515 return PyErr_SetFromWindowsErrWithFunction(rc,
1516 "RegQueryReflectionKey");
1517 return PyBool_FromLong(rc);
1518}
1519
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001520static struct PyMethodDef winreg_methods[] = {
Neal Norwitz031829d2002-03-31 14:37:44 +00001521 {"CloseKey", PyCloseKey, METH_VARARGS, CloseKey_doc},
1522 {"ConnectRegistry", PyConnectRegistry, METH_VARARGS, ConnectRegistry_doc},
1523 {"CreateKey", PyCreateKey, METH_VARARGS, CreateKey_doc},
1524 {"DeleteKey", PyDeleteKey, METH_VARARGS, DeleteKey_doc},
1525 {"DeleteValue", PyDeleteValue, METH_VARARGS, DeleteValue_doc},
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001526 {"DisableReflectionKey", PyDisableReflectionKey, METH_VARARGS, DisableReflectionKey_doc},
1527 {"EnableReflectionKey", PyEnableReflectionKey, METH_VARARGS, EnableReflectionKey_doc},
Neal Norwitz031829d2002-03-31 14:37:44 +00001528 {"EnumKey", PyEnumKey, METH_VARARGS, EnumKey_doc},
1529 {"EnumValue", PyEnumValue, METH_VARARGS, EnumValue_doc},
Christian Heimes2380ac72008-01-09 00:17:24 +00001530 {"ExpandEnvironmentStrings", PyExpandEnvironmentStrings, METH_VARARGS,
1531 ExpandEnvironmentStrings_doc },
Neal Norwitz031829d2002-03-31 14:37:44 +00001532 {"FlushKey", PyFlushKey, METH_VARARGS, FlushKey_doc},
1533 {"LoadKey", PyLoadKey, METH_VARARGS, LoadKey_doc},
1534 {"OpenKey", PyOpenKey, METH_VARARGS, OpenKey_doc},
1535 {"OpenKeyEx", PyOpenKey, METH_VARARGS, OpenKeyEx_doc},
1536 {"QueryValue", PyQueryValue, METH_VARARGS, QueryValue_doc},
1537 {"QueryValueEx", PyQueryValueEx, METH_VARARGS, QueryValueEx_doc},
1538 {"QueryInfoKey", PyQueryInfoKey, METH_VARARGS, QueryInfoKey_doc},
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001539 {"QueryReflectionKey",PyQueryReflectionKey,METH_VARARGS, QueryReflectionKey_doc},
Neal Norwitz031829d2002-03-31 14:37:44 +00001540 {"SaveKey", PySaveKey, METH_VARARGS, SaveKey_doc},
1541 {"SetValue", PySetValue, METH_VARARGS, SetValue_doc},
1542 {"SetValueEx", PySetValueEx, METH_VARARGS, SetValueEx_doc},
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001543 NULL,
1544};
1545
1546static void
1547insint(PyObject * d, char * name, long value)
1548{
Christian Heimes217cfd12007-12-02 14:31:20 +00001549 PyObject *v = PyLong_FromLong(value);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001550 if (!v || PyDict_SetItemString(d, name, v))
1551 PyErr_Clear();
1552 Py_XDECREF(v);
1553}
1554
1555#define ADD_INT(val) insint(d, #val, val)
1556
1557static void
1558inskey(PyObject * d, char * name, HKEY key)
1559{
1560 PyObject *v = PyLong_FromVoidPtr(key);
1561 if (!v || PyDict_SetItemString(d, name, v))
1562 PyErr_Clear();
1563 Py_XDECREF(v);
1564}
1565
1566#define ADD_KEY(val) inskey(d, #val, val)
1567
Georg Brandl38feaf02008-05-25 07:45:51 +00001568PyMODINIT_FUNC initwinreg(void)
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001569{
1570 PyObject *m, *d;
Georg Brandl38feaf02008-05-25 07:45:51 +00001571 m = Py_InitModule3("winreg", winreg_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001572 if (m == NULL)
1573 return;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001574 d = PyModule_GetDict(m);
Christian Heimes90aa7642007-12-19 02:45:37 +00001575 Py_TYPE(&PyHKEY_Type) = &PyType_Type;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001576 PyHKEY_Type.tp_doc = PyHKEY_doc;
1577 Py_INCREF(&PyHKEY_Type);
1578 if (PyDict_SetItemString(d, "HKEYType",
1579 (PyObject *)&PyHKEY_Type) != 0)
1580 return;
1581 Py_INCREF(PyExc_WindowsError);
1582 if (PyDict_SetItemString(d, "error",
1583 PyExc_WindowsError) != 0)
1584 return;
1585
1586 /* Add the relevant constants */
1587 ADD_KEY(HKEY_CLASSES_ROOT);
1588 ADD_KEY(HKEY_CURRENT_USER);
1589 ADD_KEY(HKEY_LOCAL_MACHINE);
1590 ADD_KEY(HKEY_USERS);
1591 ADD_KEY(HKEY_PERFORMANCE_DATA);
1592#ifdef HKEY_CURRENT_CONFIG
1593 ADD_KEY(HKEY_CURRENT_CONFIG);
1594#endif
1595#ifdef HKEY_DYN_DATA
1596 ADD_KEY(HKEY_DYN_DATA);
1597#endif
1598 ADD_INT(KEY_QUERY_VALUE);
1599 ADD_INT(KEY_SET_VALUE);
1600 ADD_INT(KEY_CREATE_SUB_KEY);
1601 ADD_INT(KEY_ENUMERATE_SUB_KEYS);
1602 ADD_INT(KEY_NOTIFY);
1603 ADD_INT(KEY_CREATE_LINK);
1604 ADD_INT(KEY_READ);
1605 ADD_INT(KEY_WRITE);
1606 ADD_INT(KEY_EXECUTE);
1607 ADD_INT(KEY_ALL_ACCESS);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001608#ifdef KEY_WOW64_64KEY
1609 ADD_INT(KEY_WOW64_64KEY);
1610#endif
1611#ifdef KEY_WOW64_32KEY
1612 ADD_INT(KEY_WOW64_32KEY);
1613#endif
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001614 ADD_INT(REG_OPTION_RESERVED);
1615 ADD_INT(REG_OPTION_NON_VOLATILE);
1616 ADD_INT(REG_OPTION_VOLATILE);
1617 ADD_INT(REG_OPTION_CREATE_LINK);
1618 ADD_INT(REG_OPTION_BACKUP_RESTORE);
1619 ADD_INT(REG_OPTION_OPEN_LINK);
1620 ADD_INT(REG_LEGAL_OPTION);
1621 ADD_INT(REG_CREATED_NEW_KEY);
1622 ADD_INT(REG_OPENED_EXISTING_KEY);
1623 ADD_INT(REG_WHOLE_HIVE_VOLATILE);
1624 ADD_INT(REG_REFRESH_HIVE);
1625 ADD_INT(REG_NO_LAZY_FLUSH);
1626 ADD_INT(REG_NOTIFY_CHANGE_NAME);
1627 ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES);
1628 ADD_INT(REG_NOTIFY_CHANGE_LAST_SET);
1629 ADD_INT(REG_NOTIFY_CHANGE_SECURITY);
1630 ADD_INT(REG_LEGAL_CHANGE_FILTER);
1631 ADD_INT(REG_NONE);
1632 ADD_INT(REG_SZ);
1633 ADD_INT(REG_EXPAND_SZ);
1634 ADD_INT(REG_BINARY);
1635 ADD_INT(REG_DWORD);
1636 ADD_INT(REG_DWORD_LITTLE_ENDIAN);
1637 ADD_INT(REG_DWORD_BIG_ENDIAN);
1638 ADD_INT(REG_LINK);
1639 ADD_INT(REG_MULTI_SZ);
1640 ADD_INT(REG_RESOURCE_LIST);
1641 ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
1642 ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
1643}
1644
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001645