blob: 1bc47b958b4367090b8bca1c5a1521fc781a4f6f [file] [log] [blame]
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001/*
Georg Brandl38feaf02008-05-25 07:45:51 +00002 winreg.c
Guido van Rossum9f3712c2000-03-28 20:37:15 +00003
4 Windows Registry access module for Python.
5
6 * Simple registry access written by Mark Hammond in win32api
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007 module circa 1995.
Guido van Rossum9f3712c2000-03-28 20:37:15 +00008 * Bill Tutt expanded the support significantly not long after.
9 * Numerous other people have submitted patches since then.
10 * Ripped from win32api module 03-Feb-2000 by Mark Hammond, and
11 basic Unicode support added.
12
13*/
14
Guido van Rossum9f3712c2000-03-28 20:37:15 +000015#include "Python.h"
16#include "structmember.h"
Guido van Rossume7ba4952007-06-06 23:52:48 +000017#include "windows.h"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000018
19static BOOL PyHKEY_AsHKEY(PyObject *ob, HKEY *pRes, BOOL bNoneOK);
20static PyObject *PyHKEY_FromHKEY(HKEY h);
21static BOOL PyHKEY_Close(PyObject *obHandle);
22
23static char errNotAHandle[] = "Object is not a handle";
24
25/* The win32api module reports the function name that failed,
26 but this concept is not in the Python core.
27 Hopefully it will one day, and in the meantime I dont
28 want to lose this info...
29*/
30#define PyErr_SetFromWindowsErrWithFunction(rc, fnname) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031 PyErr_SetFromWindowsErr(rc)
Guido van Rossum9f3712c2000-03-28 20:37:15 +000032
33/* Forward declares */
34
35/* Doc strings */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000036PyDoc_STRVAR(module_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +000037"This module provides access to the Windows registry API.\n"
38"\n"
39"Functions:\n"
40"\n"
41"CloseKey() - Closes a registry key.\n"
42"ConnectRegistry() - Establishes a connection to a predefined registry handle\n"
43" on another computer.\n"
44"CreateKey() - Creates the specified key, or opens it if it already exists.\n"
45"DeleteKey() - Deletes the specified key.\n"
46"DeleteValue() - Removes a named value from the specified registry key.\n"
47"EnumKey() - Enumerates subkeys of the specified open registry key.\n"
48"EnumValue() - Enumerates values of the specified open registry key.\n"
Christian Heimes2380ac72008-01-09 00:17:24 +000049"ExpandEnvironmentStrings() - Expand the env strings in a REG_EXPAND_SZ string.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000050"FlushKey() - Writes all the attributes of the specified key to the registry.\n"
51"LoadKey() - Creates a subkey under HKEY_USER or HKEY_LOCAL_MACHINE and stores\n"
52" registration information from a specified file into that subkey.\n"
53"OpenKey() - Alias for <om win32api.RegOpenKeyEx>\n"
54"OpenKeyEx() - Opens the specified key.\n"
55"QueryValue() - Retrieves the value associated with the unnamed value for a\n"
56" specified key in the registry.\n"
57"QueryValueEx() - Retrieves the type and data for a specified value name\n"
58" associated with an open registry key.\n"
59"QueryInfoKey() - Returns information about the specified key.\n"
60"SaveKey() - Saves the specified key, and all its subkeys a file.\n"
61"SetValue() - Associates a value with a specified key.\n"
62"SetValueEx() - Stores data in the value field of an open registry key.\n"
63"\n"
64"Special objects:\n"
65"\n"
66"HKEYType -- type object for HKEY objects\n"
67"error -- exception raised for Win32 errors\n"
68"\n"
69"Integer constants:\n"
70"Many constants are defined - see the documentation for each function\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000071"to see what constants are used, and where.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +000072
73
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000074PyDoc_STRVAR(CloseKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +000075"CloseKey(hkey) - Closes a previously opened registry key.\n"
76"\n"
77"The hkey argument specifies a previously opened key.\n"
78"\n"
79"Note that if the key is not closed using this method, it will be\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000080"closed when the hkey object is destroyed by Python.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +000081
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000082PyDoc_STRVAR(ConnectRegistry_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +000083"key = ConnectRegistry(computer_name, key) - "
84"Establishes a connection to a predefined registry handle on another computer.\n"
85"\n"
86"computer_name is the name of the remote computer, of the form \\\\computername.\n"
87" If None, the local computer is used.\n"
88"key is the predefined handle to connect to.\n"
89"\n"
90"The return value is the handle of the opened key.\n"
Brian Curtin3035c392010-04-21 23:56:21 +000091"If the function fails, a WindowsError exception is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +000092
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000093PyDoc_STRVAR(CreateKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +000094"key = CreateKey(key, sub_key) - Creates or opens the specified key.\n"
95"\n"
96"key is an already open key, or one of the predefined HKEY_* constants\n"
97"sub_key is a string that names the key this method opens or creates.\n"
98" If key is one of the predefined keys, sub_key may be None. In that case,\n"
99" the handle returned is the same key handle passed in to the function.\n"
100"\n"
101"If the key already exists, this function opens the existing key\n"
102"\n"
103"The return value is the handle of the opened key.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000104"If the function fails, an exception is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000105
Brian Curtin3035c392010-04-21 23:56:21 +0000106PyDoc_STRVAR(CreateKeyEx_doc,
107"key = CreateKeyEx(key, sub_key, res, sam) - Creates or opens the specified key.\n"
108"\n"
109"key is an already open key, or one of the predefined HKEY_* constants\n"
110"sub_key is a string that names the key this method opens or creates.\n"
111"res is a reserved integer, and must be zero. Default is zero.\n"
112"sam is an integer that specifies an access mask that describes the desired\n"
113" If key is one of the predefined keys, sub_key may be None. In that case,\n"
114" the handle returned is the same key handle passed in to the function.\n"
115"\n"
116"If the key already exists, this function opens the existing key\n"
117"\n"
118"The return value is the handle of the opened key.\n"
119"If the function fails, an exception is raised.");
120
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000121PyDoc_STRVAR(DeleteKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000122"DeleteKey(key, sub_key) - Deletes the specified key.\n"
123"\n"
124"key is an already open key, or any one of the predefined HKEY_* constants.\n"
125"sub_key is a string that must be a subkey of the key identified by the key parameter.\n"
126" This value must not be None, and the key may not have subkeys.\n"
127"\n"
128"This method can not delete keys with subkeys.\n"
129"\n"
130"If the method succeeds, the entire key, including all of its values,\n"
Brian Curtin3035c392010-04-21 23:56:21 +0000131"is removed. If the method fails, a WindowsError exception is raised.");
132
133PyDoc_STRVAR(DeleteKeyEx_doc,
134"DeleteKeyEx(key, sub_key, sam, res) - Deletes the specified key.\n"
135"\n"
136"key is an already open key, or any one of the predefined HKEY_* constants.\n"
137"sub_key is a string that must be a subkey of the key identified by the key parameter.\n"
138"res is a reserved integer, and must be zero. Default is zero.\n"
139"sam is an integer that specifies an access mask that describes the desired\n"
140" This value must not be None, and the key may not have subkeys.\n"
141"\n"
142"This method can not delete keys with subkeys.\n"
143"\n"
144"If the method succeeds, the entire key, including all of its values,\n"
145"is removed. If the method fails, a WindowsError exception is raised.\n"
146"On unsupported Windows versions, NotImplementedError is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000147
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000148PyDoc_STRVAR(DeleteValue_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000149"DeleteValue(key, value) - Removes a named value from a registry key.\n"
150"\n"
151"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000152"value is a string that identifies the value to remove.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000153
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000154PyDoc_STRVAR(EnumKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000155"string = EnumKey(key, index) - Enumerates subkeys of an open registry key.\n"
156"\n"
157"key is an already open key, or any one of the predefined HKEY_* constants.\n"
158"index is an integer that identifies the index of the key to retrieve.\n"
159"\n"
160"The function retrieves the name of one subkey each time it is called.\n"
Brian Curtin3035c392010-04-21 23:56:21 +0000161"It is typically called repeatedly until a WindowsError exception is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000162"raised, indicating no more values are available.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000163
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000164PyDoc_STRVAR(EnumValue_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000165"tuple = EnumValue(key, index) - Enumerates values of an open registry key.\n"
166"key is an already open key, or any one of the predefined HKEY_* constants.\n"
167"index is an integer that identifies the index of the value to retrieve.\n"
168"\n"
169"The function retrieves the name of one subkey each time it is called.\n"
Brian Curtin3035c392010-04-21 23:56:21 +0000170"It is typically called repeatedly, until a WindowsError exception\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000171"is raised, indicating no more values.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000172"\n"
173"The result is a tuple of 3 items:\n"
174"value_name is a string that identifies the value.\n"
175"value_data is an object that holds the value data, and whose type depends\n"
176" on the underlying registry type.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000177"data_type is an integer that identifies the type of the value data.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000178
Christian Heimes2380ac72008-01-09 00:17:24 +0000179PyDoc_STRVAR(ExpandEnvironmentStrings_doc,
180"string = ExpandEnvironmentStrings(string) - Expand environment vars.\n");
181
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000182PyDoc_STRVAR(FlushKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000183"FlushKey(key) - Writes all the attributes of a key to the registry.\n"
184"\n"
185"key is an already open key, or any one of the predefined HKEY_* constants.\n"
186"\n"
187"It is not necessary to call RegFlushKey to change a key.\n"
188"Registry changes are flushed to disk by the registry using its lazy flusher.\n"
189"Registry changes are also flushed to disk at system shutdown.\n"
190"Unlike CloseKey(), the FlushKey() method returns only when all the data has\n"
191"been written to the registry.\n"
192"An application should only call FlushKey() if it requires absolute certainty that registry changes are on disk.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000193"If you don't know whether a FlushKey() call is required, it probably isn't.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000194
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000195PyDoc_STRVAR(LoadKey_doc,
Mark Hammondb422f952000-06-09 06:01:47 +0000196"LoadKey(key, sub_key, file_name) - Creates a subkey under the specified key\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000197"and stores registration information from a specified file into that subkey.\n"
198"\n"
199"key is an already open key, or any one of the predefined HKEY_* constants.\n"
200"sub_key is a string that identifies the sub_key to load\n"
201"file_name is the name of the file to load registry data from.\n"
202" This file must have been created with the SaveKey() function.\n"
203" Under the file allocation table (FAT) file system, the filename may not\n"
204"have an extension.\n"
205"\n"
206"A call to LoadKey() fails if the calling process does not have the\n"
207"SE_RESTORE_PRIVILEGE privilege.\n"
208"\n"
209"If key is a handle returned by ConnectRegistry(), then the path specified\n"
210"in fileName is relative to the remote computer.\n"
211"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000212"The docs imply key must be in the HKEY_USER or HKEY_LOCAL_MACHINE tree");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000213
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000214PyDoc_STRVAR(OpenKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000215"key = OpenKey(key, sub_key, res = 0, sam = KEY_READ) - Opens the specified key.\n"
216"\n"
217"key is an already open key, or any one of the predefined HKEY_* constants.\n"
218"sub_key is a string that identifies the sub_key to open\n"
219"res is a reserved integer, and must be zero. Default is zero.\n"
220"sam is an integer that specifies an access mask that describes the desired\n"
221" security access for the key. Default is KEY_READ\n"
222"\n"
223"The result is a new handle to the specified key\n"
Brian Curtin3035c392010-04-21 23:56:21 +0000224"If the function fails, a WindowsError exception is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000225
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000226PyDoc_STRVAR(OpenKeyEx_doc, "See OpenKey()");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000227
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000228PyDoc_STRVAR(QueryInfoKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000229"tuple = QueryInfoKey(key) - Returns information about a key.\n"
230"\n"
231"key is an already open key, or any one of the predefined HKEY_* constants.\n"
232"\n"
233"The result is a tuple of 3 items:"
234"An integer that identifies the number of sub keys this key has.\n"
235"An integer that identifies the number of values this key has.\n"
236"A long integer that identifies when the key was last modified (if available)\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000237" as 100's of nanoseconds since Jan 1, 1600.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000238
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000239PyDoc_STRVAR(QueryValue_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000240"string = QueryValue(key, sub_key) - retrieves the unnamed value for a key.\n"
241"\n"
242"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000243"sub_key is a string that holds the name of the subkey with which the value\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000244" is associated. If this parameter is None or empty, the function retrieves\n"
245" the value set by the SetValue() method for the key identified by key."
246"\n"
247"Values in the registry have name, type, and data components. This method\n"
248"retrieves the data for a key's first value that has a NULL name.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000249"But the underlying API call doesn't return the type, Lame Lame Lame, DONT USE THIS!!!");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000250
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000251PyDoc_STRVAR(QueryValueEx_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000252"value,type_id = QueryValueEx(key, value_name) - Retrieves the type and data for a specified value name associated with an open registry key.\n"
253"\n"
254"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000255"value_name is a string indicating the value to query");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000256
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000257PyDoc_STRVAR(SaveKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000258"SaveKey(key, file_name) - Saves the specified key, and all its subkeys to the specified file.\n"
259"\n"
260"key is an already open key, or any one of the predefined HKEY_* constants.\n"
261"file_name is the name of the file to save registry data to.\n"
262" This file cannot already exist. If this filename includes an extension,\n"
263" it cannot be used on file allocation table (FAT) file systems by the\n"
264" LoadKey(), ReplaceKey() or RestoreKey() methods.\n"
265"\n"
266"If key represents a key on a remote computer, the path described by\n"
267"file_name is relative to the remote computer.\n"
268"The caller of this method must possess the SeBackupPrivilege security privilege.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000269"This function passes NULL for security_attributes to the API.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000270
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000271PyDoc_STRVAR(SetValue_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000272"SetValue(key, sub_key, type, value) - Associates a value with a specified key.\n"
273"\n"
274"key is an already open key, or any one of the predefined HKEY_* constants.\n"
275"sub_key is a string that names the subkey with which the value is associated.\n"
276"type is an integer that specifies the type of the data. Currently this\n"
277" must be REG_SZ, meaning only strings are supported.\n"
278"value is a string that specifies the new value.\n"
279"\n"
280"If the key specified by the sub_key parameter does not exist, the SetValue\n"
281"function creates it.\n"
282"\n"
283"Value lengths are limited by available memory. Long values (more than\n"
284"2048 bytes) should be stored as files with the filenames stored in \n"
285"the configuration registry. This helps the registry perform efficiently.\n"
286"\n"
287"The key identified by the key parameter must have been opened with\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000288"KEY_SET_VALUE access.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000289
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000290PyDoc_STRVAR(SetValueEx_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000291"SetValueEx(key, value_name, reserved, type, value) - Stores data in the value field of an open registry key.\n"
292"\n"
293"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Mark Hammondc9083b62003-01-15 23:38:15 +0000294"value_name is a string containing the name of the value to set, or None\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000295"type is an integer that specifies the type of the data. This should be one of:\n"
296" REG_BINARY -- Binary data in any form.\n"
297" REG_DWORD -- A 32-bit number.\n"
298" REG_DWORD_LITTLE_ENDIAN -- A 32-bit number in little-endian format.\n"
299" REG_DWORD_BIG_ENDIAN -- A 32-bit number in big-endian format.\n"
300" REG_EXPAND_SZ -- A null-terminated string that contains unexpanded references\n"
301" to environment variables (for example, %PATH%).\n"
302" REG_LINK -- A Unicode symbolic link.\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000303" REG_MULTI_SZ -- An sequence of null-terminated strings, terminated by\n"
304" two null characters. Note that Python handles this\n"
305" termination automatically.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000306" REG_NONE -- No defined value type.\n"
307" REG_RESOURCE_LIST -- A device-driver resource list.\n"
308" REG_SZ -- A null-terminated string.\n"
309"reserved can be anything - zero is always passed to the API.\n"
310"value is a string that specifies the new value.\n"
311"\n"
312"This method can also set additional value and type information for the\n"
313"specified key. The key identified by the key parameter must have been\n"
314"opened with KEY_SET_VALUE access.\n"
315"\n"
316"To open the key, use the CreateKeyEx() or OpenKeyEx() methods.\n"
317"\n"
318"Value lengths are limited by available memory. Long values (more than\n"
319"2048 bytes) should be stored as files with the filenames stored in \n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000320"the configuration registry. This helps the registry perform efficiently.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000321
Martin v. Löwisd218dc12008-04-07 03:17:54 +0000322PyDoc_STRVAR(DisableReflectionKey_doc,
Christian Heimes5e696852008-04-09 08:37:03 +0000323"Disables registry reflection for 32-bit processes running on a 64-bit\n"
Martin v. Löwisd218dc12008-04-07 03:17:54 +0000324"Operating System. Will generally raise NotImplemented if executed on\n"
Christian Heimes5e696852008-04-09 08:37:03 +0000325"a 32-bit Operating System.\n"
Martin v. Löwisd218dc12008-04-07 03:17:54 +0000326"If the key is not on the reflection list, the function succeeds but has no effect.\n"
327"Disabling reflection for a key does not affect reflection of any subkeys.");
328
329PyDoc_STRVAR(EnableReflectionKey_doc,
330"Restores registry reflection for the specified disabled key.\n"
Christian Heimes5e696852008-04-09 08:37:03 +0000331"Will generally raise NotImplemented if executed on a 32-bit Operating System.\n"
Martin v. Löwisd218dc12008-04-07 03:17:54 +0000332"Restoring reflection for a key does not affect reflection of any subkeys.");
333
334PyDoc_STRVAR(QueryReflectionKey_doc,
335"bool = QueryReflectionKey(hkey) - Determines the reflection state for the specified key.\n"
Christian Heimes5e696852008-04-09 08:37:03 +0000336"Will generally raise NotImplemented if executed on a 32-bit Operating System.\n");
Martin v. Löwisd218dc12008-04-07 03:17:54 +0000337
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000338/* PyHKEY docstrings */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000339PyDoc_STRVAR(PyHKEY_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000340"PyHKEY Object - A Python object, representing a win32 registry key.\n"
341"\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000342"This object wraps a Windows HKEY object, automatically closing it when\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000343"the object is destroyed. To guarantee cleanup, you can call either\n"
344"the Close() method on the PyHKEY, or the CloseKey() method.\n"
345"\n"
346"All functions which accept a handle object also accept an integer - \n"
347"however, use of the handle object is encouraged.\n"
348"\n"
349"Functions:\n"
350"Close() - Closes the underlying handle.\n"
351"Detach() - Returns the integer Win32 handle, detaching it from the object\n"
352"\n"
353"Properties:\n"
354"handle - The integer Win32 handle.\n"
355"\n"
356"Operations:\n"
Jack Diederich4dafcc42006-11-28 19:15:13 +0000357"__bool__ - Handles with an open object return true, otherwise false.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000358"__int__ - Converting a handle to an integer returns the Win32 handle.\n"
Mark Dickinson211c6252009-02-01 10:28:51 +0000359"rich comparison - Handle objects are compared using the handle value.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000360
361
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000362PyDoc_STRVAR(PyHKEY_Close_doc,
Mark Hammondb422f952000-06-09 06:01:47 +0000363"key.Close() - Closes the underlying Windows handle.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000364"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000365"If the handle is already closed, no error is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000366
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000367PyDoc_STRVAR(PyHKEY_Detach_doc,
Mark Hammondb422f952000-06-09 06:01:47 +0000368"int = key.Detach() - Detaches the Windows handle from the handle object.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000369"\n"
370"The result is the value of the handle before it is detached. If the\n"
371"handle is already detached, this will return zero.\n"
372"\n"
373"After calling this function, the handle is effectively invalidated,\n"
374"but the handle is not closed. You would call this function when you\n"
375"need the underlying win32 handle to exist beyond the lifetime of the\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000376"handle object.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000377"On 64 bit windows, the result of this function is a long integer");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000378
379
380/************************************************************************
381
382 The PyHKEY object definition
383
384************************************************************************/
385typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 PyObject_VAR_HEAD
387 HKEY hkey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000388} PyHKEYObject;
389
390#define PyHKEY_Check(op) ((op)->ob_type == &PyHKEY_Type)
391
392static char *failMsg = "bad operand type";
393
394static PyObject *
395PyHKEY_unaryFailureFunc(PyObject *ob)
396{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 PyErr_SetString(PyExc_TypeError, failMsg);
398 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000399}
400static PyObject *
401PyHKEY_binaryFailureFunc(PyObject *ob1, PyObject *ob2)
402{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 PyErr_SetString(PyExc_TypeError, failMsg);
404 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000405}
406static PyObject *
407PyHKEY_ternaryFailureFunc(PyObject *ob1, PyObject *ob2, PyObject *ob3)
408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 PyErr_SetString(PyExc_TypeError, failMsg);
410 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000411}
412
413static void
414PyHKEY_deallocFunc(PyObject *ob)
415{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 /* Can not call PyHKEY_Close, as the ob->tp_type
417 has already been cleared, thus causing the type
418 check to fail!
419 */
420 PyHKEYObject *obkey = (PyHKEYObject *)ob;
421 if (obkey->hkey)
422 RegCloseKey((HKEY)obkey->hkey);
423 PyObject_DEL(ob);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000424}
425
426static int
Jack Diederich4dafcc42006-11-28 19:15:13 +0000427PyHKEY_boolFunc(PyObject *ob)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 return ((PyHKEYObject *)ob)->hkey != 0;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000430}
431
432static PyObject *
433PyHKEY_intFunc(PyObject *ob)
434{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
436 return PyLong_FromVoidPtr(pyhkey->hkey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000437}
438
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000439static PyObject *
440PyHKEY_strFunc(PyObject *ob)
441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
443 return PyUnicode_FromFormat("<PyHKEY:%p>", pyhkey->hkey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000444}
445
446static int
447PyHKEY_compareFunc(PyObject *ob1, PyObject *ob2)
448{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 PyHKEYObject *pyhkey1 = (PyHKEYObject *)ob1;
450 PyHKEYObject *pyhkey2 = (PyHKEYObject *)ob2;
451 return pyhkey1 == pyhkey2 ? 0 :
452 (pyhkey1 < pyhkey2 ? -1 : 1);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000453}
454
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000455static Py_hash_t
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000456PyHKEY_hashFunc(PyObject *ob)
457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 /* Just use the address.
459 XXX - should we use the handle value?
460 */
461 return _Py_HashPointer(ob);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000462}
463
464
465static PyNumberMethods PyHKEY_NumberMethods =
466{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 PyHKEY_binaryFailureFunc, /* nb_add */
468 PyHKEY_binaryFailureFunc, /* nb_subtract */
469 PyHKEY_binaryFailureFunc, /* nb_multiply */
470 PyHKEY_binaryFailureFunc, /* nb_remainder */
471 PyHKEY_binaryFailureFunc, /* nb_divmod */
472 PyHKEY_ternaryFailureFunc, /* nb_power */
473 PyHKEY_unaryFailureFunc, /* nb_negative */
474 PyHKEY_unaryFailureFunc, /* nb_positive */
475 PyHKEY_unaryFailureFunc, /* nb_absolute */
476 PyHKEY_boolFunc, /* nb_bool */
477 PyHKEY_unaryFailureFunc, /* nb_invert */
478 PyHKEY_binaryFailureFunc, /* nb_lshift */
479 PyHKEY_binaryFailureFunc, /* nb_rshift */
480 PyHKEY_binaryFailureFunc, /* nb_and */
481 PyHKEY_binaryFailureFunc, /* nb_xor */
482 PyHKEY_binaryFailureFunc, /* nb_or */
483 PyHKEY_intFunc, /* nb_int */
484 0, /* nb_reserved */
485 PyHKEY_unaryFailureFunc, /* nb_float */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000486};
487
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000488static PyObject *PyHKEY_CloseMethod(PyObject *self, PyObject *args);
489static PyObject *PyHKEY_DetachMethod(PyObject *self, PyObject *args);
490static PyObject *PyHKEY_Enter(PyObject *self);
491static PyObject *PyHKEY_Exit(PyObject *self, PyObject *args);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000492
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000493static struct PyMethodDef PyHKEY_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 {"Close", PyHKEY_CloseMethod, METH_VARARGS, PyHKEY_Close_doc},
495 {"Detach", PyHKEY_DetachMethod, METH_VARARGS, PyHKEY_Detach_doc},
496 {"__enter__", (PyCFunction)PyHKEY_Enter, METH_NOARGS, NULL},
497 {"__exit__", PyHKEY_Exit, METH_VARARGS, NULL},
498 {NULL}
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000499};
500
501#define OFF(e) offsetof(PyHKEYObject, e)
502static PyMemberDef PyHKEY_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 {"handle", T_INT, OFF(hkey), READONLY},
504 {NULL} /* Sentinel */
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000505};
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000506
507/* The type itself */
508PyTypeObject PyHKEY_Type =
509{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 PyVarObject_HEAD_INIT(0, 0) /* fill in type at module init */
511 "PyHKEY",
512 sizeof(PyHKEYObject),
513 0,
514 PyHKEY_deallocFunc, /* tp_dealloc */
515 0, /* tp_print */
516 0, /* tp_getattr */
517 0, /* tp_setattr */
518 0, /* tp_reserved */
519 0, /* tp_repr */
520 &PyHKEY_NumberMethods, /* tp_as_number */
521 0, /* tp_as_sequence */
522 0, /* tp_as_mapping */
523 PyHKEY_hashFunc, /* tp_hash */
524 0, /* tp_call */
525 PyHKEY_strFunc, /* tp_str */
526 0, /* tp_getattro */
527 0, /* tp_setattro */
528 0, /* tp_as_buffer */
529 0, /* tp_flags */
530 PyHKEY_doc, /* tp_doc */
531 0, /*tp_traverse*/
532 0, /*tp_clear*/
533 0, /*tp_richcompare*/
534 0, /*tp_weaklistoffset*/
535 0, /*tp_iter*/
536 0, /*tp_iternext*/
537 PyHKEY_methods, /*tp_methods*/
538 PyHKEY_memberlist, /*tp_members*/
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000539};
540
541/************************************************************************
542
543 The PyHKEY object methods
544
545************************************************************************/
546static PyObject *
547PyHKEY_CloseMethod(PyObject *self, PyObject *args)
548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 if (!PyArg_ParseTuple(args, ":Close"))
550 return NULL;
551 if (!PyHKEY_Close(self))
552 return NULL;
553 Py_INCREF(Py_None);
554 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000555}
556
557static PyObject *
558PyHKEY_DetachMethod(PyObject *self, PyObject *args)
559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 void* ret;
561 PyHKEYObject *pThis = (PyHKEYObject *)self;
562 if (!PyArg_ParseTuple(args, ":Detach"))
563 return NULL;
564 ret = (void*)pThis->hkey;
565 pThis->hkey = 0;
566 return PyLong_FromVoidPtr(ret);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000567}
568
Christian Heimes2380ac72008-01-09 00:17:24 +0000569static PyObject *
570PyHKEY_Enter(PyObject *self)
571{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 Py_XINCREF(self);
573 return self;
Christian Heimes2380ac72008-01-09 00:17:24 +0000574}
575
576static PyObject *
577PyHKEY_Exit(PyObject *self, PyObject *args)
578{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 if (!PyHKEY_Close(self))
580 return NULL;
581 Py_RETURN_NONE;
Christian Heimes2380ac72008-01-09 00:17:24 +0000582}
583
584
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000585/************************************************************************
586 The public PyHKEY API (well, not public yet :-)
587************************************************************************/
588PyObject *
589PyHKEY_New(HKEY hInit)
590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 PyHKEYObject *key = PyObject_NEW(PyHKEYObject, &PyHKEY_Type);
592 if (key)
593 key->hkey = hInit;
594 return (PyObject *)key;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000595}
596
597BOOL
598PyHKEY_Close(PyObject *ob_handle)
599{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 LONG rc;
601 PyHKEYObject *key;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 if (!PyHKEY_Check(ob_handle)) {
604 PyErr_SetString(PyExc_TypeError, "bad operand type");
605 return FALSE;
606 }
607 key = (PyHKEYObject *)ob_handle;
608 rc = key->hkey ? RegCloseKey((HKEY)key->hkey) : ERROR_SUCCESS;
609 key->hkey = 0;
610 if (rc != ERROR_SUCCESS)
611 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
612 return rc == ERROR_SUCCESS;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000613}
614
615BOOL
616PyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK)
617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 if (ob == Py_None) {
619 if (!bNoneOK) {
620 PyErr_SetString(
621 PyExc_TypeError,
622 "None is not a valid HKEY in this context");
623 return FALSE;
624 }
625 *pHANDLE = (HKEY)0;
626 }
627 else if (PyHKEY_Check(ob)) {
628 PyHKEYObject *pH = (PyHKEYObject *)ob;
629 *pHANDLE = pH->hkey;
630 }
631 else if (PyLong_Check(ob)) {
632 /* We also support integers */
633 PyErr_Clear();
634 *pHANDLE = (HKEY)PyLong_AsVoidPtr(ob);
635 if (PyErr_Occurred())
636 return FALSE;
637 }
638 else {
639 PyErr_SetString(
640 PyExc_TypeError,
641 "The object is not a PyHKEY object");
642 return FALSE;
643 }
644 return TRUE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000645}
646
647PyObject *
648PyHKEY_FromHKEY(HKEY h)
649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 PyHKEYObject *op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 /* Inline PyObject_New */
653 op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject));
654 if (op == NULL)
655 return PyErr_NoMemory();
656 PyObject_INIT(op, &PyHKEY_Type);
657 op->hkey = h;
658 return (PyObject *)op;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000659}
660
661
662/************************************************************************
663 The module methods
664************************************************************************/
665BOOL
666PyWinObject_CloseHKEY(PyObject *obHandle)
667{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 BOOL ok;
669 if (PyHKEY_Check(obHandle)) {
670 ok = PyHKEY_Close(obHandle);
671 }
Fred Drake25e17262000-06-30 17:48:51 +0000672#if SIZEOF_LONG >= SIZEOF_HKEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 else if (PyLong_Check(obHandle)) {
674 long rc = RegCloseKey((HKEY)PyLong_AsLong(obHandle));
675 ok = (rc == ERROR_SUCCESS);
676 if (!ok)
677 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
678 }
Fred Drake25e17262000-06-30 17:48:51 +0000679#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 else if (PyLong_Check(obHandle)) {
681 long rc = RegCloseKey((HKEY)PyLong_AsVoidPtr(obHandle));
682 ok = (rc == ERROR_SUCCESS);
683 if (!ok)
684 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
685 }
Fred Drake25e17262000-06-30 17:48:51 +0000686#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 else {
688 PyErr_SetString(
689 PyExc_TypeError,
690 "A handle must be a HKEY object or an integer");
691 return FALSE;
692 }
693 return ok;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000694}
695
696
697/*
698 Private Helper functions for the registry interfaces
699
700** Note that fixupMultiSZ and countString have both had changes
701** made to support "incorrect strings". The registry specification
702** calls for strings to be terminated with 2 null bytes. It seems
Thomas Wouters7e474022000-07-16 12:04:32 +0000703** some commercial packages install strings which dont conform,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000704** causing this code to fail - however, "regedit" etc still work
705** with these strings (ie only we dont!).
706*/
707static void
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000708fixupMultiSZ(wchar_t **str, wchar_t *data, int len)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000709{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 wchar_t *P;
711 int i;
712 wchar_t *Q;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 Q = data + len;
715 for (P = data, i = 0; P < Q && *P != '\0'; P++, i++) {
716 str[i] = P;
717 for(; *P != '\0'; P++)
718 ;
719 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000720}
721
722static int
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000723countStrings(wchar_t *data, int len)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000724{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 int strings;
726 wchar_t *P;
727 wchar_t *Q = data + len;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 for (P = data, strings = 0; P < Q && *P != '\0'; P++, strings++)
730 for (; P < Q && *P != '\0'; P++)
731 ;
732 return strings;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000733}
734
735/* Convert PyObject into Registry data.
736 Allocates space as needed. */
737static BOOL
738Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
739{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 Py_ssize_t i,j;
741 switch (typ) {
742 case REG_DWORD:
743 if (value != Py_None && !PyLong_Check(value))
744 return FALSE;
745 *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1);
746 if (*retDataBuf==NULL){
747 PyErr_NoMemory();
748 return FALSE;
749 }
750 *retDataSize = sizeof(DWORD);
751 if (value == Py_None) {
752 DWORD zero = 0;
753 memcpy(*retDataBuf, &zero, sizeof(DWORD));
754 }
755 else {
756 DWORD d = PyLong_AsLong(value);
757 memcpy(*retDataBuf, &d, sizeof(DWORD));
758 }
759 break;
760 case REG_SZ:
761 case REG_EXPAND_SZ:
762 {
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 Curtin1771b542010-09-27 17:56:36 +0000992PyCreateKeyEx(PyObject *self, PyObject *args, PyObject *kwargs)
Brian Curtin3035c392010-04-21 23:56:21 +0000993{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 HKEY hKey;
Brian Curtin1771b542010-09-27 17:56:36 +0000995 PyObject *key;
996 wchar_t *sub_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 HKEY retKey;
Brian Curtin1771b542010-09-27 17:56:36 +0000998 int reserved = 0;
999 REGSAM access = KEY_WRITE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 long rc;
Brian Curtin1771b542010-09-27 17:56:36 +00001001
1002 char *kwlist[] = {"key", "sub_key", "reserved", "access", NULL};
1003
1004 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OZ|ii:CreateKeyEx", kwlist,
1005 &key, &sub_key, &reserved, &access))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 return NULL;
Brian Curtin1771b542010-09-27 17:56:36 +00001007 if (!PyHKEY_AsHKEY(key, &hKey, FALSE))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 return NULL;
Brian Curtin3035c392010-04-21 23:56:21 +00001009
Brian Curtin1771b542010-09-27 17:56:36 +00001010 rc = RegCreateKeyExW(hKey, sub_key, reserved, NULL, (DWORD)NULL,
1011 access, NULL, &retKey, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 if (rc != ERROR_SUCCESS)
1013 return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKeyEx");
1014 return PyHKEY_FromHKEY(retKey);
Brian Curtin3035c392010-04-21 23:56:21 +00001015}
1016
1017static PyObject *
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001018PyDeleteKey(PyObject *self, PyObject *args)
1019{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 HKEY hKey;
1021 PyObject *obKey;
1022 wchar_t *subKey;
1023 long rc;
1024 if (!PyArg_ParseTuple(args, "Ou:DeleteKey", &obKey, &subKey))
1025 return NULL;
1026 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1027 return NULL;
1028 rc = RegDeleteKeyW(hKey, subKey );
1029 if (rc != ERROR_SUCCESS)
1030 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey");
1031 Py_INCREF(Py_None);
1032 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001033}
1034
1035static PyObject *
Brian Curtin1771b542010-09-27 17:56:36 +00001036PyDeleteKeyEx(PyObject *self, PyObject *args, PyObject *kwargs)
Brian Curtin3035c392010-04-21 23:56:21 +00001037{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 HKEY hKey;
Brian Curtin1771b542010-09-27 17:56:36 +00001039 PyObject *key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 HMODULE hMod;
1041 typedef LONG (WINAPI *RDKEFunc)(HKEY, const wchar_t*, REGSAM, int);
1042 RDKEFunc pfn = NULL;
Brian Curtin1771b542010-09-27 17:56:36 +00001043 wchar_t *sub_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 long rc;
Brian Curtin1771b542010-09-27 17:56:36 +00001045 int reserved = 0;
1046 REGSAM access = KEY_WOW64_64KEY;
Brian Curtin3035c392010-04-21 23:56:21 +00001047
Brian Curtin1771b542010-09-27 17:56:36 +00001048 char *kwlist[] = {"key", "sub_key", "access", "reserved", NULL};
1049 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Ou|ii:DeleteKeyEx", kwlist,
1050 &key, &sub_key, &access, &reserved))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 return NULL;
Brian Curtin1771b542010-09-27 17:56:36 +00001052 if (!PyHKEY_AsHKEY(key, &hKey, FALSE))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 return NULL;
Brian Curtin3035c392010-04-21 23:56:21 +00001054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 /* Only available on 64bit platforms, so we must load it
1056 dynamically. */
1057 hMod = GetModuleHandle("advapi32.dll");
1058 if (hMod)
1059 pfn = (RDKEFunc)GetProcAddress(hMod,
1060 "RegDeleteKeyExW");
1061 if (!pfn) {
1062 PyErr_SetString(PyExc_NotImplementedError,
1063 "not implemented on this platform");
1064 return NULL;
1065 }
1066 Py_BEGIN_ALLOW_THREADS
Brian Curtin1771b542010-09-27 17:56:36 +00001067 rc = (*pfn)(hKey, sub_key, access, reserved);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 Py_END_ALLOW_THREADS
Brian Curtin3035c392010-04-21 23:56:21 +00001069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 if (rc != ERROR_SUCCESS)
1071 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKeyEx");
1072 Py_INCREF(Py_None);
1073 return Py_None;
Brian Curtin3035c392010-04-21 23:56:21 +00001074}
1075
1076static PyObject *
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001077PyDeleteValue(PyObject *self, PyObject *args)
1078{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 HKEY hKey;
1080 PyObject *obKey;
1081 wchar_t *subKey;
1082 long rc;
1083 if (!PyArg_ParseTuple(args, "OZ:DeleteValue", &obKey, &subKey))
1084 return NULL;
1085 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1086 return NULL;
1087 Py_BEGIN_ALLOW_THREADS
1088 rc = RegDeleteValueW(hKey, subKey);
1089 Py_END_ALLOW_THREADS
1090 if (rc !=ERROR_SUCCESS)
1091 return PyErr_SetFromWindowsErrWithFunction(rc,
1092 "RegDeleteValue");
1093 Py_INCREF(Py_None);
1094 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001095}
1096
1097static PyObject *
1098PyEnumKey(PyObject *self, PyObject *args)
1099{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 HKEY hKey;
1101 PyObject *obKey;
1102 int index;
1103 long rc;
1104 PyObject *retStr;
Brian Curtin60853212010-05-26 17:43:50 +00001105
1106 /* The Windows docs claim that the max key name length is 255
1107 * characters, plus a terminating nul character. However,
1108 * empirical testing demonstrates that it is possible to
1109 * create a 256 character key that is missing the terminating
1110 * nul. RegEnumKeyEx requires a 257 character buffer to
1111 * retrieve such a key name. */
1112 wchar_t tmpbuf[257];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 DWORD len = sizeof(tmpbuf); /* includes NULL terminator */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 if (!PyArg_ParseTuple(args, "Oi:EnumKey", &obKey, &index))
1116 return NULL;
1117 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1118 return NULL;
Tim Peters313fcd42006-02-19 04:05:39 +00001119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 Py_BEGIN_ALLOW_THREADS
1121 rc = RegEnumKeyExW(hKey, index, tmpbuf, &len, NULL, NULL, NULL, NULL);
1122 Py_END_ALLOW_THREADS
1123 if (rc != ERROR_SUCCESS)
1124 return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx");
Tim Peters313fcd42006-02-19 04:05:39 +00001125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 retStr = PyUnicode_FromUnicode(tmpbuf, len);
1127 return retStr; /* can be NULL */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001128}
1129
1130static PyObject *
1131PyEnumValue(PyObject *self, PyObject *args)
1132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 HKEY hKey;
1134 PyObject *obKey;
1135 int index;
1136 long rc;
1137 wchar_t *retValueBuf;
Amaury Forgeot d'Arcf2b69df2010-08-16 22:11:29 +00001138 BYTE *tmpBuf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 BYTE *retDataBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001140 DWORD retValueSize, bufValueSize;
1141 DWORD retDataSize, bufDataSize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 DWORD typ;
1143 PyObject *obData;
1144 PyObject *retVal;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 if (!PyArg_ParseTuple(args, "Oi:EnumValue", &obKey, &index))
1147 return NULL;
1148 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1149 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 if ((rc = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, NULL, NULL,
1152 NULL,
1153 &retValueSize, &retDataSize, NULL, NULL))
1154 != ERROR_SUCCESS)
1155 return PyErr_SetFromWindowsErrWithFunction(rc,
1156 "RegQueryInfoKey");
1157 ++retValueSize; /* include null terminators */
1158 ++retDataSize;
Brian Curtin60853212010-05-26 17:43:50 +00001159 bufDataSize = retDataSize;
1160 bufValueSize = retValueSize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 retValueBuf = (wchar_t *)PyMem_Malloc(sizeof(wchar_t) * retValueSize);
1162 if (retValueBuf == NULL)
1163 return PyErr_NoMemory();
1164 retDataBuf = (BYTE *)PyMem_Malloc(retDataSize);
1165 if (retDataBuf == NULL) {
1166 PyMem_Free(retValueBuf);
1167 return PyErr_NoMemory();
1168 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001169
Brian Curtin60853212010-05-26 17:43:50 +00001170 while (1) {
Brian Curtin60853212010-05-26 17:43:50 +00001171 Py_BEGIN_ALLOW_THREADS
1172 rc = RegEnumValueW(hKey,
1173 index,
1174 retValueBuf,
1175 &retValueSize,
1176 NULL,
1177 &typ,
1178 (BYTE *)retDataBuf,
1179 &retDataSize);
1180 Py_END_ALLOW_THREADS
1181
1182 if (rc != ERROR_MORE_DATA)
1183 break;
1184
1185 bufDataSize *= 2;
Amaury Forgeot d'Arcf2b69df2010-08-16 22:11:29 +00001186 tmpBuf = (BYTE *)PyMem_Realloc(retDataBuf, bufDataSize);
Brian Curtin9b7e2d12010-06-08 20:57:52 +00001187 if (tmpBuf == NULL) {
Brian Curtin60853212010-05-26 17:43:50 +00001188 PyErr_NoMemory();
1189 retVal = NULL;
1190 goto fail;
1191 }
Brian Curtin9b7e2d12010-06-08 20:57:52 +00001192 retDataBuf = tmpBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001193 retDataSize = bufDataSize;
1194 retValueSize = bufValueSize;
1195 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 if (rc != ERROR_SUCCESS) {
1198 retVal = PyErr_SetFromWindowsErrWithFunction(rc,
1199 "PyRegEnumValue");
1200 goto fail;
1201 }
1202 obData = Reg2Py(retDataBuf, retDataSize, typ);
1203 if (obData == NULL) {
1204 retVal = NULL;
1205 goto fail;
1206 }
1207 retVal = Py_BuildValue("uOi", retValueBuf, obData, typ);
1208 Py_DECREF(obData);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001209 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 PyMem_Free(retValueBuf);
1211 PyMem_Free(retDataBuf);
1212 return retVal;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001213}
1214
1215static PyObject *
Christian Heimes2380ac72008-01-09 00:17:24 +00001216PyExpandEnvironmentStrings(PyObject *self, PyObject *args)
1217{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 Py_UNICODE *retValue = NULL;
1219 Py_UNICODE *src;
1220 DWORD retValueSize;
1221 DWORD rc;
1222 PyObject *o;
Christian Heimes2380ac72008-01-09 00:17:24 +00001223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 if (!PyArg_ParseTuple(args, "u:ExpandEnvironmentStrings", &src))
1225 return NULL;
Christian Heimes2380ac72008-01-09 00:17:24 +00001226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 retValueSize = ExpandEnvironmentStringsW(src, retValue, 0);
1228 if (retValueSize == 0) {
1229 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1230 "ExpandEnvironmentStrings");
1231 }
1232 retValue = (Py_UNICODE *)PyMem_Malloc(retValueSize * sizeof(Py_UNICODE));
1233 if (retValue == NULL) {
1234 return PyErr_NoMemory();
1235 }
Christian Heimes2380ac72008-01-09 00:17:24 +00001236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 rc = ExpandEnvironmentStringsW(src, retValue, retValueSize);
1238 if (rc == 0) {
1239 PyMem_Free(retValue);
1240 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1241 "ExpandEnvironmentStrings");
1242 }
1243 o = PyUnicode_FromUnicode(retValue, wcslen(retValue));
1244 PyMem_Free(retValue);
1245 return o;
Christian Heimes2380ac72008-01-09 00:17:24 +00001246}
1247
1248static PyObject *
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001249PyFlushKey(PyObject *self, PyObject *args)
1250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 HKEY hKey;
1252 PyObject *obKey;
1253 long rc;
1254 if (!PyArg_ParseTuple(args, "O:FlushKey", &obKey))
1255 return NULL;
1256 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1257 return NULL;
1258 Py_BEGIN_ALLOW_THREADS
1259 rc = RegFlushKey(hKey);
1260 Py_END_ALLOW_THREADS
1261 if (rc != ERROR_SUCCESS)
1262 return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey");
1263 Py_INCREF(Py_None);
1264 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001265}
1266static PyObject *
1267PyLoadKey(PyObject *self, PyObject *args)
1268{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 HKEY hKey;
1270 PyObject *obKey;
1271 wchar_t *subKey;
1272 wchar_t *fileName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 long rc;
1275 if (!PyArg_ParseTuple(args, "Ouu:LoadKey", &obKey, &subKey, &fileName))
1276 return NULL;
1277 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1278 return NULL;
1279 Py_BEGIN_ALLOW_THREADS
1280 rc = RegLoadKeyW(hKey, subKey, fileName );
1281 Py_END_ALLOW_THREADS
1282 if (rc != ERROR_SUCCESS)
1283 return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey");
1284 Py_INCREF(Py_None);
1285 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001286}
1287
1288static PyObject *
Brian Curtin1771b542010-09-27 17:56:36 +00001289PyOpenKey(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 HKEY hKey;
Brian Curtin1771b542010-09-27 17:56:36 +00001292 PyObject *key;
1293 wchar_t *sub_key;
1294 int reserved = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 HKEY retKey;
1296 long rc;
Brian Curtin1771b542010-09-27 17:56:36 +00001297 REGSAM access = KEY_READ;
1298
1299 char *kwlist[] = {"key", "sub_key", "reserved", "access", NULL};
1300
1301 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OZ|ii:OpenKey", kwlist,
1302 &key, &sub_key, &reserved, &access))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 return NULL;
Brian Curtin1771b542010-09-27 17:56:36 +00001304 if (!PyHKEY_AsHKEY(key, &hKey, FALSE))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 Py_BEGIN_ALLOW_THREADS
Brian Curtin1771b542010-09-27 17:56:36 +00001308 rc = RegOpenKeyExW(hKey, sub_key, reserved, access, &retKey);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 Py_END_ALLOW_THREADS
1310 if (rc != ERROR_SUCCESS)
1311 return PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx");
1312 return PyHKEY_FromHKEY(retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001313}
1314
1315
1316static PyObject *
1317PyQueryInfoKey(PyObject *self, PyObject *args)
1318{
1319 HKEY hKey;
1320 PyObject *obKey;
1321 long rc;
1322 DWORD nSubKeys, nValues;
1323 FILETIME ft;
1324 LARGE_INTEGER li;
1325 PyObject *l;
1326 PyObject *ret;
1327 if (!PyArg_ParseTuple(args, "O:QueryInfoKey", &obKey))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001329 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001331 if ((rc = RegQueryInfoKey(hKey, NULL, NULL, 0, &nSubKeys, NULL, NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 &nValues, NULL, NULL, NULL, &ft))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001333 != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey");
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001335 li.LowPart = ft.dwLowDateTime;
1336 li.HighPart = ft.dwHighDateTime;
1337 l = PyLong_FromLongLong(li.QuadPart);
1338 if (l == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001340 ret = Py_BuildValue("iiO", nSubKeys, nValues, l);
1341 Py_DECREF(l);
1342 return ret;
1343}
1344
1345static PyObject *
1346PyQueryValue(PyObject *self, PyObject *args)
1347{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 HKEY hKey;
1349 PyObject *obKey;
1350 wchar_t *subKey;
1351 long rc;
1352 PyObject *retStr;
1353 wchar_t *retBuf;
Brian Curtin60853212010-05-26 17:43:50 +00001354 DWORD bufSize = 0;
1355 DWORD retSize = 0;
1356 wchar_t *tmp;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 if (!PyArg_ParseTuple(args, "OZ:QueryValue", &obKey, &subKey))
1359 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1362 return NULL;
Brian Curtin60853212010-05-26 17:43:50 +00001363
1364 rc = RegQueryValueW(hKey, subKey, NULL, &retSize);
1365 if (rc == ERROR_MORE_DATA)
1366 retSize = 256;
1367 else if (rc != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 return PyErr_SetFromWindowsErrWithFunction(rc,
1369 "RegQueryValue");
Brian Curtin60853212010-05-26 17:43:50 +00001370
1371 bufSize = retSize;
1372 retBuf = (wchar_t *) PyMem_Malloc(bufSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 if (retBuf == NULL)
1374 return PyErr_NoMemory();
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001375
Brian Curtin60853212010-05-26 17:43:50 +00001376 while (1) {
1377 retSize = bufSize;
1378 rc = RegQueryValueW(hKey, subKey, retBuf, &retSize);
1379 if (rc != ERROR_MORE_DATA)
1380 break;
1381
1382 bufSize *= 2;
1383 tmp = (wchar_t *) PyMem_Realloc(retBuf, bufSize);
1384 if (tmp == NULL) {
1385 PyMem_Free(retBuf);
1386 return PyErr_NoMemory();
1387 }
1388 retBuf = tmp;
1389 }
1390
1391 if (rc != ERROR_SUCCESS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 PyMem_Free(retBuf);
1393 return PyErr_SetFromWindowsErrWithFunction(rc,
1394 "RegQueryValue");
1395 }
Guido van Rossuma8c360e2007-07-17 20:50:43 +00001396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 retStr = PyUnicode_FromUnicode(retBuf, wcslen(retBuf));
1398 PyMem_Free(retBuf);
1399 return retStr;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001400}
1401
1402static PyObject *
1403PyQueryValueEx(PyObject *self, PyObject *args)
1404{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 HKEY hKey;
1406 PyObject *obKey;
1407 wchar_t *valueName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 long rc;
Brian Curtin60853212010-05-26 17:43:50 +00001410 BYTE *retBuf, *tmp;
1411 DWORD bufSize = 0, retSize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 DWORD typ;
1413 PyObject *obData;
1414 PyObject *result;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 if (!PyArg_ParseTuple(args, "OZ:QueryValueEx", &obKey, &valueName))
1417 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1420 return NULL;
Brian Curtin60853212010-05-26 17:43:50 +00001421
1422 rc = RegQueryValueExW(hKey, valueName, NULL, NULL, NULL, &bufSize);
1423 if (rc == ERROR_MORE_DATA)
1424 bufSize = 256;
1425 else if (rc != ERROR_SUCCESS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 return PyErr_SetFromWindowsErrWithFunction(rc,
1427 "RegQueryValueEx");
1428 retBuf = (BYTE *)PyMem_Malloc(bufSize);
1429 if (retBuf == NULL)
1430 return PyErr_NoMemory();
Brian Curtin60853212010-05-26 17:43:50 +00001431
1432 while (1) {
1433 retSize = bufSize;
1434 rc = RegQueryValueExW(hKey, valueName, NULL, &typ,
1435 (BYTE *)retBuf, &retSize);
1436 if (rc != ERROR_MORE_DATA)
1437 break;
1438
1439 bufSize *= 2;
1440 tmp = (char *) PyMem_Realloc(retBuf, bufSize);
1441 if (tmp == NULL) {
1442 PyMem_Free(retBuf);
1443 return PyErr_NoMemory();
1444 }
1445 retBuf = tmp;
1446 }
1447
1448 if (rc != ERROR_SUCCESS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 PyMem_Free(retBuf);
1450 return PyErr_SetFromWindowsErrWithFunction(rc,
1451 "RegQueryValueEx");
1452 }
1453 obData = Reg2Py(retBuf, bufSize, typ);
1454 PyMem_Free(retBuf);
1455 if (obData == NULL)
1456 return NULL;
1457 result = Py_BuildValue("Oi", obData, typ);
1458 Py_DECREF(obData);
1459 return result;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001460}
1461
1462
1463static PyObject *
1464PySaveKey(PyObject *self, PyObject *args)
1465{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 HKEY hKey;
1467 PyObject *obKey;
1468 wchar_t *fileName;
1469 LPSECURITY_ATTRIBUTES pSA = NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 long rc;
1472 if (!PyArg_ParseTuple(args, "Ou:SaveKey", &obKey, &fileName))
1473 return NULL;
1474 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1475 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001476/* One day we may get security into the core?
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE))
1478 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001479*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 Py_BEGIN_ALLOW_THREADS
1481 rc = RegSaveKeyW(hKey, fileName, pSA );
1482 Py_END_ALLOW_THREADS
1483 if (rc != ERROR_SUCCESS)
1484 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey");
1485 Py_INCREF(Py_None);
1486 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001487}
1488
1489static PyObject *
1490PySetValue(PyObject *self, PyObject *args)
1491{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 HKEY hKey;
1493 PyObject *obKey;
1494 wchar_t *subKey;
1495 wchar_t *str;
1496 DWORD typ;
1497 DWORD len;
1498 long rc;
1499 if (!PyArg_ParseTuple(args, "OZiu#:SetValue",
1500 &obKey,
1501 &subKey,
1502 &typ,
1503 &str,
1504 &len))
1505 return NULL;
1506 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1507 return NULL;
1508 if (typ != REG_SZ) {
1509 PyErr_SetString(PyExc_TypeError,
1510 "Type must be winreg.REG_SZ");
1511 return NULL;
1512 }
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 Py_BEGIN_ALLOW_THREADS
1515 rc = RegSetValueW(hKey, subKey, REG_SZ, str, len+1);
1516 Py_END_ALLOW_THREADS
1517 if (rc != ERROR_SUCCESS)
1518 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue");
1519 Py_INCREF(Py_None);
1520 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001521}
1522
1523static PyObject *
1524PySetValueEx(PyObject *self, PyObject *args)
1525{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 HKEY hKey;
1527 PyObject *obKey;
1528 Py_UNICODE *valueName;
1529 PyObject *obRes;
1530 PyObject *value;
1531 BYTE *data;
1532 DWORD len;
1533 DWORD typ;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 LONG rc;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 if (!PyArg_ParseTuple(args, "OZOiO:SetValueEx",
1538 &obKey,
1539 &valueName,
1540 &obRes,
1541 &typ,
1542 &value))
1543 return NULL;
1544 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1545 return NULL;
1546 if (!Py2Reg(value, typ, &data, &len))
1547 {
1548 if (!PyErr_Occurred())
1549 PyErr_SetString(PyExc_ValueError,
1550 "Could not convert the data to the specified type.");
1551 return NULL;
1552 }
1553 Py_BEGIN_ALLOW_THREADS
1554 rc = RegSetValueExW(hKey, valueName, 0, typ, data, len);
1555 Py_END_ALLOW_THREADS
1556 PyMem_DEL(data);
1557 if (rc != ERROR_SUCCESS)
1558 return PyErr_SetFromWindowsErrWithFunction(rc,
1559 "RegSetValueEx");
1560 Py_INCREF(Py_None);
1561 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001562}
1563
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001564static PyObject *
1565PyDisableReflectionKey(PyObject *self, PyObject *args)
1566{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 HKEY hKey;
1568 PyObject *obKey;
1569 HMODULE hMod;
1570 typedef LONG (WINAPI *RDRKFunc)(HKEY);
1571 RDRKFunc pfn = NULL;
1572 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 if (!PyArg_ParseTuple(args, "O:DisableReflectionKey", &obKey))
1575 return NULL;
1576 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1577 return NULL;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 /* Only available on 64bit platforms, so we must load it
1580 dynamically.*/
1581 hMod = GetModuleHandle("advapi32.dll");
1582 if (hMod)
1583 pfn = (RDRKFunc)GetProcAddress(hMod,
1584 "RegDisableReflectionKey");
1585 if (!pfn) {
1586 PyErr_SetString(PyExc_NotImplementedError,
1587 "not implemented on this platform");
1588 return NULL;
1589 }
1590 Py_BEGIN_ALLOW_THREADS
1591 rc = (*pfn)(hKey);
1592 Py_END_ALLOW_THREADS
1593 if (rc != ERROR_SUCCESS)
1594 return PyErr_SetFromWindowsErrWithFunction(rc,
1595 "RegDisableReflectionKey");
1596 Py_INCREF(Py_None);
1597 return Py_None;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001598}
1599
1600static PyObject *
1601PyEnableReflectionKey(PyObject *self, PyObject *args)
1602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 HKEY hKey;
1604 PyObject *obKey;
1605 HMODULE hMod;
1606 typedef LONG (WINAPI *RERKFunc)(HKEY);
1607 RERKFunc pfn = NULL;
1608 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 if (!PyArg_ParseTuple(args, "O:EnableReflectionKey", &obKey))
1611 return NULL;
1612 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1613 return NULL;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 /* Only available on 64bit platforms, so we must load it
1616 dynamically.*/
1617 hMod = GetModuleHandle("advapi32.dll");
1618 if (hMod)
1619 pfn = (RERKFunc)GetProcAddress(hMod,
1620 "RegEnableReflectionKey");
1621 if (!pfn) {
1622 PyErr_SetString(PyExc_NotImplementedError,
1623 "not implemented on this platform");
1624 return NULL;
1625 }
1626 Py_BEGIN_ALLOW_THREADS
1627 rc = (*pfn)(hKey);
1628 Py_END_ALLOW_THREADS
1629 if (rc != ERROR_SUCCESS)
1630 return PyErr_SetFromWindowsErrWithFunction(rc,
1631 "RegEnableReflectionKey");
1632 Py_INCREF(Py_None);
1633 return Py_None;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001634}
1635
1636static PyObject *
1637PyQueryReflectionKey(PyObject *self, PyObject *args)
1638{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 HKEY hKey;
1640 PyObject *obKey;
1641 HMODULE hMod;
1642 typedef LONG (WINAPI *RQRKFunc)(HKEY, BOOL *);
1643 RQRKFunc pfn = NULL;
1644 BOOL result;
1645 LONG rc;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 if (!PyArg_ParseTuple(args, "O:QueryReflectionKey", &obKey))
1648 return NULL;
1649 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1650 return NULL;
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 /* Only available on 64bit platforms, so we must load it
1653 dynamically.*/
1654 hMod = GetModuleHandle("advapi32.dll");
1655 if (hMod)
1656 pfn = (RQRKFunc)GetProcAddress(hMod,
1657 "RegQueryReflectionKey");
1658 if (!pfn) {
1659 PyErr_SetString(PyExc_NotImplementedError,
1660 "not implemented on this platform");
1661 return NULL;
1662 }
1663 Py_BEGIN_ALLOW_THREADS
1664 rc = (*pfn)(hKey, &result);
1665 Py_END_ALLOW_THREADS
1666 if (rc != ERROR_SUCCESS)
1667 return PyErr_SetFromWindowsErrWithFunction(rc,
1668 "RegQueryReflectionKey");
1669 return PyBool_FromLong(result);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001670}
1671
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001672static struct PyMethodDef winreg_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 {"CloseKey", PyCloseKey, METH_VARARGS, CloseKey_doc},
1674 {"ConnectRegistry", PyConnectRegistry, METH_VARARGS, ConnectRegistry_doc},
1675 {"CreateKey", PyCreateKey, METH_VARARGS, CreateKey_doc},
Brian Curtin1771b542010-09-27 17:56:36 +00001676 {"CreateKeyEx", (PyCFunction)PyCreateKeyEx,
1677 METH_VARARGS | METH_KEYWORDS, CreateKeyEx_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 {"DeleteKey", PyDeleteKey, METH_VARARGS, DeleteKey_doc},
Brian Curtin1771b542010-09-27 17:56:36 +00001679 {"DeleteKeyEx", (PyCFunction)PyDeleteKeyEx,
1680 METH_VARARGS | METH_KEYWORDS, DeleteKeyEx_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 {"DeleteValue", PyDeleteValue, METH_VARARGS, DeleteValue_doc},
1682 {"DisableReflectionKey", PyDisableReflectionKey, METH_VARARGS, DisableReflectionKey_doc},
1683 {"EnableReflectionKey", PyEnableReflectionKey, METH_VARARGS, EnableReflectionKey_doc},
1684 {"EnumKey", PyEnumKey, METH_VARARGS, EnumKey_doc},
1685 {"EnumValue", PyEnumValue, METH_VARARGS, EnumValue_doc},
1686 {"ExpandEnvironmentStrings", PyExpandEnvironmentStrings, METH_VARARGS,
1687 ExpandEnvironmentStrings_doc },
1688 {"FlushKey", PyFlushKey, METH_VARARGS, FlushKey_doc},
1689 {"LoadKey", PyLoadKey, METH_VARARGS, LoadKey_doc},
Brian Curtin1771b542010-09-27 17:56:36 +00001690 {"OpenKey", (PyCFunction)PyOpenKey, METH_VARARGS | METH_KEYWORDS,
1691 OpenKey_doc},
1692 {"OpenKeyEx", (PyCFunction)PyOpenKey, METH_VARARGS | METH_KEYWORDS,
1693 OpenKeyEx_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 {"QueryValue", PyQueryValue, METH_VARARGS, QueryValue_doc},
1695 {"QueryValueEx", PyQueryValueEx, METH_VARARGS, QueryValueEx_doc},
1696 {"QueryInfoKey", PyQueryInfoKey, METH_VARARGS, QueryInfoKey_doc},
1697 {"QueryReflectionKey",PyQueryReflectionKey,METH_VARARGS, QueryReflectionKey_doc},
1698 {"SaveKey", PySaveKey, METH_VARARGS, SaveKey_doc},
1699 {"SetValue", PySetValue, METH_VARARGS, SetValue_doc},
1700 {"SetValueEx", PySetValueEx, METH_VARARGS, SetValueEx_doc},
1701 NULL,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001702};
1703
1704static void
1705insint(PyObject * d, char * name, long value)
1706{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 PyObject *v = PyLong_FromLong(value);
1708 if (!v || PyDict_SetItemString(d, name, v))
1709 PyErr_Clear();
1710 Py_XDECREF(v);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001711}
1712
1713#define ADD_INT(val) insint(d, #val, val)
1714
1715static void
1716inskey(PyObject * d, char * name, HKEY key)
1717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 PyObject *v = PyLong_FromVoidPtr(key);
1719 if (!v || PyDict_SetItemString(d, name, v))
1720 PyErr_Clear();
1721 Py_XDECREF(v);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001722}
1723
1724#define ADD_KEY(val) inskey(d, #val, val)
1725
Martin v. Löwis1a214512008-06-11 05:26:20 +00001726
1727static struct PyModuleDef winregmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 PyModuleDef_HEAD_INIT,
1729 "winreg",
1730 module_doc,
1731 -1,
1732 winreg_methods,
1733 NULL,
1734 NULL,
1735 NULL,
1736 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001737};
1738
1739PyMODINIT_FUNC PyInit_winreg(void)
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001740{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 PyObject *m, *d;
1742 m = PyModule_Create(&winregmodule);
1743 if (m == NULL)
1744 return NULL;
1745 d = PyModule_GetDict(m);
1746 PyHKEY_Type.tp_doc = PyHKEY_doc;
1747 if (PyType_Ready(&PyHKEY_Type) < 0)
1748 return NULL;
1749 Py_INCREF(&PyHKEY_Type);
1750 if (PyDict_SetItemString(d, "HKEYType",
1751 (PyObject *)&PyHKEY_Type) != 0)
1752 return NULL;
1753 Py_INCREF(PyExc_WindowsError);
1754 if (PyDict_SetItemString(d, "error",
1755 PyExc_WindowsError) != 0)
1756 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 /* Add the relevant constants */
1759 ADD_KEY(HKEY_CLASSES_ROOT);
1760 ADD_KEY(HKEY_CURRENT_USER);
1761 ADD_KEY(HKEY_LOCAL_MACHINE);
1762 ADD_KEY(HKEY_USERS);
1763 ADD_KEY(HKEY_PERFORMANCE_DATA);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001764#ifdef HKEY_CURRENT_CONFIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 ADD_KEY(HKEY_CURRENT_CONFIG);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001766#endif
1767#ifdef HKEY_DYN_DATA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 ADD_KEY(HKEY_DYN_DATA);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001769#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 ADD_INT(KEY_QUERY_VALUE);
1771 ADD_INT(KEY_SET_VALUE);
1772 ADD_INT(KEY_CREATE_SUB_KEY);
1773 ADD_INT(KEY_ENUMERATE_SUB_KEYS);
1774 ADD_INT(KEY_NOTIFY);
1775 ADD_INT(KEY_CREATE_LINK);
1776 ADD_INT(KEY_READ);
1777 ADD_INT(KEY_WRITE);
1778 ADD_INT(KEY_EXECUTE);
1779 ADD_INT(KEY_ALL_ACCESS);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001780#ifdef KEY_WOW64_64KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 ADD_INT(KEY_WOW64_64KEY);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001782#endif
1783#ifdef KEY_WOW64_32KEY
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 ADD_INT(KEY_WOW64_32KEY);
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001785#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 ADD_INT(REG_OPTION_RESERVED);
1787 ADD_INT(REG_OPTION_NON_VOLATILE);
1788 ADD_INT(REG_OPTION_VOLATILE);
1789 ADD_INT(REG_OPTION_CREATE_LINK);
1790 ADD_INT(REG_OPTION_BACKUP_RESTORE);
1791 ADD_INT(REG_OPTION_OPEN_LINK);
1792 ADD_INT(REG_LEGAL_OPTION);
1793 ADD_INT(REG_CREATED_NEW_KEY);
1794 ADD_INT(REG_OPENED_EXISTING_KEY);
1795 ADD_INT(REG_WHOLE_HIVE_VOLATILE);
1796 ADD_INT(REG_REFRESH_HIVE);
1797 ADD_INT(REG_NO_LAZY_FLUSH);
1798 ADD_INT(REG_NOTIFY_CHANGE_NAME);
1799 ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES);
1800 ADD_INT(REG_NOTIFY_CHANGE_LAST_SET);
1801 ADD_INT(REG_NOTIFY_CHANGE_SECURITY);
1802 ADD_INT(REG_LEGAL_CHANGE_FILTER);
1803 ADD_INT(REG_NONE);
1804 ADD_INT(REG_SZ);
1805 ADD_INT(REG_EXPAND_SZ);
1806 ADD_INT(REG_BINARY);
1807 ADD_INT(REG_DWORD);
1808 ADD_INT(REG_DWORD_LITTLE_ENDIAN);
1809 ADD_INT(REG_DWORD_BIG_ENDIAN);
1810 ADD_INT(REG_LINK);
1811 ADD_INT(REG_MULTI_SZ);
1812 ADD_INT(REG_RESOURCE_LIST);
1813 ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
1814 ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
1815 return m;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001816}
1817
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001818