blob: 56bda5c258999267c581c86cdbc1fd3c65a21331 [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"
53"OpenKey() - Alias for <om win32api.RegOpenKeyEx>\n"
54"OpenKeyEx() - Opens the specified key.\n"
55"QueryValue() - Retrieves the value associated with the unnamed value for a\n"
56" specified key in the registry.\n"
57"QueryValueEx() - Retrieves the type and data for a specified value name\n"
58" associated with an open registry key.\n"
59"QueryInfoKey() - Returns information about the specified key.\n"
60"SaveKey() - Saves the specified key, and all its subkeys a file.\n"
61"SetValue() - Associates a value with a specified key.\n"
62"SetValueEx() - Stores data in the value field of an open registry key.\n"
63"\n"
64"Special objects:\n"
65"\n"
66"HKEYType -- type object for HKEY objects\n"
67"error -- exception raised for Win32 errors\n"
68"\n"
69"Integer constants:\n"
70"Many constants are defined - see the documentation for each function\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000071"to see what constants are used, and where.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +000072
73
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000074PyDoc_STRVAR(CloseKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +000075"CloseKey(hkey) - Closes a previously opened registry key.\n"
76"\n"
77"The hkey argument specifies a previously opened key.\n"
78"\n"
79"Note that if the key is not closed using this method, it will be\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000080"closed when the hkey object is destroyed by Python.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +000081
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000082PyDoc_STRVAR(ConnectRegistry_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +000083"key = ConnectRegistry(computer_name, key) - "
84"Establishes a connection to a predefined registry handle on another computer.\n"
85"\n"
86"computer_name is the name of the remote computer, of the form \\\\computername.\n"
87" If None, the local computer is used.\n"
88"key is the predefined handle to connect to.\n"
89"\n"
90"The return value is the handle of the opened key.\n"
Brian Curtin3035c392010-04-21 23:56:21 +000091"If the function fails, a WindowsError exception is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +000092
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000093PyDoc_STRVAR(CreateKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +000094"key = CreateKey(key, sub_key) - Creates or opens the specified key.\n"
95"\n"
96"key is an already open key, or one of the predefined HKEY_* constants\n"
97"sub_key is a string that names the key this method opens or creates.\n"
98" If key is one of the predefined keys, sub_key may be None. In that case,\n"
99" the handle returned is the same key handle passed in to the function.\n"
100"\n"
101"If the key already exists, this function opens the existing key\n"
102"\n"
103"The return value is the handle of the opened key.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000104"If the function fails, an exception is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000105
Brian Curtin3035c392010-04-21 23:56:21 +0000106PyDoc_STRVAR(CreateKeyEx_doc,
107"key = CreateKeyEx(key, sub_key, res, sam) - Creates or opens the specified key.\n"
108"\n"
109"key is an already open key, or one of the predefined HKEY_* constants\n"
110"sub_key is a string that names the key this method opens or creates.\n"
111"res is a reserved integer, and must be zero. Default is zero.\n"
112"sam is an integer that specifies an access mask that describes the desired\n"
113" If key is one of the predefined keys, sub_key may be None. In that case,\n"
114" the handle returned is the same key handle passed in to the function.\n"
115"\n"
116"If the key already exists, this function opens the existing key\n"
117"\n"
118"The return value is the handle of the opened key.\n"
119"If the function fails, an exception is raised.");
120
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000121PyDoc_STRVAR(DeleteKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000122"DeleteKey(key, sub_key) - Deletes the specified key.\n"
123"\n"
124"key is an already open key, or any one of the predefined HKEY_* constants.\n"
125"sub_key is a string that must be a subkey of the key identified by the key parameter.\n"
126" This value must not be None, and the key may not have subkeys.\n"
127"\n"
128"This method can not delete keys with subkeys.\n"
129"\n"
130"If the method succeeds, the entire key, including all of its values,\n"
Brian Curtin3035c392010-04-21 23:56:21 +0000131"is removed. If the method fails, a WindowsError exception is raised.");
132
133PyDoc_STRVAR(DeleteKeyEx_doc,
134"DeleteKeyEx(key, sub_key, sam, res) - Deletes the specified key.\n"
135"\n"
136"key is an already open key, or any one of the predefined HKEY_* constants.\n"
137"sub_key is a string that must be a subkey of the key identified by the key parameter.\n"
138"res is a reserved integer, and must be zero. Default is zero.\n"
139"sam is an integer that specifies an access mask that describes the desired\n"
140" This value must not be None, and the key may not have subkeys.\n"
141"\n"
142"This method can not delete keys with subkeys.\n"
143"\n"
144"If the method succeeds, the entire key, including all of its values,\n"
145"is removed. If the method fails, a WindowsError exception is raised.\n"
146"On unsupported Windows versions, NotImplementedError is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000147
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000148PyDoc_STRVAR(DeleteValue_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000149"DeleteValue(key, value) - Removes a named value from a registry key.\n"
150"\n"
151"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000152"value is a string that identifies the value to remove.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000153
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000154PyDoc_STRVAR(EnumKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000155"string = EnumKey(key, index) - Enumerates subkeys of an open registry key.\n"
156"\n"
157"key is an already open key, or any one of the predefined HKEY_* constants.\n"
158"index is an integer that identifies the index of the key to retrieve.\n"
159"\n"
160"The function retrieves the name of one subkey each time it is called.\n"
Brian Curtin3035c392010-04-21 23:56:21 +0000161"It is typically called repeatedly until a WindowsError exception is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000162"raised, indicating no more values are available.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000163
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000164PyDoc_STRVAR(EnumValue_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000165"tuple = EnumValue(key, index) - Enumerates values of an open registry key.\n"
166"key is an already open key, or any one of the predefined HKEY_* constants.\n"
167"index is an integer that identifies the index of the value to retrieve.\n"
168"\n"
169"The function retrieves the name of one subkey each time it is called.\n"
Brian Curtin3035c392010-04-21 23:56:21 +0000170"It is typically called repeatedly, until a WindowsError exception\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000171"is raised, indicating no more values.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000172"\n"
173"The result is a tuple of 3 items:\n"
174"value_name is a string that identifies the value.\n"
175"value_data is an object that holds the value data, and whose type depends\n"
176" on the underlying registry type.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000177"data_type is an integer that identifies the type of the value data.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000178
Christian Heimes2380ac72008-01-09 00:17:24 +0000179PyDoc_STRVAR(ExpandEnvironmentStrings_doc,
180"string = ExpandEnvironmentStrings(string) - Expand environment vars.\n");
181
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000182PyDoc_STRVAR(FlushKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000183"FlushKey(key) - Writes all the attributes of a key to the registry.\n"
184"\n"
185"key is an already open key, or any one of the predefined HKEY_* constants.\n"
186"\n"
187"It is not necessary to call RegFlushKey to change a key.\n"
188"Registry changes are flushed to disk by the registry using its lazy flusher.\n"
189"Registry changes are also flushed to disk at system shutdown.\n"
190"Unlike CloseKey(), the FlushKey() method returns only when all the data has\n"
191"been written to the registry.\n"
192"An application should only call FlushKey() if it requires absolute certainty that registry changes are on disk.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000193"If you don't know whether a FlushKey() call is required, it probably isn't.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000194
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000195PyDoc_STRVAR(LoadKey_doc,
Mark Hammondb422f952000-06-09 06:01:47 +0000196"LoadKey(key, sub_key, file_name) - Creates a subkey under the specified key\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000197"and stores registration information from a specified file into that subkey.\n"
198"\n"
199"key is an already open key, or any one of the predefined HKEY_* constants.\n"
200"sub_key is a string that identifies the sub_key to load\n"
201"file_name is the name of the file to load registry data from.\n"
202" This file must have been created with the SaveKey() function.\n"
203" Under the file allocation table (FAT) file system, the filename may not\n"
204"have an extension.\n"
205"\n"
206"A call to LoadKey() fails if the calling process does not have the\n"
207"SE_RESTORE_PRIVILEGE privilege.\n"
208"\n"
209"If key is a handle returned by ConnectRegistry(), then the path specified\n"
210"in fileName is relative to the remote computer.\n"
211"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000212"The docs imply key must be in the HKEY_USER or HKEY_LOCAL_MACHINE tree");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000213
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000214PyDoc_STRVAR(OpenKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000215"key = OpenKey(key, sub_key, res = 0, sam = KEY_READ) - Opens the specified key.\n"
216"\n"
217"key is an already open key, or any one of the predefined HKEY_* constants.\n"
218"sub_key is a string that identifies the sub_key to open\n"
219"res is a reserved integer, and must be zero. Default is zero.\n"
220"sam is an integer that specifies an access mask that describes the desired\n"
221" security access for the key. Default is KEY_READ\n"
222"\n"
223"The result is a new handle to the specified key\n"
Brian Curtin3035c392010-04-21 23:56:21 +0000224"If the function fails, a WindowsError exception is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000225
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000226PyDoc_STRVAR(OpenKeyEx_doc, "See OpenKey()");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000227
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000228PyDoc_STRVAR(QueryInfoKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000229"tuple = QueryInfoKey(key) - Returns information about a key.\n"
230"\n"
231"key is an already open key, or any one of the predefined HKEY_* constants.\n"
232"\n"
233"The result is a tuple of 3 items:"
234"An integer that identifies the number of sub keys this key has.\n"
235"An integer that identifies the number of values this key has.\n"
236"A long integer that identifies when the key was last modified (if available)\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000237" as 100's of nanoseconds since Jan 1, 1600.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000238
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000239PyDoc_STRVAR(QueryValue_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000240"string = QueryValue(key, sub_key) - retrieves the unnamed value for a key.\n"
241"\n"
242"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000243"sub_key is a string that holds the name of the subkey with which the value\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000244" is associated. If this parameter is None or empty, the function retrieves\n"
245" the value set by the SetValue() method for the key identified by key."
246"\n"
247"Values in the registry have name, type, and data components. This method\n"
248"retrieves the data for a key's first value that has a NULL name.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000249"But the underlying API call doesn't return the type, Lame Lame Lame, DONT USE THIS!!!");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000250
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000251PyDoc_STRVAR(QueryValueEx_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000252"value,type_id = QueryValueEx(key, value_name) - Retrieves the type and data for a specified value name associated with an open registry key.\n"
253"\n"
254"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000255"value_name is a string indicating the value to query");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000256
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000257PyDoc_STRVAR(SaveKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000258"SaveKey(key, file_name) - Saves the specified key, and all its subkeys to the specified file.\n"
259"\n"
260"key is an already open key, or any one of the predefined HKEY_* constants.\n"
261"file_name is the name of the file to save registry data to.\n"
262" This file cannot already exist. If this filename includes an extension,\n"
263" it cannot be used on file allocation table (FAT) file systems by the\n"
264" LoadKey(), ReplaceKey() or RestoreKey() methods.\n"
265"\n"
266"If key represents a key on a remote computer, the path described by\n"
267"file_name is relative to the remote computer.\n"
268"The caller of this method must possess the SeBackupPrivilege security privilege.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000269"This function passes NULL for security_attributes to the API.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000270
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000271PyDoc_STRVAR(SetValue_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000272"SetValue(key, sub_key, type, value) - Associates a value with a specified key.\n"
273"\n"
274"key is an already open key, or any one of the predefined HKEY_* constants.\n"
275"sub_key is a string that names the subkey with which the value is associated.\n"
276"type is an integer that specifies the type of the data. Currently this\n"
277" must be REG_SZ, meaning only strings are supported.\n"
278"value is a string that specifies the new value.\n"
279"\n"
280"If the key specified by the sub_key parameter does not exist, the SetValue\n"
281"function creates it.\n"
282"\n"
283"Value lengths are limited by available memory. Long values (more than\n"
284"2048 bytes) should be stored as files with the filenames stored in \n"
285"the configuration registry. This helps the registry perform efficiently.\n"
286"\n"
287"The key identified by the key parameter must have been opened with\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000288"KEY_SET_VALUE access.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000289
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000290PyDoc_STRVAR(SetValueEx_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000291"SetValueEx(key, value_name, reserved, type, value) - Stores data in the value field of an open registry key.\n"
292"\n"
293"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Mark Hammondc9083b62003-01-15 23:38:15 +0000294"value_name is a string containing the name of the value to set, or None\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000295"type is an integer that specifies the type of the data. This should be one of:\n"
296" REG_BINARY -- Binary data in any form.\n"
297" REG_DWORD -- A 32-bit number.\n"
298" REG_DWORD_LITTLE_ENDIAN -- A 32-bit number in little-endian format.\n"
299" REG_DWORD_BIG_ENDIAN -- A 32-bit number in big-endian format.\n"
300" REG_EXPAND_SZ -- A null-terminated string that contains unexpanded references\n"
301" to environment variables (for example, %PATH%).\n"
302" REG_LINK -- A Unicode symbolic link.\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000303" REG_MULTI_SZ -- An sequence of null-terminated strings, terminated by\n"
304" two null characters. Note that Python handles this\n"
305" termination automatically.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000306" REG_NONE -- No defined value type.\n"
307" REG_RESOURCE_LIST -- A device-driver resource list.\n"
308" REG_SZ -- A null-terminated string.\n"
309"reserved can be anything - zero is always passed to the API.\n"
310"value is a string that specifies the new value.\n"
311"\n"
312"This method can also set additional value and type information for the\n"
313"specified key. The key identified by the key parameter must have been\n"
314"opened with KEY_SET_VALUE access.\n"
315"\n"
316"To open the key, use the CreateKeyEx() or OpenKeyEx() methods.\n"
317"\n"
318"Value lengths are limited by available memory. Long values (more than\n"
319"2048 bytes) should be stored as files with the filenames stored in \n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000320"the configuration registry. This helps the registry perform efficiently.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000321
Martin v. Löwisd218dc12008-04-07 03:17:54 +0000322PyDoc_STRVAR(DisableReflectionKey_doc,
Christian Heimes5e696852008-04-09 08:37:03 +0000323"Disables registry reflection for 32-bit processes running on a 64-bit\n"
Martin v. Löwisd218dc12008-04-07 03:17:54 +0000324"Operating System. Will generally raise NotImplemented if executed on\n"
Christian Heimes5e696852008-04-09 08:37:03 +0000325"a 32-bit Operating System.\n"
Martin v. Löwisd218dc12008-04-07 03:17:54 +0000326"If the key is not on the reflection list, the function succeeds but has no effect.\n"
327"Disabling reflection for a key does not affect reflection of any subkeys.");
328
329PyDoc_STRVAR(EnableReflectionKey_doc,
330"Restores registry reflection for the specified disabled key.\n"
Christian Heimes5e696852008-04-09 08:37:03 +0000331"Will generally raise NotImplemented if executed on a 32-bit Operating System.\n"
Martin v. Löwisd218dc12008-04-07 03:17:54 +0000332"Restoring reflection for a key does not affect reflection of any subkeys.");
333
334PyDoc_STRVAR(QueryReflectionKey_doc,
335"bool = QueryReflectionKey(hkey) - Determines the reflection state for the specified key.\n"
Christian Heimes5e696852008-04-09 08:37:03 +0000336"Will generally raise NotImplemented if executed on a 32-bit Operating System.\n");
Martin v. Löwisd218dc12008-04-07 03:17:54 +0000337
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000338/* PyHKEY docstrings */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000339PyDoc_STRVAR(PyHKEY_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000340"PyHKEY Object - A Python object, representing a win32 registry key.\n"
341"\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000342"This object wraps a Windows HKEY object, automatically closing it when\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000343"the object is destroyed. To guarantee cleanup, you can call either\n"
344"the Close() method on the PyHKEY, or the CloseKey() method.\n"
345"\n"
346"All functions which accept a handle object also accept an integer - \n"
347"however, use of the handle object is encouraged.\n"
348"\n"
349"Functions:\n"
350"Close() - Closes the underlying handle.\n"
351"Detach() - Returns the integer Win32 handle, detaching it from the object\n"
352"\n"
353"Properties:\n"
354"handle - The integer Win32 handle.\n"
355"\n"
356"Operations:\n"
Jack Diederich4dafcc42006-11-28 19:15:13 +0000357"__bool__ - Handles with an open object return true, otherwise false.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000358"__int__ - Converting a handle to an integer returns the Win32 handle.\n"
Mark Dickinson211c6252009-02-01 10:28:51 +0000359"rich comparison - Handle objects are compared using the handle value.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000360
361
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000362PyDoc_STRVAR(PyHKEY_Close_doc,
Mark Hammondb422f952000-06-09 06:01:47 +0000363"key.Close() - Closes the underlying Windows handle.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000364"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000365"If the handle is already closed, no error is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000366
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000367PyDoc_STRVAR(PyHKEY_Detach_doc,
Mark Hammondb422f952000-06-09 06:01:47 +0000368"int = key.Detach() - Detaches the Windows handle from the handle object.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000369"\n"
370"The result is the value of the handle before it is detached. If the\n"
371"handle is already detached, this will return zero.\n"
372"\n"
373"After calling this function, the handle is effectively invalidated,\n"
374"but the handle is not closed. You would call this function when you\n"
375"need the underlying win32 handle to exist beyond the lifetime of the\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000376"handle object.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000377"On 64 bit windows, the result of this function is a long integer");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000378
379
380/************************************************************************
381
382 The PyHKEY object definition
383
384************************************************************************/
385typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 PyObject_VAR_HEAD
387 HKEY hkey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000388} PyHKEYObject;
389
390#define PyHKEY_Check(op) ((op)->ob_type == &PyHKEY_Type)
391
392static char *failMsg = "bad operand type";
393
394static PyObject *
395PyHKEY_unaryFailureFunc(PyObject *ob)
396{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 PyErr_SetString(PyExc_TypeError, failMsg);
398 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000399}
400static PyObject *
401PyHKEY_binaryFailureFunc(PyObject *ob1, PyObject *ob2)
402{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 PyErr_SetString(PyExc_TypeError, failMsg);
404 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000405}
406static PyObject *
407PyHKEY_ternaryFailureFunc(PyObject *ob1, PyObject *ob2, PyObject *ob3)
408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 PyErr_SetString(PyExc_TypeError, failMsg);
410 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000411}
412
413static void
414PyHKEY_deallocFunc(PyObject *ob)
415{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 /* Can not call PyHKEY_Close, as the ob->tp_type
417 has already been cleared, thus causing the type
418 check to fail!
419 */
420 PyHKEYObject *obkey = (PyHKEYObject *)ob;
421 if (obkey->hkey)
422 RegCloseKey((HKEY)obkey->hkey);
423 PyObject_DEL(ob);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000424}
425
426static int
Jack Diederich4dafcc42006-11-28 19:15:13 +0000427PyHKEY_boolFunc(PyObject *ob)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 return ((PyHKEYObject *)ob)->hkey != 0;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000430}
431
432static PyObject *
433PyHKEY_intFunc(PyObject *ob)
434{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
436 return PyLong_FromVoidPtr(pyhkey->hkey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000437}
438
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000439static PyObject *
440PyHKEY_strFunc(PyObject *ob)
441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
443 return PyUnicode_FromFormat("<PyHKEY:%p>", pyhkey->hkey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000444}
445
446static int
447PyHKEY_compareFunc(PyObject *ob1, PyObject *ob2)
448{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 PyHKEYObject *pyhkey1 = (PyHKEYObject *)ob1;
450 PyHKEYObject *pyhkey2 = (PyHKEYObject *)ob2;
451 return pyhkey1 == pyhkey2 ? 0 :
452 (pyhkey1 < pyhkey2 ? -1 : 1);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000453}
454
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000455static Py_hash_t
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000456PyHKEY_hashFunc(PyObject *ob)
457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 /* Just use the address.
459 XXX - should we use the handle value?
460 */
461 return _Py_HashPointer(ob);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000462}
463
464
465static PyNumberMethods PyHKEY_NumberMethods =
466{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 PyHKEY_binaryFailureFunc, /* nb_add */
468 PyHKEY_binaryFailureFunc, /* nb_subtract */
469 PyHKEY_binaryFailureFunc, /* nb_multiply */
470 PyHKEY_binaryFailureFunc, /* nb_remainder */
471 PyHKEY_binaryFailureFunc, /* nb_divmod */
472 PyHKEY_ternaryFailureFunc, /* nb_power */
473 PyHKEY_unaryFailureFunc, /* nb_negative */
474 PyHKEY_unaryFailureFunc, /* nb_positive */
475 PyHKEY_unaryFailureFunc, /* nb_absolute */
476 PyHKEY_boolFunc, /* nb_bool */
477 PyHKEY_unaryFailureFunc, /* nb_invert */
478 PyHKEY_binaryFailureFunc, /* nb_lshift */
479 PyHKEY_binaryFailureFunc, /* nb_rshift */
480 PyHKEY_binaryFailureFunc, /* nb_and */
481 PyHKEY_binaryFailureFunc, /* nb_xor */
482 PyHKEY_binaryFailureFunc, /* nb_or */
483 PyHKEY_intFunc, /* nb_int */
484 0, /* nb_reserved */
485 PyHKEY_unaryFailureFunc, /* nb_float */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000486};
487
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000488static PyObject *PyHKEY_CloseMethod(PyObject *self, PyObject *args);
489static PyObject *PyHKEY_DetachMethod(PyObject *self, PyObject *args);
490static PyObject *PyHKEY_Enter(PyObject *self);
491static PyObject *PyHKEY_Exit(PyObject *self, PyObject *args);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000492
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000493static struct PyMethodDef PyHKEY_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 {"Close", PyHKEY_CloseMethod, METH_VARARGS, PyHKEY_Close_doc},
495 {"Detach", PyHKEY_DetachMethod, METH_VARARGS, PyHKEY_Detach_doc},
496 {"__enter__", (PyCFunction)PyHKEY_Enter, METH_NOARGS, NULL},
497 {"__exit__", PyHKEY_Exit, METH_VARARGS, NULL},
498 {NULL}
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000499};
500
501#define OFF(e) offsetof(PyHKEYObject, e)
502static PyMemberDef PyHKEY_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 {"handle", T_INT, OFF(hkey), READONLY},
504 {NULL} /* Sentinel */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000505};
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000506
507/* The type itself */
508PyTypeObject PyHKEY_Type =
509{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 PyVarObject_HEAD_INIT(0, 0) /* fill in type at module init */
511 "PyHKEY",
512 sizeof(PyHKEYObject),
513 0,
514 PyHKEY_deallocFunc, /* tp_dealloc */
515 0, /* tp_print */
516 0, /* tp_getattr */
517 0, /* tp_setattr */
518 0, /* tp_reserved */
519 0, /* tp_repr */
520 &PyHKEY_NumberMethods, /* tp_as_number */
521 0, /* tp_as_sequence */
522 0, /* tp_as_mapping */
523 PyHKEY_hashFunc, /* tp_hash */
524 0, /* tp_call */
525 PyHKEY_strFunc, /* tp_str */
526 0, /* tp_getattro */
527 0, /* tp_setattro */
528 0, /* tp_as_buffer */
529 0, /* tp_flags */
530 PyHKEY_doc, /* tp_doc */
531 0, /*tp_traverse*/
532 0, /*tp_clear*/
533 0, /*tp_richcompare*/
534 0, /*tp_weaklistoffset*/
535 0, /*tp_iter*/
536 0, /*tp_iternext*/
537 PyHKEY_methods, /*tp_methods*/
538 PyHKEY_memberlist, /*tp_members*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000539};
540
541/************************************************************************
542
543 The PyHKEY object methods
544
545************************************************************************/
546static PyObject *
547PyHKEY_CloseMethod(PyObject *self, PyObject *args)
548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 if (!PyArg_ParseTuple(args, ":Close"))
550 return NULL;
551 if (!PyHKEY_Close(self))
552 return NULL;
553 Py_INCREF(Py_None);
554 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000555}
556
557static PyObject *
558PyHKEY_DetachMethod(PyObject *self, PyObject *args)
559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 void* ret;
561 PyHKEYObject *pThis = (PyHKEYObject *)self;
562 if (!PyArg_ParseTuple(args, ":Detach"))
563 return NULL;
564 ret = (void*)pThis->hkey;
565 pThis->hkey = 0;
566 return PyLong_FromVoidPtr(ret);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000567}
568
Christian Heimes2380ac72008-01-09 00:17:24 +0000569static PyObject *
570PyHKEY_Enter(PyObject *self)
571{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 Py_XINCREF(self);
573 return self;
Christian Heimes2380ac72008-01-09 00:17:24 +0000574}
575
576static PyObject *
577PyHKEY_Exit(PyObject *self, PyObject *args)
578{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 if (!PyHKEY_Close(self))
580 return NULL;
581 Py_RETURN_NONE;
Christian Heimes2380ac72008-01-09 00:17:24 +0000582}
583
584
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000585/************************************************************************
586 The public PyHKEY API (well, not public yet :-)
587************************************************************************/
588PyObject *
589PyHKEY_New(HKEY hInit)
590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 PyHKEYObject *key = PyObject_NEW(PyHKEYObject, &PyHKEY_Type);
592 if (key)
593 key->hkey = hInit;
594 return (PyObject *)key;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000595}
596
597BOOL
598PyHKEY_Close(PyObject *ob_handle)
599{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 LONG rc;
601 PyHKEYObject *key;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 if (!PyHKEY_Check(ob_handle)) {
604 PyErr_SetString(PyExc_TypeError, "bad operand type");
605 return FALSE;
606 }
607 key = (PyHKEYObject *)ob_handle;
608 rc = key->hkey ? RegCloseKey((HKEY)key->hkey) : ERROR_SUCCESS;
609 key->hkey = 0;
610 if (rc != ERROR_SUCCESS)
611 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
612 return rc == ERROR_SUCCESS;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000613}
614
615BOOL
616PyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK)
617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 if (ob == Py_None) {
619 if (!bNoneOK) {
620 PyErr_SetString(
621 PyExc_TypeError,
622 "None is not a valid HKEY in this context");
623 return FALSE;
624 }
625 *pHANDLE = (HKEY)0;
626 }
627 else if (PyHKEY_Check(ob)) {
628 PyHKEYObject *pH = (PyHKEYObject *)ob;
629 *pHANDLE = pH->hkey;
630 }
631 else if (PyLong_Check(ob)) {
632 /* We also support integers */
633 PyErr_Clear();
634 *pHANDLE = (HKEY)PyLong_AsVoidPtr(ob);
635 if (PyErr_Occurred())
636 return FALSE;
637 }
638 else {
639 PyErr_SetString(
640 PyExc_TypeError,
641 "The object is not a PyHKEY object");
642 return FALSE;
643 }
644 return TRUE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000645}
646
647PyObject *
648PyHKEY_FromHKEY(HKEY h)
649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 PyHKEYObject *op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 /* Inline PyObject_New */
653 op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject));
654 if (op == NULL)
655 return PyErr_NoMemory();
656 PyObject_INIT(op, &PyHKEY_Type);
657 op->hkey = h;
658 return (PyObject *)op;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000659}
660
661
662/************************************************************************
663 The module methods
664************************************************************************/
665BOOL
666PyWinObject_CloseHKEY(PyObject *obHandle)
667{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 BOOL ok;
669 if (PyHKEY_Check(obHandle)) {
670 ok = PyHKEY_Close(obHandle);
671 }
Fred Drake25e17262000-06-30 17:48:51 +0000672#if SIZEOF_LONG >= SIZEOF_HKEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 else if (PyLong_Check(obHandle)) {
674 long rc = RegCloseKey((HKEY)PyLong_AsLong(obHandle));
675 ok = (rc == ERROR_SUCCESS);
676 if (!ok)
677 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
678 }
Fred Drake25e17262000-06-30 17:48:51 +0000679#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 else if (PyLong_Check(obHandle)) {
681 long rc = RegCloseKey((HKEY)PyLong_AsVoidPtr(obHandle));
682 ok = (rc == ERROR_SUCCESS);
683 if (!ok)
684 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
685 }
Fred Drake25e17262000-06-30 17:48:51 +0000686#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 else {
688 PyErr_SetString(
689 PyExc_TypeError,
690 "A handle must be a HKEY object or an integer");
691 return FALSE;
692 }
693 return ok;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000694}
695
696
697/*
698 Private Helper functions for the registry interfaces
699
700** Note that fixupMultiSZ and countString have both had changes
701** made to support "incorrect strings". The registry specification
702** calls for strings to be terminated with 2 null bytes. It seems
Thomas Wouters7e474022000-07-16 12:04:32 +0000703** some commercial packages install strings which dont conform,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000704** causing this code to fail - however, "regedit" etc still work
705** with these strings (ie only we dont!).
706*/
707static void
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000708fixupMultiSZ(wchar_t **str, wchar_t *data, int len)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000709{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 wchar_t *P;
711 int i;
712 wchar_t *Q;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 Q = data + len;
715 for (P = data, i = 0; P < Q && *P != '\0'; P++, i++) {
716 str[i] = P;
717 for(; *P != '\0'; P++)
718 ;
719 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000720}
721
722static int
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000723countStrings(wchar_t *data, int len)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000724{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 int strings;
726 wchar_t *P;
727 wchar_t *Q = data + len;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 for (P = data, strings = 0; P < Q && *P != '\0'; P++, strings++)
730 for (; P < Q && *P != '\0'; P++)
731 ;
732 return strings;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000733}
734
735/* Convert PyObject into Registry data.
736 Allocates space as needed. */
737static BOOL
738Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
739{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 Py_ssize_t i,j;
741 switch (typ) {
742 case REG_DWORD:
743 if (value != Py_None && !PyLong_Check(value))
744 return FALSE;
745 *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1);
746 if (*retDataBuf==NULL){
747 PyErr_NoMemory();
748 return FALSE;
749 }
750 *retDataSize = sizeof(DWORD);
751 if (value == Py_None) {
752 DWORD zero = 0;
753 memcpy(*retDataBuf, &zero, sizeof(DWORD));
754 }
755 else {
756 DWORD d = PyLong_AsLong(value);
757 memcpy(*retDataBuf, &d, sizeof(DWORD));
758 }
759 break;
760 case REG_SZ:
761 case REG_EXPAND_SZ:
762 {
Victor Stinnerbe492442011-11-21 12:43:50 +0100763 if (value != Py_None) {
764 Py_ssize_t len;
765 if (!PyUnicode_Check(value))
766 return FALSE;
767 *retDataBuf = (BYTE*)PyUnicode_AsWideCharString(value, &len);
768 if (*retDataBuf == NULL)
769 return FALSE;
770 *retDataSize = Py_SAFE_DOWNCAST(
771 (len + 1) * sizeof(wchar_t),
772 Py_ssize_t, DWORD);
773 }
774 else {
775 *retDataBuf = (BYTE *)PyMem_NEW(wchar_t, 1);
776 if (*retDataBuf == NULL) {
777 PyErr_NoMemory();
778 return FALSE;
779 }
780 ((wchar_t *)*retDataBuf)[0] = L'\0';
781 *retDataSize = 1 * sizeof(wchar_t);
782 }
783 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 }
785 case REG_MULTI_SZ:
786 {
787 DWORD size = 0;
788 wchar_t *P;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 if (value == Py_None)
791 i = 0;
792 else {
793 if (!PyList_Check(value))
794 return FALSE;
795 i = PyList_Size(value);
796 }
797 for (j = 0; j < i; j++)
798 {
799 PyObject *t;
Victor Stinnerbe492442011-11-21 12:43:50 +0100800 wchar_t *wstr;
801 Py_ssize_t len;
802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 t = PyList_GET_ITEM(value, j);
804 if (!PyUnicode_Check(t))
805 return FALSE;
Victor Stinnerbe492442011-11-21 12:43:50 +0100806 wstr = PyUnicode_AsUnicodeAndSize(t, &len);
807 if (wstr == NULL)
808 return FALSE;
809 size += Py_SAFE_DOWNCAST((len + 1) * sizeof(wchar_t),
Brian Curtinabb33512010-08-17 20:08:40 +0000810 size_t, DWORD);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 *retDataSize = size + 2;
814 *retDataBuf = (BYTE *)PyMem_NEW(char,
815 *retDataSize);
816 if (*retDataBuf==NULL){
817 PyErr_NoMemory();
818 return FALSE;
819 }
820 P = (wchar_t *)*retDataBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 for (j = 0; j < i; j++)
823 {
824 PyObject *t;
Victor Stinnerbe492442011-11-21 12:43:50 +0100825 wchar_t *wstr;
826 Py_ssize_t len;
827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 t = PyList_GET_ITEM(value, j);
Victor Stinnerbe492442011-11-21 12:43:50 +0100829 wstr = PyUnicode_AsUnicodeAndSize(t, &len);
830 if (wstr == NULL)
831 return FALSE;
832 wcscpy(P, wstr);
833 P += (len + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 }
835 /* And doubly-terminate the list... */
836 *P = '\0';
837 break;
838 }
839 case REG_BINARY:
840 /* ALSO handle ALL unknown data types here. Even if we can't
841 support it natively, we should handle the bits. */
842 default:
843 if (value == Py_None)
844 *retDataSize = 0;
845 else {
846 Py_buffer view;
Neal Norwitz1385b892007-08-26 23:07:13 +0000847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 if (!PyObject_CheckBuffer(value)) {
849 PyErr_Format(PyExc_TypeError,
850 "Objects of type '%s' can not "
851 "be used as binary registry values",
852 value->ob_type->tp_name);
853 return FALSE;
854 }
Neal Norwitz1385b892007-08-26 23:07:13 +0000855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0)
857 return FALSE;
Neal Norwitz1385b892007-08-26 23:07:13 +0000858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 *retDataBuf = (BYTE *)PyMem_NEW(char, view.len);
860 if (*retDataBuf==NULL){
861 PyBuffer_Release(&view);
862 PyErr_NoMemory();
863 return FALSE;
864 }
Brian Curtinabb33512010-08-17 20:08:40 +0000865 *retDataSize = Py_SAFE_DOWNCAST(view.len, Py_ssize_t, DWORD);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 memcpy(*retDataBuf, view.buf, view.len);
867 PyBuffer_Release(&view);
868 }
869 break;
870 }
871 return TRUE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000872}
873
874/* Convert Registry data into PyObject*/
875static PyObject *
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000876Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000877{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 PyObject *obData;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 switch (typ) {
881 case REG_DWORD:
882 if (retDataSize == 0)
883 obData = PyLong_FromLong(0);
884 else
885 obData = PyLong_FromLong(*(int *)retDataBuf);
886 break;
887 case REG_SZ:
888 case REG_EXPAND_SZ:
889 {
890 /* the buffer may or may not have a trailing NULL */
891 wchar_t *data = (wchar_t *)retDataBuf;
892 int len = retDataSize / 2;
893 if (retDataSize && data[len-1] == '\0')
894 retDataSize -= 2;
895 if (retDataSize <= 0)
896 data = L"";
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200897 obData = PyUnicode_FromWideChar(data, retDataSize/2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 break;
899 }
900 case REG_MULTI_SZ:
901 if (retDataSize == 0)
902 obData = PyList_New(0);
903 else
904 {
905 int index = 0;
906 wchar_t *data = (wchar_t *)retDataBuf;
907 int len = retDataSize / 2;
908 int s = countStrings(data, len);
909 wchar_t **str = (wchar_t **)malloc(sizeof(wchar_t *)*s);
910 if (str == NULL)
911 return PyErr_NoMemory();
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 fixupMultiSZ(str, data, len);
914 obData = PyList_New(s);
915 if (obData == NULL)
916 return NULL;
917 for (index = 0; index < s; index++)
918 {
919 size_t len = wcslen(str[index]);
920 if (len > INT_MAX) {
921 PyErr_SetString(PyExc_OverflowError,
922 "registry string is too long for a Python string");
923 Py_DECREF(obData);
924 return NULL;
925 }
926 PyList_SetItem(obData,
927 index,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200928 PyUnicode_FromWideChar(str[index], len));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 }
930 free(str);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 break;
933 }
934 case REG_BINARY:
935 /* ALSO handle ALL unknown data types here. Even if we can't
936 support it natively, we should handle the bits. */
937 default:
938 if (retDataSize == 0) {
939 Py_INCREF(Py_None);
940 obData = Py_None;
941 }
942 else
943 obData = PyBytes_FromStringAndSize(
944 (char *)retDataBuf, retDataSize);
945 break;
946 }
947 return obData;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000948}
949
950/* The Python methods */
951
952static PyObject *
953PyCloseKey(PyObject *self, PyObject *args)
954{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 PyObject *obKey;
956 if (!PyArg_ParseTuple(args, "O:CloseKey", &obKey))
957 return NULL;
958 if (!PyHKEY_Close(obKey))
959 return NULL;
960 Py_INCREF(Py_None);
961 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000962}
963
964static PyObject *
965PyConnectRegistry(PyObject *self, PyObject *args)
966{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 HKEY hKey;
968 PyObject *obKey;
969 wchar_t *szCompName = NULL;
970 HKEY retKey;
971 long rc;
972 if (!PyArg_ParseTuple(args, "ZO:ConnectRegistry", &szCompName, &obKey))
973 return NULL;
974 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
975 return NULL;
976 Py_BEGIN_ALLOW_THREADS
977 rc = RegConnectRegistryW(szCompName, hKey, &retKey);
978 Py_END_ALLOW_THREADS
979 if (rc != ERROR_SUCCESS)
980 return PyErr_SetFromWindowsErrWithFunction(rc,
981 "ConnectRegistry");
982 return PyHKEY_FromHKEY(retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000983}
984
985static PyObject *
986PyCreateKey(PyObject *self, PyObject *args)
987{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 HKEY hKey;
989 PyObject *obKey;
990 wchar_t *subKey;
991 HKEY retKey;
992 long rc;
993 if (!PyArg_ParseTuple(args, "OZ:CreateKey", &obKey, &subKey))
994 return NULL;
995 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
996 return NULL;
997 rc = RegCreateKeyW(hKey, subKey, &retKey);
998 if (rc != ERROR_SUCCESS)
999 return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey");
1000 return PyHKEY_FromHKEY(retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001001}
1002
1003static PyObject *
Brian Curtin1771b542010-09-27 17:56:36 +00001004PyCreateKeyEx(PyObject *self, PyObject *args, PyObject *kwargs)
Brian Curtin3035c392010-04-21 23:56:21 +00001005{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 HKEY hKey;
Brian Curtin1771b542010-09-27 17:56:36 +00001007 PyObject *key;
1008 wchar_t *sub_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 HKEY retKey;
Brian Curtin1771b542010-09-27 17:56:36 +00001010 int reserved = 0;
1011 REGSAM access = KEY_WRITE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 long rc;
Brian Curtin1771b542010-09-27 17:56:36 +00001013
1014 char *kwlist[] = {"key", "sub_key", "reserved", "access", NULL};
1015
1016 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OZ|ii:CreateKeyEx", kwlist,
1017 &key, &sub_key, &reserved, &access))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 return NULL;
Brian Curtin1771b542010-09-27 17:56:36 +00001019 if (!PyHKEY_AsHKEY(key, &hKey, FALSE))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 return NULL;
Brian Curtin3035c392010-04-21 23:56:21 +00001021
Brian Curtin1771b542010-09-27 17:56:36 +00001022 rc = RegCreateKeyExW(hKey, sub_key, reserved, NULL, (DWORD)NULL,
1023 access, NULL, &retKey, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 if (rc != ERROR_SUCCESS)
1025 return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKeyEx");
1026 return PyHKEY_FromHKEY(retKey);
Brian Curtin3035c392010-04-21 23:56:21 +00001027}
1028
1029static PyObject *
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001030PyDeleteKey(PyObject *self, PyObject *args)
1031{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 HKEY hKey;
1033 PyObject *obKey;
1034 wchar_t *subKey;
1035 long rc;
1036 if (!PyArg_ParseTuple(args, "Ou:DeleteKey", &obKey, &subKey))
1037 return NULL;
1038 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1039 return NULL;
1040 rc = RegDeleteKeyW(hKey, subKey );
1041 if (rc != ERROR_SUCCESS)
1042 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey");
1043 Py_INCREF(Py_None);
1044 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001045}
1046
1047static PyObject *
Brian Curtin1771b542010-09-27 17:56:36 +00001048PyDeleteKeyEx(PyObject *self, PyObject *args, PyObject *kwargs)
Brian Curtin3035c392010-04-21 23:56:21 +00001049{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 HKEY hKey;
Brian Curtin1771b542010-09-27 17:56:36 +00001051 PyObject *key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 HMODULE hMod;
1053 typedef LONG (WINAPI *RDKEFunc)(HKEY, const wchar_t*, REGSAM, int);
1054 RDKEFunc pfn = NULL;
Brian Curtin1771b542010-09-27 17:56:36 +00001055 wchar_t *sub_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 long rc;
Brian Curtin1771b542010-09-27 17:56:36 +00001057 int reserved = 0;
1058 REGSAM access = KEY_WOW64_64KEY;
Brian Curtin3035c392010-04-21 23:56:21 +00001059
Brian Curtin1771b542010-09-27 17:56:36 +00001060 char *kwlist[] = {"key", "sub_key", "access", "reserved", NULL};
1061 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Ou|ii:DeleteKeyEx", kwlist,
1062 &key, &sub_key, &access, &reserved))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 return NULL;
Brian Curtin1771b542010-09-27 17:56:36 +00001064 if (!PyHKEY_AsHKEY(key, &hKey, FALSE))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 return NULL;
Brian Curtin3035c392010-04-21 23:56:21 +00001066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 /* Only available on 64bit platforms, so we must load it
1068 dynamically. */
1069 hMod = GetModuleHandle("advapi32.dll");
1070 if (hMod)
1071 pfn = (RDKEFunc)GetProcAddress(hMod,
1072 "RegDeleteKeyExW");
1073 if (!pfn) {
1074 PyErr_SetString(PyExc_NotImplementedError,
1075 "not implemented on this platform");
1076 return NULL;
1077 }
1078 Py_BEGIN_ALLOW_THREADS
Brian Curtin1771b542010-09-27 17:56:36 +00001079 rc = (*pfn)(hKey, sub_key, access, reserved);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 Py_END_ALLOW_THREADS
Brian Curtin3035c392010-04-21 23:56:21 +00001081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 if (rc != ERROR_SUCCESS)
1083 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKeyEx");
1084 Py_INCREF(Py_None);
1085 return Py_None;
Brian Curtin3035c392010-04-21 23:56:21 +00001086}
1087
1088static PyObject *
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001089PyDeleteValue(PyObject *self, PyObject *args)
1090{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 HKEY hKey;
1092 PyObject *obKey;
1093 wchar_t *subKey;
1094 long rc;
1095 if (!PyArg_ParseTuple(args, "OZ:DeleteValue", &obKey, &subKey))
1096 return NULL;
1097 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1098 return NULL;
1099 Py_BEGIN_ALLOW_THREADS
1100 rc = RegDeleteValueW(hKey, subKey);
1101 Py_END_ALLOW_THREADS
1102 if (rc !=ERROR_SUCCESS)
1103 return PyErr_SetFromWindowsErrWithFunction(rc,
1104 "RegDeleteValue");
1105 Py_INCREF(Py_None);
1106 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001107}
1108
1109static PyObject *
1110PyEnumKey(PyObject *self, PyObject *args)
1111{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 HKEY hKey;
1113 PyObject *obKey;
1114 int index;
1115 long rc;
1116 PyObject *retStr;
Brian Curtin60853212010-05-26 17:43:50 +00001117
1118 /* The Windows docs claim that the max key name length is 255
1119 * characters, plus a terminating nul character. However,
1120 * empirical testing demonstrates that it is possible to
1121 * create a 256 character key that is missing the terminating
1122 * nul. RegEnumKeyEx requires a 257 character buffer to
1123 * retrieve such a key name. */
1124 wchar_t tmpbuf[257];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 DWORD len = sizeof(tmpbuf); /* includes NULL terminator */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 if (!PyArg_ParseTuple(args, "Oi:EnumKey", &obKey, &index))
1128 return NULL;
1129 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1130 return NULL;
Tim Peters313fcd42006-02-19 04:05:39 +00001131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 Py_BEGIN_ALLOW_THREADS
1133 rc = RegEnumKeyExW(hKey, index, tmpbuf, &len, NULL, NULL, NULL, NULL);
1134 Py_END_ALLOW_THREADS
1135 if (rc != ERROR_SUCCESS)
1136 return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx");
Tim Peters313fcd42006-02-19 04:05:39 +00001137
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001138 retStr = PyUnicode_FromWideChar(tmpbuf, len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 return retStr; /* can be NULL */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001140}
1141
1142static PyObject *
1143PyEnumValue(PyObject *self, PyObject *args)
1144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 HKEY hKey;
1146 PyObject *obKey;
1147 int index;
1148 long rc;
1149 wchar_t *retValueBuf;
Amaury Forgeot d'Arcf2b69df2010-08-16 22:11:29 +00001150 BYTE *tmpBuf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 BYTE *retDataBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001152 DWORD retValueSize, bufValueSize;
1153 DWORD retDataSize, bufDataSize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 DWORD typ;
1155 PyObject *obData;
1156 PyObject *retVal;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 if (!PyArg_ParseTuple(args, "Oi:EnumValue", &obKey, &index))
1159 return NULL;
1160 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1161 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 if ((rc = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, NULL, NULL,
1164 NULL,
1165 &retValueSize, &retDataSize, NULL, NULL))
1166 != ERROR_SUCCESS)
1167 return PyErr_SetFromWindowsErrWithFunction(rc,
1168 "RegQueryInfoKey");
1169 ++retValueSize; /* include null terminators */
1170 ++retDataSize;
Brian Curtin60853212010-05-26 17:43:50 +00001171 bufDataSize = retDataSize;
1172 bufValueSize = retValueSize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 retValueBuf = (wchar_t *)PyMem_Malloc(sizeof(wchar_t) * retValueSize);
1174 if (retValueBuf == NULL)
1175 return PyErr_NoMemory();
1176 retDataBuf = (BYTE *)PyMem_Malloc(retDataSize);
1177 if (retDataBuf == NULL) {
1178 PyMem_Free(retValueBuf);
1179 return PyErr_NoMemory();
1180 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001181
Brian Curtin60853212010-05-26 17:43:50 +00001182 while (1) {
Brian Curtin60853212010-05-26 17:43:50 +00001183 Py_BEGIN_ALLOW_THREADS
1184 rc = RegEnumValueW(hKey,
1185 index,
1186 retValueBuf,
1187 &retValueSize,
1188 NULL,
1189 &typ,
1190 (BYTE *)retDataBuf,
1191 &retDataSize);
1192 Py_END_ALLOW_THREADS
1193
1194 if (rc != ERROR_MORE_DATA)
1195 break;
1196
1197 bufDataSize *= 2;
Amaury Forgeot d'Arcf2b69df2010-08-16 22:11:29 +00001198 tmpBuf = (BYTE *)PyMem_Realloc(retDataBuf, bufDataSize);
Brian Curtin9b7e2d12010-06-08 20:57:52 +00001199 if (tmpBuf == NULL) {
Brian Curtin60853212010-05-26 17:43:50 +00001200 PyErr_NoMemory();
1201 retVal = NULL;
1202 goto fail;
1203 }
Brian Curtin9b7e2d12010-06-08 20:57:52 +00001204 retDataBuf = tmpBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001205 retDataSize = bufDataSize;
1206 retValueSize = bufValueSize;
1207 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 if (rc != ERROR_SUCCESS) {
1210 retVal = PyErr_SetFromWindowsErrWithFunction(rc,
1211 "PyRegEnumValue");
1212 goto fail;
1213 }
1214 obData = Reg2Py(retDataBuf, retDataSize, typ);
1215 if (obData == NULL) {
1216 retVal = NULL;
1217 goto fail;
1218 }
1219 retVal = Py_BuildValue("uOi", retValueBuf, obData, typ);
1220 Py_DECREF(obData);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001221 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 PyMem_Free(retValueBuf);
1223 PyMem_Free(retDataBuf);
1224 return retVal;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001225}
1226
1227static PyObject *
Christian Heimes2380ac72008-01-09 00:17:24 +00001228PyExpandEnvironmentStrings(PyObject *self, PyObject *args)
1229{
Victor Stinner9d3b93b2011-11-22 02:27:30 +01001230 wchar_t *retValue = NULL;
1231 wchar_t *src;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 DWORD retValueSize;
1233 DWORD rc;
1234 PyObject *o;
Christian Heimes2380ac72008-01-09 00:17:24 +00001235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 if (!PyArg_ParseTuple(args, "u:ExpandEnvironmentStrings", &src))
1237 return NULL;
Christian Heimes2380ac72008-01-09 00:17:24 +00001238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 retValueSize = ExpandEnvironmentStringsW(src, retValue, 0);
1240 if (retValueSize == 0) {
1241 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1242 "ExpandEnvironmentStrings");
1243 }
Victor Stinner9d3b93b2011-11-22 02:27:30 +01001244 retValue = (wchar_t *)PyMem_Malloc(retValueSize * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 if (retValue == NULL) {
1246 return PyErr_NoMemory();
1247 }
Christian Heimes2380ac72008-01-09 00:17:24 +00001248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 rc = ExpandEnvironmentStringsW(src, retValue, retValueSize);
1250 if (rc == 0) {
1251 PyMem_Free(retValue);
1252 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1253 "ExpandEnvironmentStrings");
1254 }
Victor Stinner9d3b93b2011-11-22 02:27:30 +01001255 o = PyUnicode_FromWideChar(retValue, wcslen(retValue));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 PyMem_Free(retValue);
1257 return o;
Christian Heimes2380ac72008-01-09 00:17:24 +00001258}
1259
1260static PyObject *
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001261PyFlushKey(PyObject *self, PyObject *args)
1262{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 HKEY hKey;
1264 PyObject *obKey;
1265 long rc;
1266 if (!PyArg_ParseTuple(args, "O:FlushKey", &obKey))
1267 return NULL;
1268 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1269 return NULL;
1270 Py_BEGIN_ALLOW_THREADS
1271 rc = RegFlushKey(hKey);
1272 Py_END_ALLOW_THREADS
1273 if (rc != ERROR_SUCCESS)
1274 return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey");
1275 Py_INCREF(Py_None);
1276 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001277}
1278static PyObject *
1279PyLoadKey(PyObject *self, PyObject *args)
1280{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 HKEY hKey;
1282 PyObject *obKey;
1283 wchar_t *subKey;
1284 wchar_t *fileName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 long rc;
1287 if (!PyArg_ParseTuple(args, "Ouu:LoadKey", &obKey, &subKey, &fileName))
1288 return NULL;
1289 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1290 return NULL;
1291 Py_BEGIN_ALLOW_THREADS
1292 rc = RegLoadKeyW(hKey, subKey, fileName );
1293 Py_END_ALLOW_THREADS
1294 if (rc != ERROR_SUCCESS)
1295 return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey");
1296 Py_INCREF(Py_None);
1297 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001298}
1299
1300static PyObject *
Brian Curtin1771b542010-09-27 17:56:36 +00001301PyOpenKey(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001302{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 HKEY hKey;
Brian Curtin1771b542010-09-27 17:56:36 +00001304 PyObject *key;
1305 wchar_t *sub_key;
1306 int reserved = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 HKEY retKey;
1308 long rc;
Brian Curtin1771b542010-09-27 17:56:36 +00001309 REGSAM access = KEY_READ;
1310
1311 char *kwlist[] = {"key", "sub_key", "reserved", "access", NULL};
1312
1313 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OZ|ii:OpenKey", kwlist,
1314 &key, &sub_key, &reserved, &access))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 return NULL;
Brian Curtin1771b542010-09-27 17:56:36 +00001316 if (!PyHKEY_AsHKEY(key, &hKey, FALSE))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 Py_BEGIN_ALLOW_THREADS
Brian Curtin1771b542010-09-27 17:56:36 +00001320 rc = RegOpenKeyExW(hKey, sub_key, reserved, access, &retKey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 Py_END_ALLOW_THREADS
1322 if (rc != ERROR_SUCCESS)
1323 return PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx");
1324 return PyHKEY_FromHKEY(retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001325}
1326
1327
1328static PyObject *
1329PyQueryInfoKey(PyObject *self, PyObject *args)
1330{
1331 HKEY hKey;
1332 PyObject *obKey;
1333 long rc;
1334 DWORD nSubKeys, nValues;
1335 FILETIME ft;
1336 LARGE_INTEGER li;
1337 PyObject *l;
1338 PyObject *ret;
1339 if (!PyArg_ParseTuple(args, "O:QueryInfoKey", &obKey))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001341 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001343 if ((rc = RegQueryInfoKey(hKey, NULL, NULL, 0, &nSubKeys, NULL, NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 &nValues, NULL, NULL, NULL, &ft))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001345 != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey");
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001347 li.LowPart = ft.dwLowDateTime;
1348 li.HighPart = ft.dwHighDateTime;
1349 l = PyLong_FromLongLong(li.QuadPart);
1350 if (l == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001352 ret = Py_BuildValue("iiO", nSubKeys, nValues, l);
1353 Py_DECREF(l);
1354 return ret;
1355}
1356
1357static PyObject *
1358PyQueryValue(PyObject *self, PyObject *args)
1359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 HKEY hKey;
1361 PyObject *obKey;
1362 wchar_t *subKey;
1363 long rc;
1364 PyObject *retStr;
1365 wchar_t *retBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001366 DWORD bufSize = 0;
1367 DWORD retSize = 0;
1368 wchar_t *tmp;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 if (!PyArg_ParseTuple(args, "OZ:QueryValue", &obKey, &subKey))
1371 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1374 return NULL;
Brian Curtin60853212010-05-26 17:43:50 +00001375
1376 rc = RegQueryValueW(hKey, subKey, NULL, &retSize);
1377 if (rc == ERROR_MORE_DATA)
1378 retSize = 256;
1379 else if (rc != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 return PyErr_SetFromWindowsErrWithFunction(rc,
1381 "RegQueryValue");
Brian Curtin60853212010-05-26 17:43:50 +00001382
1383 bufSize = retSize;
1384 retBuf = (wchar_t *) PyMem_Malloc(bufSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 if (retBuf == NULL)
1386 return PyErr_NoMemory();
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001387
Brian Curtin60853212010-05-26 17:43:50 +00001388 while (1) {
1389 retSize = bufSize;
1390 rc = RegQueryValueW(hKey, subKey, retBuf, &retSize);
1391 if (rc != ERROR_MORE_DATA)
1392 break;
1393
1394 bufSize *= 2;
1395 tmp = (wchar_t *) PyMem_Realloc(retBuf, bufSize);
1396 if (tmp == NULL) {
1397 PyMem_Free(retBuf);
1398 return PyErr_NoMemory();
1399 }
1400 retBuf = tmp;
1401 }
1402
1403 if (rc != ERROR_SUCCESS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 PyMem_Free(retBuf);
1405 return PyErr_SetFromWindowsErrWithFunction(rc,
1406 "RegQueryValue");
1407 }
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001408
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001409 retStr = PyUnicode_FromWideChar(retBuf, wcslen(retBuf));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 PyMem_Free(retBuf);
1411 return retStr;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001412}
1413
1414static PyObject *
1415PyQueryValueEx(PyObject *self, PyObject *args)
1416{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 HKEY hKey;
1418 PyObject *obKey;
1419 wchar_t *valueName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 long rc;
Brian Curtin60853212010-05-26 17:43:50 +00001422 BYTE *retBuf, *tmp;
1423 DWORD bufSize = 0, retSize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 DWORD typ;
1425 PyObject *obData;
1426 PyObject *result;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 if (!PyArg_ParseTuple(args, "OZ:QueryValueEx", &obKey, &valueName))
1429 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1432 return NULL;
Brian Curtin60853212010-05-26 17:43:50 +00001433
1434 rc = RegQueryValueExW(hKey, valueName, NULL, NULL, NULL, &bufSize);
1435 if (rc == ERROR_MORE_DATA)
1436 bufSize = 256;
1437 else if (rc != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 return PyErr_SetFromWindowsErrWithFunction(rc,
1439 "RegQueryValueEx");
1440 retBuf = (BYTE *)PyMem_Malloc(bufSize);
1441 if (retBuf == NULL)
1442 return PyErr_NoMemory();
Brian Curtin60853212010-05-26 17:43:50 +00001443
1444 while (1) {
1445 retSize = bufSize;
1446 rc = RegQueryValueExW(hKey, valueName, NULL, &typ,
1447 (BYTE *)retBuf, &retSize);
1448 if (rc != ERROR_MORE_DATA)
1449 break;
1450
1451 bufSize *= 2;
1452 tmp = (char *) PyMem_Realloc(retBuf, bufSize);
1453 if (tmp == NULL) {
1454 PyMem_Free(retBuf);
1455 return PyErr_NoMemory();
1456 }
1457 retBuf = tmp;
1458 }
1459
1460 if (rc != ERROR_SUCCESS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 PyMem_Free(retBuf);
1462 return PyErr_SetFromWindowsErrWithFunction(rc,
1463 "RegQueryValueEx");
1464 }
1465 obData = Reg2Py(retBuf, bufSize, typ);
1466 PyMem_Free(retBuf);
1467 if (obData == NULL)
1468 return NULL;
1469 result = Py_BuildValue("Oi", obData, typ);
1470 Py_DECREF(obData);
1471 return result;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001472}
1473
1474
1475static PyObject *
1476PySaveKey(PyObject *self, PyObject *args)
1477{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 HKEY hKey;
1479 PyObject *obKey;
1480 wchar_t *fileName;
1481 LPSECURITY_ATTRIBUTES pSA = NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 long rc;
1484 if (!PyArg_ParseTuple(args, "Ou:SaveKey", &obKey, &fileName))
1485 return NULL;
1486 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1487 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001488/* One day we may get security into the core?
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE))
1490 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001491*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 Py_BEGIN_ALLOW_THREADS
1493 rc = RegSaveKeyW(hKey, fileName, pSA );
1494 Py_END_ALLOW_THREADS
1495 if (rc != ERROR_SUCCESS)
1496 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey");
1497 Py_INCREF(Py_None);
1498 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001499}
1500
1501static PyObject *
1502PySetValue(PyObject *self, PyObject *args)
1503{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 HKEY hKey;
1505 PyObject *obKey;
1506 wchar_t *subKey;
1507 wchar_t *str;
1508 DWORD typ;
1509 DWORD len;
1510 long rc;
1511 if (!PyArg_ParseTuple(args, "OZiu#:SetValue",
1512 &obKey,
1513 &subKey,
1514 &typ,
1515 &str,
1516 &len))
1517 return NULL;
1518 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1519 return NULL;
1520 if (typ != REG_SZ) {
1521 PyErr_SetString(PyExc_TypeError,
1522 "Type must be winreg.REG_SZ");
1523 return NULL;
1524 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 Py_BEGIN_ALLOW_THREADS
1527 rc = RegSetValueW(hKey, subKey, REG_SZ, str, len+1);
1528 Py_END_ALLOW_THREADS
1529 if (rc != ERROR_SUCCESS)
1530 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue");
1531 Py_INCREF(Py_None);
1532 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001533}
1534
1535static PyObject *
1536PySetValueEx(PyObject *self, PyObject *args)
1537{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 HKEY hKey;
1539 PyObject *obKey;
Victor Stinner9d3b93b2011-11-22 02:27:30 +01001540 wchar_t *valueName;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001541 PyObject *obRes;
1542 PyObject *value;
1543 BYTE *data;
1544 DWORD len;
1545 DWORD typ;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 LONG rc;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 if (!PyArg_ParseTuple(args, "OZOiO:SetValueEx",
1550 &obKey,
1551 &valueName,
1552 &obRes,
1553 &typ,
1554 &value))
1555 return NULL;
1556 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1557 return NULL;
1558 if (!Py2Reg(value, typ, &data, &len))
1559 {
1560 if (!PyErr_Occurred())
1561 PyErr_SetString(PyExc_ValueError,
1562 "Could not convert the data to the specified type.");
1563 return NULL;
1564 }
1565 Py_BEGIN_ALLOW_THREADS
1566 rc = RegSetValueExW(hKey, valueName, 0, typ, data, len);
1567 Py_END_ALLOW_THREADS
1568 PyMem_DEL(data);
1569 if (rc != ERROR_SUCCESS)
1570 return PyErr_SetFromWindowsErrWithFunction(rc,
1571 "RegSetValueEx");
1572 Py_INCREF(Py_None);
1573 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001574}
1575
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001576static PyObject *
1577PyDisableReflectionKey(PyObject *self, PyObject *args)
1578{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 HKEY hKey;
1580 PyObject *obKey;
1581 HMODULE hMod;
1582 typedef LONG (WINAPI *RDRKFunc)(HKEY);
1583 RDRKFunc pfn = NULL;
1584 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 if (!PyArg_ParseTuple(args, "O:DisableReflectionKey", &obKey))
1587 return NULL;
1588 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1589 return NULL;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 /* Only available on 64bit platforms, so we must load it
1592 dynamically.*/
1593 hMod = GetModuleHandle("advapi32.dll");
1594 if (hMod)
1595 pfn = (RDRKFunc)GetProcAddress(hMod,
1596 "RegDisableReflectionKey");
1597 if (!pfn) {
1598 PyErr_SetString(PyExc_NotImplementedError,
1599 "not implemented on this platform");
1600 return NULL;
1601 }
1602 Py_BEGIN_ALLOW_THREADS
1603 rc = (*pfn)(hKey);
1604 Py_END_ALLOW_THREADS
1605 if (rc != ERROR_SUCCESS)
1606 return PyErr_SetFromWindowsErrWithFunction(rc,
1607 "RegDisableReflectionKey");
1608 Py_INCREF(Py_None);
1609 return Py_None;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001610}
1611
1612static PyObject *
1613PyEnableReflectionKey(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 *RERKFunc)(HKEY);
1619 RERKFunc 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:EnableReflectionKey", &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.*/
1629 hMod = GetModuleHandle("advapi32.dll");
1630 if (hMod)
1631 pfn = (RERKFunc)GetProcAddress(hMod,
1632 "RegEnableReflectionKey");
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 "RegEnableReflectionKey");
1644 Py_INCREF(Py_None);
1645 return Py_None;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001646}
1647
1648static PyObject *
1649PyQueryReflectionKey(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 *RQRKFunc)(HKEY, BOOL *);
1655 RQRKFunc pfn = NULL;
1656 BOOL result;
1657 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 if (!PyArg_ParseTuple(args, "O:QueryReflectionKey", &obKey))
1660 return NULL;
1661 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1662 return NULL;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 /* Only available on 64bit platforms, so we must load it
1665 dynamically.*/
1666 hMod = GetModuleHandle("advapi32.dll");
1667 if (hMod)
1668 pfn = (RQRKFunc)GetProcAddress(hMod,
1669 "RegQueryReflectionKey");
1670 if (!pfn) {
1671 PyErr_SetString(PyExc_NotImplementedError,
1672 "not implemented on this platform");
1673 return NULL;
1674 }
1675 Py_BEGIN_ALLOW_THREADS
1676 rc = (*pfn)(hKey, &result);
1677 Py_END_ALLOW_THREADS
1678 if (rc != ERROR_SUCCESS)
1679 return PyErr_SetFromWindowsErrWithFunction(rc,
1680 "RegQueryReflectionKey");
1681 return PyBool_FromLong(result);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001682}
1683
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001684static struct PyMethodDef winreg_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 {"CloseKey", PyCloseKey, METH_VARARGS, CloseKey_doc},
1686 {"ConnectRegistry", PyConnectRegistry, METH_VARARGS, ConnectRegistry_doc},
1687 {"CreateKey", PyCreateKey, METH_VARARGS, CreateKey_doc},
Brian Curtin1771b542010-09-27 17:56:36 +00001688 {"CreateKeyEx", (PyCFunction)PyCreateKeyEx,
1689 METH_VARARGS | METH_KEYWORDS, CreateKeyEx_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 {"DeleteKey", PyDeleteKey, METH_VARARGS, DeleteKey_doc},
Brian Curtin1771b542010-09-27 17:56:36 +00001691 {"DeleteKeyEx", (PyCFunction)PyDeleteKeyEx,
1692 METH_VARARGS | METH_KEYWORDS, DeleteKeyEx_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 {"DeleteValue", PyDeleteValue, METH_VARARGS, DeleteValue_doc},
1694 {"DisableReflectionKey", PyDisableReflectionKey, METH_VARARGS, DisableReflectionKey_doc},
1695 {"EnableReflectionKey", PyEnableReflectionKey, METH_VARARGS, EnableReflectionKey_doc},
1696 {"EnumKey", PyEnumKey, METH_VARARGS, EnumKey_doc},
1697 {"EnumValue", PyEnumValue, METH_VARARGS, EnumValue_doc},
1698 {"ExpandEnvironmentStrings", PyExpandEnvironmentStrings, METH_VARARGS,
1699 ExpandEnvironmentStrings_doc },
1700 {"FlushKey", PyFlushKey, METH_VARARGS, FlushKey_doc},
1701 {"LoadKey", PyLoadKey, METH_VARARGS, LoadKey_doc},
Brian Curtin1771b542010-09-27 17:56:36 +00001702 {"OpenKey", (PyCFunction)PyOpenKey, METH_VARARGS | METH_KEYWORDS,
1703 OpenKey_doc},
1704 {"OpenKeyEx", (PyCFunction)PyOpenKey, METH_VARARGS | METH_KEYWORDS,
1705 OpenKeyEx_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 {"QueryValue", PyQueryValue, METH_VARARGS, QueryValue_doc},
1707 {"QueryValueEx", PyQueryValueEx, METH_VARARGS, QueryValueEx_doc},
1708 {"QueryInfoKey", PyQueryInfoKey, METH_VARARGS, QueryInfoKey_doc},
1709 {"QueryReflectionKey",PyQueryReflectionKey,METH_VARARGS, QueryReflectionKey_doc},
1710 {"SaveKey", PySaveKey, METH_VARARGS, SaveKey_doc},
1711 {"SetValue", PySetValue, METH_VARARGS, SetValue_doc},
1712 {"SetValueEx", PySetValueEx, METH_VARARGS, SetValueEx_doc},
1713 NULL,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001714};
1715
1716static void
1717insint(PyObject * d, char * name, long value)
1718{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 PyObject *v = PyLong_FromLong(value);
1720 if (!v || PyDict_SetItemString(d, name, v))
1721 PyErr_Clear();
1722 Py_XDECREF(v);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001723}
1724
1725#define ADD_INT(val) insint(d, #val, val)
1726
1727static void
1728inskey(PyObject * d, char * name, HKEY key)
1729{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 PyObject *v = PyLong_FromVoidPtr(key);
1731 if (!v || PyDict_SetItemString(d, name, v))
1732 PyErr_Clear();
1733 Py_XDECREF(v);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001734}
1735
1736#define ADD_KEY(val) inskey(d, #val, val)
1737
Martin v. Löwis1a214512008-06-11 05:26:20 +00001738
1739static struct PyModuleDef winregmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 PyModuleDef_HEAD_INIT,
1741 "winreg",
1742 module_doc,
1743 -1,
1744 winreg_methods,
1745 NULL,
1746 NULL,
1747 NULL,
1748 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001749};
1750
1751PyMODINIT_FUNC PyInit_winreg(void)
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001752{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 PyObject *m, *d;
1754 m = PyModule_Create(&winregmodule);
1755 if (m == NULL)
1756 return NULL;
1757 d = PyModule_GetDict(m);
1758 PyHKEY_Type.tp_doc = PyHKEY_doc;
1759 if (PyType_Ready(&PyHKEY_Type) < 0)
1760 return NULL;
1761 Py_INCREF(&PyHKEY_Type);
1762 if (PyDict_SetItemString(d, "HKEYType",
1763 (PyObject *)&PyHKEY_Type) != 0)
1764 return NULL;
1765 Py_INCREF(PyExc_WindowsError);
1766 if (PyDict_SetItemString(d, "error",
1767 PyExc_WindowsError) != 0)
1768 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 /* Add the relevant constants */
1771 ADD_KEY(HKEY_CLASSES_ROOT);
1772 ADD_KEY(HKEY_CURRENT_USER);
1773 ADD_KEY(HKEY_LOCAL_MACHINE);
1774 ADD_KEY(HKEY_USERS);
1775 ADD_KEY(HKEY_PERFORMANCE_DATA);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001776#ifdef HKEY_CURRENT_CONFIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 ADD_KEY(HKEY_CURRENT_CONFIG);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001778#endif
1779#ifdef HKEY_DYN_DATA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 ADD_KEY(HKEY_DYN_DATA);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001781#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 ADD_INT(KEY_QUERY_VALUE);
1783 ADD_INT(KEY_SET_VALUE);
1784 ADD_INT(KEY_CREATE_SUB_KEY);
1785 ADD_INT(KEY_ENUMERATE_SUB_KEYS);
1786 ADD_INT(KEY_NOTIFY);
1787 ADD_INT(KEY_CREATE_LINK);
1788 ADD_INT(KEY_READ);
1789 ADD_INT(KEY_WRITE);
1790 ADD_INT(KEY_EXECUTE);
1791 ADD_INT(KEY_ALL_ACCESS);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001792#ifdef KEY_WOW64_64KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001793 ADD_INT(KEY_WOW64_64KEY);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001794#endif
1795#ifdef KEY_WOW64_32KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796 ADD_INT(KEY_WOW64_32KEY);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001797#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001798 ADD_INT(REG_OPTION_RESERVED);
1799 ADD_INT(REG_OPTION_NON_VOLATILE);
1800 ADD_INT(REG_OPTION_VOLATILE);
1801 ADD_INT(REG_OPTION_CREATE_LINK);
1802 ADD_INT(REG_OPTION_BACKUP_RESTORE);
1803 ADD_INT(REG_OPTION_OPEN_LINK);
1804 ADD_INT(REG_LEGAL_OPTION);
1805 ADD_INT(REG_CREATED_NEW_KEY);
1806 ADD_INT(REG_OPENED_EXISTING_KEY);
1807 ADD_INT(REG_WHOLE_HIVE_VOLATILE);
1808 ADD_INT(REG_REFRESH_HIVE);
1809 ADD_INT(REG_NO_LAZY_FLUSH);
1810 ADD_INT(REG_NOTIFY_CHANGE_NAME);
1811 ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES);
1812 ADD_INT(REG_NOTIFY_CHANGE_LAST_SET);
1813 ADD_INT(REG_NOTIFY_CHANGE_SECURITY);
1814 ADD_INT(REG_LEGAL_CHANGE_FILTER);
1815 ADD_INT(REG_NONE);
1816 ADD_INT(REG_SZ);
1817 ADD_INT(REG_EXPAND_SZ);
1818 ADD_INT(REG_BINARY);
1819 ADD_INT(REG_DWORD);
1820 ADD_INT(REG_DWORD_LITTLE_ENDIAN);
1821 ADD_INT(REG_DWORD_BIG_ENDIAN);
1822 ADD_INT(REG_LINK);
1823 ADD_INT(REG_MULTI_SZ);
1824 ADD_INT(REG_RESOURCE_LIST);
1825 ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
1826 ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
1827 return m;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001828}
1829
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001830