blob: a2511d52f35700cf2c9ac204866d880db4b6e86e [file] [log] [blame]
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001/*
Georg Brandl38feaf02008-05-25 07:45:51 +00002 winreg.c
Guido van Rossum9f3712c2000-03-28 20:37:15 +00003
4 Windows Registry access module for Python.
5
6 * Simple registry access written by Mark Hammond in win32api
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007 module circa 1995.
Guido van Rossum9f3712c2000-03-28 20:37:15 +00008 * Bill Tutt expanded the support significantly not long after.
9 * Numerous other people have submitted patches since then.
10 * Ripped from win32api module 03-Feb-2000 by Mark Hammond, and
11 basic Unicode support added.
12
13*/
14
Guido van Rossum9f3712c2000-03-28 20:37:15 +000015#include "Python.h"
16#include "structmember.h"
Guido van Rossume7ba4952007-06-06 23:52:48 +000017#include "windows.h"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000018
19static BOOL PyHKEY_AsHKEY(PyObject *ob, HKEY *pRes, BOOL bNoneOK);
20static PyObject *PyHKEY_FromHKEY(HKEY h);
21static BOOL PyHKEY_Close(PyObject *obHandle);
22
23static char errNotAHandle[] = "Object is not a handle";
24
25/* The win32api module reports the function name that failed,
26 but this concept is not in the Python core.
27 Hopefully it will one day, and in the meantime I dont
28 want to lose this info...
29*/
30#define PyErr_SetFromWindowsErrWithFunction(rc, fnname) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031 PyErr_SetFromWindowsErr(rc)
Guido van Rossum9f3712c2000-03-28 20:37:15 +000032
33/* Forward declares */
34
35/* Doc strings */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000036PyDoc_STRVAR(module_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +000037"This module provides access to the Windows registry API.\n"
38"\n"
39"Functions:\n"
40"\n"
41"CloseKey() - Closes a registry key.\n"
42"ConnectRegistry() - Establishes a connection to a predefined registry handle\n"
43" on another computer.\n"
44"CreateKey() - Creates the specified key, or opens it if it already exists.\n"
45"DeleteKey() - Deletes the specified key.\n"
46"DeleteValue() - Removes a named value from the specified registry key.\n"
47"EnumKey() - Enumerates subkeys of the specified open registry key.\n"
48"EnumValue() - Enumerates values of the specified open registry key.\n"
Christian Heimes2380ac72008-01-09 00:17:24 +000049"ExpandEnvironmentStrings() - Expand the env strings in a REG_EXPAND_SZ string.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000050"FlushKey() - Writes all the attributes of the specified key to the registry.\n"
51"LoadKey() - Creates a subkey under HKEY_USER or HKEY_LOCAL_MACHINE and stores\n"
52" registration information from a specified file into that subkey.\n"
Brian Curtine9aeca72012-10-29 18:16:39 -050053"OpenKey() - Opens the specified key.\n"
54"OpenKeyEx() - Alias of OpenKey().\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000055"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,
Brian Curtine9aeca72012-10-29 18:16:39 -050075"CloseKey(hkey)\n"
76"Closes a previously opened registry key.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000077"\n"
78"The hkey argument specifies a previously opened key.\n"
79"\n"
80"Note that if the key is not closed using this method, it will be\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000081"closed when the hkey object is destroyed by Python.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +000082
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000083PyDoc_STRVAR(ConnectRegistry_doc,
Brian Curtine9aeca72012-10-29 18:16:39 -050084"ConnectRegistry(computer_name, key) -> key\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000085"Establishes a connection to a predefined registry handle on another computer.\n"
86"\n"
87"computer_name is the name of the remote computer, of the form \\\\computername.\n"
Brian Curtine9aeca72012-10-29 18:16:39 -050088" If None, the local computer is used.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000089"key is the predefined handle to connect to.\n"
90"\n"
91"The return value is the handle of the opened key.\n"
Andrew Svetlov616f8032012-10-31 19:29:33 +020092"If the function fails, an OSError exception is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +000093
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000094PyDoc_STRVAR(CreateKey_doc,
Brian Curtine9aeca72012-10-29 18:16:39 -050095"CreateKey(key, sub_key) -> key\n"
96"Creates or opens the specified key.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000097"\n"
Brian Curtine9aeca72012-10-29 18:16:39 -050098"key is an already open key, or one of the predefined HKEY_* constants.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000099"sub_key is a string that names the key this method opens or creates.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000100"\n"
Brian Curtine9aeca72012-10-29 18:16:39 -0500101"If key is one of the predefined keys, sub_key may be None. In that case,\n"
102"the handle returned is the same key handle passed in to the function.\n"
103"\n"
104"If the key already exists, this function opens the existing key.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000105"\n"
106"The return value is the handle of the opened key.\n"
Andrew Svetlov616f8032012-10-31 19:29:33 +0200107"If the function fails, an OSError exception is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000108
Brian Curtin3035c392010-04-21 23:56:21 +0000109PyDoc_STRVAR(CreateKeyEx_doc,
Brian Curtine9aeca72012-10-29 18:16:39 -0500110"CreateKeyEx(key, sub_key, reserved=0, access=KEY_WRITE) -> key\n"
111"Creates or opens the specified key.\n"
Brian Curtin3035c392010-04-21 23:56:21 +0000112"\n"
113"key is an already open key, or one of the predefined HKEY_* constants\n"
114"sub_key is a string that names the key this method opens or creates.\n"
Brian Curtine9aeca72012-10-29 18:16:39 -0500115"reserved is a reserved integer, and must be zero. Default is zero.\n"
116"access is an integer that specifies an access mask that describes the \n"
117" desired security access for the key. Default is KEY_WRITE.\n"
118"\n"
119"If key is one of the predefined keys, sub_key may be None. In that case,\n"
120"the handle returned is the same key handle passed in to the function.\n"
Brian Curtin3035c392010-04-21 23:56:21 +0000121"\n"
122"If the key already exists, this function opens the existing key\n"
123"\n"
124"The return value is the handle of the opened key.\n"
Andrew Svetlov616f8032012-10-31 19:29:33 +0200125"If the function fails, an OSError exception is raised.");
Brian Curtin3035c392010-04-21 23:56:21 +0000126
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000127PyDoc_STRVAR(DeleteKey_doc,
Brian Curtine9aeca72012-10-29 18:16:39 -0500128"DeleteKey(key, sub_key)\n"
129"Deletes the specified key.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000130"\n"
131"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Brian Curtine9aeca72012-10-29 18:16:39 -0500132"sub_key is a string that must be a subkey of the key identified by the key\n"
133" parameter. This value must not be None, and the key may not have\n"
134" subkeys.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000135"\n"
136"This method can not delete keys with subkeys.\n"
137"\n"
Brian Curtine9aeca72012-10-29 18:16:39 -0500138"If the function succeeds, the entire key, including all of its values,\n"
Andrew Svetlov616f8032012-10-31 19:29:33 +0200139"is removed. If the function fails, an OSError exception is raised.");
Brian Curtin3035c392010-04-21 23:56:21 +0000140
141PyDoc_STRVAR(DeleteKeyEx_doc,
Brian Curtine9aeca72012-10-29 18:16:39 -0500142"DeleteKeyEx(key, sub_key, access=KEY_WOW64_64KEY, reserved=0)\n"
143"Deletes the specified key (64-bit OS only).\n"
Brian Curtin3035c392010-04-21 23:56:21 +0000144"\n"
145"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Brian Curtine9aeca72012-10-29 18:16:39 -0500146"sub_key is a string that must be a subkey of the key identified by the key\n"
147" parameter. This value must not be None, and the key may not have\n"
148" subkeys.\n"
149"reserved is a reserved integer, and must be zero. Default is zero.\n"
150"access is an integer that specifies an access mask that describes the \n"
151" desired security access for the key. Default is KEY_WOW64_64KEY.\n"
Brian Curtin3035c392010-04-21 23:56:21 +0000152"\n"
153"This method can not delete keys with subkeys.\n"
154"\n"
Brian Curtine9aeca72012-10-29 18:16:39 -0500155"If the function succeeds, the entire key, including all of its values,\n"
Andrew Svetlov616f8032012-10-31 19:29:33 +0200156"is removed. If the function fails, an OSError exception is raised.\n"
Brian Curtin3035c392010-04-21 23:56:21 +0000157"On unsupported Windows versions, NotImplementedError is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000158
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000159PyDoc_STRVAR(DeleteValue_doc,
Brian Curtine9aeca72012-10-29 18:16:39 -0500160"DeleteValue(key, value)\n"
161"Removes a named value from a registry key.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000162"\n"
163"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000164"value is a string that identifies the value to remove.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000165
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000166PyDoc_STRVAR(EnumKey_doc,
Brian Curtine9aeca72012-10-29 18:16:39 -0500167"EnumKey(key, index) -> string\n"
168"Enumerates subkeys of an open registry key.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000169"\n"
170"key is an already open key, or any one of the predefined HKEY_* constants.\n"
171"index is an integer that identifies the index of the key to retrieve.\n"
172"\n"
173"The function retrieves the name of one subkey each time it is called.\n"
Andrew Svetlov616f8032012-10-31 19:29:33 +0200174"It is typically called repeatedly until an OSError exception is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000175"raised, indicating no more values are available.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000176
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000177PyDoc_STRVAR(EnumValue_doc,
Brian Curtine9aeca72012-10-29 18:16:39 -0500178"EnumValue(key, index) -> tuple\n"
179"Enumerates values of an open registry key.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000180"key is an already open key, or any one of the predefined HKEY_* constants.\n"
181"index is an integer that identifies the index of the value to retrieve.\n"
182"\n"
183"The function retrieves the name of one subkey each time it is called.\n"
Andrew Svetlov616f8032012-10-31 19:29:33 +0200184"It is typically called repeatedly, until an OSError exception\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000185"is raised, indicating no more values.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000186"\n"
187"The result is a tuple of 3 items:\n"
188"value_name is a string that identifies the value.\n"
189"value_data is an object that holds the value data, and whose type depends\n"
Brian Curtine9aeca72012-10-29 18:16:39 -0500190" on the underlying registry type.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000191"data_type is an integer that identifies the type of the value data.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000192
Christian Heimes2380ac72008-01-09 00:17:24 +0000193PyDoc_STRVAR(ExpandEnvironmentStrings_doc,
Brian Curtine9aeca72012-10-29 18:16:39 -0500194"ExpandEnvironmentStrings(string) -> string\n"
195"Expand environment vars.\n");
Christian Heimes2380ac72008-01-09 00:17:24 +0000196
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000197PyDoc_STRVAR(FlushKey_doc,
Brian Curtine9aeca72012-10-29 18:16:39 -0500198"FlushKey(key)\n"
199"Writes all the attributes of a key to the registry.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000200"\n"
201"key is an already open key, or any one of the predefined HKEY_* constants.\n"
202"\n"
Brian Curtine9aeca72012-10-29 18:16:39 -0500203"It is not necessary to call FlushKey to change a key. Registry changes are\n"
204"flushed to disk by the registry using its lazy flusher. Registry changes are\n"
205"also flushed to disk at system shutdown. Unlike CloseKey(), the FlushKey()\n"
206"method returns only when all the data has been written to the registry.\n"
207"\n"
208"An application should only call FlushKey() if it requires absolute certainty\n"
209"that registry changes are on disk. If you don't know whether a FlushKey()\n"
210"call is required, it probably isn't.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000211
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000212PyDoc_STRVAR(LoadKey_doc,
Brian Curtine9aeca72012-10-29 18:16:39 -0500213"LoadKey(key, sub_key, file_name)\n"
214"Creates a subkey under the specified key and stores registration information\n"
215"from a specified file into that subkey.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000216"\n"
217"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Brian Curtine9aeca72012-10-29 18:16:39 -0500218"sub_key is a string that identifies the sub_key to load.\n"
219"file_name is the name of the file to load registry data from. This file must\n"
220" have been created with the SaveKey() function. Under the file\n"
221" allocation table (FAT) file system, the filename may not have an\n"
222" extension.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000223"\n"
224"A call to LoadKey() fails if the calling process does not have the\n"
225"SE_RESTORE_PRIVILEGE privilege.\n"
226"\n"
227"If key is a handle returned by ConnectRegistry(), then the path specified\n"
228"in fileName is relative to the remote computer.\n"
229"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000230"The docs imply key must be in the HKEY_USER or HKEY_LOCAL_MACHINE tree");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000231
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000232PyDoc_STRVAR(OpenKey_doc,
Brian Curtine9aeca72012-10-29 18:16:39 -0500233"OpenKey(key, sub_key, reserved=0, access=KEY_READ) -> key\n"
234"Opens the specified key.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000235"\n"
236"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Brian Curtine9aeca72012-10-29 18:16:39 -0500237"sub_key is a string that identifies the sub_key to open.\n"
238"reserved is a reserved integer, and must be zero. Default is zero.\n"
239"access is an integer that specifies an access mask that describes the desired\n"
240" security access for the key. Default is KEY_READ\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000241"\n"
242"The result is a new handle to the specified key\n"
Andrew Svetlov616f8032012-10-31 19:29:33 +0200243"If the function fails, an OSError exception is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000244
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000245PyDoc_STRVAR(OpenKeyEx_doc, "See OpenKey()");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000246
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000247PyDoc_STRVAR(QueryInfoKey_doc,
Brian Curtine9aeca72012-10-29 18:16:39 -0500248"QueryInfoKey(key) -> tuple\n"
249"Returns information about a key.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000250"\n"
251"key is an already open key, or any one of the predefined HKEY_* constants.\n"
252"\n"
253"The result is a tuple of 3 items:"
254"An integer that identifies the number of sub keys this key has.\n"
255"An integer that identifies the number of values this key has.\n"
256"A long integer that identifies when the key was last modified (if available)\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000257" as 100's of nanoseconds since Jan 1, 1600.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000258
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000259PyDoc_STRVAR(QueryValue_doc,
Brian Curtine9aeca72012-10-29 18:16:39 -0500260"QueryValue(key, sub_key) -> string\n"
261"Retrieves the unnamed value for a key.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000262"\n"
263"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000264"sub_key is a string that holds the name of the subkey with which the value\n"
Brian Curtine9aeca72012-10-29 18:16:39 -0500265" is associated. If this parameter is None or empty, the function\n"
266" retrieves the value set by the SetValue() method for the key\n"
267" identified by key."
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000268"\n"
269"Values in the registry have name, type, and data components. This method\n"
270"retrieves the data for a key's first value that has a NULL name.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000271"But the underlying API call doesn't return the type, Lame Lame Lame, DONT USE THIS!!!");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000272
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000273PyDoc_STRVAR(QueryValueEx_doc,
Brian Curtine9aeca72012-10-29 18:16:39 -0500274"QueryValueEx(key, value_name) -> (value, type_id)\n"
275"Retrieves the type and data for a specified value name associated with an\n"
276"open registry key.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000277"\n"
278"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000279"value_name is a string indicating the value to query");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000280
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000281PyDoc_STRVAR(SaveKey_doc,
Brian Curtine9aeca72012-10-29 18:16:39 -0500282"SaveKey(key, file_name)\n"
283"Saves the specified key, and all its subkeys to the specified file.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000284"\n"
285"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Brian Curtine9aeca72012-10-29 18:16:39 -0500286"file_name is the name of the file to save registry data to. This file cannot\n"
287" already exist. If this filename includes an extension, it cannot be\n"
288" used on file allocation table (FAT) file systems by the LoadKey(),\n"
289" ReplaceKey() or RestoreKey() methods.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000290"\n"
Brian Curtine9aeca72012-10-29 18:16:39 -0500291"If key represents a key on a remote computer, the path described by file_name\n"
292"is relative to the remote computer.\n"
293"\n"
294"The caller of this method must possess the SeBackupPrivilege security\n"
295"privilege. This function passes NULL for security_attributes to the API.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000296
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000297PyDoc_STRVAR(SetValue_doc,
Brian Curtine9aeca72012-10-29 18:16:39 -0500298"SetValue(key, sub_key, type, value)\n"
299"Associates a value with a specified key.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000300"\n"
301"key is an already open key, or any one of the predefined HKEY_* constants.\n"
302"sub_key is a string that names the subkey with which the value is associated.\n"
Brian Curtine9aeca72012-10-29 18:16:39 -0500303"type is an integer that specifies the type of the data. Currently this must\n"
304" be REG_SZ, meaning only strings are supported.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000305"value is a string that specifies the new value.\n"
306"\n"
307"If the key specified by the sub_key parameter does not exist, the SetValue\n"
308"function creates it.\n"
309"\n"
310"Value lengths are limited by available memory. Long values (more than\n"
311"2048 bytes) should be stored as files with the filenames stored in \n"
312"the configuration registry. This helps the registry perform efficiently.\n"
313"\n"
314"The key identified by the key parameter must have been opened with\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000315"KEY_SET_VALUE access.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000316
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000317PyDoc_STRVAR(SetValueEx_doc,
Brian Curtine9aeca72012-10-29 18:16:39 -0500318"SetValueEx(key, value_name, reserved, type, value)\n"
319"Stores data in the value field of an open registry key.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000320"\n"
321"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Brian Curtine9aeca72012-10-29 18:16:39 -0500322"value_name is a string containing the name of the value to set, or None.\n"
323"reserved can be anything - zero is always passed to the API.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000324"type is an integer that specifies the type of the data. This should be one of:\n"
325" REG_BINARY -- Binary data in any form.\n"
326" REG_DWORD -- A 32-bit number.\n"
327" REG_DWORD_LITTLE_ENDIAN -- A 32-bit number in little-endian format.\n"
328" REG_DWORD_BIG_ENDIAN -- A 32-bit number in big-endian format.\n"
329" REG_EXPAND_SZ -- A null-terminated string that contains unexpanded references\n"
330" to environment variables (for example, %PATH%).\n"
331" REG_LINK -- A Unicode symbolic link.\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000332" REG_MULTI_SZ -- An sequence of null-terminated strings, terminated by\n"
333" two null characters. Note that Python handles this\n"
334" termination automatically.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000335" REG_NONE -- No defined value type.\n"
336" REG_RESOURCE_LIST -- A device-driver resource list.\n"
337" REG_SZ -- A null-terminated string.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000338"value is a string that specifies the new value.\n"
339"\n"
340"This method can also set additional value and type information for the\n"
341"specified key. The key identified by the key parameter must have been\n"
342"opened with KEY_SET_VALUE access.\n"
343"\n"
344"To open the key, use the CreateKeyEx() or OpenKeyEx() methods.\n"
345"\n"
346"Value lengths are limited by available memory. Long values (more than\n"
347"2048 bytes) should be stored as files with the filenames stored in \n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000348"the configuration registry. This helps the registry perform efficiently.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000349
Martin v. Löwisd218dc12008-04-07 03:17:54 +0000350PyDoc_STRVAR(DisableReflectionKey_doc,
Christian Heimes5e696852008-04-09 08:37:03 +0000351"Disables registry reflection for 32-bit processes running on a 64-bit\n"
Martin v. Löwisd218dc12008-04-07 03:17:54 +0000352"Operating System. Will generally raise NotImplemented if executed on\n"
Christian Heimes5e696852008-04-09 08:37:03 +0000353"a 32-bit Operating System.\n"
Brian Curtine9aeca72012-10-29 18:16:39 -0500354"\n"
Martin v. Löwisd218dc12008-04-07 03:17:54 +0000355"If the key is not on the reflection list, the function succeeds but has no effect.\n"
356"Disabling reflection for a key does not affect reflection of any subkeys.");
357
358PyDoc_STRVAR(EnableReflectionKey_doc,
359"Restores registry reflection for the specified disabled key.\n"
Christian Heimes5e696852008-04-09 08:37:03 +0000360"Will generally raise NotImplemented if executed on a 32-bit Operating System.\n"
Martin v. Löwisd218dc12008-04-07 03:17:54 +0000361"Restoring reflection for a key does not affect reflection of any subkeys.");
362
363PyDoc_STRVAR(QueryReflectionKey_doc,
Brian Curtine9aeca72012-10-29 18:16:39 -0500364"QueryReflectionKey(hkey) -> bool\n"
365"Determines the reflection state for the specified key.\n"
Christian Heimes5e696852008-04-09 08:37:03 +0000366"Will generally raise NotImplemented if executed on a 32-bit Operating System.\n");
Martin v. Löwisd218dc12008-04-07 03:17:54 +0000367
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000368/* PyHKEY docstrings */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000369PyDoc_STRVAR(PyHKEY_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000370"PyHKEY Object - A Python object, representing a win32 registry key.\n"
371"\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000372"This object wraps a Windows HKEY object, automatically closing it when\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000373"the object is destroyed. To guarantee cleanup, you can call either\n"
374"the Close() method on the PyHKEY, or the CloseKey() method.\n"
375"\n"
376"All functions which accept a handle object also accept an integer - \n"
377"however, use of the handle object is encouraged.\n"
378"\n"
379"Functions:\n"
380"Close() - Closes the underlying handle.\n"
381"Detach() - Returns the integer Win32 handle, detaching it from the object\n"
382"\n"
383"Properties:\n"
384"handle - The integer Win32 handle.\n"
385"\n"
386"Operations:\n"
Jack Diederich4dafcc42006-11-28 19:15:13 +0000387"__bool__ - Handles with an open object return true, otherwise false.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000388"__int__ - Converting a handle to an integer returns the Win32 handle.\n"
Mark Dickinson211c6252009-02-01 10:28:51 +0000389"rich comparison - Handle objects are compared using the handle value.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000390
391
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000392PyDoc_STRVAR(PyHKEY_Close_doc,
Brian Curtine9aeca72012-10-29 18:16:39 -0500393"key.Close()\n"
394"Closes the underlying Windows handle.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000395"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000396"If the handle is already closed, no error is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000397
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000398PyDoc_STRVAR(PyHKEY_Detach_doc,
Brian Curtine9aeca72012-10-29 18:16:39 -0500399"key.Detach() -> int\n"
400"Detaches the Windows handle from the handle object.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000401"\n"
402"The result is the value of the handle before it is detached. If the\n"
403"handle is already detached, this will return zero.\n"
404"\n"
405"After calling this function, the handle is effectively invalidated,\n"
406"but the handle is not closed. You would call this function when you\n"
407"need the underlying win32 handle to exist beyond the lifetime of the\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000408"handle object.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000409"On 64 bit windows, the result of this function is a long integer");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000410
411
412/************************************************************************
413
414 The PyHKEY object definition
415
416************************************************************************/
417typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 PyObject_VAR_HEAD
419 HKEY hkey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000420} PyHKEYObject;
421
422#define PyHKEY_Check(op) ((op)->ob_type == &PyHKEY_Type)
423
424static char *failMsg = "bad operand type";
425
426static PyObject *
427PyHKEY_unaryFailureFunc(PyObject *ob)
428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 PyErr_SetString(PyExc_TypeError, failMsg);
430 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000431}
432static PyObject *
433PyHKEY_binaryFailureFunc(PyObject *ob1, PyObject *ob2)
434{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 PyErr_SetString(PyExc_TypeError, failMsg);
436 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000437}
438static PyObject *
439PyHKEY_ternaryFailureFunc(PyObject *ob1, PyObject *ob2, PyObject *ob3)
440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 PyErr_SetString(PyExc_TypeError, failMsg);
442 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000443}
444
445static void
446PyHKEY_deallocFunc(PyObject *ob)
447{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 /* Can not call PyHKEY_Close, as the ob->tp_type
449 has already been cleared, thus causing the type
450 check to fail!
451 */
452 PyHKEYObject *obkey = (PyHKEYObject *)ob;
453 if (obkey->hkey)
454 RegCloseKey((HKEY)obkey->hkey);
455 PyObject_DEL(ob);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000456}
457
458static int
Jack Diederich4dafcc42006-11-28 19:15:13 +0000459PyHKEY_boolFunc(PyObject *ob)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000460{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 return ((PyHKEYObject *)ob)->hkey != 0;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000462}
463
464static PyObject *
465PyHKEY_intFunc(PyObject *ob)
466{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
468 return PyLong_FromVoidPtr(pyhkey->hkey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000469}
470
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000471static PyObject *
472PyHKEY_strFunc(PyObject *ob)
473{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
475 return PyUnicode_FromFormat("<PyHKEY:%p>", pyhkey->hkey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000476}
477
478static int
479PyHKEY_compareFunc(PyObject *ob1, PyObject *ob2)
480{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 PyHKEYObject *pyhkey1 = (PyHKEYObject *)ob1;
482 PyHKEYObject *pyhkey2 = (PyHKEYObject *)ob2;
483 return pyhkey1 == pyhkey2 ? 0 :
484 (pyhkey1 < pyhkey2 ? -1 : 1);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000485}
486
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000487static Py_hash_t
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000488PyHKEY_hashFunc(PyObject *ob)
489{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 /* Just use the address.
491 XXX - should we use the handle value?
492 */
493 return _Py_HashPointer(ob);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000494}
495
496
497static PyNumberMethods PyHKEY_NumberMethods =
498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 PyHKEY_binaryFailureFunc, /* nb_add */
500 PyHKEY_binaryFailureFunc, /* nb_subtract */
501 PyHKEY_binaryFailureFunc, /* nb_multiply */
502 PyHKEY_binaryFailureFunc, /* nb_remainder */
503 PyHKEY_binaryFailureFunc, /* nb_divmod */
504 PyHKEY_ternaryFailureFunc, /* nb_power */
505 PyHKEY_unaryFailureFunc, /* nb_negative */
506 PyHKEY_unaryFailureFunc, /* nb_positive */
507 PyHKEY_unaryFailureFunc, /* nb_absolute */
508 PyHKEY_boolFunc, /* nb_bool */
509 PyHKEY_unaryFailureFunc, /* nb_invert */
510 PyHKEY_binaryFailureFunc, /* nb_lshift */
511 PyHKEY_binaryFailureFunc, /* nb_rshift */
512 PyHKEY_binaryFailureFunc, /* nb_and */
513 PyHKEY_binaryFailureFunc, /* nb_xor */
514 PyHKEY_binaryFailureFunc, /* nb_or */
515 PyHKEY_intFunc, /* nb_int */
516 0, /* nb_reserved */
517 PyHKEY_unaryFailureFunc, /* nb_float */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000518};
519
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000520static PyObject *PyHKEY_CloseMethod(PyObject *self, PyObject *args);
521static PyObject *PyHKEY_DetachMethod(PyObject *self, PyObject *args);
522static PyObject *PyHKEY_Enter(PyObject *self);
523static PyObject *PyHKEY_Exit(PyObject *self, PyObject *args);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000524
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000525static struct PyMethodDef PyHKEY_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 {"Close", PyHKEY_CloseMethod, METH_VARARGS, PyHKEY_Close_doc},
527 {"Detach", PyHKEY_DetachMethod, METH_VARARGS, PyHKEY_Detach_doc},
528 {"__enter__", (PyCFunction)PyHKEY_Enter, METH_NOARGS, NULL},
529 {"__exit__", PyHKEY_Exit, METH_VARARGS, NULL},
530 {NULL}
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000531};
532
533#define OFF(e) offsetof(PyHKEYObject, e)
534static PyMemberDef PyHKEY_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 {"handle", T_INT, OFF(hkey), READONLY},
536 {NULL} /* Sentinel */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000537};
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000538
539/* The type itself */
540PyTypeObject PyHKEY_Type =
541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 PyVarObject_HEAD_INIT(0, 0) /* fill in type at module init */
543 "PyHKEY",
544 sizeof(PyHKEYObject),
545 0,
546 PyHKEY_deallocFunc, /* tp_dealloc */
547 0, /* tp_print */
548 0, /* tp_getattr */
549 0, /* tp_setattr */
550 0, /* tp_reserved */
551 0, /* tp_repr */
552 &PyHKEY_NumberMethods, /* tp_as_number */
553 0, /* tp_as_sequence */
554 0, /* tp_as_mapping */
555 PyHKEY_hashFunc, /* tp_hash */
556 0, /* tp_call */
557 PyHKEY_strFunc, /* tp_str */
558 0, /* tp_getattro */
559 0, /* tp_setattro */
560 0, /* tp_as_buffer */
561 0, /* tp_flags */
562 PyHKEY_doc, /* tp_doc */
563 0, /*tp_traverse*/
564 0, /*tp_clear*/
565 0, /*tp_richcompare*/
566 0, /*tp_weaklistoffset*/
567 0, /*tp_iter*/
568 0, /*tp_iternext*/
569 PyHKEY_methods, /*tp_methods*/
570 PyHKEY_memberlist, /*tp_members*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000571};
572
573/************************************************************************
574
575 The PyHKEY object methods
576
577************************************************************************/
578static PyObject *
579PyHKEY_CloseMethod(PyObject *self, PyObject *args)
580{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 if (!PyArg_ParseTuple(args, ":Close"))
582 return NULL;
583 if (!PyHKEY_Close(self))
584 return NULL;
585 Py_INCREF(Py_None);
586 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000587}
588
589static PyObject *
590PyHKEY_DetachMethod(PyObject *self, PyObject *args)
591{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 void* ret;
593 PyHKEYObject *pThis = (PyHKEYObject *)self;
594 if (!PyArg_ParseTuple(args, ":Detach"))
595 return NULL;
596 ret = (void*)pThis->hkey;
597 pThis->hkey = 0;
598 return PyLong_FromVoidPtr(ret);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000599}
600
Christian Heimes2380ac72008-01-09 00:17:24 +0000601static PyObject *
602PyHKEY_Enter(PyObject *self)
603{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 Py_XINCREF(self);
605 return self;
Christian Heimes2380ac72008-01-09 00:17:24 +0000606}
607
608static PyObject *
609PyHKEY_Exit(PyObject *self, PyObject *args)
610{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 if (!PyHKEY_Close(self))
612 return NULL;
613 Py_RETURN_NONE;
Christian Heimes2380ac72008-01-09 00:17:24 +0000614}
615
616
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000617/************************************************************************
618 The public PyHKEY API (well, not public yet :-)
619************************************************************************/
620PyObject *
621PyHKEY_New(HKEY hInit)
622{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 PyHKEYObject *key = PyObject_NEW(PyHKEYObject, &PyHKEY_Type);
624 if (key)
625 key->hkey = hInit;
626 return (PyObject *)key;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000627}
628
629BOOL
630PyHKEY_Close(PyObject *ob_handle)
631{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 LONG rc;
633 PyHKEYObject *key;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 if (!PyHKEY_Check(ob_handle)) {
636 PyErr_SetString(PyExc_TypeError, "bad operand type");
637 return FALSE;
638 }
639 key = (PyHKEYObject *)ob_handle;
640 rc = key->hkey ? RegCloseKey((HKEY)key->hkey) : ERROR_SUCCESS;
641 key->hkey = 0;
642 if (rc != ERROR_SUCCESS)
643 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
644 return rc == ERROR_SUCCESS;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000645}
646
647BOOL
648PyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK)
649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 if (ob == Py_None) {
651 if (!bNoneOK) {
652 PyErr_SetString(
653 PyExc_TypeError,
654 "None is not a valid HKEY in this context");
655 return FALSE;
656 }
657 *pHANDLE = (HKEY)0;
658 }
659 else if (PyHKEY_Check(ob)) {
660 PyHKEYObject *pH = (PyHKEYObject *)ob;
661 *pHANDLE = pH->hkey;
662 }
663 else if (PyLong_Check(ob)) {
664 /* We also support integers */
665 PyErr_Clear();
666 *pHANDLE = (HKEY)PyLong_AsVoidPtr(ob);
667 if (PyErr_Occurred())
668 return FALSE;
669 }
670 else {
671 PyErr_SetString(
672 PyExc_TypeError,
673 "The object is not a PyHKEY object");
674 return FALSE;
675 }
676 return TRUE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000677}
678
679PyObject *
680PyHKEY_FromHKEY(HKEY h)
681{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 PyHKEYObject *op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 /* Inline PyObject_New */
685 op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject));
686 if (op == NULL)
687 return PyErr_NoMemory();
688 PyObject_INIT(op, &PyHKEY_Type);
689 op->hkey = h;
690 return (PyObject *)op;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000691}
692
693
694/************************************************************************
695 The module methods
696************************************************************************/
697BOOL
698PyWinObject_CloseHKEY(PyObject *obHandle)
699{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 BOOL ok;
701 if (PyHKEY_Check(obHandle)) {
702 ok = PyHKEY_Close(obHandle);
703 }
Fred Drake25e17262000-06-30 17:48:51 +0000704#if SIZEOF_LONG >= SIZEOF_HKEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 else if (PyLong_Check(obHandle)) {
706 long rc = RegCloseKey((HKEY)PyLong_AsLong(obHandle));
707 ok = (rc == ERROR_SUCCESS);
708 if (!ok)
709 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
710 }
Fred Drake25e17262000-06-30 17:48:51 +0000711#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 else if (PyLong_Check(obHandle)) {
713 long rc = RegCloseKey((HKEY)PyLong_AsVoidPtr(obHandle));
714 ok = (rc == ERROR_SUCCESS);
715 if (!ok)
716 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
717 }
Fred Drake25e17262000-06-30 17:48:51 +0000718#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 else {
720 PyErr_SetString(
721 PyExc_TypeError,
722 "A handle must be a HKEY object or an integer");
723 return FALSE;
724 }
725 return ok;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000726}
727
728
729/*
730 Private Helper functions for the registry interfaces
731
732** Note that fixupMultiSZ and countString have both had changes
733** made to support "incorrect strings". The registry specification
734** calls for strings to be terminated with 2 null bytes. It seems
Thomas Wouters7e474022000-07-16 12:04:32 +0000735** some commercial packages install strings which dont conform,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000736** causing this code to fail - however, "regedit" etc still work
737** with these strings (ie only we dont!).
738*/
739static void
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000740fixupMultiSZ(wchar_t **str, wchar_t *data, int len)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 wchar_t *P;
743 int i;
744 wchar_t *Q;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 Q = data + len;
747 for (P = data, i = 0; P < Q && *P != '\0'; P++, i++) {
748 str[i] = P;
749 for(; *P != '\0'; P++)
750 ;
751 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000752}
753
754static int
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000755countStrings(wchar_t *data, int len)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000756{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 int strings;
758 wchar_t *P;
759 wchar_t *Q = data + len;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 for (P = data, strings = 0; P < Q && *P != '\0'; P++, strings++)
762 for (; P < Q && *P != '\0'; P++)
763 ;
764 return strings;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000765}
766
767/* Convert PyObject into Registry data.
768 Allocates space as needed. */
769static BOOL
770Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
771{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 Py_ssize_t i,j;
773 switch (typ) {
774 case REG_DWORD:
775 if (value != Py_None && !PyLong_Check(value))
776 return FALSE;
777 *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1);
778 if (*retDataBuf==NULL){
779 PyErr_NoMemory();
780 return FALSE;
781 }
782 *retDataSize = sizeof(DWORD);
783 if (value == Py_None) {
784 DWORD zero = 0;
785 memcpy(*retDataBuf, &zero, sizeof(DWORD));
786 }
787 else {
Brian Curtin12706f22012-12-27 10:12:45 -0600788 DWORD d = PyLong_AsUnsignedLong(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 memcpy(*retDataBuf, &d, sizeof(DWORD));
790 }
791 break;
792 case REG_SZ:
793 case REG_EXPAND_SZ:
794 {
Victor Stinnerbe492442011-11-21 12:43:50 +0100795 if (value != Py_None) {
796 Py_ssize_t len;
797 if (!PyUnicode_Check(value))
798 return FALSE;
799 *retDataBuf = (BYTE*)PyUnicode_AsWideCharString(value, &len);
800 if (*retDataBuf == NULL)
801 return FALSE;
802 *retDataSize = Py_SAFE_DOWNCAST(
803 (len + 1) * sizeof(wchar_t),
804 Py_ssize_t, DWORD);
805 }
806 else {
807 *retDataBuf = (BYTE *)PyMem_NEW(wchar_t, 1);
808 if (*retDataBuf == NULL) {
809 PyErr_NoMemory();
810 return FALSE;
811 }
812 ((wchar_t *)*retDataBuf)[0] = L'\0';
813 *retDataSize = 1 * sizeof(wchar_t);
814 }
815 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 }
817 case REG_MULTI_SZ:
818 {
819 DWORD size = 0;
820 wchar_t *P;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 if (value == Py_None)
823 i = 0;
824 else {
825 if (!PyList_Check(value))
826 return FALSE;
827 i = PyList_Size(value);
828 }
829 for (j = 0; j < i; j++)
830 {
831 PyObject *t;
Victor Stinnerbe492442011-11-21 12:43:50 +0100832 wchar_t *wstr;
833 Py_ssize_t len;
834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 t = PyList_GET_ITEM(value, j);
836 if (!PyUnicode_Check(t))
837 return FALSE;
Victor Stinnerbe492442011-11-21 12:43:50 +0100838 wstr = PyUnicode_AsUnicodeAndSize(t, &len);
839 if (wstr == NULL)
840 return FALSE;
841 size += Py_SAFE_DOWNCAST((len + 1) * sizeof(wchar_t),
Brian Curtinabb33512010-08-17 20:08:40 +0000842 size_t, DWORD);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 *retDataSize = size + 2;
846 *retDataBuf = (BYTE *)PyMem_NEW(char,
847 *retDataSize);
848 if (*retDataBuf==NULL){
849 PyErr_NoMemory();
850 return FALSE;
851 }
852 P = (wchar_t *)*retDataBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 for (j = 0; j < i; j++)
855 {
856 PyObject *t;
Victor Stinnerbe492442011-11-21 12:43:50 +0100857 wchar_t *wstr;
858 Py_ssize_t len;
859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 t = PyList_GET_ITEM(value, j);
Victor Stinnerbe492442011-11-21 12:43:50 +0100861 wstr = PyUnicode_AsUnicodeAndSize(t, &len);
862 if (wstr == NULL)
863 return FALSE;
864 wcscpy(P, wstr);
865 P += (len + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 }
867 /* And doubly-terminate the list... */
868 *P = '\0';
869 break;
870 }
871 case REG_BINARY:
872 /* ALSO handle ALL unknown data types here. Even if we can't
873 support it natively, we should handle the bits. */
874 default:
875 if (value == Py_None)
876 *retDataSize = 0;
877 else {
878 Py_buffer view;
Neal Norwitz1385b892007-08-26 23:07:13 +0000879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 if (!PyObject_CheckBuffer(value)) {
881 PyErr_Format(PyExc_TypeError,
882 "Objects of type '%s' can not "
883 "be used as binary registry values",
884 value->ob_type->tp_name);
885 return FALSE;
886 }
Neal Norwitz1385b892007-08-26 23:07:13 +0000887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0)
889 return FALSE;
Neal Norwitz1385b892007-08-26 23:07:13 +0000890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 *retDataBuf = (BYTE *)PyMem_NEW(char, view.len);
892 if (*retDataBuf==NULL){
893 PyBuffer_Release(&view);
894 PyErr_NoMemory();
895 return FALSE;
896 }
Brian Curtinabb33512010-08-17 20:08:40 +0000897 *retDataSize = Py_SAFE_DOWNCAST(view.len, Py_ssize_t, DWORD);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 memcpy(*retDataBuf, view.buf, view.len);
899 PyBuffer_Release(&view);
900 }
901 break;
902 }
903 return TRUE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000904}
905
906/* Convert Registry data into PyObject*/
907static PyObject *
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000908Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000909{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 PyObject *obData;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 switch (typ) {
913 case REG_DWORD:
914 if (retDataSize == 0)
Brian Curtin172e4222012-12-27 14:04:42 -0600915 obData = PyLong_FromUnsignedLong(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 else
Brian Curtin172e4222012-12-27 14:04:42 -0600917 obData = PyLong_FromUnsignedLong(*(int *)retDataBuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 break;
919 case REG_SZ:
920 case REG_EXPAND_SZ:
921 {
922 /* the buffer may or may not have a trailing NULL */
923 wchar_t *data = (wchar_t *)retDataBuf;
924 int len = retDataSize / 2;
925 if (retDataSize && data[len-1] == '\0')
926 retDataSize -= 2;
927 if (retDataSize <= 0)
928 data = L"";
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200929 obData = PyUnicode_FromWideChar(data, retDataSize/2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 break;
931 }
932 case REG_MULTI_SZ:
933 if (retDataSize == 0)
934 obData = PyList_New(0);
935 else
936 {
937 int index = 0;
938 wchar_t *data = (wchar_t *)retDataBuf;
939 int len = retDataSize / 2;
940 int s = countStrings(data, len);
941 wchar_t **str = (wchar_t **)malloc(sizeof(wchar_t *)*s);
942 if (str == NULL)
943 return PyErr_NoMemory();
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 fixupMultiSZ(str, data, len);
946 obData = PyList_New(s);
947 if (obData == NULL)
948 return NULL;
949 for (index = 0; index < s; index++)
950 {
951 size_t len = wcslen(str[index]);
952 if (len > INT_MAX) {
953 PyErr_SetString(PyExc_OverflowError,
954 "registry string is too long for a Python string");
955 Py_DECREF(obData);
956 return NULL;
957 }
958 PyList_SetItem(obData,
959 index,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200960 PyUnicode_FromWideChar(str[index], len));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 }
962 free(str);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 break;
965 }
966 case REG_BINARY:
967 /* ALSO handle ALL unknown data types here. Even if we can't
968 support it natively, we should handle the bits. */
969 default:
970 if (retDataSize == 0) {
971 Py_INCREF(Py_None);
972 obData = Py_None;
973 }
974 else
975 obData = PyBytes_FromStringAndSize(
976 (char *)retDataBuf, retDataSize);
977 break;
978 }
979 return obData;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000980}
981
982/* The Python methods */
983
984static PyObject *
985PyCloseKey(PyObject *self, PyObject *args)
986{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 PyObject *obKey;
988 if (!PyArg_ParseTuple(args, "O:CloseKey", &obKey))
989 return NULL;
990 if (!PyHKEY_Close(obKey))
991 return NULL;
992 Py_INCREF(Py_None);
993 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000994}
995
996static PyObject *
997PyConnectRegistry(PyObject *self, PyObject *args)
998{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 HKEY hKey;
1000 PyObject *obKey;
1001 wchar_t *szCompName = NULL;
1002 HKEY retKey;
1003 long rc;
1004 if (!PyArg_ParseTuple(args, "ZO:ConnectRegistry", &szCompName, &obKey))
1005 return NULL;
1006 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1007 return NULL;
1008 Py_BEGIN_ALLOW_THREADS
1009 rc = RegConnectRegistryW(szCompName, hKey, &retKey);
1010 Py_END_ALLOW_THREADS
1011 if (rc != ERROR_SUCCESS)
1012 return PyErr_SetFromWindowsErrWithFunction(rc,
1013 "ConnectRegistry");
1014 return PyHKEY_FromHKEY(retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001015}
1016
1017static PyObject *
1018PyCreateKey(PyObject *self, PyObject *args)
1019{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 HKEY hKey;
1021 PyObject *obKey;
1022 wchar_t *subKey;
1023 HKEY retKey;
1024 long rc;
1025 if (!PyArg_ParseTuple(args, "OZ:CreateKey", &obKey, &subKey))
1026 return NULL;
1027 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1028 return NULL;
1029 rc = RegCreateKeyW(hKey, subKey, &retKey);
1030 if (rc != ERROR_SUCCESS)
1031 return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey");
1032 return PyHKEY_FromHKEY(retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001033}
1034
1035static PyObject *
Brian Curtin1771b542010-09-27 17:56:36 +00001036PyCreateKeyEx(PyObject *self, PyObject *args, PyObject *kwargs)
Brian Curtin3035c392010-04-21 23:56:21 +00001037{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 HKEY hKey;
Brian Curtin1771b542010-09-27 17:56:36 +00001039 PyObject *key;
1040 wchar_t *sub_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 HKEY retKey;
Brian Curtin1771b542010-09-27 17:56:36 +00001042 int reserved = 0;
1043 REGSAM access = KEY_WRITE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 long rc;
Brian Curtin1771b542010-09-27 17:56:36 +00001045
1046 char *kwlist[] = {"key", "sub_key", "reserved", "access", NULL};
1047
1048 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OZ|ii:CreateKeyEx", kwlist,
1049 &key, &sub_key, &reserved, &access))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 return NULL;
Brian Curtin1771b542010-09-27 17:56:36 +00001051 if (!PyHKEY_AsHKEY(key, &hKey, FALSE))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 return NULL;
Brian Curtin3035c392010-04-21 23:56:21 +00001053
Brian Curtin1771b542010-09-27 17:56:36 +00001054 rc = RegCreateKeyExW(hKey, sub_key, reserved, NULL, (DWORD)NULL,
1055 access, NULL, &retKey, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 if (rc != ERROR_SUCCESS)
1057 return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKeyEx");
1058 return PyHKEY_FromHKEY(retKey);
Brian Curtin3035c392010-04-21 23:56:21 +00001059}
1060
1061static PyObject *
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001062PyDeleteKey(PyObject *self, PyObject *args)
1063{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 HKEY hKey;
1065 PyObject *obKey;
1066 wchar_t *subKey;
1067 long rc;
1068 if (!PyArg_ParseTuple(args, "Ou:DeleteKey", &obKey, &subKey))
1069 return NULL;
1070 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1071 return NULL;
1072 rc = RegDeleteKeyW(hKey, subKey );
1073 if (rc != ERROR_SUCCESS)
1074 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey");
1075 Py_INCREF(Py_None);
1076 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001077}
1078
1079static PyObject *
Brian Curtin1771b542010-09-27 17:56:36 +00001080PyDeleteKeyEx(PyObject *self, PyObject *args, PyObject *kwargs)
Brian Curtin3035c392010-04-21 23:56:21 +00001081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 HKEY hKey;
Brian Curtin1771b542010-09-27 17:56:36 +00001083 PyObject *key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 HMODULE hMod;
1085 typedef LONG (WINAPI *RDKEFunc)(HKEY, const wchar_t*, REGSAM, int);
1086 RDKEFunc pfn = NULL;
Brian Curtin1771b542010-09-27 17:56:36 +00001087 wchar_t *sub_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 long rc;
Brian Curtin1771b542010-09-27 17:56:36 +00001089 int reserved = 0;
1090 REGSAM access = KEY_WOW64_64KEY;
Brian Curtin3035c392010-04-21 23:56:21 +00001091
Brian Curtin1771b542010-09-27 17:56:36 +00001092 char *kwlist[] = {"key", "sub_key", "access", "reserved", NULL};
1093 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Ou|ii:DeleteKeyEx", kwlist,
1094 &key, &sub_key, &access, &reserved))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 return NULL;
Brian Curtin1771b542010-09-27 17:56:36 +00001096 if (!PyHKEY_AsHKEY(key, &hKey, FALSE))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 return NULL;
Brian Curtin3035c392010-04-21 23:56:21 +00001098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 /* Only available on 64bit platforms, so we must load it
1100 dynamically. */
Martin v. Löwis50590f12012-01-14 17:54:09 +01001101 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 if (hMod)
1103 pfn = (RDKEFunc)GetProcAddress(hMod,
1104 "RegDeleteKeyExW");
1105 if (!pfn) {
1106 PyErr_SetString(PyExc_NotImplementedError,
1107 "not implemented on this platform");
1108 return NULL;
1109 }
1110 Py_BEGIN_ALLOW_THREADS
Brian Curtin1771b542010-09-27 17:56:36 +00001111 rc = (*pfn)(hKey, sub_key, access, reserved);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 Py_END_ALLOW_THREADS
Brian Curtin3035c392010-04-21 23:56:21 +00001113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 if (rc != ERROR_SUCCESS)
1115 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKeyEx");
1116 Py_INCREF(Py_None);
1117 return Py_None;
Brian Curtin3035c392010-04-21 23:56:21 +00001118}
1119
1120static PyObject *
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001121PyDeleteValue(PyObject *self, PyObject *args)
1122{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 HKEY hKey;
1124 PyObject *obKey;
1125 wchar_t *subKey;
1126 long rc;
1127 if (!PyArg_ParseTuple(args, "OZ:DeleteValue", &obKey, &subKey))
1128 return NULL;
1129 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1130 return NULL;
1131 Py_BEGIN_ALLOW_THREADS
1132 rc = RegDeleteValueW(hKey, subKey);
1133 Py_END_ALLOW_THREADS
1134 if (rc !=ERROR_SUCCESS)
1135 return PyErr_SetFromWindowsErrWithFunction(rc,
1136 "RegDeleteValue");
1137 Py_INCREF(Py_None);
1138 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001139}
1140
1141static PyObject *
1142PyEnumKey(PyObject *self, PyObject *args)
1143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 HKEY hKey;
1145 PyObject *obKey;
1146 int index;
1147 long rc;
1148 PyObject *retStr;
Brian Curtin60853212010-05-26 17:43:50 +00001149
1150 /* The Windows docs claim that the max key name length is 255
1151 * characters, plus a terminating nul character. However,
1152 * empirical testing demonstrates that it is possible to
1153 * create a 256 character key that is missing the terminating
1154 * nul. RegEnumKeyEx requires a 257 character buffer to
1155 * retrieve such a key name. */
1156 wchar_t tmpbuf[257];
Kristján Valur Jónsson984dfa72012-04-02 15:23:29 +00001157 DWORD len = sizeof(tmpbuf)/sizeof(wchar_t); /* includes NULL terminator */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 if (!PyArg_ParseTuple(args, "Oi:EnumKey", &obKey, &index))
1160 return NULL;
1161 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1162 return NULL;
Tim Peters313fcd42006-02-19 04:05:39 +00001163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 Py_BEGIN_ALLOW_THREADS
1165 rc = RegEnumKeyExW(hKey, index, tmpbuf, &len, NULL, NULL, NULL, NULL);
1166 Py_END_ALLOW_THREADS
1167 if (rc != ERROR_SUCCESS)
1168 return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx");
Tim Peters313fcd42006-02-19 04:05:39 +00001169
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001170 retStr = PyUnicode_FromWideChar(tmpbuf, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 return retStr; /* can be NULL */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001172}
1173
1174static PyObject *
1175PyEnumValue(PyObject *self, PyObject *args)
1176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 HKEY hKey;
1178 PyObject *obKey;
1179 int index;
1180 long rc;
1181 wchar_t *retValueBuf;
Amaury Forgeot d'Arcf2b69df2010-08-16 22:11:29 +00001182 BYTE *tmpBuf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 BYTE *retDataBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001184 DWORD retValueSize, bufValueSize;
1185 DWORD retDataSize, bufDataSize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 DWORD typ;
1187 PyObject *obData;
1188 PyObject *retVal;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 if (!PyArg_ParseTuple(args, "Oi:EnumValue", &obKey, &index))
1191 return NULL;
1192 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1193 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 if ((rc = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, NULL, NULL,
1196 NULL,
1197 &retValueSize, &retDataSize, NULL, NULL))
1198 != ERROR_SUCCESS)
1199 return PyErr_SetFromWindowsErrWithFunction(rc,
1200 "RegQueryInfoKey");
1201 ++retValueSize; /* include null terminators */
1202 ++retDataSize;
Brian Curtin60853212010-05-26 17:43:50 +00001203 bufDataSize = retDataSize;
1204 bufValueSize = retValueSize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 retValueBuf = (wchar_t *)PyMem_Malloc(sizeof(wchar_t) * retValueSize);
1206 if (retValueBuf == NULL)
1207 return PyErr_NoMemory();
1208 retDataBuf = (BYTE *)PyMem_Malloc(retDataSize);
1209 if (retDataBuf == NULL) {
1210 PyMem_Free(retValueBuf);
1211 return PyErr_NoMemory();
1212 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001213
Brian Curtin60853212010-05-26 17:43:50 +00001214 while (1) {
Brian Curtin60853212010-05-26 17:43:50 +00001215 Py_BEGIN_ALLOW_THREADS
1216 rc = RegEnumValueW(hKey,
1217 index,
1218 retValueBuf,
1219 &retValueSize,
1220 NULL,
1221 &typ,
1222 (BYTE *)retDataBuf,
1223 &retDataSize);
1224 Py_END_ALLOW_THREADS
1225
1226 if (rc != ERROR_MORE_DATA)
1227 break;
1228
1229 bufDataSize *= 2;
Amaury Forgeot d'Arcf2b69df2010-08-16 22:11:29 +00001230 tmpBuf = (BYTE *)PyMem_Realloc(retDataBuf, bufDataSize);
Brian Curtin9b7e2d12010-06-08 20:57:52 +00001231 if (tmpBuf == NULL) {
Brian Curtin60853212010-05-26 17:43:50 +00001232 PyErr_NoMemory();
1233 retVal = NULL;
1234 goto fail;
1235 }
Brian Curtin9b7e2d12010-06-08 20:57:52 +00001236 retDataBuf = tmpBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001237 retDataSize = bufDataSize;
1238 retValueSize = bufValueSize;
1239 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 if (rc != ERROR_SUCCESS) {
1242 retVal = PyErr_SetFromWindowsErrWithFunction(rc,
1243 "PyRegEnumValue");
1244 goto fail;
1245 }
1246 obData = Reg2Py(retDataBuf, retDataSize, typ);
1247 if (obData == NULL) {
1248 retVal = NULL;
1249 goto fail;
1250 }
1251 retVal = Py_BuildValue("uOi", retValueBuf, obData, typ);
1252 Py_DECREF(obData);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001253 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 PyMem_Free(retValueBuf);
1255 PyMem_Free(retDataBuf);
1256 return retVal;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001257}
1258
1259static PyObject *
Christian Heimes2380ac72008-01-09 00:17:24 +00001260PyExpandEnvironmentStrings(PyObject *self, PyObject *args)
1261{
Victor Stinner9d3b93b2011-11-22 02:27:30 +01001262 wchar_t *retValue = NULL;
1263 wchar_t *src;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 DWORD retValueSize;
1265 DWORD rc;
1266 PyObject *o;
Christian Heimes2380ac72008-01-09 00:17:24 +00001267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 if (!PyArg_ParseTuple(args, "u:ExpandEnvironmentStrings", &src))
1269 return NULL;
Christian Heimes2380ac72008-01-09 00:17:24 +00001270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 retValueSize = ExpandEnvironmentStringsW(src, retValue, 0);
1272 if (retValueSize == 0) {
1273 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1274 "ExpandEnvironmentStrings");
1275 }
Victor Stinner9d3b93b2011-11-22 02:27:30 +01001276 retValue = (wchar_t *)PyMem_Malloc(retValueSize * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 if (retValue == NULL) {
1278 return PyErr_NoMemory();
1279 }
Christian Heimes2380ac72008-01-09 00:17:24 +00001280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 rc = ExpandEnvironmentStringsW(src, retValue, retValueSize);
1282 if (rc == 0) {
1283 PyMem_Free(retValue);
1284 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1285 "ExpandEnvironmentStrings");
1286 }
Victor Stinner9d3b93b2011-11-22 02:27:30 +01001287 o = PyUnicode_FromWideChar(retValue, wcslen(retValue));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 PyMem_Free(retValue);
1289 return o;
Christian Heimes2380ac72008-01-09 00:17:24 +00001290}
1291
1292static PyObject *
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001293PyFlushKey(PyObject *self, PyObject *args)
1294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 HKEY hKey;
1296 PyObject *obKey;
1297 long rc;
1298 if (!PyArg_ParseTuple(args, "O:FlushKey", &obKey))
1299 return NULL;
1300 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1301 return NULL;
1302 Py_BEGIN_ALLOW_THREADS
1303 rc = RegFlushKey(hKey);
1304 Py_END_ALLOW_THREADS
1305 if (rc != ERROR_SUCCESS)
1306 return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey");
1307 Py_INCREF(Py_None);
1308 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001309}
1310static PyObject *
1311PyLoadKey(PyObject *self, PyObject *args)
1312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 HKEY hKey;
1314 PyObject *obKey;
1315 wchar_t *subKey;
1316 wchar_t *fileName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 long rc;
1319 if (!PyArg_ParseTuple(args, "Ouu:LoadKey", &obKey, &subKey, &fileName))
1320 return NULL;
1321 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1322 return NULL;
1323 Py_BEGIN_ALLOW_THREADS
1324 rc = RegLoadKeyW(hKey, subKey, fileName );
1325 Py_END_ALLOW_THREADS
1326 if (rc != ERROR_SUCCESS)
1327 return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey");
1328 Py_INCREF(Py_None);
1329 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001330}
1331
1332static PyObject *
Brian Curtin1771b542010-09-27 17:56:36 +00001333PyOpenKey(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001334{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 HKEY hKey;
Brian Curtin1771b542010-09-27 17:56:36 +00001336 PyObject *key;
1337 wchar_t *sub_key;
1338 int reserved = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 HKEY retKey;
1340 long rc;
Brian Curtin1771b542010-09-27 17:56:36 +00001341 REGSAM access = KEY_READ;
1342
1343 char *kwlist[] = {"key", "sub_key", "reserved", "access", NULL};
1344
1345 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OZ|ii:OpenKey", kwlist,
1346 &key, &sub_key, &reserved, &access))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 return NULL;
Brian Curtin1771b542010-09-27 17:56:36 +00001348 if (!PyHKEY_AsHKEY(key, &hKey, FALSE))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 Py_BEGIN_ALLOW_THREADS
Brian Curtin1771b542010-09-27 17:56:36 +00001352 rc = RegOpenKeyExW(hKey, sub_key, reserved, access, &retKey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 Py_END_ALLOW_THREADS
1354 if (rc != ERROR_SUCCESS)
1355 return PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx");
1356 return PyHKEY_FromHKEY(retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001357}
1358
1359
1360static PyObject *
1361PyQueryInfoKey(PyObject *self, PyObject *args)
1362{
1363 HKEY hKey;
1364 PyObject *obKey;
1365 long rc;
1366 DWORD nSubKeys, nValues;
1367 FILETIME ft;
1368 LARGE_INTEGER li;
1369 PyObject *l;
1370 PyObject *ret;
1371 if (!PyArg_ParseTuple(args, "O:QueryInfoKey", &obKey))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001373 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001375 if ((rc = RegQueryInfoKey(hKey, NULL, NULL, 0, &nSubKeys, NULL, NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 &nValues, NULL, NULL, NULL, &ft))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001377 != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey");
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001379 li.LowPart = ft.dwLowDateTime;
1380 li.HighPart = ft.dwHighDateTime;
1381 l = PyLong_FromLongLong(li.QuadPart);
1382 if (l == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001384 ret = Py_BuildValue("iiO", nSubKeys, nValues, l);
1385 Py_DECREF(l);
1386 return ret;
1387}
1388
1389static PyObject *
1390PyQueryValue(PyObject *self, PyObject *args)
1391{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 HKEY hKey;
1393 PyObject *obKey;
1394 wchar_t *subKey;
1395 long rc;
1396 PyObject *retStr;
1397 wchar_t *retBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001398 DWORD bufSize = 0;
1399 DWORD retSize = 0;
1400 wchar_t *tmp;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 if (!PyArg_ParseTuple(args, "OZ:QueryValue", &obKey, &subKey))
1403 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1406 return NULL;
Brian Curtin60853212010-05-26 17:43:50 +00001407
1408 rc = RegQueryValueW(hKey, subKey, NULL, &retSize);
1409 if (rc == ERROR_MORE_DATA)
1410 retSize = 256;
1411 else if (rc != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 return PyErr_SetFromWindowsErrWithFunction(rc,
1413 "RegQueryValue");
Brian Curtin60853212010-05-26 17:43:50 +00001414
1415 bufSize = retSize;
1416 retBuf = (wchar_t *) PyMem_Malloc(bufSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 if (retBuf == NULL)
1418 return PyErr_NoMemory();
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001419
Brian Curtin60853212010-05-26 17:43:50 +00001420 while (1) {
1421 retSize = bufSize;
1422 rc = RegQueryValueW(hKey, subKey, retBuf, &retSize);
1423 if (rc != ERROR_MORE_DATA)
1424 break;
1425
1426 bufSize *= 2;
1427 tmp = (wchar_t *) PyMem_Realloc(retBuf, bufSize);
1428 if (tmp == NULL) {
1429 PyMem_Free(retBuf);
1430 return PyErr_NoMemory();
1431 }
1432 retBuf = tmp;
1433 }
1434
1435 if (rc != ERROR_SUCCESS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 PyMem_Free(retBuf);
1437 return PyErr_SetFromWindowsErrWithFunction(rc,
1438 "RegQueryValue");
1439 }
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001440
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001441 retStr = PyUnicode_FromWideChar(retBuf, wcslen(retBuf));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 PyMem_Free(retBuf);
1443 return retStr;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001444}
1445
1446static PyObject *
1447PyQueryValueEx(PyObject *self, PyObject *args)
1448{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 HKEY hKey;
1450 PyObject *obKey;
1451 wchar_t *valueName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 long rc;
Brian Curtin60853212010-05-26 17:43:50 +00001454 BYTE *retBuf, *tmp;
1455 DWORD bufSize = 0, retSize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 DWORD typ;
1457 PyObject *obData;
1458 PyObject *result;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 if (!PyArg_ParseTuple(args, "OZ:QueryValueEx", &obKey, &valueName))
1461 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1464 return NULL;
Brian Curtin60853212010-05-26 17:43:50 +00001465
1466 rc = RegQueryValueExW(hKey, valueName, NULL, NULL, NULL, &bufSize);
1467 if (rc == ERROR_MORE_DATA)
1468 bufSize = 256;
1469 else if (rc != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 return PyErr_SetFromWindowsErrWithFunction(rc,
1471 "RegQueryValueEx");
1472 retBuf = (BYTE *)PyMem_Malloc(bufSize);
1473 if (retBuf == NULL)
1474 return PyErr_NoMemory();
Brian Curtin60853212010-05-26 17:43:50 +00001475
1476 while (1) {
1477 retSize = bufSize;
1478 rc = RegQueryValueExW(hKey, valueName, NULL, &typ,
1479 (BYTE *)retBuf, &retSize);
1480 if (rc != ERROR_MORE_DATA)
1481 break;
1482
1483 bufSize *= 2;
1484 tmp = (char *) PyMem_Realloc(retBuf, bufSize);
1485 if (tmp == NULL) {
1486 PyMem_Free(retBuf);
1487 return PyErr_NoMemory();
1488 }
1489 retBuf = tmp;
1490 }
1491
1492 if (rc != ERROR_SUCCESS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 PyMem_Free(retBuf);
1494 return PyErr_SetFromWindowsErrWithFunction(rc,
1495 "RegQueryValueEx");
1496 }
1497 obData = Reg2Py(retBuf, bufSize, typ);
1498 PyMem_Free(retBuf);
1499 if (obData == NULL)
1500 return NULL;
1501 result = Py_BuildValue("Oi", obData, typ);
1502 Py_DECREF(obData);
1503 return result;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001504}
1505
1506
1507static PyObject *
1508PySaveKey(PyObject *self, PyObject *args)
1509{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 HKEY hKey;
1511 PyObject *obKey;
1512 wchar_t *fileName;
1513 LPSECURITY_ATTRIBUTES pSA = NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 long rc;
1516 if (!PyArg_ParseTuple(args, "Ou:SaveKey", &obKey, &fileName))
1517 return NULL;
1518 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1519 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001520/* One day we may get security into the core?
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE))
1522 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001523*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 Py_BEGIN_ALLOW_THREADS
1525 rc = RegSaveKeyW(hKey, fileName, pSA );
1526 Py_END_ALLOW_THREADS
1527 if (rc != ERROR_SUCCESS)
1528 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey");
1529 Py_INCREF(Py_None);
1530 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001531}
1532
1533static PyObject *
1534PySetValue(PyObject *self, PyObject *args)
1535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 HKEY hKey;
1537 PyObject *obKey;
1538 wchar_t *subKey;
1539 wchar_t *str;
1540 DWORD typ;
1541 DWORD len;
1542 long rc;
1543 if (!PyArg_ParseTuple(args, "OZiu#:SetValue",
1544 &obKey,
1545 &subKey,
1546 &typ,
1547 &str,
1548 &len))
1549 return NULL;
1550 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1551 return NULL;
1552 if (typ != REG_SZ) {
1553 PyErr_SetString(PyExc_TypeError,
1554 "Type must be winreg.REG_SZ");
1555 return NULL;
1556 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 Py_BEGIN_ALLOW_THREADS
1559 rc = RegSetValueW(hKey, subKey, REG_SZ, str, len+1);
1560 Py_END_ALLOW_THREADS
1561 if (rc != ERROR_SUCCESS)
1562 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue");
1563 Py_INCREF(Py_None);
1564 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001565}
1566
1567static PyObject *
1568PySetValueEx(PyObject *self, PyObject *args)
1569{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 HKEY hKey;
1571 PyObject *obKey;
Victor Stinner9d3b93b2011-11-22 02:27:30 +01001572 wchar_t *valueName;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 PyObject *obRes;
1574 PyObject *value;
1575 BYTE *data;
1576 DWORD len;
1577 DWORD typ;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 LONG rc;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 if (!PyArg_ParseTuple(args, "OZOiO:SetValueEx",
1582 &obKey,
1583 &valueName,
1584 &obRes,
1585 &typ,
1586 &value))
1587 return NULL;
1588 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1589 return NULL;
1590 if (!Py2Reg(value, typ, &data, &len))
1591 {
1592 if (!PyErr_Occurred())
1593 PyErr_SetString(PyExc_ValueError,
1594 "Could not convert the data to the specified type.");
1595 return NULL;
1596 }
1597 Py_BEGIN_ALLOW_THREADS
1598 rc = RegSetValueExW(hKey, valueName, 0, typ, data, len);
1599 Py_END_ALLOW_THREADS
1600 PyMem_DEL(data);
1601 if (rc != ERROR_SUCCESS)
1602 return PyErr_SetFromWindowsErrWithFunction(rc,
1603 "RegSetValueEx");
1604 Py_INCREF(Py_None);
1605 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001606}
1607
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001608static PyObject *
1609PyDisableReflectionKey(PyObject *self, PyObject *args)
1610{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 HKEY hKey;
1612 PyObject *obKey;
1613 HMODULE hMod;
1614 typedef LONG (WINAPI *RDRKFunc)(HKEY);
1615 RDRKFunc pfn = NULL;
1616 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 if (!PyArg_ParseTuple(args, "O:DisableReflectionKey", &obKey))
1619 return NULL;
1620 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1621 return NULL;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 /* Only available on 64bit platforms, so we must load it
1624 dynamically.*/
Martin v. Löwis50590f12012-01-14 17:54:09 +01001625 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 if (hMod)
1627 pfn = (RDRKFunc)GetProcAddress(hMod,
1628 "RegDisableReflectionKey");
1629 if (!pfn) {
1630 PyErr_SetString(PyExc_NotImplementedError,
1631 "not implemented on this platform");
1632 return NULL;
1633 }
1634 Py_BEGIN_ALLOW_THREADS
1635 rc = (*pfn)(hKey);
1636 Py_END_ALLOW_THREADS
1637 if (rc != ERROR_SUCCESS)
1638 return PyErr_SetFromWindowsErrWithFunction(rc,
1639 "RegDisableReflectionKey");
1640 Py_INCREF(Py_None);
1641 return Py_None;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001642}
1643
1644static PyObject *
1645PyEnableReflectionKey(PyObject *self, PyObject *args)
1646{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 HKEY hKey;
1648 PyObject *obKey;
1649 HMODULE hMod;
1650 typedef LONG (WINAPI *RERKFunc)(HKEY);
1651 RERKFunc pfn = NULL;
1652 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 if (!PyArg_ParseTuple(args, "O:EnableReflectionKey", &obKey))
1655 return NULL;
1656 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1657 return NULL;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 /* Only available on 64bit platforms, so we must load it
1660 dynamically.*/
Martin v. Löwis50590f12012-01-14 17:54:09 +01001661 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662 if (hMod)
1663 pfn = (RERKFunc)GetProcAddress(hMod,
1664 "RegEnableReflectionKey");
1665 if (!pfn) {
1666 PyErr_SetString(PyExc_NotImplementedError,
1667 "not implemented on this platform");
1668 return NULL;
1669 }
1670 Py_BEGIN_ALLOW_THREADS
1671 rc = (*pfn)(hKey);
1672 Py_END_ALLOW_THREADS
1673 if (rc != ERROR_SUCCESS)
1674 return PyErr_SetFromWindowsErrWithFunction(rc,
1675 "RegEnableReflectionKey");
1676 Py_INCREF(Py_None);
1677 return Py_None;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001678}
1679
1680static PyObject *
1681PyQueryReflectionKey(PyObject *self, PyObject *args)
1682{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 HKEY hKey;
1684 PyObject *obKey;
1685 HMODULE hMod;
1686 typedef LONG (WINAPI *RQRKFunc)(HKEY, BOOL *);
1687 RQRKFunc pfn = NULL;
1688 BOOL result;
1689 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 if (!PyArg_ParseTuple(args, "O:QueryReflectionKey", &obKey))
1692 return NULL;
1693 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1694 return NULL;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 /* Only available on 64bit platforms, so we must load it
1697 dynamically.*/
Martin v. Löwis50590f12012-01-14 17:54:09 +01001698 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 if (hMod)
1700 pfn = (RQRKFunc)GetProcAddress(hMod,
1701 "RegQueryReflectionKey");
1702 if (!pfn) {
1703 PyErr_SetString(PyExc_NotImplementedError,
1704 "not implemented on this platform");
1705 return NULL;
1706 }
1707 Py_BEGIN_ALLOW_THREADS
1708 rc = (*pfn)(hKey, &result);
1709 Py_END_ALLOW_THREADS
1710 if (rc != ERROR_SUCCESS)
1711 return PyErr_SetFromWindowsErrWithFunction(rc,
1712 "RegQueryReflectionKey");
1713 return PyBool_FromLong(result);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001714}
1715
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001716static struct PyMethodDef winreg_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 {"CloseKey", PyCloseKey, METH_VARARGS, CloseKey_doc},
1718 {"ConnectRegistry", PyConnectRegistry, METH_VARARGS, ConnectRegistry_doc},
1719 {"CreateKey", PyCreateKey, METH_VARARGS, CreateKey_doc},
Brian Curtin1771b542010-09-27 17:56:36 +00001720 {"CreateKeyEx", (PyCFunction)PyCreateKeyEx,
1721 METH_VARARGS | METH_KEYWORDS, CreateKeyEx_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 {"DeleteKey", PyDeleteKey, METH_VARARGS, DeleteKey_doc},
Brian Curtin1771b542010-09-27 17:56:36 +00001723 {"DeleteKeyEx", (PyCFunction)PyDeleteKeyEx,
1724 METH_VARARGS | METH_KEYWORDS, DeleteKeyEx_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 {"DeleteValue", PyDeleteValue, METH_VARARGS, DeleteValue_doc},
1726 {"DisableReflectionKey", PyDisableReflectionKey, METH_VARARGS, DisableReflectionKey_doc},
1727 {"EnableReflectionKey", PyEnableReflectionKey, METH_VARARGS, EnableReflectionKey_doc},
1728 {"EnumKey", PyEnumKey, METH_VARARGS, EnumKey_doc},
1729 {"EnumValue", PyEnumValue, METH_VARARGS, EnumValue_doc},
1730 {"ExpandEnvironmentStrings", PyExpandEnvironmentStrings, METH_VARARGS,
1731 ExpandEnvironmentStrings_doc },
1732 {"FlushKey", PyFlushKey, METH_VARARGS, FlushKey_doc},
1733 {"LoadKey", PyLoadKey, METH_VARARGS, LoadKey_doc},
Brian Curtin1771b542010-09-27 17:56:36 +00001734 {"OpenKey", (PyCFunction)PyOpenKey, METH_VARARGS | METH_KEYWORDS,
1735 OpenKey_doc},
1736 {"OpenKeyEx", (PyCFunction)PyOpenKey, METH_VARARGS | METH_KEYWORDS,
1737 OpenKeyEx_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 {"QueryValue", PyQueryValue, METH_VARARGS, QueryValue_doc},
1739 {"QueryValueEx", PyQueryValueEx, METH_VARARGS, QueryValueEx_doc},
1740 {"QueryInfoKey", PyQueryInfoKey, METH_VARARGS, QueryInfoKey_doc},
1741 {"QueryReflectionKey",PyQueryReflectionKey,METH_VARARGS, QueryReflectionKey_doc},
1742 {"SaveKey", PySaveKey, METH_VARARGS, SaveKey_doc},
1743 {"SetValue", PySetValue, METH_VARARGS, SetValue_doc},
1744 {"SetValueEx", PySetValueEx, METH_VARARGS, SetValueEx_doc},
1745 NULL,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001746};
1747
1748static void
1749insint(PyObject * d, char * name, long value)
1750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 PyObject *v = PyLong_FromLong(value);
1752 if (!v || PyDict_SetItemString(d, name, v))
1753 PyErr_Clear();
1754 Py_XDECREF(v);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001755}
1756
1757#define ADD_INT(val) insint(d, #val, val)
1758
1759static void
1760inskey(PyObject * d, char * name, HKEY key)
1761{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 PyObject *v = PyLong_FromVoidPtr(key);
1763 if (!v || PyDict_SetItemString(d, name, v))
1764 PyErr_Clear();
1765 Py_XDECREF(v);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001766}
1767
1768#define ADD_KEY(val) inskey(d, #val, val)
1769
Martin v. Löwis1a214512008-06-11 05:26:20 +00001770
1771static struct PyModuleDef winregmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 PyModuleDef_HEAD_INIT,
1773 "winreg",
1774 module_doc,
1775 -1,
1776 winreg_methods,
1777 NULL,
1778 NULL,
1779 NULL,
1780 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001781};
1782
1783PyMODINIT_FUNC PyInit_winreg(void)
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 PyObject *m, *d;
1786 m = PyModule_Create(&winregmodule);
1787 if (m == NULL)
1788 return NULL;
1789 d = PyModule_GetDict(m);
1790 PyHKEY_Type.tp_doc = PyHKEY_doc;
1791 if (PyType_Ready(&PyHKEY_Type) < 0)
1792 return NULL;
1793 Py_INCREF(&PyHKEY_Type);
1794 if (PyDict_SetItemString(d, "HKEYType",
1795 (PyObject *)&PyHKEY_Type) != 0)
1796 return NULL;
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001797 Py_INCREF(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001798 if (PyDict_SetItemString(d, "error",
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001799 PyExc_OSError) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001802 /* Add the relevant constants */
1803 ADD_KEY(HKEY_CLASSES_ROOT);
1804 ADD_KEY(HKEY_CURRENT_USER);
1805 ADD_KEY(HKEY_LOCAL_MACHINE);
1806 ADD_KEY(HKEY_USERS);
1807 ADD_KEY(HKEY_PERFORMANCE_DATA);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001808#ifdef HKEY_CURRENT_CONFIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001809 ADD_KEY(HKEY_CURRENT_CONFIG);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001810#endif
1811#ifdef HKEY_DYN_DATA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001812 ADD_KEY(HKEY_DYN_DATA);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001813#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 ADD_INT(KEY_QUERY_VALUE);
1815 ADD_INT(KEY_SET_VALUE);
1816 ADD_INT(KEY_CREATE_SUB_KEY);
1817 ADD_INT(KEY_ENUMERATE_SUB_KEYS);
1818 ADD_INT(KEY_NOTIFY);
1819 ADD_INT(KEY_CREATE_LINK);
1820 ADD_INT(KEY_READ);
1821 ADD_INT(KEY_WRITE);
1822 ADD_INT(KEY_EXECUTE);
1823 ADD_INT(KEY_ALL_ACCESS);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001824#ifdef KEY_WOW64_64KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 ADD_INT(KEY_WOW64_64KEY);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001826#endif
1827#ifdef KEY_WOW64_32KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 ADD_INT(KEY_WOW64_32KEY);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001829#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 ADD_INT(REG_OPTION_RESERVED);
1831 ADD_INT(REG_OPTION_NON_VOLATILE);
1832 ADD_INT(REG_OPTION_VOLATILE);
1833 ADD_INT(REG_OPTION_CREATE_LINK);
1834 ADD_INT(REG_OPTION_BACKUP_RESTORE);
1835 ADD_INT(REG_OPTION_OPEN_LINK);
1836 ADD_INT(REG_LEGAL_OPTION);
1837 ADD_INT(REG_CREATED_NEW_KEY);
1838 ADD_INT(REG_OPENED_EXISTING_KEY);
1839 ADD_INT(REG_WHOLE_HIVE_VOLATILE);
1840 ADD_INT(REG_REFRESH_HIVE);
1841 ADD_INT(REG_NO_LAZY_FLUSH);
1842 ADD_INT(REG_NOTIFY_CHANGE_NAME);
1843 ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES);
1844 ADD_INT(REG_NOTIFY_CHANGE_LAST_SET);
1845 ADD_INT(REG_NOTIFY_CHANGE_SECURITY);
1846 ADD_INT(REG_LEGAL_CHANGE_FILTER);
1847 ADD_INT(REG_NONE);
1848 ADD_INT(REG_SZ);
1849 ADD_INT(REG_EXPAND_SZ);
1850 ADD_INT(REG_BINARY);
1851 ADD_INT(REG_DWORD);
1852 ADD_INT(REG_DWORD_LITTLE_ENDIAN);
1853 ADD_INT(REG_DWORD_BIG_ENDIAN);
1854 ADD_INT(REG_LINK);
1855 ADD_INT(REG_MULTI_SZ);
1856 ADD_INT(REG_RESOURCE_LIST);
1857 ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
1858 ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
1859 return m;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001860}
1861
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001862