blob: d3c39f91eac9848eacde3697fdcd1e67680a5280 [file] [log] [blame]
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001/*
Fred Drake270e19b2000-06-29 16:14:14 +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 Pitrouc83ea132010-05-09 14:46:46 +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"
17#include "malloc.h" /* for alloca */
Kristján Valur Jónsson629ec262007-05-26 19:31:39 +000018#include "windows.h"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000019
20static BOOL PyHKEY_AsHKEY(PyObject *ob, HKEY *pRes, BOOL bNoneOK);
21static PyObject *PyHKEY_FromHKEY(HKEY h);
22static BOOL PyHKEY_Close(PyObject *obHandle);
23
24static char errNotAHandle[] = "Object is not a handle";
25
26/* The win32api module reports the function name that failed,
27 but this concept is not in the Python core.
Mark Dickinson3e4caeb2009-02-21 20:27:01 +000028 Hopefully it will one day, and in the meantime I don't
Guido van Rossum9f3712c2000-03-28 20:37:15 +000029 want to lose this info...
30*/
31#define PyErr_SetFromWindowsErrWithFunction(rc, fnname) \
Antoine Pitrouc83ea132010-05-09 14:46:46 +000032 PyErr_SetFromWindowsErr(rc)
Guido van Rossum9f3712c2000-03-28 20:37:15 +000033
34/* Forward declares */
35
36/* Doc strings */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000037PyDoc_STRVAR(module_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +000038"This module provides access to the Windows registry API.\n"
39"\n"
40"Functions:\n"
41"\n"
42"CloseKey() - Closes a registry key.\n"
43"ConnectRegistry() - Establishes a connection to a predefined registry handle\n"
44" on another computer.\n"
45"CreateKey() - Creates the specified key, or opens it if it already exists.\n"
46"DeleteKey() - Deletes the specified key.\n"
47"DeleteValue() - Removes a named value from the specified registry key.\n"
48"EnumKey() - Enumerates subkeys of the specified open registry key.\n"
49"EnumValue() - Enumerates values of the specified open registry key.\n"
Christian Heimesb39a7562008-01-08 15:46:10 +000050"ExpandEnvironmentStrings() - Expand the env strings in a REG_EXPAND_SZ string.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000051"FlushKey() - Writes all the attributes of the specified key to the registry.\n"
52"LoadKey() - Creates a subkey under HKEY_USER or HKEY_LOCAL_MACHINE and stores\n"
53" registration information from a specified file into that subkey.\n"
54"OpenKey() - Alias for <om win32api.RegOpenKeyEx>\n"
55"OpenKeyEx() - Opens the specified key.\n"
56"QueryValue() - Retrieves the value associated with the unnamed value for a\n"
57" specified key in the registry.\n"
58"QueryValueEx() - Retrieves the type and data for a specified value name\n"
59" associated with an open registry key.\n"
60"QueryInfoKey() - Returns information about the specified key.\n"
61"SaveKey() - Saves the specified key, and all its subkeys a file.\n"
62"SetValue() - Associates a value with a specified key.\n"
63"SetValueEx() - Stores data in the value field of an open registry key.\n"
64"\n"
65"Special objects:\n"
66"\n"
67"HKEYType -- type object for HKEY objects\n"
68"error -- exception raised for Win32 errors\n"
69"\n"
70"Integer constants:\n"
71"Many constants are defined - see the documentation for each function\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000072"to see what constants are used, and where.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +000073
74
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000075PyDoc_STRVAR(CloseKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +000076"CloseKey(hkey) - Closes a previously opened registry key.\n"
77"\n"
78"The hkey argument specifies a previously opened key.\n"
79"\n"
80"Note that if the key is not closed using this method, it will be\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000081"closed when the hkey object is destroyed by Python.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +000082
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000083PyDoc_STRVAR(ConnectRegistry_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +000084"key = ConnectRegistry(computer_name, key) - "
85"Establishes a connection to a predefined registry handle on another computer.\n"
86"\n"
87"computer_name is the name of the remote computer, of the form \\\\computername.\n"
88" If None, the local computer is used.\n"
89"key is the predefined handle to connect to.\n"
90"\n"
91"The return value is the handle of the opened key.\n"
Brian Curtine33fa882010-04-02 21:18:14 +000092"If the function fails, a WindowsError exception is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +000093
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000094PyDoc_STRVAR(CreateKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +000095"key = CreateKey(key, sub_key) - Creates or opens the specified key.\n"
96"\n"
97"key is an already open key, or one of the predefined HKEY_* constants\n"
98"sub_key is a string that names the key this method opens or creates.\n"
99" If key is one of the predefined keys, sub_key may be None. In that case,\n"
100" the handle returned is the same key handle passed in to the function.\n"
101"\n"
102"If the key already exists, this function opens the existing key\n"
103"\n"
104"The return value is the handle of the opened key.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000105"If the function fails, an exception is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000106
Brian Curtine33fa882010-04-02 21:18:14 +0000107PyDoc_STRVAR(CreateKeyEx_doc,
108"key = CreateKeyEx(key, sub_key, res, sam) - Creates or opens the specified key.\n"
109"\n"
110"key is an already open key, or one of the predefined HKEY_* constants\n"
111"sub_key is a string that names the key this method opens or creates.\n"
112"res is a reserved integer, and must be zero. Default is zero.\n"
113"sam is an integer that specifies an access mask that describes the desired\n"
114" If key is one of the predefined keys, sub_key may be None. In that case,\n"
115" the handle returned is the same key handle passed in to the function.\n"
116"\n"
117"If the key already exists, this function opens the existing key\n"
118"\n"
119"The return value is the handle of the opened key.\n"
120"If the function fails, an exception is raised.");
121
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000122PyDoc_STRVAR(DeleteKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000123"DeleteKey(key, sub_key) - Deletes the specified key.\n"
124"\n"
125"key is an already open key, or any one of the predefined HKEY_* constants.\n"
126"sub_key is a string that must be a subkey of the key identified by the key parameter.\n"
127" This value must not be None, and the key may not have subkeys.\n"
128"\n"
129"This method can not delete keys with subkeys.\n"
130"\n"
131"If the method succeeds, the entire key, including all of its values,\n"
Brian Curtine33fa882010-04-02 21:18:14 +0000132"is removed. If the method fails, a WindowsError exception is raised.");
133
134PyDoc_STRVAR(DeleteKeyEx_doc,
135"DeleteKeyEx(key, sub_key, sam, res) - Deletes the specified key.\n"
136"\n"
137"key is an already open key, or any one of the predefined HKEY_* constants.\n"
138"sub_key is a string that must be a subkey of the key identified by the key parameter.\n"
139"res is a reserved integer, and must be zero. Default is zero.\n"
140"sam is an integer that specifies an access mask that describes the desired\n"
141" This value must not be None, and the key may not have subkeys.\n"
142"\n"
143"This method can not delete keys with subkeys.\n"
144"\n"
145"If the method succeeds, the entire key, including all of its values,\n"
146"is removed. If the method fails, a WindowsError exception is raised.\n"
147"On unsupported Windows versions, NotImplementedError is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000148
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000149PyDoc_STRVAR(DeleteValue_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000150"DeleteValue(key, value) - Removes a named value from a registry key.\n"
151"\n"
152"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000153"value is a string that identifies the value to remove.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000154
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000155PyDoc_STRVAR(EnumKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000156"string = EnumKey(key, index) - Enumerates subkeys of an open registry key.\n"
157"\n"
158"key is an already open key, or any one of the predefined HKEY_* constants.\n"
159"index is an integer that identifies the index of the key to retrieve.\n"
160"\n"
161"The function retrieves the name of one subkey each time it is called.\n"
Brian Curtine33fa882010-04-02 21:18:14 +0000162"It is typically called repeatedly until a WindowsError exception is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000163"raised, indicating no more values are available.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000164
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000165PyDoc_STRVAR(EnumValue_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000166"tuple = EnumValue(key, index) - Enumerates values of an open registry key.\n"
167"key is an already open key, or any one of the predefined HKEY_* constants.\n"
168"index is an integer that identifies the index of the value to retrieve.\n"
169"\n"
170"The function retrieves the name of one subkey each time it is called.\n"
Brian Curtine33fa882010-04-02 21:18:14 +0000171"It is typically called repeatedly, until a WindowsError exception\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000172"is raised, indicating no more values.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000173"\n"
174"The result is a tuple of 3 items:\n"
175"value_name is a string that identifies the value.\n"
176"value_data is an object that holds the value data, and whose type depends\n"
177" on the underlying registry type.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000178"data_type is an integer that identifies the type of the value data.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000179
Christian Heimesb39a7562008-01-08 15:46:10 +0000180PyDoc_STRVAR(ExpandEnvironmentStrings_doc,
181"string = ExpandEnvironmentStrings(string) - Expand environment vars.\n");
182
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000183PyDoc_STRVAR(FlushKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000184"FlushKey(key) - Writes all the attributes of a key to the registry.\n"
185"\n"
186"key is an already open key, or any one of the predefined HKEY_* constants.\n"
187"\n"
188"It is not necessary to call RegFlushKey to change a key.\n"
189"Registry changes are flushed to disk by the registry using its lazy flusher.\n"
190"Registry changes are also flushed to disk at system shutdown.\n"
191"Unlike CloseKey(), the FlushKey() method returns only when all the data has\n"
192"been written to the registry.\n"
193"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 +0000194"If you don't know whether a FlushKey() call is required, it probably isn't.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000195
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000196PyDoc_STRVAR(LoadKey_doc,
Mark Hammondb422f952000-06-09 06:01:47 +0000197"LoadKey(key, sub_key, file_name) - Creates a subkey under the specified key\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000198"and stores registration information from a specified file into that subkey.\n"
199"\n"
200"key is an already open key, or any one of the predefined HKEY_* constants.\n"
201"sub_key is a string that identifies the sub_key to load\n"
202"file_name is the name of the file to load registry data from.\n"
203" This file must have been created with the SaveKey() function.\n"
204" Under the file allocation table (FAT) file system, the filename may not\n"
205"have an extension.\n"
206"\n"
207"A call to LoadKey() fails if the calling process does not have the\n"
208"SE_RESTORE_PRIVILEGE privilege.\n"
209"\n"
210"If key is a handle returned by ConnectRegistry(), then the path specified\n"
211"in fileName is relative to the remote computer.\n"
212"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000213"The docs imply key must be in the HKEY_USER or HKEY_LOCAL_MACHINE tree");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000214
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000215PyDoc_STRVAR(OpenKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000216"key = OpenKey(key, sub_key, res = 0, sam = KEY_READ) - Opens the specified key.\n"
217"\n"
218"key is an already open key, or any one of the predefined HKEY_* constants.\n"
219"sub_key is a string that identifies the sub_key to open\n"
220"res is a reserved integer, and must be zero. Default is zero.\n"
221"sam is an integer that specifies an access mask that describes the desired\n"
222" security access for the key. Default is KEY_READ\n"
223"\n"
224"The result is a new handle to the specified key\n"
Brian Curtine33fa882010-04-02 21:18:14 +0000225"If the function fails, a WindowsError exception is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000226
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000227PyDoc_STRVAR(OpenKeyEx_doc, "See OpenKey()");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000228
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000229PyDoc_STRVAR(QueryInfoKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000230"tuple = QueryInfoKey(key) - Returns information about a key.\n"
231"\n"
232"key is an already open key, or any one of the predefined HKEY_* constants.\n"
233"\n"
234"The result is a tuple of 3 items:"
235"An integer that identifies the number of sub keys this key has.\n"
236"An integer that identifies the number of values this key has.\n"
237"A long integer that identifies when the key was last modified (if available)\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000238" as 100's of nanoseconds since Jan 1, 1600.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000239
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000240PyDoc_STRVAR(QueryValue_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000241"string = QueryValue(key, sub_key) - retrieves the unnamed value for a key.\n"
242"\n"
243"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000244"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 +0000245" is associated. If this parameter is None or empty, the function retrieves\n"
246" the value set by the SetValue() method for the key identified by key."
247"\n"
248"Values in the registry have name, type, and data components. This method\n"
249"retrieves the data for a key's first value that has a NULL name.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000250"But the underlying API call doesn't return the type, Lame Lame Lame, DONT USE THIS!!!");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000251
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000252PyDoc_STRVAR(QueryValueEx_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000253"value,type_id = QueryValueEx(key, value_name) - Retrieves the type and data for a specified value name associated with an open registry key.\n"
254"\n"
255"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000256"value_name is a string indicating the value to query");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000257
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000258PyDoc_STRVAR(SaveKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000259"SaveKey(key, file_name) - Saves the specified key, and all its subkeys to the specified file.\n"
260"\n"
261"key is an already open key, or any one of the predefined HKEY_* constants.\n"
262"file_name is the name of the file to save registry data to.\n"
263" This file cannot already exist. If this filename includes an extension,\n"
264" it cannot be used on file allocation table (FAT) file systems by the\n"
265" LoadKey(), ReplaceKey() or RestoreKey() methods.\n"
266"\n"
267"If key represents a key on a remote computer, the path described by\n"
268"file_name is relative to the remote computer.\n"
269"The caller of this method must possess the SeBackupPrivilege security privilege.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000270"This function passes NULL for security_attributes to the API.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000271
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000272PyDoc_STRVAR(SetValue_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000273"SetValue(key, sub_key, type, value) - Associates a value with a specified key.\n"
274"\n"
275"key is an already open key, or any one of the predefined HKEY_* constants.\n"
276"sub_key is a string that names the subkey with which the value is associated.\n"
277"type is an integer that specifies the type of the data. Currently this\n"
278" must be REG_SZ, meaning only strings are supported.\n"
279"value is a string that specifies the new value.\n"
280"\n"
281"If the key specified by the sub_key parameter does not exist, the SetValue\n"
282"function creates it.\n"
283"\n"
284"Value lengths are limited by available memory. Long values (more than\n"
285"2048 bytes) should be stored as files with the filenames stored in \n"
286"the configuration registry. This helps the registry perform efficiently.\n"
287"\n"
288"The key identified by the key parameter must have been opened with\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000289"KEY_SET_VALUE access.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000290
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000291PyDoc_STRVAR(SetValueEx_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000292"SetValueEx(key, value_name, reserved, type, value) - Stores data in the value field of an open registry key.\n"
293"\n"
294"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Mark Hammondc9083b62003-01-15 23:38:15 +0000295"value_name is a string containing the name of the value to set, or None\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000296"type is an integer that specifies the type of the data. This should be one of:\n"
297" REG_BINARY -- Binary data in any form.\n"
298" REG_DWORD -- A 32-bit number.\n"
299" REG_DWORD_LITTLE_ENDIAN -- A 32-bit number in little-endian format.\n"
300" REG_DWORD_BIG_ENDIAN -- A 32-bit number in big-endian format.\n"
301" REG_EXPAND_SZ -- A null-terminated string that contains unexpanded references\n"
302" to environment variables (for example, %PATH%).\n"
303" REG_LINK -- A Unicode symbolic link.\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000304" REG_MULTI_SZ -- An sequence of null-terminated strings, terminated by\n"
305" two null characters. Note that Python handles this\n"
306" termination automatically.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000307" REG_NONE -- No defined value type.\n"
308" REG_RESOURCE_LIST -- A device-driver resource list.\n"
309" REG_SZ -- A null-terminated string.\n"
310"reserved can be anything - zero is always passed to the API.\n"
311"value is a string that specifies the new value.\n"
312"\n"
313"This method can also set additional value and type information for the\n"
314"specified key. The key identified by the key parameter must have been\n"
315"opened with KEY_SET_VALUE access.\n"
316"\n"
317"To open the key, use the CreateKeyEx() or OpenKeyEx() methods.\n"
318"\n"
319"Value lengths are limited by available memory. Long values (more than\n"
320"2048 bytes) should be stored as files with the filenames stored in \n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000321"the configuration registry. This helps the registry perform efficiently.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000322
Mark Hammond8a3c8712008-04-06 01:42:06 +0000323PyDoc_STRVAR(DisableReflectionKey_doc,
Andrew M. Kuchlingeba00042008-04-08 01:33:10 +0000324"Disables registry reflection for 32-bit processes running on a 64-bit\n"
Mark Hammond8a3c8712008-04-06 01:42:06 +0000325"Operating System. Will generally raise NotImplemented if executed on\n"
Andrew M. Kuchlingeba00042008-04-08 01:33:10 +0000326"a 32-bit Operating System.\n"
Mark Hammond8a3c8712008-04-06 01:42:06 +0000327"If the key is not on the reflection list, the function succeeds but has no effect.\n"
328"Disabling reflection for a key does not affect reflection of any subkeys.");
329
330PyDoc_STRVAR(EnableReflectionKey_doc,
331"Restores registry reflection for the specified disabled key.\n"
Andrew M. Kuchlingeba00042008-04-08 01:33:10 +0000332"Will generally raise NotImplemented if executed on a 32-bit Operating System.\n"
Mark Hammond8a3c8712008-04-06 01:42:06 +0000333"Restoring reflection for a key does not affect reflection of any subkeys.");
334
335PyDoc_STRVAR(QueryReflectionKey_doc,
336"bool = QueryReflectionKey(hkey) - Determines the reflection state for the specified key.\n"
Andrew M. Kuchlingeba00042008-04-08 01:33:10 +0000337"Will generally raise NotImplemented if executed on a 32-bit Operating System.\n");
Mark Hammond8a3c8712008-04-06 01:42:06 +0000338
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000339/* PyHKEY docstrings */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000340PyDoc_STRVAR(PyHKEY_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000341"PyHKEY Object - A Python object, representing a win32 registry key.\n"
342"\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000343"This object wraps a Windows HKEY object, automatically closing it when\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000344"the object is destroyed. To guarantee cleanup, you can call either\n"
345"the Close() method on the PyHKEY, or the CloseKey() method.\n"
346"\n"
347"All functions which accept a handle object also accept an integer - \n"
348"however, use of the handle object is encouraged.\n"
349"\n"
350"Functions:\n"
351"Close() - Closes the underlying handle.\n"
352"Detach() - Returns the integer Win32 handle, detaching it from the object\n"
353"\n"
354"Properties:\n"
355"handle - The integer Win32 handle.\n"
356"\n"
357"Operations:\n"
358"__nonzero__ - Handles with an open object return true, otherwise false.\n"
359"__int__ - Converting a handle to an integer returns the Win32 handle.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000360"__cmp__ - Handle objects are compared using the handle value.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000361
362
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000363PyDoc_STRVAR(PyHKEY_Close_doc,
Mark Hammondb422f952000-06-09 06:01:47 +0000364"key.Close() - Closes the underlying Windows handle.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000365"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000366"If the handle is already closed, no error is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000367
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000368PyDoc_STRVAR(PyHKEY_Detach_doc,
Mark Hammondb422f952000-06-09 06:01:47 +0000369"int = key.Detach() - Detaches the Windows handle from the handle object.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000370"\n"
371"The result is the value of the handle before it is detached. If the\n"
372"handle is already detached, this will return zero.\n"
373"\n"
374"After calling this function, the handle is effectively invalidated,\n"
375"but the handle is not closed. You would call this function when you\n"
376"need the underlying win32 handle to exist beyond the lifetime of the\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000377"handle object.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000378"On 64 bit windows, the result of this function is a long integer");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000379
380
381/************************************************************************
382
383 The PyHKEY object definition
384
385************************************************************************/
386typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000387 PyObject_VAR_HEAD
388 HKEY hkey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000389} PyHKEYObject;
390
391#define PyHKEY_Check(op) ((op)->ob_type == &PyHKEY_Type)
392
393static char *failMsg = "bad operand type";
394
395static PyObject *
396PyHKEY_unaryFailureFunc(PyObject *ob)
397{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000398 PyErr_SetString(PyExc_TypeError, failMsg);
399 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000400}
401static PyObject *
402PyHKEY_binaryFailureFunc(PyObject *ob1, PyObject *ob2)
403{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000404 PyErr_SetString(PyExc_TypeError, failMsg);
405 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000406}
407static PyObject *
408PyHKEY_ternaryFailureFunc(PyObject *ob1, PyObject *ob2, PyObject *ob3)
409{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000410 PyErr_SetString(PyExc_TypeError, failMsg);
411 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000412}
413
414static void
415PyHKEY_deallocFunc(PyObject *ob)
416{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000417 /* Can not call PyHKEY_Close, as the ob->tp_type
418 has already been cleared, thus causing the type
419 check to fail!
420 */
421 PyHKEYObject *obkey = (PyHKEYObject *)ob;
422 if (obkey->hkey)
423 RegCloseKey((HKEY)obkey->hkey);
424 PyObject_DEL(ob);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000425}
426
427static int
428PyHKEY_nonzeroFunc(PyObject *ob)
429{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000430 return ((PyHKEYObject *)ob)->hkey != 0;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000431}
432
433static PyObject *
434PyHKEY_intFunc(PyObject *ob)
435{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000436 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
437 return PyLong_FromVoidPtr(pyhkey->hkey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000438}
439
440static int
441PyHKEY_printFunc(PyObject *ob, FILE *fp, int flags)
442{
Amaury Forgeot d'Arca18392a2009-01-13 23:19:08 +0000443 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
444 fprintf(fp, "<PyHKEY at %p (%p)>",
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000445 ob, pyhkey->hkey);
Amaury Forgeot d'Arca18392a2009-01-13 23:19:08 +0000446 return 0;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000447}
448
449static PyObject *
450PyHKEY_strFunc(PyObject *ob)
451{
Amaury Forgeot d'Arca18392a2009-01-13 23:19:08 +0000452 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
453 return PyString_FromFormat("<PyHKEY:%p>", pyhkey->hkey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000454}
455
456static int
457PyHKEY_compareFunc(PyObject *ob1, PyObject *ob2)
458{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000459 PyHKEYObject *pyhkey1 = (PyHKEYObject *)ob1;
460 PyHKEYObject *pyhkey2 = (PyHKEYObject *)ob2;
461 return pyhkey1 == pyhkey2 ? 0 :
462 (pyhkey1 < pyhkey2 ? -1 : 1);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000463}
464
465static long
466PyHKEY_hashFunc(PyObject *ob)
467{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000468 /* Just use the address.
469 XXX - should we use the handle value?
470 */
471 return _Py_HashPointer(ob);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000472}
473
474
475static PyNumberMethods PyHKEY_NumberMethods =
476{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000477 PyHKEY_binaryFailureFunc, /* nb_add */
478 PyHKEY_binaryFailureFunc, /* nb_subtract */
479 PyHKEY_binaryFailureFunc, /* nb_multiply */
480 PyHKEY_binaryFailureFunc, /* nb_divide */
481 PyHKEY_binaryFailureFunc, /* nb_remainder */
482 PyHKEY_binaryFailureFunc, /* nb_divmod */
483 PyHKEY_ternaryFailureFunc, /* nb_power */
484 PyHKEY_unaryFailureFunc, /* nb_negative */
485 PyHKEY_unaryFailureFunc, /* nb_positive */
486 PyHKEY_unaryFailureFunc, /* nb_absolute */
487 PyHKEY_nonzeroFunc, /* nb_nonzero */
488 PyHKEY_unaryFailureFunc, /* nb_invert */
489 PyHKEY_binaryFailureFunc, /* nb_lshift */
490 PyHKEY_binaryFailureFunc, /* nb_rshift */
491 PyHKEY_binaryFailureFunc, /* nb_and */
492 PyHKEY_binaryFailureFunc, /* nb_xor */
493 PyHKEY_binaryFailureFunc, /* nb_or */
494 0, /* nb_coerce (allowed to be zero) */
495 PyHKEY_intFunc, /* nb_int */
496 PyHKEY_unaryFailureFunc, /* nb_long */
497 PyHKEY_unaryFailureFunc, /* nb_float */
498 PyHKEY_unaryFailureFunc, /* nb_oct */
499 PyHKEY_unaryFailureFunc, /* nb_hex */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000500};
501
Amaury Forgeot d'Arc901f2002009-06-09 23:08:13 +0000502static PyObject *PyHKEY_CloseMethod(PyObject *self, PyObject *args);
503static PyObject *PyHKEY_DetachMethod(PyObject *self, PyObject *args);
504static PyObject *PyHKEY_Enter(PyObject *self);
505static PyObject *PyHKEY_Exit(PyObject *self, PyObject *args);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000506
Amaury Forgeot d'Arc901f2002009-06-09 23:08:13 +0000507static struct PyMethodDef PyHKEY_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000508 {"Close", PyHKEY_CloseMethod, METH_VARARGS, PyHKEY_Close_doc},
509 {"Detach", PyHKEY_DetachMethod, METH_VARARGS, PyHKEY_Detach_doc},
510 {"__enter__", (PyCFunction)PyHKEY_Enter, METH_NOARGS, NULL},
511 {"__exit__", PyHKEY_Exit, METH_VARARGS, NULL},
512 {NULL}
Amaury Forgeot d'Arc901f2002009-06-09 23:08:13 +0000513};
514
515static PyMemberDef PyHKEY_memberlist[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000516 {"handle", T_PYSSIZET, offsetof(PyHKEYObject, hkey), READONLY},
517 {NULL} /* Sentinel */
Amaury Forgeot d'Arc901f2002009-06-09 23:08:13 +0000518};
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000519
520/* The type itself */
521PyTypeObject PyHKEY_Type =
522{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000523 PyVarObject_HEAD_INIT(0, 0) /* fill in type at module init */
524 "PyHKEY",
525 sizeof(PyHKEYObject),
526 0,
527 PyHKEY_deallocFunc, /* tp_dealloc */
528 PyHKEY_printFunc, /* tp_print */
529 0, /* tp_getattr */
530 0, /* tp_setattr */
531 PyHKEY_compareFunc, /* tp_compare */
532 0, /* tp_repr */
533 &PyHKEY_NumberMethods, /* tp_as_number */
534 0, /* tp_as_sequence */
535 0, /* tp_as_mapping */
536 PyHKEY_hashFunc, /* tp_hash */
537 0, /* tp_call */
538 PyHKEY_strFunc, /* tp_str */
539 0, /* tp_getattro */
540 0, /* tp_setattro */
541 0, /* tp_as_buffer */
542 Py_TPFLAGS_DEFAULT, /* tp_flags */
543 PyHKEY_doc, /* tp_doc */
544 0, /* tp_traverse */
545 0, /* tp_clear */
546 0, /* tp_richcompare */
547 0, /* tp_weaklistoffset */
548 0, /* tp_iter */
549 0, /* tp_iternext */
550 PyHKEY_methods, /* tp_methods */
551 PyHKEY_memberlist, /* tp_members */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000552};
553
554/************************************************************************
555
556 The PyHKEY object methods
557
558************************************************************************/
559static PyObject *
560PyHKEY_CloseMethod(PyObject *self, PyObject *args)
561{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000562 if (!PyArg_ParseTuple(args, ":Close"))
563 return NULL;
564 if (!PyHKEY_Close(self))
565 return NULL;
566 Py_INCREF(Py_None);
567 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000568}
569
570static PyObject *
571PyHKEY_DetachMethod(PyObject *self, PyObject *args)
572{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000573 void* ret;
574 PyHKEYObject *pThis = (PyHKEYObject *)self;
575 if (!PyArg_ParseTuple(args, ":Detach"))
576 return NULL;
577 ret = (void*)pThis->hkey;
578 pThis->hkey = 0;
579 return PyLong_FromVoidPtr(ret);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000580}
581
Christian Heimesb39a7562008-01-08 15:46:10 +0000582static PyObject *
583PyHKEY_Enter(PyObject *self)
584{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000585 Py_XINCREF(self);
586 return self;
Christian Heimesb39a7562008-01-08 15:46:10 +0000587}
588
589static PyObject *
590PyHKEY_Exit(PyObject *self, PyObject *args)
591{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000592 if (!PyHKEY_Close(self))
593 return NULL;
594 Py_RETURN_NONE;
Christian Heimesb39a7562008-01-08 15:46:10 +0000595}
596
597
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000598/************************************************************************
599 The public PyHKEY API (well, not public yet :-)
600************************************************************************/
601PyObject *
602PyHKEY_New(HKEY hInit)
603{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000604 PyHKEYObject *key = PyObject_NEW(PyHKEYObject, &PyHKEY_Type);
605 if (key)
606 key->hkey = hInit;
607 return (PyObject *)key;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000608}
609
610BOOL
611PyHKEY_Close(PyObject *ob_handle)
612{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000613 LONG rc;
614 PyHKEYObject *key;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000615
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000616 if (!PyHKEY_Check(ob_handle)) {
617 PyErr_SetString(PyExc_TypeError, "bad operand type");
618 return FALSE;
619 }
620 key = (PyHKEYObject *)ob_handle;
621 rc = key->hkey ? RegCloseKey((HKEY)key->hkey) : ERROR_SUCCESS;
622 key->hkey = 0;
623 if (rc != ERROR_SUCCESS)
624 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
625 return rc == ERROR_SUCCESS;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000626}
627
628BOOL
629PyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK)
630{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000631 if (ob == Py_None) {
632 if (!bNoneOK) {
633 PyErr_SetString(
634 PyExc_TypeError,
635 "None is not a valid HKEY in this context");
636 return FALSE;
637 }
638 *pHANDLE = (HKEY)0;
639 }
640 else if (PyHKEY_Check(ob)) {
641 PyHKEYObject *pH = (PyHKEYObject *)ob;
642 *pHANDLE = pH->hkey;
643 }
644 else if (PyInt_Check(ob) || PyLong_Check(ob)) {
645 /* We also support integers */
646 PyErr_Clear();
647 *pHANDLE = (HKEY)PyLong_AsVoidPtr(ob);
648 if (PyErr_Occurred())
649 return FALSE;
650 }
651 else {
652 PyErr_SetString(
653 PyExc_TypeError,
654 "The object is not a PyHKEY object");
655 return FALSE;
656 }
657 return TRUE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000658}
659
660PyObject *
661PyHKEY_FromHKEY(HKEY h)
662{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000663 PyHKEYObject *op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000664
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000665 /* Inline PyObject_New */
666 op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject));
667 if (op == NULL)
668 return PyErr_NoMemory();
669 PyObject_INIT(op, &PyHKEY_Type);
670 op->hkey = h;
671 return (PyObject *)op;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000672}
673
674
675/************************************************************************
676 The module methods
677************************************************************************/
678BOOL
679PyWinObject_CloseHKEY(PyObject *obHandle)
680{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000681 BOOL ok;
682 if (PyHKEY_Check(obHandle)) {
683 ok = PyHKEY_Close(obHandle);
684 }
Fred Drake25e17262000-06-30 17:48:51 +0000685#if SIZEOF_LONG >= SIZEOF_HKEY
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000686 else if (PyInt_Check(obHandle)) {
687 long rc = RegCloseKey((HKEY)PyInt_AsLong(obHandle));
688 ok = (rc == ERROR_SUCCESS);
689 if (!ok)
690 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
691 }
Fred Drake25e17262000-06-30 17:48:51 +0000692#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000693 else if (PyLong_Check(obHandle)) {
694 long rc = RegCloseKey((HKEY)PyLong_AsVoidPtr(obHandle));
695 ok = (rc == ERROR_SUCCESS);
696 if (!ok)
697 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
698 }
Fred Drake25e17262000-06-30 17:48:51 +0000699#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000700 else {
701 PyErr_SetString(
702 PyExc_TypeError,
703 "A handle must be a HKEY object or an integer");
704 return FALSE;
705 }
706 return ok;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000707}
708
709
710/*
711 Private Helper functions for the registry interfaces
712
713** Note that fixupMultiSZ and countString have both had changes
714** made to support "incorrect strings". The registry specification
715** calls for strings to be terminated with 2 null bytes. It seems
Mark Dickinson3e4caeb2009-02-21 20:27:01 +0000716** some commercial packages install strings which don't conform,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000717** causing this code to fail - however, "regedit" etc still work
Mark Dickinson3e4caeb2009-02-21 20:27:01 +0000718** with these strings (ie only we don't!).
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000719*/
720static void
721fixupMultiSZ(char **str, char *data, int len)
722{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000723 char *P;
724 int i;
725 char *Q;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000726
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000727 Q = data + len;
728 for (P = data, i = 0; P < Q && *P != '\0'; P++, i++) {
729 str[i] = P;
730 for(; *P != '\0'; P++)
731 ;
732 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000733}
734
735static int
736countStrings(char *data, int len)
737{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000738 int strings;
739 char *P;
740 char *Q = data + len;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000741
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000742 for (P = data, strings = 0; P < Q && *P != '\0'; P++, strings++)
743 for (; P < Q && *P != '\0'; P++)
744 ;
745 return strings;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000746}
747
748/* Convert PyObject into Registry data.
749 Allocates space as needed. */
750static BOOL
751Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
752{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000753 Py_ssize_t i,j;
754 switch (typ) {
755 case REG_DWORD:
756 if (value != Py_None && !PyInt_Check(value))
757 return FALSE;
758 *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1);
759 if (*retDataBuf==NULL){
760 PyErr_NoMemory();
761 return FALSE;
762 }
763 *retDataSize = sizeof(DWORD);
764 if (value == Py_None) {
765 DWORD zero = 0;
766 memcpy(*retDataBuf, &zero, sizeof(DWORD));
767 }
768 else
769 memcpy(*retDataBuf,
770 &PyInt_AS_LONG((PyIntObject *)value),
771 sizeof(DWORD));
772 break;
773 case REG_SZ:
774 case REG_EXPAND_SZ:
775 {
776 int need_decref = 0;
777 if (value == Py_None)
778 *retDataSize = 1;
779 else {
780 if (PyUnicode_Check(value)) {
781 value = PyUnicode_AsEncodedString(
782 value,
783 "mbcs",
784 NULL);
785 if (value==NULL)
786 return FALSE;
787 need_decref = 1;
788 }
789 if (!PyString_Check(value))
790 return FALSE;
791 *retDataSize = 1 + strlen(
792 PyString_AS_STRING(
793 (PyStringObject *)value));
794 }
795 *retDataBuf = (BYTE *)PyMem_NEW(DWORD, *retDataSize);
796 if (*retDataBuf==NULL){
797 PyErr_NoMemory();
798 return FALSE;
799 }
800 if (value == Py_None)
801 strcpy((char *)*retDataBuf, "");
802 else
803 strcpy((char *)*retDataBuf,
804 PyString_AS_STRING(
805 (PyStringObject *)value));
806 if (need_decref)
807 Py_DECREF(value);
808 break;
809 }
810 case REG_MULTI_SZ:
811 {
812 DWORD size = 0;
813 char *P;
814 PyObject **obs = NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000815
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000816 if (value == Py_None)
817 i = 0;
818 else {
819 if (!PyList_Check(value))
820 return FALSE;
821 i = PyList_Size(value);
822 }
823 obs = malloc(sizeof(PyObject *) * i);
824 memset(obs, 0, sizeof(PyObject *) * i);
825 for (j = 0; j < i; j++)
826 {
827 PyObject *t;
828 t = PyList_GET_ITEM(
829 (PyListObject *)value,j);
830 if (PyString_Check(t)) {
831 obs[j] = t;
832 Py_INCREF(t);
833 } else if (PyUnicode_Check(t)) {
834 obs[j] = PyUnicode_AsEncodedString(
835 t,
836 "mbcs",
837 NULL);
838 if (obs[j]==NULL)
839 goto reg_multi_fail;
840 } else
841 goto reg_multi_fail;
842 size += 1 + strlen(
843 PyString_AS_STRING(
844 (PyStringObject *)obs[j]));
845 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000846
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000847 *retDataSize = size + 1;
848 *retDataBuf = (BYTE *)PyMem_NEW(char,
849 *retDataSize);
850 if (*retDataBuf==NULL){
851 PyErr_NoMemory();
852 goto reg_multi_fail;
853 }
854 P = (char *)*retDataBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000855
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000856 for (j = 0; j < i; j++)
857 {
858 PyObject *t;
859 t = obs[j];
860 strcpy(P,
861 PyString_AS_STRING(
862 (PyStringObject *)t));
863 P += 1 + strlen(
864 PyString_AS_STRING(
865 (PyStringObject *)t));
866 Py_DECREF(obs[j]);
867 }
868 /* And doubly-terminate the list... */
869 *P = '\0';
870 free(obs);
871 break;
872 reg_multi_fail:
873 if (obs) {
874 for (j = 0; j < i; j++)
875 Py_XDECREF(obs[j]);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000876
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000877 free(obs);
878 }
879 return FALSE;
880 }
881 case REG_BINARY:
882 /* ALSO handle ALL unknown data types here. Even if we can't
883 support it natively, we should handle the bits. */
884 default:
885 if (value == Py_None)
886 *retDataSize = 0;
887 else {
888 void *src_buf;
889 PyBufferProcs *pb = value->ob_type->tp_as_buffer;
890 if (pb==NULL) {
891 PyErr_Format(PyExc_TypeError,
892 "Objects of type '%s' can not "
893 "be used as binary registry values",
894 value->ob_type->tp_name);
895 return FALSE;
896 }
897 *retDataSize = (*pb->bf_getreadbuffer)(value, 0, &src_buf);
898 *retDataBuf = (BYTE *)PyMem_NEW(char,
899 *retDataSize);
900 if (*retDataBuf==NULL){
901 PyErr_NoMemory();
902 return FALSE;
903 }
904 memcpy(*retDataBuf, src_buf, *retDataSize);
905 }
906 break;
907 }
908 return TRUE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000909}
910
911/* Convert Registry data into PyObject*/
912static PyObject *
913Reg2Py(char *retDataBuf, DWORD retDataSize, DWORD typ)
914{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000915 PyObject *obData;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000916
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000917 switch (typ) {
918 case REG_DWORD:
919 if (retDataSize == 0)
920 obData = Py_BuildValue("i", 0);
921 else
922 obData = Py_BuildValue("i",
923 *(int *)retDataBuf);
924 break;
925 case REG_SZ:
926 case REG_EXPAND_SZ:
927 /* retDataBuf may or may not have a trailing NULL in
928 the buffer. */
929 if (retDataSize && retDataBuf[retDataSize-1] == '\0')
930 --retDataSize;
931 if (retDataSize ==0)
932 retDataBuf = "";
933 obData = PyUnicode_DecodeMBCS(retDataBuf,
934 retDataSize,
935 NULL);
936 break;
937 case REG_MULTI_SZ:
938 if (retDataSize == 0)
939 obData = PyList_New(0);
940 else
941 {
942 int index = 0;
943 int s = countStrings(retDataBuf, retDataSize);
944 char **str = (char **)malloc(sizeof(char *)*s);
945 if (str == NULL)
946 return PyErr_NoMemory();
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000947
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000948 fixupMultiSZ(str, retDataBuf, retDataSize);
949 obData = PyList_New(s);
950 if (obData == NULL)
951 return NULL;
952 for (index = 0; index < s; index++)
953 {
954 size_t len = _mbstrlen(str[index]);
955 if (len > INT_MAX) {
956 PyErr_SetString(PyExc_OverflowError,
957 "registry string is too long for a Python string");
958 Py_DECREF(obData);
959 return NULL;
960 }
961 PyList_SetItem(obData,
962 index,
963 PyUnicode_DecodeMBCS(
964 (const char *)str[index],
965 (int)len,
966 NULL)
967 );
968 }
969 free(str);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000970
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000971 break;
972 }
973 case REG_BINARY:
974 /* ALSO handle ALL unknown data types here. Even if we can't
975 support it natively, we should handle the bits. */
976 default:
977 if (retDataSize == 0) {
978 Py_INCREF(Py_None);
979 obData = Py_None;
980 }
981 else
982 obData = Py_BuildValue("s#",
983 (char *)retDataBuf,
984 retDataSize);
985 break;
986 }
987 if (obData == NULL)
988 return NULL;
989 else
990 return obData;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000991}
992
993/* The Python methods */
994
995static PyObject *
996PyCloseKey(PyObject *self, PyObject *args)
997{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000998 PyObject *obKey;
999 if (!PyArg_ParseTuple(args, "O:CloseKey", &obKey))
1000 return NULL;
1001 if (!PyHKEY_Close(obKey))
1002 return NULL;
1003 Py_INCREF(Py_None);
1004 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001005}
1006
1007static PyObject *
1008PyConnectRegistry(PyObject *self, PyObject *args)
1009{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001010 HKEY hKey;
1011 PyObject *obKey;
1012 char *szCompName = NULL;
1013 HKEY retKey;
1014 long rc;
1015 if (!PyArg_ParseTuple(args, "zO:ConnectRegistry", &szCompName, &obKey))
1016 return NULL;
1017 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1018 return NULL;
1019 Py_BEGIN_ALLOW_THREADS
1020 rc = RegConnectRegistry(szCompName, hKey, &retKey);
1021 Py_END_ALLOW_THREADS
1022 if (rc != ERROR_SUCCESS)
1023 return PyErr_SetFromWindowsErrWithFunction(rc,
1024 "ConnectRegistry");
1025 return PyHKEY_FromHKEY(retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001026}
1027
1028static PyObject *
1029PyCreateKey(PyObject *self, PyObject *args)
1030{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001031 HKEY hKey;
1032 PyObject *obKey;
1033 char *subKey;
1034 HKEY retKey;
1035 long rc;
1036 if (!PyArg_ParseTuple(args, "Oz:CreateKey", &obKey, &subKey))
1037 return NULL;
1038 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1039 return NULL;
1040 rc = RegCreateKey(hKey, subKey, &retKey);
1041 if (rc != ERROR_SUCCESS)
1042 return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey");
1043 return PyHKEY_FromHKEY(retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001044}
1045
1046static PyObject *
Brian Curtine33fa882010-04-02 21:18:14 +00001047PyCreateKeyEx(PyObject *self, PyObject *args)
1048{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001049 HKEY hKey;
1050 PyObject *obKey;
1051 char *subKey;
1052 HKEY retKey;
1053 int res = 0;
1054 REGSAM sam = KEY_WRITE;
1055 long rc;
1056 if (!PyArg_ParseTuple(args, "Oz|ii:CreateKeyEx", &obKey, &subKey,
1057 &res, &sam))
1058 return NULL;
1059 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1060 return NULL;
Brian Curtine33fa882010-04-02 21:18:14 +00001061
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001062 rc = RegCreateKeyEx(hKey, subKey, res, NULL, (DWORD)NULL,
1063 sam, NULL, &retKey, NULL);
1064 if (rc != ERROR_SUCCESS)
1065 return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKeyEx");
1066 return PyHKEY_FromHKEY(retKey);
Brian Curtine33fa882010-04-02 21:18:14 +00001067}
1068
1069static PyObject *
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001070PyDeleteKey(PyObject *self, PyObject *args)
1071{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001072 HKEY hKey;
1073 PyObject *obKey;
1074 char *subKey;
1075 long rc;
1076 if (!PyArg_ParseTuple(args, "Os:DeleteKey", &obKey, &subKey))
1077 return NULL;
1078 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1079 return NULL;
1080 rc = RegDeleteKey(hKey, subKey );
1081 if (rc != ERROR_SUCCESS)
1082 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey");
1083 Py_INCREF(Py_None);
1084 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001085}
1086
1087static PyObject *
Brian Curtine33fa882010-04-02 21:18:14 +00001088PyDeleteKeyEx(PyObject *self, PyObject *args)
1089{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001090 HKEY hKey;
1091 PyObject *obKey;
1092 HMODULE hMod;
1093 typedef LONG (WINAPI *RDKEFunc)(HKEY, const char*, REGSAM, int);
1094 RDKEFunc pfn = NULL;
1095 char *subKey;
1096 long rc;
1097 int res = 0;
1098 REGSAM sam = KEY_WOW64_64KEY;
Brian Curtine33fa882010-04-02 21:18:14 +00001099
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001100 if (!PyArg_ParseTuple(args, "Os|ii:DeleteKeyEx",
1101 &obKey, &subKey, &sam, &res))
1102 return NULL;
1103 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1104 return NULL;
Brian Curtine33fa882010-04-02 21:18:14 +00001105
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001106 /* Only available on 64bit platforms, so we must load it
1107 dynamically. */
1108 hMod = GetModuleHandle("advapi32.dll");
1109 if (hMod)
1110 pfn = (RDKEFunc)GetProcAddress(hMod,
1111 "RegDeleteKeyExA");
1112 if (!pfn) {
1113 PyErr_SetString(PyExc_NotImplementedError,
1114 "not implemented on this platform");
1115 return NULL;
1116 }
1117 Py_BEGIN_ALLOW_THREADS
1118 rc = (*pfn)(hKey, subKey, sam, res);
1119 Py_END_ALLOW_THREADS
Brian Curtine33fa882010-04-02 21:18:14 +00001120
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001121 if (rc != ERROR_SUCCESS)
1122 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKeyEx");
1123 Py_INCREF(Py_None);
1124 return Py_None;
Brian Curtine33fa882010-04-02 21:18:14 +00001125}
1126
1127static PyObject *
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001128PyDeleteValue(PyObject *self, PyObject *args)
1129{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001130 HKEY hKey;
1131 PyObject *obKey;
1132 char *subKey;
1133 long rc;
1134 if (!PyArg_ParseTuple(args, "Oz:DeleteValue", &obKey, &subKey))
1135 return NULL;
1136 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1137 return NULL;
1138 Py_BEGIN_ALLOW_THREADS
1139 rc = RegDeleteValue(hKey, subKey);
1140 Py_END_ALLOW_THREADS
1141 if (rc !=ERROR_SUCCESS)
1142 return PyErr_SetFromWindowsErrWithFunction(rc,
1143 "RegDeleteValue");
1144 Py_INCREF(Py_None);
1145 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001146}
1147
1148static PyObject *
1149PyEnumKey(PyObject *self, PyObject *args)
1150{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001151 HKEY hKey;
1152 PyObject *obKey;
1153 int index;
1154 long rc;
1155 PyObject *retStr;
Brian Curtinb64c89b2010-05-25 15:06:15 +00001156
1157 /* The Windows docs claim that the max key name length is 255
1158 * characters, plus a terminating nul character. However,
1159 * empirical testing demonstrates that it is possible to
1160 * create a 256 character key that is missing the terminating
1161 * nul. RegEnumKeyEx requires a 257 character buffer to
1162 * retrieve such a key name. */
1163 char tmpbuf[257];
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001164 DWORD len = sizeof(tmpbuf); /* includes NULL terminator */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001165
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001166 if (!PyArg_ParseTuple(args, "Oi:EnumKey", &obKey, &index))
1167 return NULL;
1168 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1169 return NULL;
Tim Peters313fcd42006-02-19 04:05:39 +00001170
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001171 Py_BEGIN_ALLOW_THREADS
1172 rc = RegEnumKeyEx(hKey, index, tmpbuf, &len, NULL, NULL, NULL, NULL);
1173 Py_END_ALLOW_THREADS
1174 if (rc != ERROR_SUCCESS)
1175 return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx");
Tim Peters313fcd42006-02-19 04:05:39 +00001176
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001177 retStr = PyString_FromStringAndSize(tmpbuf, len);
1178 return retStr; /* can be NULL */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001179}
1180
1181static PyObject *
1182PyEnumValue(PyObject *self, PyObject *args)
1183{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001184 HKEY hKey;
1185 PyObject *obKey;
1186 int index;
1187 long rc;
1188 char *retValueBuf;
1189 char *retDataBuf;
Brian Curtinb64c89b2010-05-25 15:06:15 +00001190 DWORD retValueSize, bufValueSize;
1191 DWORD retDataSize, bufDataSize;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001192 DWORD typ;
1193 PyObject *obData;
1194 PyObject *retVal;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001195
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001196 if (!PyArg_ParseTuple(args, "Oi:EnumValue", &obKey, &index))
1197 return NULL;
1198 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1199 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001200
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001201 if ((rc = RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, NULL, NULL,
1202 NULL,
1203 &retValueSize, &retDataSize, NULL, NULL))
1204 != ERROR_SUCCESS)
1205 return PyErr_SetFromWindowsErrWithFunction(rc,
1206 "RegQueryInfoKey");
1207 ++retValueSize; /* include null terminators */
1208 ++retDataSize;
Brian Curtinb64c89b2010-05-25 15:06:15 +00001209 bufDataSize = retDataSize;
1210 bufValueSize = retValueSize;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001211 retValueBuf = (char *)PyMem_Malloc(retValueSize);
1212 if (retValueBuf == NULL)
1213 return PyErr_NoMemory();
1214 retDataBuf = (char *)PyMem_Malloc(retDataSize);
1215 if (retDataBuf == NULL) {
1216 PyMem_Free(retValueBuf);
1217 return PyErr_NoMemory();
1218 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001219
Brian Curtinb64c89b2010-05-25 15:06:15 +00001220 while (1) {
1221 char *tmp;
1222 Py_BEGIN_ALLOW_THREADS
1223 rc = RegEnumValue(hKey,
1224 index,
1225 retValueBuf,
1226 &retValueSize,
1227 NULL,
1228 &typ,
1229 (BYTE *)retDataBuf,
1230 &retDataSize);
1231 Py_END_ALLOW_THREADS
1232
1233 if (rc != ERROR_MORE_DATA)
1234 break;
1235
1236 bufDataSize *= 2;
1237 tmp = (char *)PyMem_Realloc(retDataBuf, bufDataSize);
1238 if (tmp == NULL) {
1239 PyErr_NoMemory();
1240 retVal = NULL;
1241 goto fail;
1242 }
1243 retDataBuf = tmp;
1244 retDataSize = bufDataSize;
1245 retValueSize = bufValueSize;
1246 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001247
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001248 if (rc != ERROR_SUCCESS) {
1249 retVal = PyErr_SetFromWindowsErrWithFunction(rc,
1250 "PyRegEnumValue");
1251 goto fail;
1252 }
1253 obData = Reg2Py(retDataBuf, retDataSize, typ);
1254 if (obData == NULL) {
1255 retVal = NULL;
1256 goto fail;
1257 }
1258 retVal = Py_BuildValue("sOi", retValueBuf, obData, typ);
1259 Py_DECREF(obData);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001260 fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001261 PyMem_Free(retValueBuf);
1262 PyMem_Free(retDataBuf);
1263 return retVal;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001264}
1265
1266static PyObject *
Christian Heimesb39a7562008-01-08 15:46:10 +00001267PyExpandEnvironmentStrings(PyObject *self, PyObject *args)
1268{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001269 Py_UNICODE *retValue = NULL;
1270 Py_UNICODE *src;
1271 DWORD retValueSize;
1272 DWORD rc;
1273 PyObject *o;
Christian Heimesb39a7562008-01-08 15:46:10 +00001274
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001275 if (!PyArg_ParseTuple(args, "u:ExpandEnvironmentStrings", &src))
1276 return NULL;
Christian Heimesb39a7562008-01-08 15:46:10 +00001277
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001278 retValueSize = ExpandEnvironmentStringsW(src, retValue, 0);
1279 if (retValueSize == 0) {
1280 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1281 "ExpandEnvironmentStrings");
1282 }
1283 retValue = (Py_UNICODE *)PyMem_Malloc(retValueSize * sizeof(Py_UNICODE));
1284 if (retValue == NULL) {
1285 return PyErr_NoMemory();
1286 }
Christian Heimesb39a7562008-01-08 15:46:10 +00001287
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001288 rc = ExpandEnvironmentStringsW(src, retValue, retValueSize);
1289 if (rc == 0) {
1290 PyMem_Free(retValue);
1291 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1292 "ExpandEnvironmentStrings");
1293 }
1294 o = PyUnicode_FromUnicode(retValue, wcslen(retValue));
1295 PyMem_Free(retValue);
1296 return o;
Christian Heimesb39a7562008-01-08 15:46:10 +00001297}
1298
1299static PyObject *
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001300PyFlushKey(PyObject *self, PyObject *args)
1301{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001302 HKEY hKey;
1303 PyObject *obKey;
1304 long rc;
1305 if (!PyArg_ParseTuple(args, "O:FlushKey", &obKey))
1306 return NULL;
1307 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1308 return NULL;
1309 Py_BEGIN_ALLOW_THREADS
1310 rc = RegFlushKey(hKey);
1311 Py_END_ALLOW_THREADS
1312 if (rc != ERROR_SUCCESS)
1313 return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey");
1314 Py_INCREF(Py_None);
1315 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001316}
1317static PyObject *
1318PyLoadKey(PyObject *self, PyObject *args)
1319{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001320 HKEY hKey;
1321 PyObject *obKey;
1322 char *subKey;
1323 char *fileName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001324
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001325 long rc;
1326 if (!PyArg_ParseTuple(args, "Oss:LoadKey", &obKey, &subKey, &fileName))
1327 return NULL;
1328 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1329 return NULL;
1330 Py_BEGIN_ALLOW_THREADS
1331 rc = RegLoadKey(hKey, subKey, fileName );
1332 Py_END_ALLOW_THREADS
1333 if (rc != ERROR_SUCCESS)
1334 return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey");
1335 Py_INCREF(Py_None);
1336 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001337}
1338
1339static PyObject *
1340PyOpenKey(PyObject *self, PyObject *args)
1341{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001342 HKEY hKey;
1343 PyObject *obKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001344
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001345 char *subKey;
1346 int res = 0;
1347 HKEY retKey;
1348 long rc;
1349 REGSAM sam = KEY_READ;
1350 if (!PyArg_ParseTuple(args, "Oz|ii:OpenKey", &obKey, &subKey,
1351 &res, &sam))
1352 return NULL;
1353 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1354 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001355
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001356 Py_BEGIN_ALLOW_THREADS
1357 rc = RegOpenKeyEx(hKey, subKey, res, sam, &retKey);
1358 Py_END_ALLOW_THREADS
1359 if (rc != ERROR_SUCCESS)
1360 return PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx");
1361 return PyHKEY_FromHKEY(retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001362}
1363
1364
1365static PyObject *
1366PyQueryInfoKey(PyObject *self, PyObject *args)
1367{
1368 HKEY hKey;
1369 PyObject *obKey;
1370 long rc;
1371 DWORD nSubKeys, nValues;
1372 FILETIME ft;
1373 LARGE_INTEGER li;
1374 PyObject *l;
1375 PyObject *ret;
1376 if (!PyArg_ParseTuple(args, "O:QueryInfoKey", &obKey))
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001377 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001378 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001379 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001380 if ((rc = RegQueryInfoKey(hKey, NULL, NULL, 0, &nSubKeys, NULL, NULL,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001381 &nValues, NULL, NULL, NULL, &ft))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001382 != ERROR_SUCCESS)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001383 return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey");
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001384 li.LowPart = ft.dwLowDateTime;
1385 li.HighPart = ft.dwHighDateTime;
1386 l = PyLong_FromLongLong(li.QuadPart);
1387 if (l == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001388 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001389 ret = Py_BuildValue("iiO", nSubKeys, nValues, l);
1390 Py_DECREF(l);
1391 return ret;
1392}
1393
1394static PyObject *
1395PyQueryValue(PyObject *self, PyObject *args)
1396{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001397 HKEY hKey;
1398 PyObject *obKey;
1399 char *subKey;
1400 long rc;
1401 PyObject *retStr;
1402 char *retBuf;
Brian Curtinb64c89b2010-05-25 15:06:15 +00001403 DWORD bufSize = 0;
1404 DWORD retSize = 0;
1405 char *tmp;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001406
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001407 if (!PyArg_ParseTuple(args, "Oz:QueryValue", &obKey, &subKey))
1408 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001409
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001410 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1411 return NULL;
Brian Curtinb64c89b2010-05-25 15:06:15 +00001412
1413 rc = RegQueryValue(hKey, subKey, NULL, &retSize);
1414 if (rc == ERROR_MORE_DATA)
1415 retSize = 256;
1416 else if (rc != ERROR_SUCCESS)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001417 return PyErr_SetFromWindowsErrWithFunction(rc,
1418 "RegQueryValue");
Brian Curtinb64c89b2010-05-25 15:06:15 +00001419
1420 bufSize = retSize;
1421 retBuf = (char *) PyMem_Malloc(bufSize);
1422 if (retBuf == NULL)
1423 return PyErr_NoMemory();
1424
1425 while (1) {
1426 retSize = bufSize;
1427 rc = RegQueryValue(hKey, subKey, retBuf, &retSize);
1428 if (rc != ERROR_MORE_DATA)
1429 break;
1430
1431 bufSize *= 2;
1432 tmp = (char *) PyMem_Realloc(retBuf, bufSize);
1433 if (tmp == NULL) {
1434 PyMem_Free(retBuf);
1435 return PyErr_NoMemory();
1436 }
1437 retBuf = tmp;
1438 }
1439
1440 if (rc != ERROR_SUCCESS) {
1441 PyMem_Free(retBuf);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001442 return PyErr_SetFromWindowsErrWithFunction(rc,
1443 "RegQueryValue");
1444 }
Brian Curtinb64c89b2010-05-25 15:06:15 +00001445
1446 if (retBuf[retSize-1] == '\x00')
1447 retSize--;
1448 retStr = PyString_FromStringAndSize(retBuf, retSize);
1449 if (retStr == NULL) {
1450 PyMem_Free(retBuf);
1451 return NULL;
1452 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001453 return retStr;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001454}
1455
1456static PyObject *
1457PyQueryValueEx(PyObject *self, PyObject *args)
1458{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001459 HKEY hKey;
1460 PyObject *obKey;
1461 char *valueName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001462
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001463 long rc;
Brian Curtinb64c89b2010-05-25 15:06:15 +00001464 char *retBuf, *tmp;
1465 DWORD bufSize = 0, retSize;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001466 DWORD typ;
1467 PyObject *obData;
1468 PyObject *result;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001469
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001470 if (!PyArg_ParseTuple(args, "Oz:QueryValueEx", &obKey, &valueName))
1471 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001472
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001473 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1474 return NULL;
Brian Curtinb64c89b2010-05-25 15:06:15 +00001475
1476 rc = RegQueryValueEx(hKey, valueName, NULL, NULL, NULL, &bufSize);
1477 if (rc == ERROR_MORE_DATA)
1478 bufSize = 256;
1479 else if (rc != ERROR_SUCCESS)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001480 return PyErr_SetFromWindowsErrWithFunction(rc,
1481 "RegQueryValueEx");
1482 retBuf = (char *)PyMem_Malloc(bufSize);
1483 if (retBuf == NULL)
1484 return PyErr_NoMemory();
Brian Curtinb64c89b2010-05-25 15:06:15 +00001485
1486 while (1) {
1487 retSize = bufSize;
1488 rc = RegQueryValueEx(hKey, valueName, NULL, &typ,
1489 (BYTE *)retBuf, &retSize);
1490 if (rc != ERROR_MORE_DATA)
1491 break;
1492
1493 bufSize *= 2;
1494 tmp = (char *) PyMem_Realloc(retBuf, bufSize);
1495 if (tmp == NULL) {
1496 PyMem_Free(retBuf);
1497 return PyErr_NoMemory();
1498 }
1499 retBuf = tmp;
1500 }
1501
1502 if (rc != ERROR_SUCCESS) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001503 PyMem_Free(retBuf);
1504 return PyErr_SetFromWindowsErrWithFunction(rc,
1505 "RegQueryValueEx");
1506 }
1507 obData = Reg2Py(retBuf, bufSize, typ);
1508 PyMem_Free((void *)retBuf);
1509 if (obData == NULL)
1510 return NULL;
1511 result = Py_BuildValue("Oi", obData, typ);
1512 Py_DECREF(obData);
1513 return result;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001514}
1515
1516
1517static PyObject *
1518PySaveKey(PyObject *self, PyObject *args)
1519{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001520 HKEY hKey;
1521 PyObject *obKey;
1522 char *fileName;
1523 LPSECURITY_ATTRIBUTES pSA = NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001524
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001525 long rc;
1526 if (!PyArg_ParseTuple(args, "Os:SaveKey", &obKey, &fileName))
1527 return NULL;
1528 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1529 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001530/* One day we may get security into the core?
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001531 if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE))
1532 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001533*/
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001534 Py_BEGIN_ALLOW_THREADS
1535 rc = RegSaveKey(hKey, fileName, pSA );
1536 Py_END_ALLOW_THREADS
1537 if (rc != ERROR_SUCCESS)
1538 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey");
1539 Py_INCREF(Py_None);
1540 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001541}
1542
1543static PyObject *
1544PySetValue(PyObject *self, PyObject *args)
1545{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001546 HKEY hKey;
1547 PyObject *obKey;
1548 char *subKey;
1549 char *str;
1550 DWORD typ;
1551 DWORD len;
1552 long rc;
1553 PyObject *obStrVal;
1554 PyObject *obSubKey;
1555 if (!PyArg_ParseTuple(args, "OOiO:SetValue",
1556 &obKey,
1557 &obSubKey,
1558 &typ,
1559 &obStrVal))
1560 return NULL;
1561 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1562 return NULL;
1563 if (typ != REG_SZ) {
1564 PyErr_SetString(PyExc_TypeError,
1565 "Type must be _winreg.REG_SZ");
1566 return NULL;
1567 }
1568 /* XXX - need Unicode support */
1569 str = PyString_AsString(obStrVal);
1570 if (str == NULL)
1571 return NULL;
1572 len = PyString_Size(obStrVal);
1573 if (obSubKey == Py_None)
1574 subKey = NULL;
1575 else {
1576 subKey = PyString_AsString(obSubKey);
1577 if (subKey == NULL)
1578 return NULL;
1579 }
1580 Py_BEGIN_ALLOW_THREADS
1581 rc = RegSetValue(hKey, subKey, REG_SZ, str, len+1);
1582 Py_END_ALLOW_THREADS
1583 if (rc != ERROR_SUCCESS)
1584 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue");
1585 Py_INCREF(Py_None);
1586 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001587}
1588
1589static PyObject *
1590PySetValueEx(PyObject *self, PyObject *args)
1591{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001592 HKEY hKey;
1593 PyObject *obKey;
1594 char *valueName;
1595 PyObject *obRes;
1596 PyObject *value;
1597 BYTE *data;
1598 DWORD len;
1599 DWORD typ;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001600
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001601 LONG rc;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001602
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001603 if (!PyArg_ParseTuple(args, "OzOiO:SetValueEx",
1604 &obKey,
1605 &valueName,
1606 &obRes,
1607 &typ,
1608 &value))
1609 return NULL;
1610 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1611 return NULL;
1612 if (!Py2Reg(value, typ, &data, &len))
1613 {
1614 if (!PyErr_Occurred())
1615 PyErr_SetString(PyExc_ValueError,
1616 "Could not convert the data to the specified type.");
1617 return NULL;
1618 }
1619 Py_BEGIN_ALLOW_THREADS
1620 rc = RegSetValueEx(hKey, valueName, 0, typ, data, len);
1621 Py_END_ALLOW_THREADS
1622 PyMem_DEL(data);
1623 if (rc != ERROR_SUCCESS)
1624 return PyErr_SetFromWindowsErrWithFunction(rc,
1625 "RegSetValueEx");
1626 Py_INCREF(Py_None);
1627 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001628}
1629
Mark Hammond8a3c8712008-04-06 01:42:06 +00001630static PyObject *
1631PyDisableReflectionKey(PyObject *self, PyObject *args)
1632{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001633 HKEY hKey;
1634 PyObject *obKey;
1635 HMODULE hMod;
1636 typedef LONG (WINAPI *RDRKFunc)(HKEY);
1637 RDRKFunc pfn = NULL;
1638 LONG rc;
Mark Hammond8a3c8712008-04-06 01:42:06 +00001639
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001640 if (!PyArg_ParseTuple(args, "O:DisableReflectionKey", &obKey))
1641 return NULL;
1642 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1643 return NULL;
Mark Hammond8a3c8712008-04-06 01:42:06 +00001644
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001645 /* Only available on 64bit platforms, so we must load it
1646 dynamically. */
1647 hMod = GetModuleHandle("advapi32.dll");
1648 if (hMod)
1649 pfn = (RDRKFunc)GetProcAddress(hMod,
1650 "RegDisableReflectionKey");
1651 if (!pfn) {
1652 PyErr_SetString(PyExc_NotImplementedError,
1653 "not implemented on this platform");
1654 return NULL;
1655 }
1656 Py_BEGIN_ALLOW_THREADS
1657 rc = (*pfn)(hKey);
1658 Py_END_ALLOW_THREADS
1659 if (rc != ERROR_SUCCESS)
1660 return PyErr_SetFromWindowsErrWithFunction(rc,
1661 "RegDisableReflectionKey");
1662 Py_INCREF(Py_None);
1663 return Py_None;
Mark Hammond8a3c8712008-04-06 01:42:06 +00001664}
1665
1666static PyObject *
1667PyEnableReflectionKey(PyObject *self, PyObject *args)
1668{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001669 HKEY hKey;
1670 PyObject *obKey;
1671 HMODULE hMod;
1672 typedef LONG (WINAPI *RERKFunc)(HKEY);
1673 RERKFunc pfn = NULL;
1674 LONG rc;
Mark Hammond8a3c8712008-04-06 01:42:06 +00001675
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001676 if (!PyArg_ParseTuple(args, "O:EnableReflectionKey", &obKey))
1677 return NULL;
1678 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1679 return NULL;
Mark Hammond8a3c8712008-04-06 01:42:06 +00001680
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001681 /* Only available on 64bit platforms, so we must load it
1682 dynamically. */
1683 hMod = GetModuleHandle("advapi32.dll");
1684 if (hMod)
1685 pfn = (RERKFunc)GetProcAddress(hMod,
1686 "RegEnableReflectionKey");
1687 if (!pfn) {
1688 PyErr_SetString(PyExc_NotImplementedError,
1689 "not implemented on this platform");
1690 return NULL;
1691 }
1692 Py_BEGIN_ALLOW_THREADS
1693 rc = (*pfn)(hKey);
1694 Py_END_ALLOW_THREADS
1695 if (rc != ERROR_SUCCESS)
1696 return PyErr_SetFromWindowsErrWithFunction(rc,
1697 "RegEnableReflectionKey");
1698 Py_INCREF(Py_None);
1699 return Py_None;
Mark Hammond8a3c8712008-04-06 01:42:06 +00001700}
1701
1702static PyObject *
1703PyQueryReflectionKey(PyObject *self, PyObject *args)
1704{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001705 HKEY hKey;
1706 PyObject *obKey;
1707 HMODULE hMod;
1708 typedef LONG (WINAPI *RQRKFunc)(HKEY, BOOL *);
1709 RQRKFunc pfn = NULL;
1710 BOOL result;
1711 LONG rc;
Mark Hammond8a3c8712008-04-06 01:42:06 +00001712
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001713 if (!PyArg_ParseTuple(args, "O:QueryReflectionKey", &obKey))
1714 return NULL;
1715 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1716 return NULL;
Mark Hammond8a3c8712008-04-06 01:42:06 +00001717
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001718 /* Only available on 64bit platforms, so we must load it
1719 dynamically. */
1720 hMod = GetModuleHandle("advapi32.dll");
1721 if (hMod)
1722 pfn = (RQRKFunc)GetProcAddress(hMod,
1723 "RegQueryReflectionKey");
1724 if (!pfn) {
1725 PyErr_SetString(PyExc_NotImplementedError,
1726 "not implemented on this platform");
1727 return NULL;
1728 }
1729 Py_BEGIN_ALLOW_THREADS
1730 rc = (*pfn)(hKey, &result);
1731 Py_END_ALLOW_THREADS
1732 if (rc != ERROR_SUCCESS)
1733 return PyErr_SetFromWindowsErrWithFunction(rc,
1734 "RegQueryReflectionKey");
1735 return PyBool_FromLong(result);
Mark Hammond8a3c8712008-04-06 01:42:06 +00001736}
1737
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001738static struct PyMethodDef winreg_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001739 {"CloseKey", PyCloseKey, METH_VARARGS, CloseKey_doc},
1740 {"ConnectRegistry", PyConnectRegistry, METH_VARARGS, ConnectRegistry_doc},
1741 {"CreateKey", PyCreateKey, METH_VARARGS, CreateKey_doc},
1742 {"CreateKeyEx", PyCreateKeyEx, METH_VARARGS, CreateKeyEx_doc},
1743 {"DeleteKey", PyDeleteKey, METH_VARARGS, DeleteKey_doc},
1744 {"DeleteKeyEx", PyDeleteKeyEx, METH_VARARGS, DeleteKeyEx_doc},
1745 {"DeleteValue", PyDeleteValue, METH_VARARGS, DeleteValue_doc},
1746 {"DisableReflectionKey", PyDisableReflectionKey, METH_VARARGS, DisableReflectionKey_doc},
1747 {"EnableReflectionKey", PyEnableReflectionKey, METH_VARARGS, EnableReflectionKey_doc},
1748 {"EnumKey", PyEnumKey, METH_VARARGS, EnumKey_doc},
1749 {"EnumValue", PyEnumValue, METH_VARARGS, EnumValue_doc},
1750 {"ExpandEnvironmentStrings", PyExpandEnvironmentStrings, METH_VARARGS,
1751 ExpandEnvironmentStrings_doc },
1752 {"FlushKey", PyFlushKey, METH_VARARGS, FlushKey_doc},
1753 {"LoadKey", PyLoadKey, METH_VARARGS, LoadKey_doc},
1754 {"OpenKey", PyOpenKey, METH_VARARGS, OpenKey_doc},
1755 {"OpenKeyEx", PyOpenKey, METH_VARARGS, OpenKeyEx_doc},
1756 {"QueryValue", PyQueryValue, METH_VARARGS, QueryValue_doc},
1757 {"QueryValueEx", PyQueryValueEx, METH_VARARGS, QueryValueEx_doc},
1758 {"QueryInfoKey", PyQueryInfoKey, METH_VARARGS, QueryInfoKey_doc},
1759 {"QueryReflectionKey",PyQueryReflectionKey,METH_VARARGS, QueryReflectionKey_doc},
1760 {"SaveKey", PySaveKey, METH_VARARGS, SaveKey_doc},
1761 {"SetValue", PySetValue, METH_VARARGS, SetValue_doc},
1762 {"SetValueEx", PySetValueEx, METH_VARARGS, SetValueEx_doc},
1763 NULL,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001764};
1765
1766static void
1767insint(PyObject * d, char * name, long value)
1768{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001769 PyObject *v = PyInt_FromLong(value);
1770 if (!v || PyDict_SetItemString(d, name, v))
1771 PyErr_Clear();
1772 Py_XDECREF(v);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001773}
1774
1775#define ADD_INT(val) insint(d, #val, val)
1776
1777static void
1778inskey(PyObject * d, char * name, HKEY key)
1779{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001780 PyObject *v = PyLong_FromVoidPtr(key);
1781 if (!v || PyDict_SetItemString(d, name, v))
1782 PyErr_Clear();
1783 Py_XDECREF(v);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001784}
1785
1786#define ADD_KEY(val) inskey(d, #val, val)
1787
Mark Hammond8235ea12002-07-19 06:55:41 +00001788PyMODINIT_FUNC init_winreg(void)
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001789{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001790 PyObject *m, *d;
1791 m = Py_InitModule3("_winreg", winreg_methods, module_doc);
1792 if (m == NULL)
1793 return;
1794 d = PyModule_GetDict(m);
1795 if (PyType_Ready(&PyHKEY_Type) < 0)
1796 return;
1797 PyHKEY_Type.tp_doc = PyHKEY_doc;
1798 Py_INCREF(&PyHKEY_Type);
1799 if (PyDict_SetItemString(d, "HKEYType",
1800 (PyObject *)&PyHKEY_Type) != 0)
1801 return;
1802 Py_INCREF(PyExc_WindowsError);
1803 if (PyDict_SetItemString(d, "error",
1804 PyExc_WindowsError) != 0)
1805 return;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001806
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001807 /* Add the relevant constants */
1808 ADD_KEY(HKEY_CLASSES_ROOT);
1809 ADD_KEY(HKEY_CURRENT_USER);
1810 ADD_KEY(HKEY_LOCAL_MACHINE);
1811 ADD_KEY(HKEY_USERS);
1812 ADD_KEY(HKEY_PERFORMANCE_DATA);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001813#ifdef HKEY_CURRENT_CONFIG
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001814 ADD_KEY(HKEY_CURRENT_CONFIG);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001815#endif
1816#ifdef HKEY_DYN_DATA
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001817 ADD_KEY(HKEY_DYN_DATA);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001818#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001819 ADD_INT(KEY_QUERY_VALUE);
1820 ADD_INT(KEY_SET_VALUE);
1821 ADD_INT(KEY_CREATE_SUB_KEY);
1822 ADD_INT(KEY_ENUMERATE_SUB_KEYS);
1823 ADD_INT(KEY_NOTIFY);
1824 ADD_INT(KEY_CREATE_LINK);
1825 ADD_INT(KEY_READ);
1826 ADD_INT(KEY_WRITE);
1827 ADD_INT(KEY_EXECUTE);
1828 ADD_INT(KEY_ALL_ACCESS);
Mark Hammond8a3c8712008-04-06 01:42:06 +00001829#ifdef KEY_WOW64_64KEY
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001830 ADD_INT(KEY_WOW64_64KEY);
Mark Hammond8a3c8712008-04-06 01:42:06 +00001831#endif
1832#ifdef KEY_WOW64_32KEY
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001833 ADD_INT(KEY_WOW64_32KEY);
Mark Hammond8a3c8712008-04-06 01:42:06 +00001834#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001835 ADD_INT(REG_OPTION_RESERVED);
1836 ADD_INT(REG_OPTION_NON_VOLATILE);
1837 ADD_INT(REG_OPTION_VOLATILE);
1838 ADD_INT(REG_OPTION_CREATE_LINK);
1839 ADD_INT(REG_OPTION_BACKUP_RESTORE);
1840 ADD_INT(REG_OPTION_OPEN_LINK);
1841 ADD_INT(REG_LEGAL_OPTION);
1842 ADD_INT(REG_CREATED_NEW_KEY);
1843 ADD_INT(REG_OPENED_EXISTING_KEY);
1844 ADD_INT(REG_WHOLE_HIVE_VOLATILE);
1845 ADD_INT(REG_REFRESH_HIVE);
1846 ADD_INT(REG_NO_LAZY_FLUSH);
1847 ADD_INT(REG_NOTIFY_CHANGE_NAME);
1848 ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES);
1849 ADD_INT(REG_NOTIFY_CHANGE_LAST_SET);
1850 ADD_INT(REG_NOTIFY_CHANGE_SECURITY);
1851 ADD_INT(REG_LEGAL_CHANGE_FILTER);
1852 ADD_INT(REG_NONE);
1853 ADD_INT(REG_SZ);
1854 ADD_INT(REG_EXPAND_SZ);
1855 ADD_INT(REG_BINARY);
1856 ADD_INT(REG_DWORD);
1857 ADD_INT(REG_DWORD_LITTLE_ENDIAN);
1858 ADD_INT(REG_DWORD_BIG_ENDIAN);
1859 ADD_INT(REG_LINK);
1860 ADD_INT(REG_MULTI_SZ);
1861 ADD_INT(REG_RESOURCE_LIST);
1862 ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
1863 ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001864}
1865