blob: 390a9ce2a7810890c94b9e95aa8f506dfa78d4c9 [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
455static long
456PyHKEY_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 {
763 if (value == Py_None)
764 *retDataSize = 1;
765 else {
766 if (!PyUnicode_Check(value))
767 return FALSE;
Brian Curtinabb33512010-08-17 20:08:40 +0000768 *retDataSize = Py_SAFE_DOWNCAST(
769 2 + PyUnicode_GET_DATA_SIZE(value),
770 size_t, DWORD);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 }
772 *retDataBuf = (BYTE *)PyMem_NEW(DWORD, *retDataSize);
773 if (*retDataBuf==NULL){
774 PyErr_NoMemory();
775 return FALSE;
776 }
777 if (value == Py_None)
778 wcscpy((wchar_t *)*retDataBuf, L"");
779 else
780 wcscpy((wchar_t *)*retDataBuf,
781 PyUnicode_AS_UNICODE(value));
782 break;
783 }
784 case REG_MULTI_SZ:
785 {
786 DWORD size = 0;
787 wchar_t *P;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 if (value == Py_None)
790 i = 0;
791 else {
792 if (!PyList_Check(value))
793 return FALSE;
794 i = PyList_Size(value);
795 }
796 for (j = 0; j < i; j++)
797 {
798 PyObject *t;
799 t = PyList_GET_ITEM(value, j);
800 if (!PyUnicode_Check(t))
801 return FALSE;
Brian Curtinabb33512010-08-17 20:08:40 +0000802 size += Py_SAFE_DOWNCAST(2 + PyUnicode_GET_DATA_SIZE(t),
803 size_t, DWORD);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 *retDataSize = size + 2;
807 *retDataBuf = (BYTE *)PyMem_NEW(char,
808 *retDataSize);
809 if (*retDataBuf==NULL){
810 PyErr_NoMemory();
811 return FALSE;
812 }
813 P = (wchar_t *)*retDataBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 for (j = 0; j < i; j++)
816 {
817 PyObject *t;
818 t = PyList_GET_ITEM(value, j);
819 wcscpy(P, PyUnicode_AS_UNICODE(t));
820 P += 1 + wcslen(
821 PyUnicode_AS_UNICODE(t));
822 }
823 /* And doubly-terminate the list... */
824 *P = '\0';
825 break;
826 }
827 case REG_BINARY:
828 /* ALSO handle ALL unknown data types here. Even if we can't
829 support it natively, we should handle the bits. */
830 default:
831 if (value == Py_None)
832 *retDataSize = 0;
833 else {
834 Py_buffer view;
Neal Norwitz1385b892007-08-26 23:07:13 +0000835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 if (!PyObject_CheckBuffer(value)) {
837 PyErr_Format(PyExc_TypeError,
838 "Objects of type '%s' can not "
839 "be used as binary registry values",
840 value->ob_type->tp_name);
841 return FALSE;
842 }
Neal Norwitz1385b892007-08-26 23:07:13 +0000843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0)
845 return FALSE;
Neal Norwitz1385b892007-08-26 23:07:13 +0000846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 *retDataBuf = (BYTE *)PyMem_NEW(char, view.len);
848 if (*retDataBuf==NULL){
849 PyBuffer_Release(&view);
850 PyErr_NoMemory();
851 return FALSE;
852 }
Brian Curtinabb33512010-08-17 20:08:40 +0000853 *retDataSize = Py_SAFE_DOWNCAST(view.len, Py_ssize_t, DWORD);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 memcpy(*retDataBuf, view.buf, view.len);
855 PyBuffer_Release(&view);
856 }
857 break;
858 }
859 return TRUE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000860}
861
862/* Convert Registry data into PyObject*/
863static PyObject *
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000864Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000865{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 PyObject *obData;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 switch (typ) {
869 case REG_DWORD:
870 if (retDataSize == 0)
871 obData = PyLong_FromLong(0);
872 else
873 obData = PyLong_FromLong(*(int *)retDataBuf);
874 break;
875 case REG_SZ:
876 case REG_EXPAND_SZ:
877 {
878 /* the buffer may or may not have a trailing NULL */
879 wchar_t *data = (wchar_t *)retDataBuf;
880 int len = retDataSize / 2;
881 if (retDataSize && data[len-1] == '\0')
882 retDataSize -= 2;
883 if (retDataSize <= 0)
884 data = L"";
885 obData = PyUnicode_FromUnicode(data, retDataSize/2);
886 break;
887 }
888 case REG_MULTI_SZ:
889 if (retDataSize == 0)
890 obData = PyList_New(0);
891 else
892 {
893 int index = 0;
894 wchar_t *data = (wchar_t *)retDataBuf;
895 int len = retDataSize / 2;
896 int s = countStrings(data, len);
897 wchar_t **str = (wchar_t **)malloc(sizeof(wchar_t *)*s);
898 if (str == NULL)
899 return PyErr_NoMemory();
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 fixupMultiSZ(str, data, len);
902 obData = PyList_New(s);
903 if (obData == NULL)
904 return NULL;
905 for (index = 0; index < s; index++)
906 {
907 size_t len = wcslen(str[index]);
908 if (len > INT_MAX) {
909 PyErr_SetString(PyExc_OverflowError,
910 "registry string is too long for a Python string");
911 Py_DECREF(obData);
912 return NULL;
913 }
914 PyList_SetItem(obData,
915 index,
916 PyUnicode_FromUnicode(str[index], len));
917 }
918 free(str);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 break;
921 }
922 case REG_BINARY:
923 /* ALSO handle ALL unknown data types here. Even if we can't
924 support it natively, we should handle the bits. */
925 default:
926 if (retDataSize == 0) {
927 Py_INCREF(Py_None);
928 obData = Py_None;
929 }
930 else
931 obData = PyBytes_FromStringAndSize(
932 (char *)retDataBuf, retDataSize);
933 break;
934 }
935 return obData;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000936}
937
938/* The Python methods */
939
940static PyObject *
941PyCloseKey(PyObject *self, PyObject *args)
942{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 PyObject *obKey;
944 if (!PyArg_ParseTuple(args, "O:CloseKey", &obKey))
945 return NULL;
946 if (!PyHKEY_Close(obKey))
947 return NULL;
948 Py_INCREF(Py_None);
949 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000950}
951
952static PyObject *
953PyConnectRegistry(PyObject *self, PyObject *args)
954{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 HKEY hKey;
956 PyObject *obKey;
957 wchar_t *szCompName = NULL;
958 HKEY retKey;
959 long rc;
960 if (!PyArg_ParseTuple(args, "ZO:ConnectRegistry", &szCompName, &obKey))
961 return NULL;
962 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
963 return NULL;
964 Py_BEGIN_ALLOW_THREADS
965 rc = RegConnectRegistryW(szCompName, hKey, &retKey);
966 Py_END_ALLOW_THREADS
967 if (rc != ERROR_SUCCESS)
968 return PyErr_SetFromWindowsErrWithFunction(rc,
969 "ConnectRegistry");
970 return PyHKEY_FromHKEY(retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000971}
972
973static PyObject *
974PyCreateKey(PyObject *self, PyObject *args)
975{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 HKEY hKey;
977 PyObject *obKey;
978 wchar_t *subKey;
979 HKEY retKey;
980 long rc;
981 if (!PyArg_ParseTuple(args, "OZ:CreateKey", &obKey, &subKey))
982 return NULL;
983 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
984 return NULL;
985 rc = RegCreateKeyW(hKey, subKey, &retKey);
986 if (rc != ERROR_SUCCESS)
987 return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey");
988 return PyHKEY_FromHKEY(retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000989}
990
991static PyObject *
Brian Curtin3035c392010-04-21 23:56:21 +0000992PyCreateKeyEx(PyObject *self, PyObject *args)
993{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 HKEY hKey;
995 PyObject *obKey;
996 wchar_t *subKey;
997 HKEY retKey;
998 int res = 0;
999 REGSAM sam = KEY_WRITE;
1000 long rc;
1001 if (!PyArg_ParseTuple(args, "OZ|ii:CreateKeyEx", &obKey, &subKey,
1002 &res, &sam))
1003 return NULL;
1004 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1005 return NULL;
Brian Curtin3035c392010-04-21 23:56:21 +00001006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 rc = RegCreateKeyExW(hKey, subKey, res, NULL, (DWORD)NULL,
1008 sam, NULL, &retKey, NULL);
1009 if (rc != ERROR_SUCCESS)
1010 return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKeyEx");
1011 return PyHKEY_FromHKEY(retKey);
Brian Curtin3035c392010-04-21 23:56:21 +00001012}
1013
1014static PyObject *
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001015PyDeleteKey(PyObject *self, PyObject *args)
1016{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 HKEY hKey;
1018 PyObject *obKey;
1019 wchar_t *subKey;
1020 long rc;
1021 if (!PyArg_ParseTuple(args, "Ou:DeleteKey", &obKey, &subKey))
1022 return NULL;
1023 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1024 return NULL;
1025 rc = RegDeleteKeyW(hKey, subKey );
1026 if (rc != ERROR_SUCCESS)
1027 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey");
1028 Py_INCREF(Py_None);
1029 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001030}
1031
1032static PyObject *
Brian Curtin3035c392010-04-21 23:56:21 +00001033PyDeleteKeyEx(PyObject *self, PyObject *args)
1034{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 HKEY hKey;
1036 PyObject *obKey;
1037 HMODULE hMod;
1038 typedef LONG (WINAPI *RDKEFunc)(HKEY, const wchar_t*, REGSAM, int);
1039 RDKEFunc pfn = NULL;
1040 wchar_t *subKey;
1041 long rc;
1042 int res = 0;
1043 REGSAM sam = KEY_WOW64_64KEY;
Brian Curtin3035c392010-04-21 23:56:21 +00001044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 if (!PyArg_ParseTuple(args, "Ou|ii:DeleteKeyEx",
1046 &obKey, &subKey, &sam, &res))
1047 return NULL;
1048 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1049 return NULL;
Brian Curtin3035c392010-04-21 23:56:21 +00001050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 /* Only available on 64bit platforms, so we must load it
1052 dynamically. */
1053 hMod = GetModuleHandle("advapi32.dll");
1054 if (hMod)
1055 pfn = (RDKEFunc)GetProcAddress(hMod,
1056 "RegDeleteKeyExW");
1057 if (!pfn) {
1058 PyErr_SetString(PyExc_NotImplementedError,
1059 "not implemented on this platform");
1060 return NULL;
1061 }
1062 Py_BEGIN_ALLOW_THREADS
1063 rc = (*pfn)(hKey, subKey, sam, res);
1064 Py_END_ALLOW_THREADS
Brian Curtin3035c392010-04-21 23:56:21 +00001065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 if (rc != ERROR_SUCCESS)
1067 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKeyEx");
1068 Py_INCREF(Py_None);
1069 return Py_None;
Brian Curtin3035c392010-04-21 23:56:21 +00001070}
1071
1072static PyObject *
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001073PyDeleteValue(PyObject *self, PyObject *args)
1074{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 HKEY hKey;
1076 PyObject *obKey;
1077 wchar_t *subKey;
1078 long rc;
1079 if (!PyArg_ParseTuple(args, "OZ:DeleteValue", &obKey, &subKey))
1080 return NULL;
1081 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1082 return NULL;
1083 Py_BEGIN_ALLOW_THREADS
1084 rc = RegDeleteValueW(hKey, subKey);
1085 Py_END_ALLOW_THREADS
1086 if (rc !=ERROR_SUCCESS)
1087 return PyErr_SetFromWindowsErrWithFunction(rc,
1088 "RegDeleteValue");
1089 Py_INCREF(Py_None);
1090 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001091}
1092
1093static PyObject *
1094PyEnumKey(PyObject *self, PyObject *args)
1095{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 HKEY hKey;
1097 PyObject *obKey;
1098 int index;
1099 long rc;
1100 PyObject *retStr;
Brian Curtin60853212010-05-26 17:43:50 +00001101
1102 /* The Windows docs claim that the max key name length is 255
1103 * characters, plus a terminating nul character. However,
1104 * empirical testing demonstrates that it is possible to
1105 * create a 256 character key that is missing the terminating
1106 * nul. RegEnumKeyEx requires a 257 character buffer to
1107 * retrieve such a key name. */
1108 wchar_t tmpbuf[257];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 DWORD len = sizeof(tmpbuf); /* includes NULL terminator */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 if (!PyArg_ParseTuple(args, "Oi:EnumKey", &obKey, &index))
1112 return NULL;
1113 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1114 return NULL;
Tim Peters313fcd42006-02-19 04:05:39 +00001115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 Py_BEGIN_ALLOW_THREADS
1117 rc = RegEnumKeyExW(hKey, index, tmpbuf, &len, NULL, NULL, NULL, NULL);
1118 Py_END_ALLOW_THREADS
1119 if (rc != ERROR_SUCCESS)
1120 return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx");
Tim Peters313fcd42006-02-19 04:05:39 +00001121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 retStr = PyUnicode_FromUnicode(tmpbuf, len);
1123 return retStr; /* can be NULL */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001124}
1125
1126static PyObject *
1127PyEnumValue(PyObject *self, PyObject *args)
1128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 HKEY hKey;
1130 PyObject *obKey;
1131 int index;
1132 long rc;
1133 wchar_t *retValueBuf;
Amaury Forgeot d'Arcf2b69df2010-08-16 22:11:29 +00001134 BYTE *tmpBuf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 BYTE *retDataBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001136 DWORD retValueSize, bufValueSize;
1137 DWORD retDataSize, bufDataSize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 DWORD typ;
1139 PyObject *obData;
1140 PyObject *retVal;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 if (!PyArg_ParseTuple(args, "Oi:EnumValue", &obKey, &index))
1143 return NULL;
1144 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1145 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 if ((rc = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, NULL, NULL,
1148 NULL,
1149 &retValueSize, &retDataSize, NULL, NULL))
1150 != ERROR_SUCCESS)
1151 return PyErr_SetFromWindowsErrWithFunction(rc,
1152 "RegQueryInfoKey");
1153 ++retValueSize; /* include null terminators */
1154 ++retDataSize;
Brian Curtin60853212010-05-26 17:43:50 +00001155 bufDataSize = retDataSize;
1156 bufValueSize = retValueSize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 retValueBuf = (wchar_t *)PyMem_Malloc(sizeof(wchar_t) * retValueSize);
1158 if (retValueBuf == NULL)
1159 return PyErr_NoMemory();
1160 retDataBuf = (BYTE *)PyMem_Malloc(retDataSize);
1161 if (retDataBuf == NULL) {
1162 PyMem_Free(retValueBuf);
1163 return PyErr_NoMemory();
1164 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001165
Brian Curtin60853212010-05-26 17:43:50 +00001166 while (1) {
Brian Curtin60853212010-05-26 17:43:50 +00001167 Py_BEGIN_ALLOW_THREADS
1168 rc = RegEnumValueW(hKey,
1169 index,
1170 retValueBuf,
1171 &retValueSize,
1172 NULL,
1173 &typ,
1174 (BYTE *)retDataBuf,
1175 &retDataSize);
1176 Py_END_ALLOW_THREADS
1177
1178 if (rc != ERROR_MORE_DATA)
1179 break;
1180
1181 bufDataSize *= 2;
Amaury Forgeot d'Arcf2b69df2010-08-16 22:11:29 +00001182 tmpBuf = (BYTE *)PyMem_Realloc(retDataBuf, bufDataSize);
Brian Curtin9b7e2d12010-06-08 20:57:52 +00001183 if (tmpBuf == NULL) {
Brian Curtin60853212010-05-26 17:43:50 +00001184 PyErr_NoMemory();
1185 retVal = NULL;
1186 goto fail;
1187 }
Brian Curtin9b7e2d12010-06-08 20:57:52 +00001188 retDataBuf = tmpBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001189 retDataSize = bufDataSize;
1190 retValueSize = bufValueSize;
1191 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 if (rc != ERROR_SUCCESS) {
1194 retVal = PyErr_SetFromWindowsErrWithFunction(rc,
1195 "PyRegEnumValue");
1196 goto fail;
1197 }
1198 obData = Reg2Py(retDataBuf, retDataSize, typ);
1199 if (obData == NULL) {
1200 retVal = NULL;
1201 goto fail;
1202 }
1203 retVal = Py_BuildValue("uOi", retValueBuf, obData, typ);
1204 Py_DECREF(obData);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001205 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 PyMem_Free(retValueBuf);
1207 PyMem_Free(retDataBuf);
1208 return retVal;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001209}
1210
1211static PyObject *
Christian Heimes2380ac72008-01-09 00:17:24 +00001212PyExpandEnvironmentStrings(PyObject *self, PyObject *args)
1213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 Py_UNICODE *retValue = NULL;
1215 Py_UNICODE *src;
1216 DWORD retValueSize;
1217 DWORD rc;
1218 PyObject *o;
Christian Heimes2380ac72008-01-09 00:17:24 +00001219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 if (!PyArg_ParseTuple(args, "u:ExpandEnvironmentStrings", &src))
1221 return NULL;
Christian Heimes2380ac72008-01-09 00:17:24 +00001222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 retValueSize = ExpandEnvironmentStringsW(src, retValue, 0);
1224 if (retValueSize == 0) {
1225 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1226 "ExpandEnvironmentStrings");
1227 }
1228 retValue = (Py_UNICODE *)PyMem_Malloc(retValueSize * sizeof(Py_UNICODE));
1229 if (retValue == NULL) {
1230 return PyErr_NoMemory();
1231 }
Christian Heimes2380ac72008-01-09 00:17:24 +00001232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 rc = ExpandEnvironmentStringsW(src, retValue, retValueSize);
1234 if (rc == 0) {
1235 PyMem_Free(retValue);
1236 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1237 "ExpandEnvironmentStrings");
1238 }
1239 o = PyUnicode_FromUnicode(retValue, wcslen(retValue));
1240 PyMem_Free(retValue);
1241 return o;
Christian Heimes2380ac72008-01-09 00:17:24 +00001242}
1243
1244static PyObject *
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001245PyFlushKey(PyObject *self, PyObject *args)
1246{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 HKEY hKey;
1248 PyObject *obKey;
1249 long rc;
1250 if (!PyArg_ParseTuple(args, "O:FlushKey", &obKey))
1251 return NULL;
1252 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1253 return NULL;
1254 Py_BEGIN_ALLOW_THREADS
1255 rc = RegFlushKey(hKey);
1256 Py_END_ALLOW_THREADS
1257 if (rc != ERROR_SUCCESS)
1258 return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey");
1259 Py_INCREF(Py_None);
1260 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001261}
1262static PyObject *
1263PyLoadKey(PyObject *self, PyObject *args)
1264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 HKEY hKey;
1266 PyObject *obKey;
1267 wchar_t *subKey;
1268 wchar_t *fileName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 long rc;
1271 if (!PyArg_ParseTuple(args, "Ouu:LoadKey", &obKey, &subKey, &fileName))
1272 return NULL;
1273 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1274 return NULL;
1275 Py_BEGIN_ALLOW_THREADS
1276 rc = RegLoadKeyW(hKey, subKey, fileName );
1277 Py_END_ALLOW_THREADS
1278 if (rc != ERROR_SUCCESS)
1279 return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey");
1280 Py_INCREF(Py_None);
1281 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001282}
1283
1284static PyObject *
1285PyOpenKey(PyObject *self, PyObject *args)
1286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 HKEY hKey;
1288 PyObject *obKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 wchar_t *subKey;
1291 int res = 0;
1292 HKEY retKey;
1293 long rc;
1294 REGSAM sam = KEY_READ;
1295 if (!PyArg_ParseTuple(args, "OZ|ii:OpenKey", &obKey, &subKey,
1296 &res, &sam))
1297 return NULL;
1298 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1299 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 Py_BEGIN_ALLOW_THREADS
1302 rc = RegOpenKeyExW(hKey, subKey, res, sam, &retKey);
1303 Py_END_ALLOW_THREADS
1304 if (rc != ERROR_SUCCESS)
1305 return PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx");
1306 return PyHKEY_FromHKEY(retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001307}
1308
1309
1310static PyObject *
1311PyQueryInfoKey(PyObject *self, PyObject *args)
1312{
1313 HKEY hKey;
1314 PyObject *obKey;
1315 long rc;
1316 DWORD nSubKeys, nValues;
1317 FILETIME ft;
1318 LARGE_INTEGER li;
1319 PyObject *l;
1320 PyObject *ret;
1321 if (!PyArg_ParseTuple(args, "O:QueryInfoKey", &obKey))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001323 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001325 if ((rc = RegQueryInfoKey(hKey, NULL, NULL, 0, &nSubKeys, NULL, NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 &nValues, NULL, NULL, NULL, &ft))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001327 != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey");
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001329 li.LowPart = ft.dwLowDateTime;
1330 li.HighPart = ft.dwHighDateTime;
1331 l = PyLong_FromLongLong(li.QuadPart);
1332 if (l == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001334 ret = Py_BuildValue("iiO", nSubKeys, nValues, l);
1335 Py_DECREF(l);
1336 return ret;
1337}
1338
1339static PyObject *
1340PyQueryValue(PyObject *self, PyObject *args)
1341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 HKEY hKey;
1343 PyObject *obKey;
1344 wchar_t *subKey;
1345 long rc;
1346 PyObject *retStr;
1347 wchar_t *retBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001348 DWORD bufSize = 0;
1349 DWORD retSize = 0;
1350 wchar_t *tmp;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 if (!PyArg_ParseTuple(args, "OZ:QueryValue", &obKey, &subKey))
1353 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1356 return NULL;
Brian Curtin60853212010-05-26 17:43:50 +00001357
1358 rc = RegQueryValueW(hKey, subKey, NULL, &retSize);
1359 if (rc == ERROR_MORE_DATA)
1360 retSize = 256;
1361 else if (rc != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 return PyErr_SetFromWindowsErrWithFunction(rc,
1363 "RegQueryValue");
Brian Curtin60853212010-05-26 17:43:50 +00001364
1365 bufSize = retSize;
1366 retBuf = (wchar_t *) PyMem_Malloc(bufSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 if (retBuf == NULL)
1368 return PyErr_NoMemory();
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001369
Brian Curtin60853212010-05-26 17:43:50 +00001370 while (1) {
1371 retSize = bufSize;
1372 rc = RegQueryValueW(hKey, subKey, retBuf, &retSize);
1373 if (rc != ERROR_MORE_DATA)
1374 break;
1375
1376 bufSize *= 2;
1377 tmp = (wchar_t *) PyMem_Realloc(retBuf, bufSize);
1378 if (tmp == NULL) {
1379 PyMem_Free(retBuf);
1380 return PyErr_NoMemory();
1381 }
1382 retBuf = tmp;
1383 }
1384
1385 if (rc != ERROR_SUCCESS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 PyMem_Free(retBuf);
1387 return PyErr_SetFromWindowsErrWithFunction(rc,
1388 "RegQueryValue");
1389 }
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 retStr = PyUnicode_FromUnicode(retBuf, wcslen(retBuf));
1392 PyMem_Free(retBuf);
1393 return retStr;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001394}
1395
1396static PyObject *
1397PyQueryValueEx(PyObject *self, PyObject *args)
1398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 HKEY hKey;
1400 PyObject *obKey;
1401 wchar_t *valueName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 long rc;
Brian Curtin60853212010-05-26 17:43:50 +00001404 BYTE *retBuf, *tmp;
1405 DWORD bufSize = 0, retSize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 DWORD typ;
1407 PyObject *obData;
1408 PyObject *result;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 if (!PyArg_ParseTuple(args, "OZ:QueryValueEx", &obKey, &valueName))
1411 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1414 return NULL;
Brian Curtin60853212010-05-26 17:43:50 +00001415
1416 rc = RegQueryValueExW(hKey, valueName, NULL, NULL, NULL, &bufSize);
1417 if (rc == ERROR_MORE_DATA)
1418 bufSize = 256;
1419 else if (rc != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 return PyErr_SetFromWindowsErrWithFunction(rc,
1421 "RegQueryValueEx");
1422 retBuf = (BYTE *)PyMem_Malloc(bufSize);
1423 if (retBuf == NULL)
1424 return PyErr_NoMemory();
Brian Curtin60853212010-05-26 17:43:50 +00001425
1426 while (1) {
1427 retSize = bufSize;
1428 rc = RegQueryValueExW(hKey, valueName, NULL, &typ,
1429 (BYTE *)retBuf, &retSize);
1430 if (rc != ERROR_MORE_DATA)
1431 break;
1432
1433 bufSize *= 2;
1434 tmp = (char *) PyMem_Realloc(retBuf, bufSize);
1435 if (tmp == NULL) {
1436 PyMem_Free(retBuf);
1437 return PyErr_NoMemory();
1438 }
1439 retBuf = tmp;
1440 }
1441
1442 if (rc != ERROR_SUCCESS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 PyMem_Free(retBuf);
1444 return PyErr_SetFromWindowsErrWithFunction(rc,
1445 "RegQueryValueEx");
1446 }
1447 obData = Reg2Py(retBuf, bufSize, typ);
1448 PyMem_Free(retBuf);
1449 if (obData == NULL)
1450 return NULL;
1451 result = Py_BuildValue("Oi", obData, typ);
1452 Py_DECREF(obData);
1453 return result;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001454}
1455
1456
1457static PyObject *
1458PySaveKey(PyObject *self, PyObject *args)
1459{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 HKEY hKey;
1461 PyObject *obKey;
1462 wchar_t *fileName;
1463 LPSECURITY_ATTRIBUTES pSA = NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 long rc;
1466 if (!PyArg_ParseTuple(args, "Ou:SaveKey", &obKey, &fileName))
1467 return NULL;
1468 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1469 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001470/* One day we may get security into the core?
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE))
1472 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001473*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 Py_BEGIN_ALLOW_THREADS
1475 rc = RegSaveKeyW(hKey, fileName, pSA );
1476 Py_END_ALLOW_THREADS
1477 if (rc != ERROR_SUCCESS)
1478 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey");
1479 Py_INCREF(Py_None);
1480 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001481}
1482
1483static PyObject *
1484PySetValue(PyObject *self, PyObject *args)
1485{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 HKEY hKey;
1487 PyObject *obKey;
1488 wchar_t *subKey;
1489 wchar_t *str;
1490 DWORD typ;
1491 DWORD len;
1492 long rc;
1493 if (!PyArg_ParseTuple(args, "OZiu#:SetValue",
1494 &obKey,
1495 &subKey,
1496 &typ,
1497 &str,
1498 &len))
1499 return NULL;
1500 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1501 return NULL;
1502 if (typ != REG_SZ) {
1503 PyErr_SetString(PyExc_TypeError,
1504 "Type must be winreg.REG_SZ");
1505 return NULL;
1506 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508 Py_BEGIN_ALLOW_THREADS
1509 rc = RegSetValueW(hKey, subKey, REG_SZ, str, len+1);
1510 Py_END_ALLOW_THREADS
1511 if (rc != ERROR_SUCCESS)
1512 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue");
1513 Py_INCREF(Py_None);
1514 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001515}
1516
1517static PyObject *
1518PySetValueEx(PyObject *self, PyObject *args)
1519{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 HKEY hKey;
1521 PyObject *obKey;
1522 Py_UNICODE *valueName;
1523 PyObject *obRes;
1524 PyObject *value;
1525 BYTE *data;
1526 DWORD len;
1527 DWORD typ;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 LONG rc;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001531 if (!PyArg_ParseTuple(args, "OZOiO:SetValueEx",
1532 &obKey,
1533 &valueName,
1534 &obRes,
1535 &typ,
1536 &value))
1537 return NULL;
1538 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1539 return NULL;
1540 if (!Py2Reg(value, typ, &data, &len))
1541 {
1542 if (!PyErr_Occurred())
1543 PyErr_SetString(PyExc_ValueError,
1544 "Could not convert the data to the specified type.");
1545 return NULL;
1546 }
1547 Py_BEGIN_ALLOW_THREADS
1548 rc = RegSetValueExW(hKey, valueName, 0, typ, data, len);
1549 Py_END_ALLOW_THREADS
1550 PyMem_DEL(data);
1551 if (rc != ERROR_SUCCESS)
1552 return PyErr_SetFromWindowsErrWithFunction(rc,
1553 "RegSetValueEx");
1554 Py_INCREF(Py_None);
1555 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001556}
1557
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001558static PyObject *
1559PyDisableReflectionKey(PyObject *self, PyObject *args)
1560{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 HKEY hKey;
1562 PyObject *obKey;
1563 HMODULE hMod;
1564 typedef LONG (WINAPI *RDRKFunc)(HKEY);
1565 RDRKFunc pfn = NULL;
1566 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 if (!PyArg_ParseTuple(args, "O:DisableReflectionKey", &obKey))
1569 return NULL;
1570 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1571 return NULL;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 /* Only available on 64bit platforms, so we must load it
1574 dynamically.*/
1575 hMod = GetModuleHandle("advapi32.dll");
1576 if (hMod)
1577 pfn = (RDRKFunc)GetProcAddress(hMod,
1578 "RegDisableReflectionKey");
1579 if (!pfn) {
1580 PyErr_SetString(PyExc_NotImplementedError,
1581 "not implemented on this platform");
1582 return NULL;
1583 }
1584 Py_BEGIN_ALLOW_THREADS
1585 rc = (*pfn)(hKey);
1586 Py_END_ALLOW_THREADS
1587 if (rc != ERROR_SUCCESS)
1588 return PyErr_SetFromWindowsErrWithFunction(rc,
1589 "RegDisableReflectionKey");
1590 Py_INCREF(Py_None);
1591 return Py_None;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001592}
1593
1594static PyObject *
1595PyEnableReflectionKey(PyObject *self, PyObject *args)
1596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 HKEY hKey;
1598 PyObject *obKey;
1599 HMODULE hMod;
1600 typedef LONG (WINAPI *RERKFunc)(HKEY);
1601 RERKFunc pfn = NULL;
1602 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 if (!PyArg_ParseTuple(args, "O:EnableReflectionKey", &obKey))
1605 return NULL;
1606 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1607 return NULL;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 /* Only available on 64bit platforms, so we must load it
1610 dynamically.*/
1611 hMod = GetModuleHandle("advapi32.dll");
1612 if (hMod)
1613 pfn = (RERKFunc)GetProcAddress(hMod,
1614 "RegEnableReflectionKey");
1615 if (!pfn) {
1616 PyErr_SetString(PyExc_NotImplementedError,
1617 "not implemented on this platform");
1618 return NULL;
1619 }
1620 Py_BEGIN_ALLOW_THREADS
1621 rc = (*pfn)(hKey);
1622 Py_END_ALLOW_THREADS
1623 if (rc != ERROR_SUCCESS)
1624 return PyErr_SetFromWindowsErrWithFunction(rc,
1625 "RegEnableReflectionKey");
1626 Py_INCREF(Py_None);
1627 return Py_None;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001628}
1629
1630static PyObject *
1631PyQueryReflectionKey(PyObject *self, PyObject *args)
1632{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 HKEY hKey;
1634 PyObject *obKey;
1635 HMODULE hMod;
1636 typedef LONG (WINAPI *RQRKFunc)(HKEY, BOOL *);
1637 RQRKFunc pfn = NULL;
1638 BOOL result;
1639 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 if (!PyArg_ParseTuple(args, "O:QueryReflectionKey", &obKey))
1642 return NULL;
1643 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1644 return NULL;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 /* Only available on 64bit platforms, so we must load it
1647 dynamically.*/
1648 hMod = GetModuleHandle("advapi32.dll");
1649 if (hMod)
1650 pfn = (RQRKFunc)GetProcAddress(hMod,
1651 "RegQueryReflectionKey");
1652 if (!pfn) {
1653 PyErr_SetString(PyExc_NotImplementedError,
1654 "not implemented on this platform");
1655 return NULL;
1656 }
1657 Py_BEGIN_ALLOW_THREADS
1658 rc = (*pfn)(hKey, &result);
1659 Py_END_ALLOW_THREADS
1660 if (rc != ERROR_SUCCESS)
1661 return PyErr_SetFromWindowsErrWithFunction(rc,
1662 "RegQueryReflectionKey");
1663 return PyBool_FromLong(result);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001664}
1665
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001666static struct PyMethodDef winreg_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 {"CloseKey", PyCloseKey, METH_VARARGS, CloseKey_doc},
1668 {"ConnectRegistry", PyConnectRegistry, METH_VARARGS, ConnectRegistry_doc},
1669 {"CreateKey", PyCreateKey, METH_VARARGS, CreateKey_doc},
1670 {"CreateKeyEx", PyCreateKeyEx, METH_VARARGS, CreateKeyEx_doc},
1671 {"DeleteKey", PyDeleteKey, METH_VARARGS, DeleteKey_doc},
1672 {"DeleteKeyEx", PyDeleteKeyEx, METH_VARARGS, DeleteKeyEx_doc},
1673 {"DeleteValue", PyDeleteValue, METH_VARARGS, DeleteValue_doc},
1674 {"DisableReflectionKey", PyDisableReflectionKey, METH_VARARGS, DisableReflectionKey_doc},
1675 {"EnableReflectionKey", PyEnableReflectionKey, METH_VARARGS, EnableReflectionKey_doc},
1676 {"EnumKey", PyEnumKey, METH_VARARGS, EnumKey_doc},
1677 {"EnumValue", PyEnumValue, METH_VARARGS, EnumValue_doc},
1678 {"ExpandEnvironmentStrings", PyExpandEnvironmentStrings, METH_VARARGS,
1679 ExpandEnvironmentStrings_doc },
1680 {"FlushKey", PyFlushKey, METH_VARARGS, FlushKey_doc},
1681 {"LoadKey", PyLoadKey, METH_VARARGS, LoadKey_doc},
1682 {"OpenKey", PyOpenKey, METH_VARARGS, OpenKey_doc},
1683 {"OpenKeyEx", PyOpenKey, METH_VARARGS, OpenKeyEx_doc},
1684 {"QueryValue", PyQueryValue, METH_VARARGS, QueryValue_doc},
1685 {"QueryValueEx", PyQueryValueEx, METH_VARARGS, QueryValueEx_doc},
1686 {"QueryInfoKey", PyQueryInfoKey, METH_VARARGS, QueryInfoKey_doc},
1687 {"QueryReflectionKey",PyQueryReflectionKey,METH_VARARGS, QueryReflectionKey_doc},
1688 {"SaveKey", PySaveKey, METH_VARARGS, SaveKey_doc},
1689 {"SetValue", PySetValue, METH_VARARGS, SetValue_doc},
1690 {"SetValueEx", PySetValueEx, METH_VARARGS, SetValueEx_doc},
1691 NULL,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001692};
1693
1694static void
1695insint(PyObject * d, char * name, long value)
1696{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 PyObject *v = PyLong_FromLong(value);
1698 if (!v || PyDict_SetItemString(d, name, v))
1699 PyErr_Clear();
1700 Py_XDECREF(v);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001701}
1702
1703#define ADD_INT(val) insint(d, #val, val)
1704
1705static void
1706inskey(PyObject * d, char * name, HKEY key)
1707{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 PyObject *v = PyLong_FromVoidPtr(key);
1709 if (!v || PyDict_SetItemString(d, name, v))
1710 PyErr_Clear();
1711 Py_XDECREF(v);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001712}
1713
1714#define ADD_KEY(val) inskey(d, #val, val)
1715
Martin v. Löwis1a214512008-06-11 05:26:20 +00001716
1717static struct PyModuleDef winregmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 PyModuleDef_HEAD_INIT,
1719 "winreg",
1720 module_doc,
1721 -1,
1722 winreg_methods,
1723 NULL,
1724 NULL,
1725 NULL,
1726 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001727};
1728
1729PyMODINIT_FUNC PyInit_winreg(void)
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001730{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 PyObject *m, *d;
1732 m = PyModule_Create(&winregmodule);
1733 if (m == NULL)
1734 return NULL;
1735 d = PyModule_GetDict(m);
1736 PyHKEY_Type.tp_doc = PyHKEY_doc;
1737 if (PyType_Ready(&PyHKEY_Type) < 0)
1738 return NULL;
1739 Py_INCREF(&PyHKEY_Type);
1740 if (PyDict_SetItemString(d, "HKEYType",
1741 (PyObject *)&PyHKEY_Type) != 0)
1742 return NULL;
1743 Py_INCREF(PyExc_WindowsError);
1744 if (PyDict_SetItemString(d, "error",
1745 PyExc_WindowsError) != 0)
1746 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 /* Add the relevant constants */
1749 ADD_KEY(HKEY_CLASSES_ROOT);
1750 ADD_KEY(HKEY_CURRENT_USER);
1751 ADD_KEY(HKEY_LOCAL_MACHINE);
1752 ADD_KEY(HKEY_USERS);
1753 ADD_KEY(HKEY_PERFORMANCE_DATA);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001754#ifdef HKEY_CURRENT_CONFIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 ADD_KEY(HKEY_CURRENT_CONFIG);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001756#endif
1757#ifdef HKEY_DYN_DATA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 ADD_KEY(HKEY_DYN_DATA);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001759#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760 ADD_INT(KEY_QUERY_VALUE);
1761 ADD_INT(KEY_SET_VALUE);
1762 ADD_INT(KEY_CREATE_SUB_KEY);
1763 ADD_INT(KEY_ENUMERATE_SUB_KEYS);
1764 ADD_INT(KEY_NOTIFY);
1765 ADD_INT(KEY_CREATE_LINK);
1766 ADD_INT(KEY_READ);
1767 ADD_INT(KEY_WRITE);
1768 ADD_INT(KEY_EXECUTE);
1769 ADD_INT(KEY_ALL_ACCESS);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001770#ifdef KEY_WOW64_64KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 ADD_INT(KEY_WOW64_64KEY);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001772#endif
1773#ifdef KEY_WOW64_32KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 ADD_INT(KEY_WOW64_32KEY);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001775#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 ADD_INT(REG_OPTION_RESERVED);
1777 ADD_INT(REG_OPTION_NON_VOLATILE);
1778 ADD_INT(REG_OPTION_VOLATILE);
1779 ADD_INT(REG_OPTION_CREATE_LINK);
1780 ADD_INT(REG_OPTION_BACKUP_RESTORE);
1781 ADD_INT(REG_OPTION_OPEN_LINK);
1782 ADD_INT(REG_LEGAL_OPTION);
1783 ADD_INT(REG_CREATED_NEW_KEY);
1784 ADD_INT(REG_OPENED_EXISTING_KEY);
1785 ADD_INT(REG_WHOLE_HIVE_VOLATILE);
1786 ADD_INT(REG_REFRESH_HIVE);
1787 ADD_INT(REG_NO_LAZY_FLUSH);
1788 ADD_INT(REG_NOTIFY_CHANGE_NAME);
1789 ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES);
1790 ADD_INT(REG_NOTIFY_CHANGE_LAST_SET);
1791 ADD_INT(REG_NOTIFY_CHANGE_SECURITY);
1792 ADD_INT(REG_LEGAL_CHANGE_FILTER);
1793 ADD_INT(REG_NONE);
1794 ADD_INT(REG_SZ);
1795 ADD_INT(REG_EXPAND_SZ);
1796 ADD_INT(REG_BINARY);
1797 ADD_INT(REG_DWORD);
1798 ADD_INT(REG_DWORD_LITTLE_ENDIAN);
1799 ADD_INT(REG_DWORD_BIG_ENDIAN);
1800 ADD_INT(REG_LINK);
1801 ADD_INT(REG_MULTI_SZ);
1802 ADD_INT(REG_RESOURCE_LIST);
1803 ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
1804 ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
1805 return m;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001806}
1807
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001808