blob: 293f1dddcadffb2d17778888473906c7a69f31ad [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 Pitrouc7c96a92010-05-09 15:15:40 +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.
Jesus Cea585ad8a2009-07-02 15:37:21 +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 Pitrouc7c96a92010-05-09 15:15:40 +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"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000092"If the function fails, an EnvironmentError 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
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000107PyDoc_STRVAR(DeleteKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000108"DeleteKey(key, sub_key) - Deletes the specified key.\n"
109"\n"
110"key is an already open key, or any one of the predefined HKEY_* constants.\n"
111"sub_key is a string that must be a subkey of the key identified by the key parameter.\n"
112" This value must not be None, and the key may not have subkeys.\n"
113"\n"
114"This method can not delete keys with subkeys.\n"
115"\n"
116"If the method succeeds, the entire key, including all of its values,\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000117"is removed. If the method fails, an EnvironmentError exception is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000118
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000119PyDoc_STRVAR(DeleteValue_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000120"DeleteValue(key, value) - Removes a named value from a registry key.\n"
121"\n"
122"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000123"value is a string that identifies the value to remove.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000124
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000125PyDoc_STRVAR(EnumKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000126"string = EnumKey(key, index) - Enumerates subkeys of an open registry key.\n"
127"\n"
128"key is an already open key, or any one of the predefined HKEY_* constants.\n"
129"index is an integer that identifies the index of the key to retrieve.\n"
130"\n"
131"The function retrieves the name of one subkey each time it is called.\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000132"It is typically called repeatedly until an EnvironmentError exception is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000133"raised, indicating no more values are available.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000134
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000135PyDoc_STRVAR(EnumValue_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000136"tuple = EnumValue(key, index) - Enumerates values of an open registry key.\n"
137"key is an already open key, or any one of the predefined HKEY_* constants.\n"
138"index is an integer that identifies the index of the value to retrieve.\n"
139"\n"
140"The function retrieves the name of one subkey each time it is called.\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000141"It is typically called repeatedly, until an EnvironmentError exception\n"
142"is raised, indicating no more values.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000143"\n"
144"The result is a tuple of 3 items:\n"
145"value_name is a string that identifies the value.\n"
146"value_data is an object that holds the value data, and whose type depends\n"
147" on the underlying registry type.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000148"data_type is an integer that identifies the type of the value data.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000149
Christian Heimesb39a7562008-01-08 15:46:10 +0000150PyDoc_STRVAR(ExpandEnvironmentStrings_doc,
151"string = ExpandEnvironmentStrings(string) - Expand environment vars.\n");
152
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000153PyDoc_STRVAR(FlushKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000154"FlushKey(key) - Writes all the attributes of a key to the registry.\n"
155"\n"
156"key is an already open key, or any one of the predefined HKEY_* constants.\n"
157"\n"
158"It is not necessary to call RegFlushKey to change a key.\n"
159"Registry changes are flushed to disk by the registry using its lazy flusher.\n"
160"Registry changes are also flushed to disk at system shutdown.\n"
161"Unlike CloseKey(), the FlushKey() method returns only when all the data has\n"
162"been written to the registry.\n"
163"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 +0000164"If you don't know whether a FlushKey() call is required, it probably isn't.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000165
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000166PyDoc_STRVAR(LoadKey_doc,
Mark Hammondb422f952000-06-09 06:01:47 +0000167"LoadKey(key, sub_key, file_name) - Creates a subkey under the specified key\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000168"and stores registration information from a specified file into that subkey.\n"
169"\n"
170"key is an already open key, or any one of the predefined HKEY_* constants.\n"
171"sub_key is a string that identifies the sub_key to load\n"
172"file_name is the name of the file to load registry data from.\n"
173" This file must have been created with the SaveKey() function.\n"
174" Under the file allocation table (FAT) file system, the filename may not\n"
175"have an extension.\n"
176"\n"
177"A call to LoadKey() fails if the calling process does not have the\n"
178"SE_RESTORE_PRIVILEGE privilege.\n"
179"\n"
180"If key is a handle returned by ConnectRegistry(), then the path specified\n"
181"in fileName is relative to the remote computer.\n"
182"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000183"The docs imply key must be in the HKEY_USER or HKEY_LOCAL_MACHINE tree");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000184
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000185PyDoc_STRVAR(OpenKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000186"key = OpenKey(key, sub_key, res = 0, sam = KEY_READ) - Opens the specified key.\n"
187"\n"
188"key is an already open key, or any one of the predefined HKEY_* constants.\n"
189"sub_key is a string that identifies the sub_key to open\n"
190"res is a reserved integer, and must be zero. Default is zero.\n"
191"sam is an integer that specifies an access mask that describes the desired\n"
192" security access for the key. Default is KEY_READ\n"
193"\n"
194"The result is a new handle to the specified key\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000195"If the function fails, an EnvironmentError exception is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000196
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000197PyDoc_STRVAR(OpenKeyEx_doc, "See OpenKey()");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000198
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000199PyDoc_STRVAR(QueryInfoKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000200"tuple = QueryInfoKey(key) - Returns information about a key.\n"
201"\n"
202"key is an already open key, or any one of the predefined HKEY_* constants.\n"
203"\n"
204"The result is a tuple of 3 items:"
205"An integer that identifies the number of sub keys this key has.\n"
206"An integer that identifies the number of values this key has.\n"
207"A long integer that identifies when the key was last modified (if available)\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000208" as 100's of nanoseconds since Jan 1, 1600.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000209
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000210PyDoc_STRVAR(QueryValue_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000211"string = QueryValue(key, sub_key) - retrieves the unnamed value for a key.\n"
212"\n"
213"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000214"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 +0000215" is associated. If this parameter is None or empty, the function retrieves\n"
216" the value set by the SetValue() method for the key identified by key."
217"\n"
218"Values in the registry have name, type, and data components. This method\n"
219"retrieves the data for a key's first value that has a NULL name.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000220"But the underlying API call doesn't return the type, Lame Lame Lame, DONT USE THIS!!!");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000221
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000222PyDoc_STRVAR(QueryValueEx_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000223"value,type_id = QueryValueEx(key, value_name) - Retrieves the type and data for a specified value name associated with an open registry key.\n"
224"\n"
225"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000226"value_name is a string indicating the value to query");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000227
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000228PyDoc_STRVAR(SaveKey_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000229"SaveKey(key, file_name) - Saves the specified key, and all its subkeys to the specified file.\n"
230"\n"
231"key is an already open key, or any one of the predefined HKEY_* constants.\n"
232"file_name is the name of the file to save registry data to.\n"
233" This file cannot already exist. If this filename includes an extension,\n"
234" it cannot be used on file allocation table (FAT) file systems by the\n"
235" LoadKey(), ReplaceKey() or RestoreKey() methods.\n"
236"\n"
237"If key represents a key on a remote computer, the path described by\n"
238"file_name is relative to the remote computer.\n"
239"The caller of this method must possess the SeBackupPrivilege security privilege.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000240"This function passes NULL for security_attributes to the API.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000241
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000242PyDoc_STRVAR(SetValue_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000243"SetValue(key, sub_key, type, value) - Associates a value with a specified key.\n"
244"\n"
245"key is an already open key, or any one of the predefined HKEY_* constants.\n"
246"sub_key is a string that names the subkey with which the value is associated.\n"
247"type is an integer that specifies the type of the data. Currently this\n"
248" must be REG_SZ, meaning only strings are supported.\n"
249"value is a string that specifies the new value.\n"
250"\n"
251"If the key specified by the sub_key parameter does not exist, the SetValue\n"
252"function creates it.\n"
253"\n"
254"Value lengths are limited by available memory. Long values (more than\n"
255"2048 bytes) should be stored as files with the filenames stored in \n"
256"the configuration registry. This helps the registry perform efficiently.\n"
257"\n"
258"The key identified by the key parameter must have been opened with\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000259"KEY_SET_VALUE access.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000260
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000261PyDoc_STRVAR(SetValueEx_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000262"SetValueEx(key, value_name, reserved, type, value) - Stores data in the value field of an open registry key.\n"
263"\n"
264"key is an already open key, or any one of the predefined HKEY_* constants.\n"
Mark Hammondc9083b62003-01-15 23:38:15 +0000265"value_name is a string containing the name of the value to set, or None\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000266"type is an integer that specifies the type of the data. This should be one of:\n"
267" REG_BINARY -- Binary data in any form.\n"
268" REG_DWORD -- A 32-bit number.\n"
269" REG_DWORD_LITTLE_ENDIAN -- A 32-bit number in little-endian format.\n"
270" REG_DWORD_BIG_ENDIAN -- A 32-bit number in big-endian format.\n"
271" REG_EXPAND_SZ -- A null-terminated string that contains unexpanded references\n"
272" to environment variables (for example, %PATH%).\n"
273" REG_LINK -- A Unicode symbolic link.\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000274" REG_MULTI_SZ -- An sequence of null-terminated strings, terminated by\n"
275" two null characters. Note that Python handles this\n"
276" termination automatically.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000277" REG_NONE -- No defined value type.\n"
278" REG_RESOURCE_LIST -- A device-driver resource list.\n"
279" REG_SZ -- A null-terminated string.\n"
280"reserved can be anything - zero is always passed to the API.\n"
281"value is a string that specifies the new value.\n"
282"\n"
283"This method can also set additional value and type information for the\n"
284"specified key. The key identified by the key parameter must have been\n"
285"opened with KEY_SET_VALUE access.\n"
286"\n"
287"To open the key, use the CreateKeyEx() or OpenKeyEx() methods.\n"
288"\n"
289"Value lengths are limited by available memory. Long values (more than\n"
290"2048 bytes) should be stored as files with the filenames stored in \n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000291"the configuration registry. This helps the registry perform efficiently.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000292
Mark Hammond8a3c8712008-04-06 01:42:06 +0000293PyDoc_STRVAR(DisableReflectionKey_doc,
Andrew M. Kuchlingeba00042008-04-08 01:33:10 +0000294"Disables registry reflection for 32-bit processes running on a 64-bit\n"
Mark Hammond8a3c8712008-04-06 01:42:06 +0000295"Operating System. Will generally raise NotImplemented if executed on\n"
Andrew M. Kuchlingeba00042008-04-08 01:33:10 +0000296"a 32-bit Operating System.\n"
Mark Hammond8a3c8712008-04-06 01:42:06 +0000297"If the key is not on the reflection list, the function succeeds but has no effect.\n"
298"Disabling reflection for a key does not affect reflection of any subkeys.");
299
300PyDoc_STRVAR(EnableReflectionKey_doc,
301"Restores registry reflection for the specified disabled key.\n"
Andrew M. Kuchlingeba00042008-04-08 01:33:10 +0000302"Will generally raise NotImplemented if executed on a 32-bit Operating System.\n"
Mark Hammond8a3c8712008-04-06 01:42:06 +0000303"Restoring reflection for a key does not affect reflection of any subkeys.");
304
305PyDoc_STRVAR(QueryReflectionKey_doc,
306"bool = QueryReflectionKey(hkey) - Determines the reflection state for the specified key.\n"
Andrew M. Kuchlingeba00042008-04-08 01:33:10 +0000307"Will generally raise NotImplemented if executed on a 32-bit Operating System.\n");
Mark Hammond8a3c8712008-04-06 01:42:06 +0000308
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000309/* PyHKEY docstrings */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000310PyDoc_STRVAR(PyHKEY_doc,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000311"PyHKEY Object - A Python object, representing a win32 registry key.\n"
312"\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000313"This object wraps a Windows HKEY object, automatically closing it when\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000314"the object is destroyed. To guarantee cleanup, you can call either\n"
315"the Close() method on the PyHKEY, or the CloseKey() method.\n"
316"\n"
317"All functions which accept a handle object also accept an integer - \n"
318"however, use of the handle object is encouraged.\n"
319"\n"
320"Functions:\n"
321"Close() - Closes the underlying handle.\n"
322"Detach() - Returns the integer Win32 handle, detaching it from the object\n"
323"\n"
324"Properties:\n"
325"handle - The integer Win32 handle.\n"
326"\n"
327"Operations:\n"
328"__nonzero__ - Handles with an open object return true, otherwise false.\n"
329"__int__ - Converting a handle to an integer returns the Win32 handle.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000330"__cmp__ - Handle objects are compared using the handle value.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000331
332
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000333PyDoc_STRVAR(PyHKEY_Close_doc,
Mark Hammondb422f952000-06-09 06:01:47 +0000334"key.Close() - Closes the underlying Windows handle.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000335"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000336"If the handle is already closed, no error is raised.");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000337
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000338PyDoc_STRVAR(PyHKEY_Detach_doc,
Mark Hammondb422f952000-06-09 06:01:47 +0000339"int = key.Detach() - Detaches the Windows handle from the handle object.\n"
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000340"\n"
341"The result is the value of the handle before it is detached. If the\n"
342"handle is already detached, this will return zero.\n"
343"\n"
344"After calling this function, the handle is effectively invalidated,\n"
345"but the handle is not closed. You would call this function when you\n"
346"need the underlying win32 handle to exist beyond the lifetime of the\n"
Mark Hammondb422f952000-06-09 06:01:47 +0000347"handle object.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000348"On 64 bit windows, the result of this function is a long integer");
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000349
350
351/************************************************************************
352
353 The PyHKEY object definition
354
355************************************************************************/
356typedef struct {
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000357 PyObject_VAR_HEAD
358 HKEY hkey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000359} PyHKEYObject;
360
361#define PyHKEY_Check(op) ((op)->ob_type == &PyHKEY_Type)
362
363static char *failMsg = "bad operand type";
364
365static PyObject *
366PyHKEY_unaryFailureFunc(PyObject *ob)
367{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000368 PyErr_SetString(PyExc_TypeError, failMsg);
369 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000370}
371static PyObject *
372PyHKEY_binaryFailureFunc(PyObject *ob1, PyObject *ob2)
373{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000374 PyErr_SetString(PyExc_TypeError, failMsg);
375 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000376}
377static PyObject *
378PyHKEY_ternaryFailureFunc(PyObject *ob1, PyObject *ob2, PyObject *ob3)
379{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000380 PyErr_SetString(PyExc_TypeError, failMsg);
381 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000382}
383
384static void
385PyHKEY_deallocFunc(PyObject *ob)
386{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000387 /* Can not call PyHKEY_Close, as the ob->tp_type
388 has already been cleared, thus causing the type
389 check to fail!
390 */
391 PyHKEYObject *obkey = (PyHKEYObject *)ob;
392 if (obkey->hkey)
393 RegCloseKey((HKEY)obkey->hkey);
394 PyObject_DEL(ob);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000395}
396
397static int
398PyHKEY_nonzeroFunc(PyObject *ob)
399{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000400 return ((PyHKEYObject *)ob)->hkey != 0;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000401}
402
403static PyObject *
404PyHKEY_intFunc(PyObject *ob)
405{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000406 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
407 return PyLong_FromVoidPtr(pyhkey->hkey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000408}
409
410static int
411PyHKEY_printFunc(PyObject *ob, FILE *fp, int flags)
412{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000413 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
414 char resBuf[160];
415 wsprintf(resBuf, "<PyHKEY at %p (%p)>",
416 ob, pyhkey->hkey);
417 fputs(resBuf, fp);
418 return 0;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000419}
420
421static PyObject *
422PyHKEY_strFunc(PyObject *ob)
423{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000424 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
425 char resBuf[160];
426 wsprintf(resBuf, "<PyHKEY:%p>", pyhkey->hkey);
427 return PyString_FromString(resBuf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000428}
429
430static int
431PyHKEY_compareFunc(PyObject *ob1, PyObject *ob2)
432{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000433 PyHKEYObject *pyhkey1 = (PyHKEYObject *)ob1;
434 PyHKEYObject *pyhkey2 = (PyHKEYObject *)ob2;
435 return pyhkey1 == pyhkey2 ? 0 :
436 (pyhkey1 < pyhkey2 ? -1 : 1);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000437}
438
439static long
440PyHKEY_hashFunc(PyObject *ob)
441{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000442 /* Just use the address.
443 XXX - should we use the handle value?
444 */
445 return _Py_HashPointer(ob);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000446}
447
448
449static PyNumberMethods PyHKEY_NumberMethods =
450{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000451 PyHKEY_binaryFailureFunc, /* nb_add */
452 PyHKEY_binaryFailureFunc, /* nb_subtract */
453 PyHKEY_binaryFailureFunc, /* nb_multiply */
454 PyHKEY_binaryFailureFunc, /* nb_divide */
455 PyHKEY_binaryFailureFunc, /* nb_remainder */
456 PyHKEY_binaryFailureFunc, /* nb_divmod */
457 PyHKEY_ternaryFailureFunc, /* nb_power */
458 PyHKEY_unaryFailureFunc, /* nb_negative */
459 PyHKEY_unaryFailureFunc, /* nb_positive */
460 PyHKEY_unaryFailureFunc, /* nb_absolute */
461 PyHKEY_nonzeroFunc, /* nb_nonzero */
462 PyHKEY_unaryFailureFunc, /* nb_invert */
463 PyHKEY_binaryFailureFunc, /* nb_lshift */
464 PyHKEY_binaryFailureFunc, /* nb_rshift */
465 PyHKEY_binaryFailureFunc, /* nb_and */
466 PyHKEY_binaryFailureFunc, /* nb_xor */
467 PyHKEY_binaryFailureFunc, /* nb_or */
468 0, /* nb_coerce (allowed to be zero) */
469 PyHKEY_intFunc, /* nb_int */
470 PyHKEY_unaryFailureFunc, /* nb_long */
471 PyHKEY_unaryFailureFunc, /* nb_float */
472 PyHKEY_unaryFailureFunc, /* nb_oct */
473 PyHKEY_unaryFailureFunc, /* nb_hex */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000474};
475
476
477/* fwd declare __getattr__ */
Tim Petersc3d12ac2005-12-24 06:03:06 +0000478static PyObject *PyHKEY_getattr(PyObject *self, const char *name);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000479
480/* The type itself */
481PyTypeObject PyHKEY_Type =
482{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000483 PyVarObject_HEAD_INIT(0, 0) /* fill in type at module init */
484 "PyHKEY",
485 sizeof(PyHKEYObject),
486 0,
487 PyHKEY_deallocFunc, /* tp_dealloc */
488 PyHKEY_printFunc, /* tp_print */
489 PyHKEY_getattr, /* tp_getattr */
490 0, /* tp_setattr */
491 PyHKEY_compareFunc, /* tp_compare */
492 0, /* tp_repr */
493 &PyHKEY_NumberMethods, /* tp_as_number */
494 0, /* tp_as_sequence */
495 0, /* tp_as_mapping */
496 PyHKEY_hashFunc, /* tp_hash */
497 0, /* tp_call */
498 PyHKEY_strFunc, /* tp_str */
499 0, /* tp_getattro */
500 0, /* tp_setattro */
501 0, /* tp_as_buffer */
502 0, /* tp_flags */
503 PyHKEY_doc, /* tp_doc */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000504};
505
506#define OFF(e) offsetof(PyHKEYObject, e)
507
508static struct memberlist PyHKEY_memberlist[] = {
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000509 {"handle", T_INT, OFF(hkey)},
510 {NULL} /* Sentinel */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000511};
512
513/************************************************************************
514
515 The PyHKEY object methods
516
517************************************************************************/
518static PyObject *
519PyHKEY_CloseMethod(PyObject *self, PyObject *args)
520{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000521 if (!PyArg_ParseTuple(args, ":Close"))
522 return NULL;
523 if (!PyHKEY_Close(self))
524 return NULL;
525 Py_INCREF(Py_None);
526 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000527}
528
529static PyObject *
530PyHKEY_DetachMethod(PyObject *self, PyObject *args)
531{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000532 void* ret;
533 PyHKEYObject *pThis = (PyHKEYObject *)self;
534 if (!PyArg_ParseTuple(args, ":Detach"))
535 return NULL;
536 ret = (void*)pThis->hkey;
537 pThis->hkey = 0;
538 return PyLong_FromVoidPtr(ret);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000539}
540
Christian Heimesb39a7562008-01-08 15:46:10 +0000541static PyObject *
542PyHKEY_Enter(PyObject *self)
543{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000544 Py_XINCREF(self);
545 return self;
Christian Heimesb39a7562008-01-08 15:46:10 +0000546}
547
548static PyObject *
549PyHKEY_Exit(PyObject *self, PyObject *args)
550{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000551 if (!PyHKEY_Close(self))
552 return NULL;
553 Py_RETURN_NONE;
Christian Heimesb39a7562008-01-08 15:46:10 +0000554}
555
556
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000557static struct PyMethodDef PyHKEY_methods[] = {
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000558 {"Close", PyHKEY_CloseMethod, METH_VARARGS, PyHKEY_Close_doc},
559 {"Detach", PyHKEY_DetachMethod, METH_VARARGS, PyHKEY_Detach_doc},
560 {"__enter__", (PyCFunction)PyHKEY_Enter, METH_NOARGS, NULL},
561 {"__exit__", PyHKEY_Exit, METH_VARARGS, NULL},
562 {NULL}
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000563};
564
565/*static*/ PyObject *
Tim Petersc3d12ac2005-12-24 06:03:06 +0000566PyHKEY_getattr(PyObject *self, const char *name)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000567{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000568 PyObject *res;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000569
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000570 res = Py_FindMethod(PyHKEY_methods, self, name);
571 if (res != NULL)
572 return res;
573 PyErr_Clear();
574 if (strcmp(name, "handle") == 0)
575 return PyLong_FromVoidPtr(((PyHKEYObject *)self)->hkey);
576 return PyMember_Get((char *)self, PyHKEY_memberlist, name);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000577}
578
579/************************************************************************
580 The public PyHKEY API (well, not public yet :-)
581************************************************************************/
582PyObject *
583PyHKEY_New(HKEY hInit)
584{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000585 PyHKEYObject *key = PyObject_NEW(PyHKEYObject, &PyHKEY_Type);
586 if (key)
587 key->hkey = hInit;
588 return (PyObject *)key;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000589}
590
591BOOL
592PyHKEY_Close(PyObject *ob_handle)
593{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000594 LONG rc;
595 PyHKEYObject *key;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000596
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000597 if (!PyHKEY_Check(ob_handle)) {
598 PyErr_SetString(PyExc_TypeError, "bad operand type");
599 return FALSE;
600 }
601 key = (PyHKEYObject *)ob_handle;
602 rc = key->hkey ? RegCloseKey((HKEY)key->hkey) : ERROR_SUCCESS;
603 key->hkey = 0;
604 if (rc != ERROR_SUCCESS)
605 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
606 return rc == ERROR_SUCCESS;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000607}
608
609BOOL
610PyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK)
611{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000612 if (ob == Py_None) {
613 if (!bNoneOK) {
614 PyErr_SetString(
615 PyExc_TypeError,
616 "None is not a valid HKEY in this context");
617 return FALSE;
618 }
619 *pHANDLE = (HKEY)0;
620 }
621 else if (PyHKEY_Check(ob)) {
622 PyHKEYObject *pH = (PyHKEYObject *)ob;
623 *pHANDLE = pH->hkey;
624 }
625 else if (PyInt_Check(ob) || PyLong_Check(ob)) {
626 /* We also support integers */
627 PyErr_Clear();
628 *pHANDLE = (HKEY)PyLong_AsVoidPtr(ob);
629 if (PyErr_Occurred())
630 return FALSE;
631 }
632 else {
633 PyErr_SetString(
634 PyExc_TypeError,
635 "The object is not a PyHKEY object");
636 return FALSE;
637 }
638 return TRUE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000639}
640
641PyObject *
642PyHKEY_FromHKEY(HKEY h)
643{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000644 PyHKEYObject *op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000645
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000646 /* Inline PyObject_New */
647 op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject));
648 if (op == NULL)
649 return PyErr_NoMemory();
650 PyObject_INIT(op, &PyHKEY_Type);
651 op->hkey = h;
652 return (PyObject *)op;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000653}
654
655
656/************************************************************************
657 The module methods
658************************************************************************/
659BOOL
660PyWinObject_CloseHKEY(PyObject *obHandle)
661{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000662 BOOL ok;
663 if (PyHKEY_Check(obHandle)) {
664 ok = PyHKEY_Close(obHandle);
665 }
Fred Drake25e17262000-06-30 17:48:51 +0000666#if SIZEOF_LONG >= SIZEOF_HKEY
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000667 else if (PyInt_Check(obHandle)) {
668 long rc = RegCloseKey((HKEY)PyInt_AsLong(obHandle));
669 ok = (rc == ERROR_SUCCESS);
670 if (!ok)
671 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
672 }
Fred Drake25e17262000-06-30 17:48:51 +0000673#else
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000674 else if (PyLong_Check(obHandle)) {
675 long rc = RegCloseKey((HKEY)PyLong_AsVoidPtr(obHandle));
676 ok = (rc == ERROR_SUCCESS);
677 if (!ok)
678 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
679 }
Fred Drake25e17262000-06-30 17:48:51 +0000680#endif
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000681 else {
682 PyErr_SetString(
683 PyExc_TypeError,
684 "A handle must be a HKEY object or an integer");
685 return FALSE;
686 }
687 return ok;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000688}
689
690
691/*
692 Private Helper functions for the registry interfaces
693
694** Note that fixupMultiSZ and countString have both had changes
695** made to support "incorrect strings". The registry specification
696** calls for strings to be terminated with 2 null bytes. It seems
Jesus Cea585ad8a2009-07-02 15:37:21 +0000697** some commercial packages install strings which don't conform,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000698** causing this code to fail - however, "regedit" etc still work
Jesus Cea585ad8a2009-07-02 15:37:21 +0000699** with these strings (ie only we don't!).
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000700*/
701static void
702fixupMultiSZ(char **str, char *data, int len)
703{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000704 char *P;
705 int i;
706 char *Q;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000707
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000708 Q = data + len;
709 for (P = data, i = 0; P < Q && *P != '\0'; P++, i++) {
710 str[i] = P;
711 for(; *P != '\0'; P++)
712 ;
713 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000714}
715
716static int
717countStrings(char *data, int len)
718{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000719 int strings;
720 char *P;
721 char *Q = data + len;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000722
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000723 for (P = data, strings = 0; P < Q && *P != '\0'; P++, strings++)
724 for (; P < Q && *P != '\0'; P++)
725 ;
726 return strings;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000727}
728
729/* Convert PyObject into Registry data.
730 Allocates space as needed. */
731static BOOL
732Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
733{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000734 Py_ssize_t i,j;
735 switch (typ) {
736 case REG_DWORD:
737 if (value != Py_None && !PyInt_Check(value))
738 return FALSE;
739 *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1);
740 if (*retDataBuf==NULL){
741 PyErr_NoMemory();
742 return FALSE;
743 }
744 *retDataSize = sizeof(DWORD);
745 if (value == Py_None) {
746 DWORD zero = 0;
747 memcpy(*retDataBuf, &zero, sizeof(DWORD));
748 }
749 else
750 memcpy(*retDataBuf,
751 &PyInt_AS_LONG((PyIntObject *)value),
752 sizeof(DWORD));
753 break;
754 case REG_SZ:
755 case REG_EXPAND_SZ:
756 {
757 int need_decref = 0;
758 if (value == Py_None)
759 *retDataSize = 1;
760 else {
761 if (PyUnicode_Check(value)) {
762 value = PyUnicode_AsEncodedString(
763 value,
764 "mbcs",
765 NULL);
766 if (value==NULL)
767 return FALSE;
768 need_decref = 1;
769 }
770 if (!PyString_Check(value))
771 return FALSE;
772 *retDataSize = 1 + strlen(
773 PyString_AS_STRING(
774 (PyStringObject *)value));
775 }
776 *retDataBuf = (BYTE *)PyMem_NEW(DWORD, *retDataSize);
777 if (*retDataBuf==NULL){
778 PyErr_NoMemory();
779 return FALSE;
780 }
781 if (value == Py_None)
782 strcpy((char *)*retDataBuf, "");
783 else
784 strcpy((char *)*retDataBuf,
785 PyString_AS_STRING(
786 (PyStringObject *)value));
787 if (need_decref)
788 Py_DECREF(value);
789 break;
790 }
791 case REG_MULTI_SZ:
792 {
793 DWORD size = 0;
794 char *P;
795 PyObject **obs = NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000796
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000797 if (value == Py_None)
798 i = 0;
799 else {
800 if (!PyList_Check(value))
801 return FALSE;
802 i = PyList_Size(value);
803 }
804 obs = malloc(sizeof(PyObject *) * i);
805 memset(obs, 0, sizeof(PyObject *) * i);
806 for (j = 0; j < i; j++)
807 {
808 PyObject *t;
809 t = PyList_GET_ITEM(
810 (PyListObject *)value,j);
811 if (PyString_Check(t)) {
812 obs[j] = t;
813 Py_INCREF(t);
814 } else if (PyUnicode_Check(t)) {
815 obs[j] = PyUnicode_AsEncodedString(
816 t,
817 "mbcs",
818 NULL);
819 if (obs[j]==NULL)
820 goto reg_multi_fail;
821 } else
822 goto reg_multi_fail;
823 size += 1 + strlen(
824 PyString_AS_STRING(
825 (PyStringObject *)obs[j]));
826 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000827
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000828 *retDataSize = size + 1;
829 *retDataBuf = (BYTE *)PyMem_NEW(char,
830 *retDataSize);
831 if (*retDataBuf==NULL){
832 PyErr_NoMemory();
833 goto reg_multi_fail;
834 }
835 P = (char *)*retDataBuf;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000836
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000837 for (j = 0; j < i; j++)
838 {
839 PyObject *t;
840 t = obs[j];
841 strcpy(P,
842 PyString_AS_STRING(
843 (PyStringObject *)t));
844 P += 1 + strlen(
845 PyString_AS_STRING(
846 (PyStringObject *)t));
847 Py_DECREF(obs[j]);
848 }
849 /* And doubly-terminate the list... */
850 *P = '\0';
851 free(obs);
852 break;
853 reg_multi_fail:
854 if (obs) {
855 for (j = 0; j < i; j++)
856 Py_XDECREF(obs[j]);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000857
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000858 free(obs);
859 }
860 return FALSE;
861 }
862 case REG_BINARY:
863 /* ALSO handle ALL unknown data types here. Even if we can't
864 support it natively, we should handle the bits. */
865 default:
866 if (value == Py_None)
867 *retDataSize = 0;
868 else {
869 void *src_buf;
870 PyBufferProcs *pb = value->ob_type->tp_as_buffer;
871 if (pb==NULL) {
872 PyErr_Format(PyExc_TypeError,
873 "Objects of type '%s' can not "
874 "be used as binary registry values",
875 value->ob_type->tp_name);
876 return FALSE;
877 }
878 *retDataSize = (*pb->bf_getreadbuffer)(value, 0, &src_buf);
879 *retDataBuf = (BYTE *)PyMem_NEW(char,
880 *retDataSize);
881 if (*retDataBuf==NULL){
882 PyErr_NoMemory();
883 return FALSE;
884 }
885 memcpy(*retDataBuf, src_buf, *retDataSize);
886 }
887 break;
888 }
889 return TRUE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000890}
891
892/* Convert Registry data into PyObject*/
893static PyObject *
894Reg2Py(char *retDataBuf, DWORD retDataSize, DWORD typ)
895{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000896 PyObject *obData;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000897
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000898 switch (typ) {
899 case REG_DWORD:
900 if (retDataSize == 0)
901 obData = Py_BuildValue("i", 0);
902 else
903 obData = Py_BuildValue("i",
904 *(int *)retDataBuf);
905 break;
906 case REG_SZ:
907 case REG_EXPAND_SZ:
908 /* retDataBuf may or may not have a trailing NULL in
909 the buffer. */
910 if (retDataSize && retDataBuf[retDataSize-1] == '\0')
911 --retDataSize;
912 if (retDataSize ==0)
913 retDataBuf = "";
914 obData = PyUnicode_DecodeMBCS(retDataBuf,
915 retDataSize,
916 NULL);
917 break;
918 case REG_MULTI_SZ:
919 if (retDataSize == 0)
920 obData = PyList_New(0);
921 else
922 {
923 int index = 0;
924 int s = countStrings(retDataBuf, retDataSize);
925 char **str = (char **)malloc(sizeof(char *)*s);
926 if (str == NULL)
927 return PyErr_NoMemory();
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000928
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000929 fixupMultiSZ(str, retDataBuf, retDataSize);
930 obData = PyList_New(s);
931 if (obData == NULL)
932 return NULL;
933 for (index = 0; index < s; index++)
934 {
935 size_t len = _mbstrlen(str[index]);
936 if (len > INT_MAX) {
937 PyErr_SetString(PyExc_OverflowError,
938 "registry string is too long for a Python string");
939 Py_DECREF(obData);
940 return NULL;
941 }
942 PyList_SetItem(obData,
943 index,
944 PyUnicode_DecodeMBCS(
945 (const char *)str[index],
946 (int)len,
947 NULL)
948 );
949 }
950 free(str);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000951
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000952 break;
953 }
954 case REG_BINARY:
955 /* ALSO handle ALL unknown data types here. Even if we can't
956 support it natively, we should handle the bits. */
957 default:
958 if (retDataSize == 0) {
959 Py_INCREF(Py_None);
960 obData = Py_None;
961 }
962 else
963 obData = Py_BuildValue("s#",
964 (char *)retDataBuf,
965 retDataSize);
966 break;
967 }
968 if (obData == NULL)
969 return NULL;
970 else
971 return obData;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000972}
973
974/* The Python methods */
975
976static PyObject *
977PyCloseKey(PyObject *self, PyObject *args)
978{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000979 PyObject *obKey;
980 if (!PyArg_ParseTuple(args, "O:CloseKey", &obKey))
981 return NULL;
982 if (!PyHKEY_Close(obKey))
983 return NULL;
984 Py_INCREF(Py_None);
985 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000986}
987
988static PyObject *
989PyConnectRegistry(PyObject *self, PyObject *args)
990{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000991 HKEY hKey;
992 PyObject *obKey;
993 char *szCompName = NULL;
994 HKEY retKey;
995 long rc;
996 if (!PyArg_ParseTuple(args, "zO:ConnectRegistry", &szCompName, &obKey))
997 return NULL;
998 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
999 return NULL;
1000 Py_BEGIN_ALLOW_THREADS
1001 rc = RegConnectRegistry(szCompName, hKey, &retKey);
1002 Py_END_ALLOW_THREADS
1003 if (rc != ERROR_SUCCESS)
1004 return PyErr_SetFromWindowsErrWithFunction(rc,
1005 "ConnectRegistry");
1006 return PyHKEY_FromHKEY(retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001007}
1008
1009static PyObject *
1010PyCreateKey(PyObject *self, PyObject *args)
1011{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001012 HKEY hKey;
1013 PyObject *obKey;
1014 char *subKey;
1015 HKEY retKey;
1016 long rc;
1017 if (!PyArg_ParseTuple(args, "Oz:CreateKey", &obKey, &subKey))
1018 return NULL;
1019 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1020 return NULL;
1021 rc = RegCreateKey(hKey, subKey, &retKey);
1022 if (rc != ERROR_SUCCESS)
1023 return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey");
1024 return PyHKEY_FromHKEY(retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001025}
1026
1027static PyObject *
1028PyDeleteKey(PyObject *self, PyObject *args)
1029{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001030 HKEY hKey;
1031 PyObject *obKey;
1032 char *subKey;
1033 long rc;
1034 if (!PyArg_ParseTuple(args, "Os:DeleteKey", &obKey, &subKey))
1035 return NULL;
1036 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1037 return NULL;
1038 rc = RegDeleteKey(hKey, subKey );
1039 if (rc != ERROR_SUCCESS)
1040 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey");
1041 Py_INCREF(Py_None);
1042 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001043}
1044
1045static PyObject *
1046PyDeleteValue(PyObject *self, PyObject *args)
1047{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001048 HKEY hKey;
1049 PyObject *obKey;
1050 char *subKey;
1051 long rc;
1052 if (!PyArg_ParseTuple(args, "Oz:DeleteValue", &obKey, &subKey))
1053 return NULL;
1054 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1055 return NULL;
1056 Py_BEGIN_ALLOW_THREADS
1057 rc = RegDeleteValue(hKey, subKey);
1058 Py_END_ALLOW_THREADS
1059 if (rc !=ERROR_SUCCESS)
1060 return PyErr_SetFromWindowsErrWithFunction(rc,
1061 "RegDeleteValue");
1062 Py_INCREF(Py_None);
1063 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001064}
1065
1066static PyObject *
1067PyEnumKey(PyObject *self, PyObject *args)
1068{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001069 HKEY hKey;
1070 PyObject *obKey;
1071 int index;
1072 long rc;
1073 PyObject *retStr;
Brian Curtinb7b21f12010-05-26 13:24:57 +00001074
1075 /* The Windows docs claim that the max key name length is 255
1076 * characters, plus a terminating nul character. However,
1077 * empirical testing demonstrates that it is possible to
1078 * create a 256 character key that is missing the terminating
1079 * nul. RegEnumKeyEx requires a 257 character buffer to
1080 * retrieve such a key name. */
1081 char tmpbuf[257];
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001082 DWORD len = sizeof(tmpbuf); /* includes NULL terminator */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001083
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001084 if (!PyArg_ParseTuple(args, "Oi:EnumKey", &obKey, &index))
1085 return NULL;
1086 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1087 return NULL;
Tim Peters313fcd42006-02-19 04:05:39 +00001088
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001089 Py_BEGIN_ALLOW_THREADS
1090 rc = RegEnumKeyEx(hKey, index, tmpbuf, &len, NULL, NULL, NULL, NULL);
1091 Py_END_ALLOW_THREADS
1092 if (rc != ERROR_SUCCESS)
1093 return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx");
Tim Peters313fcd42006-02-19 04:05:39 +00001094
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001095 retStr = PyString_FromStringAndSize(tmpbuf, len);
1096 return retStr; /* can be NULL */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001097}
1098
1099static PyObject *
1100PyEnumValue(PyObject *self, PyObject *args)
1101{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001102 HKEY hKey;
1103 PyObject *obKey;
1104 int index;
1105 long rc;
1106 char *retValueBuf;
1107 char *retDataBuf;
Brian Curtin46a955b2010-06-08 21:17:16 +00001108 char *tmpBuf;
Brian Curtinb7b21f12010-05-26 13:24:57 +00001109 DWORD retValueSize, bufValueSize;
1110 DWORD retDataSize, bufDataSize;
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001111 DWORD typ;
1112 PyObject *obData;
1113 PyObject *retVal;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001114
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001115 if (!PyArg_ParseTuple(args, "Oi:EnumValue", &obKey, &index))
1116 return NULL;
1117 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1118 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001119
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001120 if ((rc = RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, NULL, NULL,
1121 NULL,
1122 &retValueSize, &retDataSize, NULL, NULL))
1123 != ERROR_SUCCESS)
1124 return PyErr_SetFromWindowsErrWithFunction(rc,
1125 "RegQueryInfoKey");
1126 ++retValueSize; /* include null terminators */
1127 ++retDataSize;
Brian Curtinb7b21f12010-05-26 13:24:57 +00001128 bufDataSize = retDataSize;
1129 bufValueSize = retValueSize;
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001130 retValueBuf = (char *)PyMem_Malloc(retValueSize);
1131 if (retValueBuf == NULL)
1132 return PyErr_NoMemory();
1133 retDataBuf = (char *)PyMem_Malloc(retDataSize);
1134 if (retDataBuf == NULL) {
1135 PyMem_Free(retValueBuf);
1136 return PyErr_NoMemory();
1137 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001138
Brian Curtinb7b21f12010-05-26 13:24:57 +00001139 while (1) {
Brian Curtinb7b21f12010-05-26 13:24:57 +00001140 Py_BEGIN_ALLOW_THREADS
1141 rc = RegEnumValue(hKey,
1142 index,
1143 retValueBuf,
1144 &retValueSize,
1145 NULL,
1146 &typ,
1147 (BYTE *)retDataBuf,
1148 &retDataSize);
1149 Py_END_ALLOW_THREADS
1150
1151 if (rc != ERROR_MORE_DATA)
1152 break;
1153
1154 bufDataSize *= 2;
Brian Curtin46a955b2010-06-08 21:17:16 +00001155 tmpBuf = (char *)PyMem_Realloc(retDataBuf, bufDataSize);
1156 if (tmpBuf == NULL) {
Brian Curtinb7b21f12010-05-26 13:24:57 +00001157 PyErr_NoMemory();
1158 retVal = NULL;
1159 goto fail;
1160 }
Brian Curtin46a955b2010-06-08 21:17:16 +00001161 retDataBuf = tmpBuf;
Brian Curtinb7b21f12010-05-26 13:24:57 +00001162 retDataSize = bufDataSize;
1163 retValueSize = bufValueSize;
1164 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001165
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001166 if (rc != ERROR_SUCCESS) {
1167 retVal = PyErr_SetFromWindowsErrWithFunction(rc,
1168 "PyRegEnumValue");
1169 goto fail;
1170 }
1171 obData = Reg2Py(retDataBuf, retDataSize, typ);
1172 if (obData == NULL) {
1173 retVal = NULL;
1174 goto fail;
1175 }
1176 retVal = Py_BuildValue("sOi", retValueBuf, obData, typ);
1177 Py_DECREF(obData);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001178 fail:
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001179 PyMem_Free(retValueBuf);
1180 PyMem_Free(retDataBuf);
1181 return retVal;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001182}
1183
1184static PyObject *
Christian Heimesb39a7562008-01-08 15:46:10 +00001185PyExpandEnvironmentStrings(PyObject *self, PyObject *args)
1186{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001187 Py_UNICODE *retValue = NULL;
1188 Py_UNICODE *src;
1189 DWORD retValueSize;
1190 DWORD rc;
1191 PyObject *o;
Christian Heimesb39a7562008-01-08 15:46:10 +00001192
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001193 if (!PyArg_ParseTuple(args, "u:ExpandEnvironmentStrings", &src))
1194 return NULL;
Christian Heimesb39a7562008-01-08 15:46:10 +00001195
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001196 retValueSize = ExpandEnvironmentStringsW(src, retValue, 0);
1197 if (retValueSize == 0) {
1198 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1199 "ExpandEnvironmentStrings");
1200 }
1201 retValue = (Py_UNICODE *)PyMem_Malloc(retValueSize * sizeof(Py_UNICODE));
1202 if (retValue == NULL) {
1203 return PyErr_NoMemory();
1204 }
Christian Heimesb39a7562008-01-08 15:46:10 +00001205
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001206 rc = ExpandEnvironmentStringsW(src, retValue, retValueSize);
1207 if (rc == 0) {
1208 PyMem_Free(retValue);
1209 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1210 "ExpandEnvironmentStrings");
1211 }
1212 o = PyUnicode_FromUnicode(retValue, wcslen(retValue));
1213 PyMem_Free(retValue);
1214 return o;
Christian Heimesb39a7562008-01-08 15:46:10 +00001215}
1216
1217static PyObject *
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001218PyFlushKey(PyObject *self, PyObject *args)
1219{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001220 HKEY hKey;
1221 PyObject *obKey;
1222 long rc;
1223 if (!PyArg_ParseTuple(args, "O:FlushKey", &obKey))
1224 return NULL;
1225 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1226 return NULL;
1227 Py_BEGIN_ALLOW_THREADS
1228 rc = RegFlushKey(hKey);
1229 Py_END_ALLOW_THREADS
1230 if (rc != ERROR_SUCCESS)
1231 return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey");
1232 Py_INCREF(Py_None);
1233 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001234}
1235static PyObject *
1236PyLoadKey(PyObject *self, PyObject *args)
1237{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001238 HKEY hKey;
1239 PyObject *obKey;
1240 char *subKey;
1241 char *fileName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001242
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001243 long rc;
1244 if (!PyArg_ParseTuple(args, "Oss:LoadKey", &obKey, &subKey, &fileName))
1245 return NULL;
1246 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1247 return NULL;
1248 Py_BEGIN_ALLOW_THREADS
1249 rc = RegLoadKey(hKey, subKey, fileName );
1250 Py_END_ALLOW_THREADS
1251 if (rc != ERROR_SUCCESS)
1252 return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey");
1253 Py_INCREF(Py_None);
1254 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001255}
1256
1257static PyObject *
1258PyOpenKey(PyObject *self, PyObject *args)
1259{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001260 HKEY hKey;
1261 PyObject *obKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001262
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001263 char *subKey;
1264 int res = 0;
1265 HKEY retKey;
1266 long rc;
1267 REGSAM sam = KEY_READ;
1268 if (!PyArg_ParseTuple(args, "Oz|ii:OpenKey", &obKey, &subKey,
1269 &res, &sam))
1270 return NULL;
1271 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1272 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001273
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001274 Py_BEGIN_ALLOW_THREADS
1275 rc = RegOpenKeyEx(hKey, subKey, res, sam, &retKey);
1276 Py_END_ALLOW_THREADS
1277 if (rc != ERROR_SUCCESS)
1278 return PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx");
1279 return PyHKEY_FromHKEY(retKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001280}
1281
1282
1283static PyObject *
1284PyQueryInfoKey(PyObject *self, PyObject *args)
1285{
1286 HKEY hKey;
1287 PyObject *obKey;
1288 long rc;
1289 DWORD nSubKeys, nValues;
1290 FILETIME ft;
1291 LARGE_INTEGER li;
1292 PyObject *l;
1293 PyObject *ret;
1294 if (!PyArg_ParseTuple(args, "O:QueryInfoKey", &obKey))
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001295 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001296 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001297 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001298 if ((rc = RegQueryInfoKey(hKey, NULL, NULL, 0, &nSubKeys, NULL, NULL,
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001299 &nValues, NULL, NULL, NULL, &ft))
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001300 != ERROR_SUCCESS)
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001301 return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey");
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001302 li.LowPart = ft.dwLowDateTime;
1303 li.HighPart = ft.dwHighDateTime;
1304 l = PyLong_FromLongLong(li.QuadPart);
1305 if (l == NULL)
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001306 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001307 ret = Py_BuildValue("iiO", nSubKeys, nValues, l);
1308 Py_DECREF(l);
1309 return ret;
1310}
1311
1312static PyObject *
1313PyQueryValue(PyObject *self, PyObject *args)
1314{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001315 HKEY hKey;
1316 PyObject *obKey;
1317 char *subKey;
1318 long rc;
1319 PyObject *retStr;
1320 char *retBuf;
Brian Curtinb7b21f12010-05-26 13:24:57 +00001321 DWORD bufSize = 0;
1322 DWORD retSize = 0;
1323 char *tmp;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001324
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001325 if (!PyArg_ParseTuple(args, "Oz:QueryValue", &obKey, &subKey))
1326 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001327
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001328 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1329 return NULL;
Brian Curtinb7b21f12010-05-26 13:24:57 +00001330
1331 rc = RegQueryValue(hKey, subKey, NULL, &retSize);
1332 if (rc == ERROR_MORE_DATA)
1333 retSize = 256;
1334 else if (rc != ERROR_SUCCESS)
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001335 return PyErr_SetFromWindowsErrWithFunction(rc,
1336 "RegQueryValue");
Brian Curtinb7b21f12010-05-26 13:24:57 +00001337
1338 bufSize = retSize;
1339 retBuf = (char *) PyMem_Malloc(bufSize);
1340 if (retBuf == NULL)
1341 return PyErr_NoMemory();
1342
1343 while (1) {
1344 retSize = bufSize;
1345 rc = RegQueryValue(hKey, subKey, retBuf, &retSize);
1346 if (rc != ERROR_MORE_DATA)
1347 break;
1348
1349 bufSize *= 2;
1350 tmp = (char *) PyMem_Realloc(retBuf, bufSize);
1351 if (tmp == NULL) {
1352 PyMem_Free(retBuf);
1353 return PyErr_NoMemory();
1354 }
1355 retBuf = tmp;
1356 }
1357
1358 if (rc != ERROR_SUCCESS) {
1359 PyMem_Free(retBuf);
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001360 return PyErr_SetFromWindowsErrWithFunction(rc,
1361 "RegQueryValue");
1362 }
Brian Curtinb7b21f12010-05-26 13:24:57 +00001363
1364 if (retBuf[retSize-1] == '\x00')
1365 retSize--;
1366 retStr = PyString_FromStringAndSize(retBuf, retSize);
1367 if (retStr == NULL) {
1368 PyMem_Free(retBuf);
1369 return NULL;
1370 }
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001371 return retStr;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001372}
1373
1374static PyObject *
1375PyQueryValueEx(PyObject *self, PyObject *args)
1376{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001377 HKEY hKey;
1378 PyObject *obKey;
1379 char *valueName;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001380
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001381 long rc;
Brian Curtinb7b21f12010-05-26 13:24:57 +00001382 char *retBuf, *tmp;
1383 DWORD bufSize = 0, retSize;
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001384 DWORD typ;
1385 PyObject *obData;
1386 PyObject *result;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001387
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001388 if (!PyArg_ParseTuple(args, "Oz:QueryValueEx", &obKey, &valueName))
1389 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001390
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001391 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1392 return NULL;
Brian Curtinb7b21f12010-05-26 13:24:57 +00001393
1394 rc = RegQueryValueEx(hKey, valueName, NULL, NULL, NULL, &bufSize);
1395 if (rc == ERROR_MORE_DATA)
1396 bufSize = 256;
1397 else if (rc != ERROR_SUCCESS)
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001398 return PyErr_SetFromWindowsErrWithFunction(rc,
1399 "RegQueryValueEx");
1400 retBuf = (char *)PyMem_Malloc(bufSize);
1401 if (retBuf == NULL)
1402 return PyErr_NoMemory();
Brian Curtinb7b21f12010-05-26 13:24:57 +00001403
1404 while (1) {
1405 retSize = bufSize;
1406 rc = RegQueryValueEx(hKey, valueName, NULL, &typ,
1407 (BYTE *)retBuf, &retSize);
1408 if (rc != ERROR_MORE_DATA)
1409 break;
1410
1411 bufSize *= 2;
1412 tmp = (char *) PyMem_Realloc(retBuf, bufSize);
1413 if (tmp == NULL) {
1414 PyMem_Free(retBuf);
1415 return PyErr_NoMemory();
1416 }
1417 retBuf = tmp;
1418 }
1419
1420 if (rc != ERROR_SUCCESS) {
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001421 PyMem_Free(retBuf);
1422 return PyErr_SetFromWindowsErrWithFunction(rc,
1423 "RegQueryValueEx");
1424 }
1425 obData = Reg2Py(retBuf, bufSize, typ);
1426 PyMem_Free((void *)retBuf);
1427 if (obData == NULL)
1428 return NULL;
1429 result = Py_BuildValue("Oi", obData, typ);
1430 Py_DECREF(obData);
1431 return result;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001432}
1433
1434
1435static PyObject *
1436PySaveKey(PyObject *self, PyObject *args)
1437{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001438 HKEY hKey;
1439 PyObject *obKey;
1440 char *fileName;
1441 LPSECURITY_ATTRIBUTES pSA = NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001442
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001443 long rc;
1444 if (!PyArg_ParseTuple(args, "Os:SaveKey", &obKey, &fileName))
1445 return NULL;
1446 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1447 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001448/* One day we may get security into the core?
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001449 if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE))
1450 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001451*/
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001452 Py_BEGIN_ALLOW_THREADS
1453 rc = RegSaveKey(hKey, fileName, pSA );
1454 Py_END_ALLOW_THREADS
1455 if (rc != ERROR_SUCCESS)
1456 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey");
1457 Py_INCREF(Py_None);
1458 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001459}
1460
1461static PyObject *
1462PySetValue(PyObject *self, PyObject *args)
1463{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001464 HKEY hKey;
1465 PyObject *obKey;
1466 char *subKey;
1467 char *str;
1468 DWORD typ;
1469 DWORD len;
1470 long rc;
1471 PyObject *obStrVal;
1472 PyObject *obSubKey;
1473 if (!PyArg_ParseTuple(args, "OOiO:SetValue",
1474 &obKey,
1475 &obSubKey,
1476 &typ,
1477 &obStrVal))
1478 return NULL;
1479 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1480 return NULL;
1481 if (typ != REG_SZ) {
1482 PyErr_SetString(PyExc_TypeError,
1483 "Type must be _winreg.REG_SZ");
1484 return NULL;
1485 }
1486 /* XXX - need Unicode support */
1487 str = PyString_AsString(obStrVal);
1488 if (str == NULL)
1489 return NULL;
1490 len = PyString_Size(obStrVal);
1491 if (obSubKey == Py_None)
1492 subKey = NULL;
1493 else {
1494 subKey = PyString_AsString(obSubKey);
1495 if (subKey == NULL)
1496 return NULL;
1497 }
1498 Py_BEGIN_ALLOW_THREADS
1499 rc = RegSetValue(hKey, subKey, REG_SZ, str, len+1);
1500 Py_END_ALLOW_THREADS
1501 if (rc != ERROR_SUCCESS)
1502 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue");
1503 Py_INCREF(Py_None);
1504 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001505}
1506
1507static PyObject *
1508PySetValueEx(PyObject *self, PyObject *args)
1509{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001510 HKEY hKey;
1511 PyObject *obKey;
1512 char *valueName;
1513 PyObject *obRes;
1514 PyObject *value;
1515 BYTE *data;
1516 DWORD len;
1517 DWORD typ;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001518
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001519 LONG rc;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001520
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001521 if (!PyArg_ParseTuple(args, "OzOiO:SetValueEx",
1522 &obKey,
1523 &valueName,
1524 &obRes,
1525 &typ,
1526 &value))
1527 return NULL;
1528 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1529 return NULL;
1530 if (!Py2Reg(value, typ, &data, &len))
1531 {
1532 if (!PyErr_Occurred())
1533 PyErr_SetString(PyExc_ValueError,
1534 "Could not convert the data to the specified type.");
1535 return NULL;
1536 }
1537 Py_BEGIN_ALLOW_THREADS
1538 rc = RegSetValueEx(hKey, valueName, 0, typ, data, len);
1539 Py_END_ALLOW_THREADS
1540 PyMem_DEL(data);
1541 if (rc != ERROR_SUCCESS)
1542 return PyErr_SetFromWindowsErrWithFunction(rc,
1543 "RegSetValueEx");
1544 Py_INCREF(Py_None);
1545 return Py_None;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001546}
1547
Mark Hammond8a3c8712008-04-06 01:42:06 +00001548static PyObject *
1549PyDisableReflectionKey(PyObject *self, PyObject *args)
1550{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001551 HKEY hKey;
1552 PyObject *obKey;
1553 HMODULE hMod;
1554 typedef LONG (WINAPI *RDRKFunc)(HKEY);
1555 RDRKFunc pfn = NULL;
1556 LONG rc;
Mark Hammond8a3c8712008-04-06 01:42:06 +00001557
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001558 if (!PyArg_ParseTuple(args, "O:DisableReflectionKey", &obKey))
1559 return NULL;
1560 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1561 return NULL;
Mark Hammond8a3c8712008-04-06 01:42:06 +00001562
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001563 // Only available on 64bit platforms, so we must load it
1564 // dynamically.
1565 hMod = GetModuleHandle("advapi32.dll");
1566 if (hMod)
1567 pfn = (RDRKFunc)GetProcAddress(hMod,
1568 "RegDisableReflectionKey");
1569 if (!pfn) {
1570 PyErr_SetString(PyExc_NotImplementedError,
1571 "not implemented on this platform");
1572 return NULL;
1573 }
1574 Py_BEGIN_ALLOW_THREADS
1575 rc = (*pfn)(hKey);
1576 Py_END_ALLOW_THREADS
1577 if (rc != ERROR_SUCCESS)
1578 return PyErr_SetFromWindowsErrWithFunction(rc,
1579 "RegDisableReflectionKey");
1580 Py_INCREF(Py_None);
1581 return Py_None;
Mark Hammond8a3c8712008-04-06 01:42:06 +00001582}
1583
1584static PyObject *
1585PyEnableReflectionKey(PyObject *self, PyObject *args)
1586{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001587 HKEY hKey;
1588 PyObject *obKey;
1589 HMODULE hMod;
1590 typedef LONG (WINAPI *RERKFunc)(HKEY);
1591 RERKFunc pfn = NULL;
1592 LONG rc;
Mark Hammond8a3c8712008-04-06 01:42:06 +00001593
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001594 if (!PyArg_ParseTuple(args, "O:EnableReflectionKey", &obKey))
1595 return NULL;
1596 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1597 return NULL;
Mark Hammond8a3c8712008-04-06 01:42:06 +00001598
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001599 // Only available on 64bit platforms, so we must load it
1600 // dynamically.
1601 hMod = GetModuleHandle("advapi32.dll");
1602 if (hMod)
1603 pfn = (RERKFunc)GetProcAddress(hMod,
1604 "RegEnableReflectionKey");
1605 if (!pfn) {
1606 PyErr_SetString(PyExc_NotImplementedError,
1607 "not implemented on this platform");
1608 return NULL;
1609 }
1610 Py_BEGIN_ALLOW_THREADS
1611 rc = (*pfn)(hKey);
1612 Py_END_ALLOW_THREADS
1613 if (rc != ERROR_SUCCESS)
1614 return PyErr_SetFromWindowsErrWithFunction(rc,
1615 "RegEnableReflectionKey");
1616 Py_INCREF(Py_None);
1617 return Py_None;
Mark Hammond8a3c8712008-04-06 01:42:06 +00001618}
1619
1620static PyObject *
1621PyQueryReflectionKey(PyObject *self, PyObject *args)
1622{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001623 HKEY hKey;
1624 PyObject *obKey;
1625 HMODULE hMod;
1626 typedef LONG (WINAPI *RQRKFunc)(HKEY, BOOL *);
1627 RQRKFunc pfn = NULL;
1628 BOOL result;
1629 LONG rc;
Mark Hammond8a3c8712008-04-06 01:42:06 +00001630
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001631 if (!PyArg_ParseTuple(args, "O:QueryReflectionKey", &obKey))
1632 return NULL;
1633 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1634 return NULL;
Mark Hammond8a3c8712008-04-06 01:42:06 +00001635
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001636 // Only available on 64bit platforms, so we must load it
1637 // dynamically.
1638 hMod = GetModuleHandle("advapi32.dll");
1639 if (hMod)
1640 pfn = (RQRKFunc)GetProcAddress(hMod,
1641 "RegQueryReflectionKey");
1642 if (!pfn) {
1643 PyErr_SetString(PyExc_NotImplementedError,
1644 "not implemented on this platform");
1645 return NULL;
1646 }
1647 Py_BEGIN_ALLOW_THREADS
1648 rc = (*pfn)(hKey, &result);
1649 Py_END_ALLOW_THREADS
1650 if (rc != ERROR_SUCCESS)
1651 return PyErr_SetFromWindowsErrWithFunction(rc,
1652 "RegQueryReflectionKey");
1653 return PyBool_FromLong(rc);
Mark Hammond8a3c8712008-04-06 01:42:06 +00001654}
1655
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001656static struct PyMethodDef winreg_methods[] = {
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001657 {"CloseKey", PyCloseKey, METH_VARARGS, CloseKey_doc},
1658 {"ConnectRegistry", PyConnectRegistry, METH_VARARGS, ConnectRegistry_doc},
1659 {"CreateKey", PyCreateKey, METH_VARARGS, CreateKey_doc},
1660 {"DeleteKey", PyDeleteKey, METH_VARARGS, DeleteKey_doc},
1661 {"DeleteValue", PyDeleteValue, METH_VARARGS, DeleteValue_doc},
1662 {"DisableReflectionKey", PyDisableReflectionKey, METH_VARARGS, DisableReflectionKey_doc},
1663 {"EnableReflectionKey", PyEnableReflectionKey, METH_VARARGS, EnableReflectionKey_doc},
1664 {"EnumKey", PyEnumKey, METH_VARARGS, EnumKey_doc},
1665 {"EnumValue", PyEnumValue, METH_VARARGS, EnumValue_doc},
1666 {"ExpandEnvironmentStrings", PyExpandEnvironmentStrings, METH_VARARGS,
1667 ExpandEnvironmentStrings_doc },
1668 {"FlushKey", PyFlushKey, METH_VARARGS, FlushKey_doc},
1669 {"LoadKey", PyLoadKey, METH_VARARGS, LoadKey_doc},
1670 {"OpenKey", PyOpenKey, METH_VARARGS, OpenKey_doc},
1671 {"OpenKeyEx", PyOpenKey, METH_VARARGS, OpenKeyEx_doc},
1672 {"QueryValue", PyQueryValue, METH_VARARGS, QueryValue_doc},
1673 {"QueryValueEx", PyQueryValueEx, METH_VARARGS, QueryValueEx_doc},
1674 {"QueryInfoKey", PyQueryInfoKey, METH_VARARGS, QueryInfoKey_doc},
1675 {"QueryReflectionKey",PyQueryReflectionKey,METH_VARARGS, QueryReflectionKey_doc},
1676 {"SaveKey", PySaveKey, METH_VARARGS, SaveKey_doc},
1677 {"SetValue", PySetValue, METH_VARARGS, SetValue_doc},
1678 {"SetValueEx", PySetValueEx, METH_VARARGS, SetValueEx_doc},
1679 NULL,
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001680};
1681
1682static void
1683insint(PyObject * d, char * name, long value)
1684{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001685 PyObject *v = PyInt_FromLong(value);
1686 if (!v || PyDict_SetItemString(d, name, v))
1687 PyErr_Clear();
1688 Py_XDECREF(v);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001689}
1690
1691#define ADD_INT(val) insint(d, #val, val)
1692
1693static void
1694inskey(PyObject * d, char * name, HKEY key)
1695{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001696 PyObject *v = PyLong_FromVoidPtr(key);
1697 if (!v || PyDict_SetItemString(d, name, v))
1698 PyErr_Clear();
1699 Py_XDECREF(v);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001700}
1701
1702#define ADD_KEY(val) inskey(d, #val, val)
1703
Mark Hammond8235ea12002-07-19 06:55:41 +00001704PyMODINIT_FUNC init_winreg(void)
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001705{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001706 PyObject *m, *d;
1707 m = Py_InitModule3("_winreg", winreg_methods, module_doc);
1708 if (m == NULL)
1709 return;
1710 d = PyModule_GetDict(m);
1711 PyHKEY_Type.ob_type = &PyType_Type;
1712 PyHKEY_Type.tp_doc = PyHKEY_doc;
1713 Py_INCREF(&PyHKEY_Type);
1714 if (PyDict_SetItemString(d, "HKEYType",
1715 (PyObject *)&PyHKEY_Type) != 0)
1716 return;
1717 Py_INCREF(PyExc_WindowsError);
1718 if (PyDict_SetItemString(d, "error",
1719 PyExc_WindowsError) != 0)
1720 return;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001721
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001722 /* Add the relevant constants */
1723 ADD_KEY(HKEY_CLASSES_ROOT);
1724 ADD_KEY(HKEY_CURRENT_USER);
1725 ADD_KEY(HKEY_LOCAL_MACHINE);
1726 ADD_KEY(HKEY_USERS);
1727 ADD_KEY(HKEY_PERFORMANCE_DATA);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001728#ifdef HKEY_CURRENT_CONFIG
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001729 ADD_KEY(HKEY_CURRENT_CONFIG);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001730#endif
1731#ifdef HKEY_DYN_DATA
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001732 ADD_KEY(HKEY_DYN_DATA);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001733#endif
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001734 ADD_INT(KEY_QUERY_VALUE);
1735 ADD_INT(KEY_SET_VALUE);
1736 ADD_INT(KEY_CREATE_SUB_KEY);
1737 ADD_INT(KEY_ENUMERATE_SUB_KEYS);
1738 ADD_INT(KEY_NOTIFY);
1739 ADD_INT(KEY_CREATE_LINK);
1740 ADD_INT(KEY_READ);
1741 ADD_INT(KEY_WRITE);
1742 ADD_INT(KEY_EXECUTE);
1743 ADD_INT(KEY_ALL_ACCESS);
Mark Hammond8a3c8712008-04-06 01:42:06 +00001744#ifdef KEY_WOW64_64KEY
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001745 ADD_INT(KEY_WOW64_64KEY);
Mark Hammond8a3c8712008-04-06 01:42:06 +00001746#endif
1747#ifdef KEY_WOW64_32KEY
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001748 ADD_INT(KEY_WOW64_32KEY);
Mark Hammond8a3c8712008-04-06 01:42:06 +00001749#endif
Antoine Pitrouc7c96a92010-05-09 15:15:40 +00001750 ADD_INT(REG_OPTION_RESERVED);
1751 ADD_INT(REG_OPTION_NON_VOLATILE);
1752 ADD_INT(REG_OPTION_VOLATILE);
1753 ADD_INT(REG_OPTION_CREATE_LINK);
1754 ADD_INT(REG_OPTION_BACKUP_RESTORE);
1755 ADD_INT(REG_OPTION_OPEN_LINK);
1756 ADD_INT(REG_LEGAL_OPTION);
1757 ADD_INT(REG_CREATED_NEW_KEY);
1758 ADD_INT(REG_OPENED_EXISTING_KEY);
1759 ADD_INT(REG_WHOLE_HIVE_VOLATILE);
1760 ADD_INT(REG_REFRESH_HIVE);
1761 ADD_INT(REG_NO_LAZY_FLUSH);
1762 ADD_INT(REG_NOTIFY_CHANGE_NAME);
1763 ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES);
1764 ADD_INT(REG_NOTIFY_CHANGE_LAST_SET);
1765 ADD_INT(REG_NOTIFY_CHANGE_SECURITY);
1766 ADD_INT(REG_LEGAL_CHANGE_FILTER);
1767 ADD_INT(REG_NONE);
1768 ADD_INT(REG_SZ);
1769 ADD_INT(REG_EXPAND_SZ);
1770 ADD_INT(REG_BINARY);
1771 ADD_INT(REG_DWORD);
1772 ADD_INT(REG_DWORD_LITTLE_ENDIAN);
1773 ADD_INT(REG_DWORD_BIG_ENDIAN);
1774 ADD_INT(REG_LINK);
1775 ADD_INT(REG_MULTI_SZ);
1776 ADD_INT(REG_RESOURCE_LIST);
1777 ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
1778 ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001779}
1780