blob: 19d5a70391872e8de899cff19ef917d4be329159 [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"
Serhiy Storchaka95949422013-08-27 19:40:23 +0300256"An 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"
Serhiy Storchaka95949422013-08-27 19:40:23 +0300408"handle object.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000409
410
411/************************************************************************
412
413 The PyHKEY object definition
414
415************************************************************************/
416typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 PyObject_VAR_HEAD
418 HKEY hkey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000419} PyHKEYObject;
420
421#define PyHKEY_Check(op) ((op)->ob_type == &PyHKEY_Type)
422
423static char *failMsg = "bad operand type";
424
425static PyObject *
426PyHKEY_unaryFailureFunc(PyObject *ob)
427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 PyErr_SetString(PyExc_TypeError, failMsg);
429 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000430}
431static PyObject *
432PyHKEY_binaryFailureFunc(PyObject *ob1, PyObject *ob2)
433{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 PyErr_SetString(PyExc_TypeError, failMsg);
435 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000436}
437static PyObject *
438PyHKEY_ternaryFailureFunc(PyObject *ob1, PyObject *ob2, PyObject *ob3)
439{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 PyErr_SetString(PyExc_TypeError, failMsg);
441 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000442}
443
444static void
445PyHKEY_deallocFunc(PyObject *ob)
446{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 /* Can not call PyHKEY_Close, as the ob->tp_type
448 has already been cleared, thus causing the type
449 check to fail!
450 */
451 PyHKEYObject *obkey = (PyHKEYObject *)ob;
452 if (obkey->hkey)
453 RegCloseKey((HKEY)obkey->hkey);
454 PyObject_DEL(ob);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000455}
456
457static int
Jack Diederich4dafcc42006-11-28 19:15:13 +0000458PyHKEY_boolFunc(PyObject *ob)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000459{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 return ((PyHKEYObject *)ob)->hkey != 0;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000461}
462
463static PyObject *
464PyHKEY_intFunc(PyObject *ob)
465{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
467 return PyLong_FromVoidPtr(pyhkey->hkey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000468}
469
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000470static PyObject *
471PyHKEY_strFunc(PyObject *ob)
472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
474 return PyUnicode_FromFormat("<PyHKEY:%p>", pyhkey->hkey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000475}
476
477static int
478PyHKEY_compareFunc(PyObject *ob1, PyObject *ob2)
479{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 PyHKEYObject *pyhkey1 = (PyHKEYObject *)ob1;
481 PyHKEYObject *pyhkey2 = (PyHKEYObject *)ob2;
482 return pyhkey1 == pyhkey2 ? 0 :
483 (pyhkey1 < pyhkey2 ? -1 : 1);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000484}
485
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000486static Py_hash_t
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000487PyHKEY_hashFunc(PyObject *ob)
488{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 /* Just use the address.
490 XXX - should we use the handle value?
491 */
492 return _Py_HashPointer(ob);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000493}
494
495
496static PyNumberMethods PyHKEY_NumberMethods =
497{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 PyHKEY_binaryFailureFunc, /* nb_add */
499 PyHKEY_binaryFailureFunc, /* nb_subtract */
500 PyHKEY_binaryFailureFunc, /* nb_multiply */
501 PyHKEY_binaryFailureFunc, /* nb_remainder */
502 PyHKEY_binaryFailureFunc, /* nb_divmod */
503 PyHKEY_ternaryFailureFunc, /* nb_power */
504 PyHKEY_unaryFailureFunc, /* nb_negative */
505 PyHKEY_unaryFailureFunc, /* nb_positive */
506 PyHKEY_unaryFailureFunc, /* nb_absolute */
507 PyHKEY_boolFunc, /* nb_bool */
508 PyHKEY_unaryFailureFunc, /* nb_invert */
509 PyHKEY_binaryFailureFunc, /* nb_lshift */
510 PyHKEY_binaryFailureFunc, /* nb_rshift */
511 PyHKEY_binaryFailureFunc, /* nb_and */
512 PyHKEY_binaryFailureFunc, /* nb_xor */
513 PyHKEY_binaryFailureFunc, /* nb_or */
514 PyHKEY_intFunc, /* nb_int */
515 0, /* nb_reserved */
516 PyHKEY_unaryFailureFunc, /* nb_float */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000517};
518
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000519static PyObject *PyHKEY_CloseMethod(PyObject *self, PyObject *args);
520static PyObject *PyHKEY_DetachMethod(PyObject *self, PyObject *args);
521static PyObject *PyHKEY_Enter(PyObject *self);
522static PyObject *PyHKEY_Exit(PyObject *self, PyObject *args);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000523
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000524static struct PyMethodDef PyHKEY_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 {"Close", PyHKEY_CloseMethod, METH_VARARGS, PyHKEY_Close_doc},
526 {"Detach", PyHKEY_DetachMethod, METH_VARARGS, PyHKEY_Detach_doc},
527 {"__enter__", (PyCFunction)PyHKEY_Enter, METH_NOARGS, NULL},
528 {"__exit__", PyHKEY_Exit, METH_VARARGS, NULL},
529 {NULL}
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000530};
531
532#define OFF(e) offsetof(PyHKEYObject, e)
533static PyMemberDef PyHKEY_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 {"handle", T_INT, OFF(hkey), READONLY},
535 {NULL} /* Sentinel */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000536};
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000537
538/* The type itself */
539PyTypeObject PyHKEY_Type =
540{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 PyVarObject_HEAD_INIT(0, 0) /* fill in type at module init */
542 "PyHKEY",
543 sizeof(PyHKEYObject),
544 0,
545 PyHKEY_deallocFunc, /* tp_dealloc */
546 0, /* tp_print */
547 0, /* tp_getattr */
548 0, /* tp_setattr */
549 0, /* tp_reserved */
550 0, /* tp_repr */
551 &PyHKEY_NumberMethods, /* tp_as_number */
552 0, /* tp_as_sequence */
553 0, /* tp_as_mapping */
554 PyHKEY_hashFunc, /* tp_hash */
555 0, /* tp_call */
556 PyHKEY_strFunc, /* tp_str */
557 0, /* tp_getattro */
558 0, /* tp_setattro */
559 0, /* tp_as_buffer */
560 0, /* tp_flags */
561 PyHKEY_doc, /* tp_doc */
562 0, /*tp_traverse*/
563 0, /*tp_clear*/
564 0, /*tp_richcompare*/
565 0, /*tp_weaklistoffset*/
566 0, /*tp_iter*/
567 0, /*tp_iternext*/
568 PyHKEY_methods, /*tp_methods*/
569 PyHKEY_memberlist, /*tp_members*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000570};
571
572/************************************************************************
573
574 The PyHKEY object methods
575
576************************************************************************/
577static PyObject *
578PyHKEY_CloseMethod(PyObject *self, PyObject *args)
579{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 if (!PyArg_ParseTuple(args, ":Close"))
581 return NULL;
582 if (!PyHKEY_Close(self))
583 return NULL;
584 Py_INCREF(Py_None);
585 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000586}
587
588static PyObject *
589PyHKEY_DetachMethod(PyObject *self, PyObject *args)
590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 void* ret;
592 PyHKEYObject *pThis = (PyHKEYObject *)self;
593 if (!PyArg_ParseTuple(args, ":Detach"))
594 return NULL;
595 ret = (void*)pThis->hkey;
596 pThis->hkey = 0;
597 return PyLong_FromVoidPtr(ret);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000598}
599
Christian Heimes2380ac72008-01-09 00:17:24 +0000600static PyObject *
601PyHKEY_Enter(PyObject *self)
602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 Py_XINCREF(self);
604 return self;
Christian Heimes2380ac72008-01-09 00:17:24 +0000605}
606
607static PyObject *
608PyHKEY_Exit(PyObject *self, PyObject *args)
609{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 if (!PyHKEY_Close(self))
611 return NULL;
612 Py_RETURN_NONE;
Christian Heimes2380ac72008-01-09 00:17:24 +0000613}
614
615
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000616/************************************************************************
617 The public PyHKEY API (well, not public yet :-)
618************************************************************************/
619PyObject *
620PyHKEY_New(HKEY hInit)
621{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 PyHKEYObject *key = PyObject_NEW(PyHKEYObject, &PyHKEY_Type);
623 if (key)
624 key->hkey = hInit;
625 return (PyObject *)key;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000626}
627
628BOOL
629PyHKEY_Close(PyObject *ob_handle)
630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 LONG rc;
632 PyHKEYObject *key;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 if (!PyHKEY_Check(ob_handle)) {
635 PyErr_SetString(PyExc_TypeError, "bad operand type");
636 return FALSE;
637 }
638 key = (PyHKEYObject *)ob_handle;
639 rc = key->hkey ? RegCloseKey((HKEY)key->hkey) : ERROR_SUCCESS;
640 key->hkey = 0;
641 if (rc != ERROR_SUCCESS)
642 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
643 return rc == ERROR_SUCCESS;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000644}
645
646BOOL
647PyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK)
648{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 if (ob == Py_None) {
650 if (!bNoneOK) {
651 PyErr_SetString(
652 PyExc_TypeError,
653 "None is not a valid HKEY in this context");
654 return FALSE;
655 }
656 *pHANDLE = (HKEY)0;
657 }
658 else if (PyHKEY_Check(ob)) {
659 PyHKEYObject *pH = (PyHKEYObject *)ob;
660 *pHANDLE = pH->hkey;
661 }
662 else if (PyLong_Check(ob)) {
663 /* We also support integers */
664 PyErr_Clear();
665 *pHANDLE = (HKEY)PyLong_AsVoidPtr(ob);
666 if (PyErr_Occurred())
667 return FALSE;
668 }
669 else {
670 PyErr_SetString(
671 PyExc_TypeError,
672 "The object is not a PyHKEY object");
673 return FALSE;
674 }
675 return TRUE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000676}
677
678PyObject *
679PyHKEY_FromHKEY(HKEY h)
680{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 PyHKEYObject *op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 /* Inline PyObject_New */
684 op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject));
685 if (op == NULL)
686 return PyErr_NoMemory();
687 PyObject_INIT(op, &PyHKEY_Type);
688 op->hkey = h;
689 return (PyObject *)op;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000690}
691
692
693/************************************************************************
694 The module methods
695************************************************************************/
696BOOL
697PyWinObject_CloseHKEY(PyObject *obHandle)
698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 BOOL ok;
700 if (PyHKEY_Check(obHandle)) {
701 ok = PyHKEY_Close(obHandle);
702 }
Fred Drake25e17262000-06-30 17:48:51 +0000703#if SIZEOF_LONG >= SIZEOF_HKEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 else if (PyLong_Check(obHandle)) {
705 long rc = RegCloseKey((HKEY)PyLong_AsLong(obHandle));
706 ok = (rc == ERROR_SUCCESS);
707 if (!ok)
708 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
709 }
Fred Drake25e17262000-06-30 17:48:51 +0000710#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 else if (PyLong_Check(obHandle)) {
712 long rc = RegCloseKey((HKEY)PyLong_AsVoidPtr(obHandle));
713 ok = (rc == ERROR_SUCCESS);
714 if (!ok)
715 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
716 }
Fred Drake25e17262000-06-30 17:48:51 +0000717#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 else {
719 PyErr_SetString(
720 PyExc_TypeError,
721 "A handle must be a HKEY object or an integer");
722 return FALSE;
723 }
724 return ok;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000725}
726
727
728/*
729 Private Helper functions for the registry interfaces
730
731** Note that fixupMultiSZ and countString have both had changes
732** made to support "incorrect strings". The registry specification
733** calls for strings to be terminated with 2 null bytes. It seems
Thomas Wouters7e474022000-07-16 12:04:32 +0000734** some commercial packages install strings which dont conform,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000735** causing this code to fail - however, "regedit" etc still work
736** with these strings (ie only we dont!).
737*/
738static void
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000739fixupMultiSZ(wchar_t **str, wchar_t *data, int len)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000740{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 wchar_t *P;
742 int i;
743 wchar_t *Q;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 Q = data + len;
746 for (P = data, i = 0; P < Q && *P != '\0'; P++, i++) {
747 str[i] = P;
748 for(; *P != '\0'; P++)
749 ;
750 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000751}
752
753static int
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000754countStrings(wchar_t *data, int len)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000755{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 int strings;
757 wchar_t *P;
758 wchar_t *Q = data + len;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 for (P = data, strings = 0; P < Q && *P != '\0'; P++, strings++)
761 for (; P < Q && *P != '\0'; P++)
762 ;
763 return strings;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000764}
765
766/* Convert PyObject into Registry data.
767 Allocates space as needed. */
768static BOOL
769Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
770{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 Py_ssize_t i,j;
772 switch (typ) {
773 case REG_DWORD:
774 if (value != Py_None && !PyLong_Check(value))
775 return FALSE;
776 *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1);
777 if (*retDataBuf==NULL){
778 PyErr_NoMemory();
779 return FALSE;
780 }
781 *retDataSize = sizeof(DWORD);
782 if (value == Py_None) {
783 DWORD zero = 0;
784 memcpy(*retDataBuf, &zero, sizeof(DWORD));
785 }
786 else {
Brian Curtin12706f22012-12-27 10:12:45 -0600787 DWORD d = PyLong_AsUnsignedLong(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 memcpy(*retDataBuf, &d, sizeof(DWORD));
789 }
790 break;
791 case REG_SZ:
792 case REG_EXPAND_SZ:
793 {
Victor Stinnerbe492442011-11-21 12:43:50 +0100794 if (value != Py_None) {
795 Py_ssize_t len;
796 if (!PyUnicode_Check(value))
797 return FALSE;
798 *retDataBuf = (BYTE*)PyUnicode_AsWideCharString(value, &len);
799 if (*retDataBuf == NULL)
800 return FALSE;
801 *retDataSize = Py_SAFE_DOWNCAST(
802 (len + 1) * sizeof(wchar_t),
803 Py_ssize_t, DWORD);
804 }
805 else {
806 *retDataBuf = (BYTE *)PyMem_NEW(wchar_t, 1);
807 if (*retDataBuf == NULL) {
808 PyErr_NoMemory();
809 return FALSE;
810 }
811 ((wchar_t *)*retDataBuf)[0] = L'\0';
812 *retDataSize = 1 * sizeof(wchar_t);
813 }
814 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 }
816 case REG_MULTI_SZ:
817 {
818 DWORD size = 0;
819 wchar_t *P;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 if (value == Py_None)
822 i = 0;
823 else {
824 if (!PyList_Check(value))
825 return FALSE;
826 i = PyList_Size(value);
827 }
828 for (j = 0; j < i; j++)
829 {
830 PyObject *t;
Victor Stinnerbe492442011-11-21 12:43:50 +0100831 wchar_t *wstr;
832 Py_ssize_t len;
833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 t = PyList_GET_ITEM(value, j);
835 if (!PyUnicode_Check(t))
836 return FALSE;
Victor Stinnerbe492442011-11-21 12:43:50 +0100837 wstr = PyUnicode_AsUnicodeAndSize(t, &len);
838 if (wstr == NULL)
839 return FALSE;
840 size += Py_SAFE_DOWNCAST((len + 1) * sizeof(wchar_t),
Brian Curtinabb33512010-08-17 20:08:40 +0000841 size_t, DWORD);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 *retDataSize = size + 2;
845 *retDataBuf = (BYTE *)PyMem_NEW(char,
846 *retDataSize);
847 if (*retDataBuf==NULL){
848 PyErr_NoMemory();
849 return FALSE;
850 }
851 P = (wchar_t *)*retDataBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 for (j = 0; j < i; j++)
854 {
855 PyObject *t;
Victor Stinnerbe492442011-11-21 12:43:50 +0100856 wchar_t *wstr;
857 Py_ssize_t len;
858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 t = PyList_GET_ITEM(value, j);
Victor Stinnerbe492442011-11-21 12:43:50 +0100860 wstr = PyUnicode_AsUnicodeAndSize(t, &len);
861 if (wstr == NULL)
862 return FALSE;
863 wcscpy(P, wstr);
864 P += (len + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 }
866 /* And doubly-terminate the list... */
867 *P = '\0';
868 break;
869 }
870 case REG_BINARY:
871 /* ALSO handle ALL unknown data types here. Even if we can't
872 support it natively, we should handle the bits. */
873 default:
Zachary Waread4690f2014-07-03 10:58:06 -0500874 if (value == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 *retDataSize = 0;
Zachary Waread4690f2014-07-03 10:58:06 -0500876 *retDataBuf = NULL;
877 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 else {
879 Py_buffer view;
Neal Norwitz1385b892007-08-26 23:07:13 +0000880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 if (!PyObject_CheckBuffer(value)) {
882 PyErr_Format(PyExc_TypeError,
883 "Objects of type '%s' can not "
884 "be used as binary registry values",
885 value->ob_type->tp_name);
886 return FALSE;
887 }
Neal Norwitz1385b892007-08-26 23:07:13 +0000888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0)
890 return FALSE;
Neal Norwitz1385b892007-08-26 23:07:13 +0000891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 *retDataBuf = (BYTE *)PyMem_NEW(char, view.len);
893 if (*retDataBuf==NULL){
894 PyBuffer_Release(&view);
895 PyErr_NoMemory();
896 return FALSE;
897 }
Brian Curtinabb33512010-08-17 20:08:40 +0000898 *retDataSize = Py_SAFE_DOWNCAST(view.len, Py_ssize_t, DWORD);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 memcpy(*retDataBuf, view.buf, view.len);
900 PyBuffer_Release(&view);
901 }
902 break;
903 }
904 return TRUE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000905}
906
907/* Convert Registry data into PyObject*/
908static PyObject *
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000909Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000910{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 PyObject *obData;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 switch (typ) {
914 case REG_DWORD:
915 if (retDataSize == 0)
Brian Curtin172e4222012-12-27 14:04:42 -0600916 obData = PyLong_FromUnsignedLong(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 else
Brian Curtin172e4222012-12-27 14:04:42 -0600918 obData = PyLong_FromUnsignedLong(*(int *)retDataBuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 break;
920 case REG_SZ:
921 case REG_EXPAND_SZ:
922 {
923 /* the buffer may or may not have a trailing NULL */
924 wchar_t *data = (wchar_t *)retDataBuf;
925 int len = retDataSize / 2;
926 if (retDataSize && data[len-1] == '\0')
927 retDataSize -= 2;
928 if (retDataSize <= 0)
929 data = L"";
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200930 obData = PyUnicode_FromWideChar(data, retDataSize/2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 break;
932 }
933 case REG_MULTI_SZ:
934 if (retDataSize == 0)
935 obData = PyList_New(0);
936 else
937 {
938 int index = 0;
939 wchar_t *data = (wchar_t *)retDataBuf;
940 int len = retDataSize / 2;
941 int s = countStrings(data, len);
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +0200942 wchar_t **str = PyMem_New(wchar_t *, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 if (str == NULL)
944 return PyErr_NoMemory();
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 fixupMultiSZ(str, data, len);
947 obData = PyList_New(s);
Jesus Cea782c4cf2014-03-13 17:35:32 +0100948 if (obData == NULL) {
Victor Stinner9cb1ec52014-03-13 19:08:10 +0100949 PyMem_Free(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 return NULL;
Jesus Cea782c4cf2014-03-13 17:35:32 +0100951 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 for (index = 0; index < s; index++)
953 {
954 size_t len = wcslen(str[index]);
955 if (len > INT_MAX) {
956 PyErr_SetString(PyExc_OverflowError,
957 "registry string is too long for a Python string");
958 Py_DECREF(obData);
Victor Stinner9cb1ec52014-03-13 19:08:10 +0100959 PyMem_Free(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 return NULL;
961 }
962 PyList_SetItem(obData,
963 index,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200964 PyUnicode_FromWideChar(str[index], len));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 }
Victor Stinnerb6404912013-07-07 16:21:41 +0200966 PyMem_Free(str);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 break;
969 }
970 case REG_BINARY:
971 /* ALSO handle ALL unknown data types here. Even if we can't
972 support it natively, we should handle the bits. */
973 default:
974 if (retDataSize == 0) {
975 Py_INCREF(Py_None);
976 obData = Py_None;
977 }
978 else
979 obData = PyBytes_FromStringAndSize(
980 (char *)retDataBuf, retDataSize);
981 break;
982 }
983 return obData;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000984}
985
986/* The Python methods */
987
988static PyObject *
989PyCloseKey(PyObject *self, PyObject *args)
990{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 PyObject *obKey;
992 if (!PyArg_ParseTuple(args, "O:CloseKey", &obKey))
993 return NULL;
994 if (!PyHKEY_Close(obKey))
995 return NULL;
996 Py_INCREF(Py_None);
997 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000998}
999
1000static PyObject *
1001PyConnectRegistry(PyObject *self, PyObject *args)
1002{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 HKEY hKey;
1004 PyObject *obKey;
1005 wchar_t *szCompName = NULL;
1006 HKEY retKey;
1007 long rc;
1008 if (!PyArg_ParseTuple(args, "ZO:ConnectRegistry", &szCompName, &obKey))
1009 return NULL;
1010 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1011 return NULL;
1012 Py_BEGIN_ALLOW_THREADS
1013 rc = RegConnectRegistryW(szCompName, hKey, &retKey);
1014 Py_END_ALLOW_THREADS
1015 if (rc != ERROR_SUCCESS)
1016 return PyErr_SetFromWindowsErrWithFunction(rc,
1017 "ConnectRegistry");
1018 return PyHKEY_FromHKEY(retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001019}
1020
1021static PyObject *
1022PyCreateKey(PyObject *self, PyObject *args)
1023{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 HKEY hKey;
1025 PyObject *obKey;
1026 wchar_t *subKey;
1027 HKEY retKey;
1028 long rc;
1029 if (!PyArg_ParseTuple(args, "OZ:CreateKey", &obKey, &subKey))
1030 return NULL;
1031 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1032 return NULL;
1033 rc = RegCreateKeyW(hKey, subKey, &retKey);
1034 if (rc != ERROR_SUCCESS)
1035 return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey");
1036 return PyHKEY_FromHKEY(retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001037}
1038
1039static PyObject *
Brian Curtin1771b542010-09-27 17:56:36 +00001040PyCreateKeyEx(PyObject *self, PyObject *args, PyObject *kwargs)
Brian Curtin3035c392010-04-21 23:56:21 +00001041{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 HKEY hKey;
Brian Curtin1771b542010-09-27 17:56:36 +00001043 PyObject *key;
1044 wchar_t *sub_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 HKEY retKey;
Brian Curtin1771b542010-09-27 17:56:36 +00001046 int reserved = 0;
1047 REGSAM access = KEY_WRITE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 long rc;
Brian Curtin1771b542010-09-27 17:56:36 +00001049
1050 char *kwlist[] = {"key", "sub_key", "reserved", "access", NULL};
1051
1052 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OZ|ii:CreateKeyEx", kwlist,
1053 &key, &sub_key, &reserved, &access))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 return NULL;
Brian Curtin1771b542010-09-27 17:56:36 +00001055 if (!PyHKEY_AsHKEY(key, &hKey, FALSE))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 return NULL;
Brian Curtin3035c392010-04-21 23:56:21 +00001057
Brian Curtin1771b542010-09-27 17:56:36 +00001058 rc = RegCreateKeyExW(hKey, sub_key, reserved, NULL, (DWORD)NULL,
1059 access, NULL, &retKey, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 if (rc != ERROR_SUCCESS)
1061 return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKeyEx");
1062 return PyHKEY_FromHKEY(retKey);
Brian Curtin3035c392010-04-21 23:56:21 +00001063}
1064
1065static PyObject *
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001066PyDeleteKey(PyObject *self, PyObject *args)
1067{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 HKEY hKey;
1069 PyObject *obKey;
1070 wchar_t *subKey;
1071 long rc;
1072 if (!PyArg_ParseTuple(args, "Ou:DeleteKey", &obKey, &subKey))
1073 return NULL;
1074 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1075 return NULL;
1076 rc = RegDeleteKeyW(hKey, subKey );
1077 if (rc != ERROR_SUCCESS)
1078 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey");
1079 Py_INCREF(Py_None);
1080 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001081}
1082
1083static PyObject *
Brian Curtin1771b542010-09-27 17:56:36 +00001084PyDeleteKeyEx(PyObject *self, PyObject *args, PyObject *kwargs)
Brian Curtin3035c392010-04-21 23:56:21 +00001085{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 HKEY hKey;
Brian Curtin1771b542010-09-27 17:56:36 +00001087 PyObject *key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 HMODULE hMod;
1089 typedef LONG (WINAPI *RDKEFunc)(HKEY, const wchar_t*, REGSAM, int);
1090 RDKEFunc pfn = NULL;
Brian Curtin1771b542010-09-27 17:56:36 +00001091 wchar_t *sub_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 long rc;
Brian Curtin1771b542010-09-27 17:56:36 +00001093 int reserved = 0;
1094 REGSAM access = KEY_WOW64_64KEY;
Brian Curtin3035c392010-04-21 23:56:21 +00001095
Brian Curtin1771b542010-09-27 17:56:36 +00001096 char *kwlist[] = {"key", "sub_key", "access", "reserved", NULL};
1097 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Ou|ii:DeleteKeyEx", kwlist,
1098 &key, &sub_key, &access, &reserved))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 return NULL;
Brian Curtin1771b542010-09-27 17:56:36 +00001100 if (!PyHKEY_AsHKEY(key, &hKey, FALSE))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 return NULL;
Brian Curtin3035c392010-04-21 23:56:21 +00001102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 /* Only available on 64bit platforms, so we must load it
1104 dynamically. */
Martin v. Löwis50590f12012-01-14 17:54:09 +01001105 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 if (hMod)
1107 pfn = (RDKEFunc)GetProcAddress(hMod,
1108 "RegDeleteKeyExW");
1109 if (!pfn) {
1110 PyErr_SetString(PyExc_NotImplementedError,
1111 "not implemented on this platform");
1112 return NULL;
1113 }
1114 Py_BEGIN_ALLOW_THREADS
Brian Curtin1771b542010-09-27 17:56:36 +00001115 rc = (*pfn)(hKey, sub_key, access, reserved);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 Py_END_ALLOW_THREADS
Brian Curtin3035c392010-04-21 23:56:21 +00001117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 if (rc != ERROR_SUCCESS)
1119 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKeyEx");
1120 Py_INCREF(Py_None);
1121 return Py_None;
Brian Curtin3035c392010-04-21 23:56:21 +00001122}
1123
1124static PyObject *
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001125PyDeleteValue(PyObject *self, PyObject *args)
1126{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 HKEY hKey;
1128 PyObject *obKey;
1129 wchar_t *subKey;
1130 long rc;
1131 if (!PyArg_ParseTuple(args, "OZ:DeleteValue", &obKey, &subKey))
1132 return NULL;
1133 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1134 return NULL;
1135 Py_BEGIN_ALLOW_THREADS
1136 rc = RegDeleteValueW(hKey, subKey);
1137 Py_END_ALLOW_THREADS
1138 if (rc !=ERROR_SUCCESS)
1139 return PyErr_SetFromWindowsErrWithFunction(rc,
1140 "RegDeleteValue");
1141 Py_INCREF(Py_None);
1142 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001143}
1144
1145static PyObject *
1146PyEnumKey(PyObject *self, PyObject *args)
1147{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 HKEY hKey;
1149 PyObject *obKey;
1150 int index;
1151 long rc;
1152 PyObject *retStr;
Brian Curtin60853212010-05-26 17:43:50 +00001153
1154 /* The Windows docs claim that the max key name length is 255
1155 * characters, plus a terminating nul character. However,
1156 * empirical testing demonstrates that it is possible to
1157 * create a 256 character key that is missing the terminating
1158 * nul. RegEnumKeyEx requires a 257 character buffer to
1159 * retrieve such a key name. */
1160 wchar_t tmpbuf[257];
Kristján Valur Jónsson984dfa72012-04-02 15:23:29 +00001161 DWORD len = sizeof(tmpbuf)/sizeof(wchar_t); /* includes NULL terminator */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 if (!PyArg_ParseTuple(args, "Oi:EnumKey", &obKey, &index))
1164 return NULL;
1165 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1166 return NULL;
Tim Peters313fcd42006-02-19 04:05:39 +00001167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 Py_BEGIN_ALLOW_THREADS
1169 rc = RegEnumKeyExW(hKey, index, tmpbuf, &len, NULL, NULL, NULL, NULL);
1170 Py_END_ALLOW_THREADS
1171 if (rc != ERROR_SUCCESS)
1172 return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx");
Tim Peters313fcd42006-02-19 04:05:39 +00001173
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001174 retStr = PyUnicode_FromWideChar(tmpbuf, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 return retStr; /* can be NULL */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001176}
1177
1178static PyObject *
1179PyEnumValue(PyObject *self, PyObject *args)
1180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 HKEY hKey;
1182 PyObject *obKey;
1183 int index;
1184 long rc;
1185 wchar_t *retValueBuf;
Amaury Forgeot d'Arcf2b69df2010-08-16 22:11:29 +00001186 BYTE *tmpBuf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 BYTE *retDataBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001188 DWORD retValueSize, bufValueSize;
1189 DWORD retDataSize, bufDataSize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 DWORD typ;
1191 PyObject *obData;
1192 PyObject *retVal;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 if (!PyArg_ParseTuple(args, "Oi:EnumValue", &obKey, &index))
1195 return NULL;
1196 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1197 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 if ((rc = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, NULL, NULL,
1200 NULL,
1201 &retValueSize, &retDataSize, NULL, NULL))
1202 != ERROR_SUCCESS)
1203 return PyErr_SetFromWindowsErrWithFunction(rc,
1204 "RegQueryInfoKey");
1205 ++retValueSize; /* include null terminators */
1206 ++retDataSize;
Brian Curtin60853212010-05-26 17:43:50 +00001207 bufDataSize = retDataSize;
1208 bufValueSize = retValueSize;
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02001209 retValueBuf = PyMem_New(wchar_t, retValueSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 if (retValueBuf == NULL)
1211 return PyErr_NoMemory();
1212 retDataBuf = (BYTE *)PyMem_Malloc(retDataSize);
1213 if (retDataBuf == NULL) {
1214 PyMem_Free(retValueBuf);
1215 return PyErr_NoMemory();
1216 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001217
Brian Curtin60853212010-05-26 17:43:50 +00001218 while (1) {
Brian Curtin60853212010-05-26 17:43:50 +00001219 Py_BEGIN_ALLOW_THREADS
1220 rc = RegEnumValueW(hKey,
1221 index,
1222 retValueBuf,
1223 &retValueSize,
1224 NULL,
1225 &typ,
1226 (BYTE *)retDataBuf,
1227 &retDataSize);
1228 Py_END_ALLOW_THREADS
1229
1230 if (rc != ERROR_MORE_DATA)
1231 break;
1232
1233 bufDataSize *= 2;
Amaury Forgeot d'Arcf2b69df2010-08-16 22:11:29 +00001234 tmpBuf = (BYTE *)PyMem_Realloc(retDataBuf, bufDataSize);
Brian Curtin9b7e2d12010-06-08 20:57:52 +00001235 if (tmpBuf == NULL) {
Brian Curtin60853212010-05-26 17:43:50 +00001236 PyErr_NoMemory();
1237 retVal = NULL;
1238 goto fail;
1239 }
Brian Curtin9b7e2d12010-06-08 20:57:52 +00001240 retDataBuf = tmpBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001241 retDataSize = bufDataSize;
1242 retValueSize = bufValueSize;
1243 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 if (rc != ERROR_SUCCESS) {
1246 retVal = PyErr_SetFromWindowsErrWithFunction(rc,
1247 "PyRegEnumValue");
1248 goto fail;
1249 }
1250 obData = Reg2Py(retDataBuf, retDataSize, typ);
1251 if (obData == NULL) {
1252 retVal = NULL;
1253 goto fail;
1254 }
1255 retVal = Py_BuildValue("uOi", retValueBuf, obData, typ);
1256 Py_DECREF(obData);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001257 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 PyMem_Free(retValueBuf);
1259 PyMem_Free(retDataBuf);
1260 return retVal;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001261}
1262
1263static PyObject *
Christian Heimes2380ac72008-01-09 00:17:24 +00001264PyExpandEnvironmentStrings(PyObject *self, PyObject *args)
1265{
Victor Stinner9d3b93b2011-11-22 02:27:30 +01001266 wchar_t *retValue = NULL;
1267 wchar_t *src;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 DWORD retValueSize;
1269 DWORD rc;
1270 PyObject *o;
Christian Heimes2380ac72008-01-09 00:17:24 +00001271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 if (!PyArg_ParseTuple(args, "u:ExpandEnvironmentStrings", &src))
1273 return NULL;
Christian Heimes2380ac72008-01-09 00:17:24 +00001274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 retValueSize = ExpandEnvironmentStringsW(src, retValue, 0);
1276 if (retValueSize == 0) {
1277 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1278 "ExpandEnvironmentStrings");
1279 }
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02001280 retValue = PyMem_New(wchar_t, retValueSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 if (retValue == NULL) {
1282 return PyErr_NoMemory();
1283 }
Christian Heimes2380ac72008-01-09 00:17:24 +00001284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 rc = ExpandEnvironmentStringsW(src, retValue, retValueSize);
1286 if (rc == 0) {
1287 PyMem_Free(retValue);
1288 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1289 "ExpandEnvironmentStrings");
1290 }
Victor Stinner9d3b93b2011-11-22 02:27:30 +01001291 o = PyUnicode_FromWideChar(retValue, wcslen(retValue));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 PyMem_Free(retValue);
1293 return o;
Christian Heimes2380ac72008-01-09 00:17:24 +00001294}
1295
1296static PyObject *
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001297PyFlushKey(PyObject *self, PyObject *args)
1298{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 HKEY hKey;
1300 PyObject *obKey;
1301 long rc;
1302 if (!PyArg_ParseTuple(args, "O:FlushKey", &obKey))
1303 return NULL;
1304 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1305 return NULL;
1306 Py_BEGIN_ALLOW_THREADS
1307 rc = RegFlushKey(hKey);
1308 Py_END_ALLOW_THREADS
1309 if (rc != ERROR_SUCCESS)
1310 return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey");
1311 Py_INCREF(Py_None);
1312 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001313}
1314static PyObject *
1315PyLoadKey(PyObject *self, PyObject *args)
1316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 HKEY hKey;
1318 PyObject *obKey;
1319 wchar_t *subKey;
1320 wchar_t *fileName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 long rc;
1323 if (!PyArg_ParseTuple(args, "Ouu:LoadKey", &obKey, &subKey, &fileName))
1324 return NULL;
1325 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1326 return NULL;
1327 Py_BEGIN_ALLOW_THREADS
1328 rc = RegLoadKeyW(hKey, subKey, fileName );
1329 Py_END_ALLOW_THREADS
1330 if (rc != ERROR_SUCCESS)
1331 return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey");
1332 Py_INCREF(Py_None);
1333 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001334}
1335
1336static PyObject *
Brian Curtin1771b542010-09-27 17:56:36 +00001337PyOpenKey(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 HKEY hKey;
Brian Curtin1771b542010-09-27 17:56:36 +00001340 PyObject *key;
1341 wchar_t *sub_key;
1342 int reserved = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 HKEY retKey;
1344 long rc;
Brian Curtin1771b542010-09-27 17:56:36 +00001345 REGSAM access = KEY_READ;
1346
1347 char *kwlist[] = {"key", "sub_key", "reserved", "access", NULL};
1348
1349 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OZ|ii:OpenKey", kwlist,
1350 &key, &sub_key, &reserved, &access))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 return NULL;
Brian Curtin1771b542010-09-27 17:56:36 +00001352 if (!PyHKEY_AsHKEY(key, &hKey, FALSE))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 Py_BEGIN_ALLOW_THREADS
Brian Curtin1771b542010-09-27 17:56:36 +00001356 rc = RegOpenKeyExW(hKey, sub_key, reserved, access, &retKey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 Py_END_ALLOW_THREADS
1358 if (rc != ERROR_SUCCESS)
1359 return PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx");
1360 return PyHKEY_FromHKEY(retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001361}
1362
1363
1364static PyObject *
1365PyQueryInfoKey(PyObject *self, PyObject *args)
1366{
1367 HKEY hKey;
1368 PyObject *obKey;
1369 long rc;
1370 DWORD nSubKeys, nValues;
1371 FILETIME ft;
1372 LARGE_INTEGER li;
1373 PyObject *l;
1374 PyObject *ret;
1375 if (!PyArg_ParseTuple(args, "O:QueryInfoKey", &obKey))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001377 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001379 if ((rc = RegQueryInfoKey(hKey, NULL, NULL, 0, &nSubKeys, NULL, NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 &nValues, NULL, NULL, NULL, &ft))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001381 != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey");
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001383 li.LowPart = ft.dwLowDateTime;
1384 li.HighPart = ft.dwHighDateTime;
1385 l = PyLong_FromLongLong(li.QuadPart);
1386 if (l == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001388 ret = Py_BuildValue("iiO", nSubKeys, nValues, l);
1389 Py_DECREF(l);
1390 return ret;
1391}
1392
1393static PyObject *
1394PyQueryValue(PyObject *self, PyObject *args)
1395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 HKEY hKey;
1397 PyObject *obKey;
1398 wchar_t *subKey;
1399 long rc;
1400 PyObject *retStr;
1401 wchar_t *retBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001402 DWORD bufSize = 0;
1403 DWORD retSize = 0;
1404 wchar_t *tmp;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 if (!PyArg_ParseTuple(args, "OZ:QueryValue", &obKey, &subKey))
1407 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1410 return NULL;
Brian Curtin60853212010-05-26 17:43:50 +00001411
1412 rc = RegQueryValueW(hKey, subKey, NULL, &retSize);
1413 if (rc == ERROR_MORE_DATA)
1414 retSize = 256;
1415 else if (rc != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 return PyErr_SetFromWindowsErrWithFunction(rc,
1417 "RegQueryValue");
Brian Curtin60853212010-05-26 17:43:50 +00001418
1419 bufSize = retSize;
1420 retBuf = (wchar_t *) PyMem_Malloc(bufSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 if (retBuf == NULL)
1422 return PyErr_NoMemory();
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001423
Brian Curtin60853212010-05-26 17:43:50 +00001424 while (1) {
1425 retSize = bufSize;
1426 rc = RegQueryValueW(hKey, subKey, retBuf, &retSize);
1427 if (rc != ERROR_MORE_DATA)
1428 break;
1429
1430 bufSize *= 2;
1431 tmp = (wchar_t *) PyMem_Realloc(retBuf, bufSize);
1432 if (tmp == NULL) {
1433 PyMem_Free(retBuf);
1434 return PyErr_NoMemory();
1435 }
1436 retBuf = tmp;
1437 }
1438
1439 if (rc != ERROR_SUCCESS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 PyMem_Free(retBuf);
1441 return PyErr_SetFromWindowsErrWithFunction(rc,
1442 "RegQueryValue");
1443 }
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001444
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001445 retStr = PyUnicode_FromWideChar(retBuf, wcslen(retBuf));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 PyMem_Free(retBuf);
1447 return retStr;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001448}
1449
1450static PyObject *
1451PyQueryValueEx(PyObject *self, PyObject *args)
1452{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 HKEY hKey;
1454 PyObject *obKey;
1455 wchar_t *valueName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 long rc;
Brian Curtin60853212010-05-26 17:43:50 +00001458 BYTE *retBuf, *tmp;
1459 DWORD bufSize = 0, retSize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 DWORD typ;
1461 PyObject *obData;
1462 PyObject *result;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 if (!PyArg_ParseTuple(args, "OZ:QueryValueEx", &obKey, &valueName))
1465 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1468 return NULL;
Brian Curtin60853212010-05-26 17:43:50 +00001469
1470 rc = RegQueryValueExW(hKey, valueName, NULL, NULL, NULL, &bufSize);
1471 if (rc == ERROR_MORE_DATA)
1472 bufSize = 256;
1473 else if (rc != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 return PyErr_SetFromWindowsErrWithFunction(rc,
1475 "RegQueryValueEx");
1476 retBuf = (BYTE *)PyMem_Malloc(bufSize);
1477 if (retBuf == NULL)
1478 return PyErr_NoMemory();
Brian Curtin60853212010-05-26 17:43:50 +00001479
1480 while (1) {
1481 retSize = bufSize;
1482 rc = RegQueryValueExW(hKey, valueName, NULL, &typ,
1483 (BYTE *)retBuf, &retSize);
1484 if (rc != ERROR_MORE_DATA)
1485 break;
1486
1487 bufSize *= 2;
1488 tmp = (char *) PyMem_Realloc(retBuf, bufSize);
1489 if (tmp == NULL) {
1490 PyMem_Free(retBuf);
1491 return PyErr_NoMemory();
1492 }
1493 retBuf = tmp;
1494 }
1495
1496 if (rc != ERROR_SUCCESS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 PyMem_Free(retBuf);
1498 return PyErr_SetFromWindowsErrWithFunction(rc,
1499 "RegQueryValueEx");
1500 }
1501 obData = Reg2Py(retBuf, bufSize, typ);
1502 PyMem_Free(retBuf);
1503 if (obData == NULL)
1504 return NULL;
1505 result = Py_BuildValue("Oi", obData, typ);
1506 Py_DECREF(obData);
1507 return result;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001508}
1509
1510
1511static PyObject *
1512PySaveKey(PyObject *self, PyObject *args)
1513{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 HKEY hKey;
1515 PyObject *obKey;
1516 wchar_t *fileName;
1517 LPSECURITY_ATTRIBUTES pSA = NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 long rc;
1520 if (!PyArg_ParseTuple(args, "Ou:SaveKey", &obKey, &fileName))
1521 return NULL;
1522 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1523 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001524/* One day we may get security into the core?
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE))
1526 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001527*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 Py_BEGIN_ALLOW_THREADS
1529 rc = RegSaveKeyW(hKey, fileName, pSA );
1530 Py_END_ALLOW_THREADS
1531 if (rc != ERROR_SUCCESS)
1532 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey");
1533 Py_INCREF(Py_None);
1534 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001535}
1536
1537static PyObject *
1538PySetValue(PyObject *self, PyObject *args)
1539{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 HKEY hKey;
1541 PyObject *obKey;
1542 wchar_t *subKey;
1543 wchar_t *str;
1544 DWORD typ;
1545 DWORD len;
1546 long rc;
1547 if (!PyArg_ParseTuple(args, "OZiu#:SetValue",
1548 &obKey,
1549 &subKey,
1550 &typ,
1551 &str,
1552 &len))
1553 return NULL;
1554 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1555 return NULL;
1556 if (typ != REG_SZ) {
1557 PyErr_SetString(PyExc_TypeError,
1558 "Type must be winreg.REG_SZ");
1559 return NULL;
1560 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 Py_BEGIN_ALLOW_THREADS
1563 rc = RegSetValueW(hKey, subKey, REG_SZ, str, len+1);
1564 Py_END_ALLOW_THREADS
1565 if (rc != ERROR_SUCCESS)
1566 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue");
1567 Py_INCREF(Py_None);
1568 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001569}
1570
1571static PyObject *
1572PySetValueEx(PyObject *self, PyObject *args)
1573{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 HKEY hKey;
1575 PyObject *obKey;
Victor Stinner9d3b93b2011-11-22 02:27:30 +01001576 wchar_t *valueName;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 PyObject *obRes;
1578 PyObject *value;
1579 BYTE *data;
1580 DWORD len;
1581 DWORD typ;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 LONG rc;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 if (!PyArg_ParseTuple(args, "OZOiO:SetValueEx",
1586 &obKey,
1587 &valueName,
1588 &obRes,
1589 &typ,
1590 &value))
1591 return NULL;
1592 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1593 return NULL;
1594 if (!Py2Reg(value, typ, &data, &len))
1595 {
1596 if (!PyErr_Occurred())
1597 PyErr_SetString(PyExc_ValueError,
1598 "Could not convert the data to the specified type.");
1599 return NULL;
1600 }
1601 Py_BEGIN_ALLOW_THREADS
1602 rc = RegSetValueExW(hKey, valueName, 0, typ, data, len);
1603 Py_END_ALLOW_THREADS
1604 PyMem_DEL(data);
1605 if (rc != ERROR_SUCCESS)
1606 return PyErr_SetFromWindowsErrWithFunction(rc,
1607 "RegSetValueEx");
1608 Py_INCREF(Py_None);
1609 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001610}
1611
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001612static PyObject *
1613PyDisableReflectionKey(PyObject *self, PyObject *args)
1614{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 HKEY hKey;
1616 PyObject *obKey;
1617 HMODULE hMod;
1618 typedef LONG (WINAPI *RDRKFunc)(HKEY);
1619 RDRKFunc pfn = NULL;
1620 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 if (!PyArg_ParseTuple(args, "O:DisableReflectionKey", &obKey))
1623 return NULL;
1624 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1625 return NULL;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 /* Only available on 64bit platforms, so we must load it
1628 dynamically.*/
Martin v. Löwis50590f12012-01-14 17:54:09 +01001629 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 if (hMod)
1631 pfn = (RDRKFunc)GetProcAddress(hMod,
1632 "RegDisableReflectionKey");
1633 if (!pfn) {
1634 PyErr_SetString(PyExc_NotImplementedError,
1635 "not implemented on this platform");
1636 return NULL;
1637 }
1638 Py_BEGIN_ALLOW_THREADS
1639 rc = (*pfn)(hKey);
1640 Py_END_ALLOW_THREADS
1641 if (rc != ERROR_SUCCESS)
1642 return PyErr_SetFromWindowsErrWithFunction(rc,
1643 "RegDisableReflectionKey");
1644 Py_INCREF(Py_None);
1645 return Py_None;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001646}
1647
1648static PyObject *
1649PyEnableReflectionKey(PyObject *self, PyObject *args)
1650{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 HKEY hKey;
1652 PyObject *obKey;
1653 HMODULE hMod;
1654 typedef LONG (WINAPI *RERKFunc)(HKEY);
1655 RERKFunc pfn = NULL;
1656 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 if (!PyArg_ParseTuple(args, "O:EnableReflectionKey", &obKey))
1659 return NULL;
1660 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1661 return NULL;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 /* Only available on 64bit platforms, so we must load it
1664 dynamically.*/
Martin v. Löwis50590f12012-01-14 17:54:09 +01001665 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 if (hMod)
1667 pfn = (RERKFunc)GetProcAddress(hMod,
1668 "RegEnableReflectionKey");
1669 if (!pfn) {
1670 PyErr_SetString(PyExc_NotImplementedError,
1671 "not implemented on this platform");
1672 return NULL;
1673 }
1674 Py_BEGIN_ALLOW_THREADS
1675 rc = (*pfn)(hKey);
1676 Py_END_ALLOW_THREADS
1677 if (rc != ERROR_SUCCESS)
1678 return PyErr_SetFromWindowsErrWithFunction(rc,
1679 "RegEnableReflectionKey");
1680 Py_INCREF(Py_None);
1681 return Py_None;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001682}
1683
1684static PyObject *
1685PyQueryReflectionKey(PyObject *self, PyObject *args)
1686{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 HKEY hKey;
1688 PyObject *obKey;
1689 HMODULE hMod;
1690 typedef LONG (WINAPI *RQRKFunc)(HKEY, BOOL *);
1691 RQRKFunc pfn = NULL;
1692 BOOL result;
1693 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 if (!PyArg_ParseTuple(args, "O:QueryReflectionKey", &obKey))
1696 return NULL;
1697 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1698 return NULL;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 /* Only available on 64bit platforms, so we must load it
1701 dynamically.*/
Martin v. Löwis50590f12012-01-14 17:54:09 +01001702 hMod = GetModuleHandleW(L"advapi32.dll");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 if (hMod)
1704 pfn = (RQRKFunc)GetProcAddress(hMod,
1705 "RegQueryReflectionKey");
1706 if (!pfn) {
1707 PyErr_SetString(PyExc_NotImplementedError,
1708 "not implemented on this platform");
1709 return NULL;
1710 }
1711 Py_BEGIN_ALLOW_THREADS
1712 rc = (*pfn)(hKey, &result);
1713 Py_END_ALLOW_THREADS
1714 if (rc != ERROR_SUCCESS)
1715 return PyErr_SetFromWindowsErrWithFunction(rc,
1716 "RegQueryReflectionKey");
1717 return PyBool_FromLong(result);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001718}
1719
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001720static struct PyMethodDef winreg_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 {"CloseKey", PyCloseKey, METH_VARARGS, CloseKey_doc},
1722 {"ConnectRegistry", PyConnectRegistry, METH_VARARGS, ConnectRegistry_doc},
1723 {"CreateKey", PyCreateKey, METH_VARARGS, CreateKey_doc},
Brian Curtin1771b542010-09-27 17:56:36 +00001724 {"CreateKeyEx", (PyCFunction)PyCreateKeyEx,
1725 METH_VARARGS | METH_KEYWORDS, CreateKeyEx_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 {"DeleteKey", PyDeleteKey, METH_VARARGS, DeleteKey_doc},
Brian Curtin1771b542010-09-27 17:56:36 +00001727 {"DeleteKeyEx", (PyCFunction)PyDeleteKeyEx,
1728 METH_VARARGS | METH_KEYWORDS, DeleteKeyEx_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 {"DeleteValue", PyDeleteValue, METH_VARARGS, DeleteValue_doc},
1730 {"DisableReflectionKey", PyDisableReflectionKey, METH_VARARGS, DisableReflectionKey_doc},
1731 {"EnableReflectionKey", PyEnableReflectionKey, METH_VARARGS, EnableReflectionKey_doc},
1732 {"EnumKey", PyEnumKey, METH_VARARGS, EnumKey_doc},
1733 {"EnumValue", PyEnumValue, METH_VARARGS, EnumValue_doc},
1734 {"ExpandEnvironmentStrings", PyExpandEnvironmentStrings, METH_VARARGS,
1735 ExpandEnvironmentStrings_doc },
1736 {"FlushKey", PyFlushKey, METH_VARARGS, FlushKey_doc},
1737 {"LoadKey", PyLoadKey, METH_VARARGS, LoadKey_doc},
Brian Curtin1771b542010-09-27 17:56:36 +00001738 {"OpenKey", (PyCFunction)PyOpenKey, METH_VARARGS | METH_KEYWORDS,
1739 OpenKey_doc},
1740 {"OpenKeyEx", (PyCFunction)PyOpenKey, METH_VARARGS | METH_KEYWORDS,
1741 OpenKeyEx_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 {"QueryValue", PyQueryValue, METH_VARARGS, QueryValue_doc},
1743 {"QueryValueEx", PyQueryValueEx, METH_VARARGS, QueryValueEx_doc},
1744 {"QueryInfoKey", PyQueryInfoKey, METH_VARARGS, QueryInfoKey_doc},
1745 {"QueryReflectionKey",PyQueryReflectionKey,METH_VARARGS, QueryReflectionKey_doc},
1746 {"SaveKey", PySaveKey, METH_VARARGS, SaveKey_doc},
1747 {"SetValue", PySetValue, METH_VARARGS, SetValue_doc},
1748 {"SetValueEx", PySetValueEx, METH_VARARGS, SetValueEx_doc},
1749 NULL,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001750};
1751
1752static void
1753insint(PyObject * d, char * name, long value)
1754{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 PyObject *v = PyLong_FromLong(value);
1756 if (!v || PyDict_SetItemString(d, name, v))
1757 PyErr_Clear();
1758 Py_XDECREF(v);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001759}
1760
1761#define ADD_INT(val) insint(d, #val, val)
1762
1763static void
1764inskey(PyObject * d, char * name, HKEY key)
1765{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 PyObject *v = PyLong_FromVoidPtr(key);
1767 if (!v || PyDict_SetItemString(d, name, v))
1768 PyErr_Clear();
1769 Py_XDECREF(v);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001770}
1771
1772#define ADD_KEY(val) inskey(d, #val, val)
1773
Martin v. Löwis1a214512008-06-11 05:26:20 +00001774
1775static struct PyModuleDef winregmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 PyModuleDef_HEAD_INIT,
1777 "winreg",
1778 module_doc,
1779 -1,
1780 winreg_methods,
1781 NULL,
1782 NULL,
1783 NULL,
1784 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001785};
1786
1787PyMODINIT_FUNC PyInit_winreg(void)
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001788{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 PyObject *m, *d;
1790 m = PyModule_Create(&winregmodule);
1791 if (m == NULL)
1792 return NULL;
1793 d = PyModule_GetDict(m);
1794 PyHKEY_Type.tp_doc = PyHKEY_doc;
1795 if (PyType_Ready(&PyHKEY_Type) < 0)
1796 return NULL;
1797 Py_INCREF(&PyHKEY_Type);
1798 if (PyDict_SetItemString(d, "HKEYType",
1799 (PyObject *)&PyHKEY_Type) != 0)
1800 return NULL;
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001801 Py_INCREF(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001802 if (PyDict_SetItemString(d, "error",
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001803 PyExc_OSError) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001804 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806 /* Add the relevant constants */
1807 ADD_KEY(HKEY_CLASSES_ROOT);
1808 ADD_KEY(HKEY_CURRENT_USER);
1809 ADD_KEY(HKEY_LOCAL_MACHINE);
1810 ADD_KEY(HKEY_USERS);
1811 ADD_KEY(HKEY_PERFORMANCE_DATA);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001812#ifdef HKEY_CURRENT_CONFIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 ADD_KEY(HKEY_CURRENT_CONFIG);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001814#endif
1815#ifdef HKEY_DYN_DATA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 ADD_KEY(HKEY_DYN_DATA);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001817#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 ADD_INT(KEY_QUERY_VALUE);
1819 ADD_INT(KEY_SET_VALUE);
1820 ADD_INT(KEY_CREATE_SUB_KEY);
1821 ADD_INT(KEY_ENUMERATE_SUB_KEYS);
1822 ADD_INT(KEY_NOTIFY);
1823 ADD_INT(KEY_CREATE_LINK);
1824 ADD_INT(KEY_READ);
1825 ADD_INT(KEY_WRITE);
1826 ADD_INT(KEY_EXECUTE);
1827 ADD_INT(KEY_ALL_ACCESS);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001828#ifdef KEY_WOW64_64KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 ADD_INT(KEY_WOW64_64KEY);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001830#endif
1831#ifdef KEY_WOW64_32KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 ADD_INT(KEY_WOW64_32KEY);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001833#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 ADD_INT(REG_OPTION_RESERVED);
1835 ADD_INT(REG_OPTION_NON_VOLATILE);
1836 ADD_INT(REG_OPTION_VOLATILE);
1837 ADD_INT(REG_OPTION_CREATE_LINK);
1838 ADD_INT(REG_OPTION_BACKUP_RESTORE);
1839 ADD_INT(REG_OPTION_OPEN_LINK);
1840 ADD_INT(REG_LEGAL_OPTION);
1841 ADD_INT(REG_CREATED_NEW_KEY);
1842 ADD_INT(REG_OPENED_EXISTING_KEY);
1843 ADD_INT(REG_WHOLE_HIVE_VOLATILE);
1844 ADD_INT(REG_REFRESH_HIVE);
1845 ADD_INT(REG_NO_LAZY_FLUSH);
1846 ADD_INT(REG_NOTIFY_CHANGE_NAME);
1847 ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES);
1848 ADD_INT(REG_NOTIFY_CHANGE_LAST_SET);
1849 ADD_INT(REG_NOTIFY_CHANGE_SECURITY);
1850 ADD_INT(REG_LEGAL_CHANGE_FILTER);
1851 ADD_INT(REG_NONE);
1852 ADD_INT(REG_SZ);
1853 ADD_INT(REG_EXPAND_SZ);
1854 ADD_INT(REG_BINARY);
1855 ADD_INT(REG_DWORD);
1856 ADD_INT(REG_DWORD_LITTLE_ENDIAN);
1857 ADD_INT(REG_DWORD_BIG_ENDIAN);
1858 ADD_INT(REG_LINK);
1859 ADD_INT(REG_MULTI_SZ);
1860 ADD_INT(REG_RESOURCE_LIST);
1861 ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
1862 ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
1863 return m;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001864}
1865
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001866