blob: 9f234a1a18a354b94924b730b524ee5fbe4b7d41 [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
7 module circa 1995.
8 * Bill Tutt expanded the support significantly not long after.
9 * Numerous other people have submitted patches since then.
10 * Ripped from win32api module 03-Feb-2000 by Mark Hammond, and
11 basic Unicode support added.
12
13*/
14
Guido van Rossum9f3712c2000-03-28 20:37:15 +000015#include "Python.h"
16#include "structmember.h"
17#include "malloc.h" /* for alloca */
Kristján Valur Jónsson629ec262007-05-26 19:31:39 +000018#include "windows.h"
Guido van Rossum9f3712c2000-03-28 20:37:15 +000019
20static BOOL PyHKEY_AsHKEY(PyObject *ob, HKEY *pRes, BOOL bNoneOK);
21static PyObject *PyHKEY_FromHKEY(HKEY h);
22static BOOL PyHKEY_Close(PyObject *obHandle);
23
24static char errNotAHandle[] = "Object is not a handle";
25
26/* The win32api module reports the function name that failed,
27 but this concept is not in the Python core.
Mark Dickinson3e4caeb2009-02-21 20:27:01 +000028 Hopefully it will one day, and in the meantime I don't
Guido van Rossum9f3712c2000-03-28 20:37:15 +000029 want to lose this info...
30*/
31#define PyErr_SetFromWindowsErrWithFunction(rc, fnname) \
32 PyErr_SetFromWindowsErr(rc)
33
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 {
357 PyObject_VAR_HEAD
358 HKEY hkey;
359} 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{
368 PyErr_SetString(PyExc_TypeError, failMsg);
369 return NULL;
370}
371static PyObject *
372PyHKEY_binaryFailureFunc(PyObject *ob1, PyObject *ob2)
373{
374 PyErr_SetString(PyExc_TypeError, failMsg);
375 return NULL;
376}
377static PyObject *
378PyHKEY_ternaryFailureFunc(PyObject *ob1, PyObject *ob2, PyObject *ob3)
379{
380 PyErr_SetString(PyExc_TypeError, failMsg);
381 return NULL;
382}
383
384static void
385PyHKEY_deallocFunc(PyObject *ob)
386{
387 /* 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);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000394 PyObject_DEL(ob);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000395}
396
397static int
398PyHKEY_nonzeroFunc(PyObject *ob)
399{
400 return ((PyHKEYObject *)ob)->hkey != 0;
401}
402
403static PyObject *
404PyHKEY_intFunc(PyObject *ob)
405{
406 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
407 return PyLong_FromVoidPtr(pyhkey->hkey);
408}
409
410static int
411PyHKEY_printFunc(PyObject *ob, FILE *fp, int flags)
412{
Amaury Forgeot d'Arca18392a2009-01-13 23:19:08 +0000413 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
414 fprintf(fp, "<PyHKEY at %p (%p)>",
415 ob, pyhkey->hkey);
416 return 0;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000417}
418
419static PyObject *
420PyHKEY_strFunc(PyObject *ob)
421{
Amaury Forgeot d'Arca18392a2009-01-13 23:19:08 +0000422 PyHKEYObject *pyhkey = (PyHKEYObject *)ob;
423 return PyString_FromFormat("<PyHKEY:%p>", pyhkey->hkey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000424}
425
426static int
427PyHKEY_compareFunc(PyObject *ob1, PyObject *ob2)
428{
429 PyHKEYObject *pyhkey1 = (PyHKEYObject *)ob1;
430 PyHKEYObject *pyhkey2 = (PyHKEYObject *)ob2;
431 return pyhkey1 == pyhkey2 ? 0 :
432 (pyhkey1 < pyhkey2 ? -1 : 1);
433}
434
435static long
436PyHKEY_hashFunc(PyObject *ob)
437{
438 /* Just use the address.
439 XXX - should we use the handle value?
440 */
Fred Drake13634cf2000-06-29 19:17:04 +0000441 return _Py_HashPointer(ob);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000442}
443
444
445static PyNumberMethods PyHKEY_NumberMethods =
446{
447 PyHKEY_binaryFailureFunc, /* nb_add */
448 PyHKEY_binaryFailureFunc, /* nb_subtract */
449 PyHKEY_binaryFailureFunc, /* nb_multiply */
450 PyHKEY_binaryFailureFunc, /* nb_divide */
451 PyHKEY_binaryFailureFunc, /* nb_remainder */
452 PyHKEY_binaryFailureFunc, /* nb_divmod */
453 PyHKEY_ternaryFailureFunc, /* nb_power */
454 PyHKEY_unaryFailureFunc, /* nb_negative */
455 PyHKEY_unaryFailureFunc, /* nb_positive */
456 PyHKEY_unaryFailureFunc, /* nb_absolute */
457 PyHKEY_nonzeroFunc, /* nb_nonzero */
458 PyHKEY_unaryFailureFunc, /* nb_invert */
459 PyHKEY_binaryFailureFunc, /* nb_lshift */
460 PyHKEY_binaryFailureFunc, /* nb_rshift */
461 PyHKEY_binaryFailureFunc, /* nb_and */
462 PyHKEY_binaryFailureFunc, /* nb_xor */
463 PyHKEY_binaryFailureFunc, /* nb_or */
464 0, /* nb_coerce (allowed to be zero) */
465 PyHKEY_intFunc, /* nb_int */
466 PyHKEY_unaryFailureFunc, /* nb_long */
467 PyHKEY_unaryFailureFunc, /* nb_float */
468 PyHKEY_unaryFailureFunc, /* nb_oct */
469 PyHKEY_unaryFailureFunc, /* nb_hex */
470};
471
472
473/* fwd declare __getattr__ */
Tim Petersc3d12ac2005-12-24 06:03:06 +0000474static PyObject *PyHKEY_getattr(PyObject *self, const char *name);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000475
476/* The type itself */
477PyTypeObject PyHKEY_Type =
478{
Martin v. Löwis68192102007-07-21 06:55:02 +0000479 PyVarObject_HEAD_INIT(0, 0) /* fill in type at module init */
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000480 "PyHKEY",
481 sizeof(PyHKEYObject),
482 0,
483 PyHKEY_deallocFunc, /* tp_dealloc */
484 PyHKEY_printFunc, /* tp_print */
485 PyHKEY_getattr, /* tp_getattr */
486 0, /* tp_setattr */
487 PyHKEY_compareFunc, /* tp_compare */
488 0, /* tp_repr */
489 &PyHKEY_NumberMethods, /* tp_as_number */
490 0, /* tp_as_sequence */
491 0, /* tp_as_mapping */
492 PyHKEY_hashFunc, /* tp_hash */
493 0, /* tp_call */
494 PyHKEY_strFunc, /* tp_str */
495 0, /* tp_getattro */
496 0, /* tp_setattro */
497 0, /* tp_as_buffer */
498 0, /* tp_flags */
499 PyHKEY_doc, /* tp_doc */
500};
501
502#define OFF(e) offsetof(PyHKEYObject, e)
503
504static struct memberlist PyHKEY_memberlist[] = {
505 {"handle", T_INT, OFF(hkey)},
506 {NULL} /* Sentinel */
507};
508
509/************************************************************************
510
511 The PyHKEY object methods
512
513************************************************************************/
514static PyObject *
515PyHKEY_CloseMethod(PyObject *self, PyObject *args)
516{
517 if (!PyArg_ParseTuple(args, ":Close"))
518 return NULL;
519 if (!PyHKEY_Close(self))
520 return NULL;
521 Py_INCREF(Py_None);
522 return Py_None;
523}
524
525static PyObject *
526PyHKEY_DetachMethod(PyObject *self, PyObject *args)
527{
528 void* ret;
529 PyHKEYObject *pThis = (PyHKEYObject *)self;
530 if (!PyArg_ParseTuple(args, ":Detach"))
531 return NULL;
532 ret = (void*)pThis->hkey;
533 pThis->hkey = 0;
534 return PyLong_FromVoidPtr(ret);
535}
536
Christian Heimesb39a7562008-01-08 15:46:10 +0000537static PyObject *
538PyHKEY_Enter(PyObject *self)
539{
540 Py_XINCREF(self);
541 return self;
542}
543
544static PyObject *
545PyHKEY_Exit(PyObject *self, PyObject *args)
546{
547 if (!PyHKEY_Close(self))
548 return NULL;
549 Py_RETURN_NONE;
550}
551
552
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000553static struct PyMethodDef PyHKEY_methods[] = {
Neal Norwitz031829d2002-03-31 14:37:44 +0000554 {"Close", PyHKEY_CloseMethod, METH_VARARGS, PyHKEY_Close_doc},
555 {"Detach", PyHKEY_DetachMethod, METH_VARARGS, PyHKEY_Detach_doc},
Christian Heimesb39a7562008-01-08 15:46:10 +0000556 {"__enter__", (PyCFunction)PyHKEY_Enter, METH_NOARGS, NULL},
557 {"__exit__", PyHKEY_Exit, METH_VARARGS, NULL},
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000558 {NULL}
559};
560
561/*static*/ PyObject *
Tim Petersc3d12ac2005-12-24 06:03:06 +0000562PyHKEY_getattr(PyObject *self, const char *name)
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000563{
564 PyObject *res;
565
566 res = Py_FindMethod(PyHKEY_methods, self, name);
567 if (res != NULL)
568 return res;
569 PyErr_Clear();
570 if (strcmp(name, "handle") == 0)
571 return PyLong_FromVoidPtr(((PyHKEYObject *)self)->hkey);
572 return PyMember_Get((char *)self, PyHKEY_memberlist, name);
573}
574
575/************************************************************************
576 The public PyHKEY API (well, not public yet :-)
577************************************************************************/
578PyObject *
579PyHKEY_New(HKEY hInit)
580{
581 PyHKEYObject *key = PyObject_NEW(PyHKEYObject, &PyHKEY_Type);
582 if (key)
583 key->hkey = hInit;
584 return (PyObject *)key;
585}
586
587BOOL
588PyHKEY_Close(PyObject *ob_handle)
589{
590 LONG rc;
591 PyHKEYObject *key;
592
593 if (!PyHKEY_Check(ob_handle)) {
594 PyErr_SetString(PyExc_TypeError, "bad operand type");
595 return FALSE;
596 }
597 key = (PyHKEYObject *)ob_handle;
598 rc = key->hkey ? RegCloseKey((HKEY)key->hkey) : ERROR_SUCCESS;
599 key->hkey = 0;
600 if (rc != ERROR_SUCCESS)
601 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
602 return rc == ERROR_SUCCESS;
603}
604
605BOOL
606PyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK)
607{
608 if (ob == Py_None) {
609 if (!bNoneOK) {
610 PyErr_SetString(
611 PyExc_TypeError,
612 "None is not a valid HKEY in this context");
613 return FALSE;
614 }
615 *pHANDLE = (HKEY)0;
616 }
617 else if (PyHKEY_Check(ob)) {
618 PyHKEYObject *pH = (PyHKEYObject *)ob;
619 *pHANDLE = pH->hkey;
620 }
621 else if (PyInt_Check(ob) || PyLong_Check(ob)) {
622 /* We also support integers */
623 PyErr_Clear();
624 *pHANDLE = (HKEY)PyLong_AsVoidPtr(ob);
625 if (PyErr_Occurred())
626 return FALSE;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000627 }
628 else {
629 PyErr_SetString(
630 PyExc_TypeError,
631 "The object is not a PyHKEY object");
632 return FALSE;
633 }
634 return TRUE;
635}
636
637PyObject *
638PyHKEY_FromHKEY(HKEY h)
639{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000640 PyHKEYObject *op;
641
Guido van Rossume3a8e7e2002-08-19 19:26:42 +0000642 /* Inline PyObject_New */
Guido van Rossumb18618d2000-05-03 23:44:39 +0000643 op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject));
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000644 if (op == NULL)
645 return PyErr_NoMemory();
Guido van Rossumb18618d2000-05-03 23:44:39 +0000646 PyObject_INIT(op, &PyHKEY_Type);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000647 op->hkey = h;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000648 return (PyObject *)op;
649}
650
651
652/************************************************************************
653 The module methods
654************************************************************************/
655BOOL
656PyWinObject_CloseHKEY(PyObject *obHandle)
657{
658 BOOL ok;
659 if (PyHKEY_Check(obHandle)) {
660 ok = PyHKEY_Close(obHandle);
661 }
Fred Drake25e17262000-06-30 17:48:51 +0000662#if SIZEOF_LONG >= SIZEOF_HKEY
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000663 else if (PyInt_Check(obHandle)) {
664 long rc = RegCloseKey((HKEY)PyInt_AsLong(obHandle));
665 ok = (rc == ERROR_SUCCESS);
666 if (!ok)
667 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
668 }
Fred Drake25e17262000-06-30 17:48:51 +0000669#else
670 else if (PyLong_Check(obHandle)) {
671 long rc = RegCloseKey((HKEY)PyLong_AsVoidPtr(obHandle));
672 ok = (rc == ERROR_SUCCESS);
673 if (!ok)
674 PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey");
675 }
676#endif
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000677 else {
678 PyErr_SetString(
679 PyExc_TypeError,
680 "A handle must be a HKEY object or an integer");
681 return FALSE;
682 }
683 return ok;
684}
685
686
687/*
688 Private Helper functions for the registry interfaces
689
690** Note that fixupMultiSZ and countString have both had changes
691** made to support "incorrect strings". The registry specification
692** calls for strings to be terminated with 2 null bytes. It seems
Mark Dickinson3e4caeb2009-02-21 20:27:01 +0000693** some commercial packages install strings which don't conform,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000694** causing this code to fail - however, "regedit" etc still work
Mark Dickinson3e4caeb2009-02-21 20:27:01 +0000695** with these strings (ie only we don't!).
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000696*/
697static void
698fixupMultiSZ(char **str, char *data, int len)
699{
700 char *P;
701 int i;
702 char *Q;
703
704 Q = data + len;
705 for (P = data, i = 0; P < Q && *P != '\0'; P++, i++) {
706 str[i] = P;
707 for(; *P != '\0'; P++)
708 ;
709 }
710}
711
712static int
713countStrings(char *data, int len)
714{
715 int strings;
716 char *P;
717 char *Q = data + len;
718
719 for (P = data, strings = 0; P < Q && *P != '\0'; P++, strings++)
720 for (; P < Q && *P != '\0'; P++)
721 ;
722 return strings;
723}
724
725/* Convert PyObject into Registry data.
726 Allocates space as needed. */
727static BOOL
728Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
729{
Neal Norwitz4677fbf72008-03-25 04:18:18 +0000730 Py_ssize_t i,j;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000731 switch (typ) {
732 case REG_DWORD:
733 if (value != Py_None && !PyInt_Check(value))
734 return FALSE;
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000735 *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000736 if (*retDataBuf==NULL){
737 PyErr_NoMemory();
738 return FALSE;
739 }
740 *retDataSize = sizeof(DWORD);
741 if (value == Py_None) {
742 DWORD zero = 0;
743 memcpy(*retDataBuf, &zero, sizeof(DWORD));
744 }
745 else
746 memcpy(*retDataBuf,
747 &PyInt_AS_LONG((PyIntObject *)value),
748 sizeof(DWORD));
749 break;
750 case REG_SZ:
751 case REG_EXPAND_SZ:
752 {
753 int need_decref = 0;
754 if (value == Py_None)
755 *retDataSize = 1;
756 else {
757 if (PyUnicode_Check(value)) {
758 value = PyUnicode_AsEncodedString(
759 value,
760 "mbcs",
761 NULL);
762 if (value==NULL)
763 return FALSE;
764 need_decref = 1;
765 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000766 if (!PyString_Check(value))
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000767 return FALSE;
768 *retDataSize = 1 + strlen(
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000769 PyString_AS_STRING(
770 (PyStringObject *)value));
Guido van Rossum9f3712c2000-03-28 20:37:15 +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 strcpy((char *)*retDataBuf, "");
779 else
780 strcpy((char *)*retDataBuf,
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000781 PyString_AS_STRING(
782 (PyStringObject *)value));
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000783 if (need_decref)
784 Py_DECREF(value);
785 break;
786 }
787 case REG_MULTI_SZ:
788 {
789 DWORD size = 0;
790 char *P;
791 PyObject **obs = NULL;
792
793 if (value == Py_None)
794 i = 0;
795 else {
796 if (!PyList_Check(value))
797 return FALSE;
798 i = PyList_Size(value);
799 }
800 obs = malloc(sizeof(PyObject *) * i);
801 memset(obs, 0, sizeof(PyObject *) * i);
802 for (j = 0; j < i; j++)
803 {
804 PyObject *t;
805 t = PyList_GET_ITEM(
806 (PyListObject *)value,j);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000807 if (PyString_Check(t)) {
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000808 obs[j] = t;
809 Py_INCREF(t);
810 } else if (PyUnicode_Check(t)) {
811 obs[j] = PyUnicode_AsEncodedString(
812 t,
813 "mbcs",
814 NULL);
815 if (obs[j]==NULL)
816 goto reg_multi_fail;
817 } else
818 goto reg_multi_fail;
819 size += 1 + strlen(
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000820 PyString_AS_STRING(
821 (PyStringObject *)obs[j]));
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000822 }
823
824 *retDataSize = size + 1;
825 *retDataBuf = (BYTE *)PyMem_NEW(char,
826 *retDataSize);
827 if (*retDataBuf==NULL){
828 PyErr_NoMemory();
829 goto reg_multi_fail;
830 }
831 P = (char *)*retDataBuf;
832
833 for (j = 0; j < i; j++)
834 {
835 PyObject *t;
836 t = obs[j];
837 strcpy(P,
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000838 PyString_AS_STRING(
839 (PyStringObject *)t));
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000840 P += 1 + strlen(
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000841 PyString_AS_STRING(
842 (PyStringObject *)t));
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000843 Py_DECREF(obs[j]);
844 }
845 /* And doubly-terminate the list... */
846 *P = '\0';
847 free(obs);
848 break;
849 reg_multi_fail:
850 if (obs) {
851 for (j = 0; j < i; j++)
852 Py_XDECREF(obs[j]);
853
854 free(obs);
855 }
856 return FALSE;
857 }
858 case REG_BINARY:
859 /* ALSO handle ALL unknown data types here. Even if we can't
860 support it natively, we should handle the bits. */
861 default:
862 if (value == Py_None)
863 *retDataSize = 0;
864 else {
Mark Hammond4e80bb52000-07-28 03:44:41 +0000865 void *src_buf;
866 PyBufferProcs *pb = value->ob_type->tp_as_buffer;
867 if (pb==NULL) {
Tim Peters313fcd42006-02-19 04:05:39 +0000868 PyErr_Format(PyExc_TypeError,
Mark Hammond4e80bb52000-07-28 03:44:41 +0000869 "Objects of type '%s' can not "
Tim Peters313fcd42006-02-19 04:05:39 +0000870 "be used as binary registry values",
Mark Hammond4e80bb52000-07-28 03:44:41 +0000871 value->ob_type->tp_name);
872 return FALSE;
873 }
874 *retDataSize = (*pb->bf_getreadbuffer)(value, 0, &src_buf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000875 *retDataBuf = (BYTE *)PyMem_NEW(char,
876 *retDataSize);
877 if (*retDataBuf==NULL){
878 PyErr_NoMemory();
879 return FALSE;
880 }
Mark Hammond4e80bb52000-07-28 03:44:41 +0000881 memcpy(*retDataBuf, src_buf, *retDataSize);
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000882 }
883 break;
884 }
885 return TRUE;
886}
887
888/* Convert Registry data into PyObject*/
889static PyObject *
890Reg2Py(char *retDataBuf, DWORD retDataSize, DWORD typ)
891{
892 PyObject *obData;
893
894 switch (typ) {
895 case REG_DWORD:
896 if (retDataSize == 0)
897 obData = Py_BuildValue("i", 0);
898 else
899 obData = Py_BuildValue("i",
900 *(int *)retDataBuf);
901 break;
902 case REG_SZ:
903 case REG_EXPAND_SZ:
904 /* retDataBuf may or may not have a trailing NULL in
905 the buffer. */
906 if (retDataSize && retDataBuf[retDataSize-1] == '\0')
907 --retDataSize;
908 if (retDataSize ==0)
909 retDataBuf = "";
910 obData = PyUnicode_DecodeMBCS(retDataBuf,
911 retDataSize,
912 NULL);
913 break;
914 case REG_MULTI_SZ:
915 if (retDataSize == 0)
916 obData = PyList_New(0);
917 else
918 {
919 int index = 0;
920 int s = countStrings(retDataBuf, retDataSize);
921 char **str = (char **)malloc(sizeof(char *)*s);
922 if (str == NULL)
923 return PyErr_NoMemory();
924
925 fixupMultiSZ(str, retDataBuf, retDataSize);
926 obData = PyList_New(s);
Fred Drake25e17262000-06-30 17:48:51 +0000927 if (obData == NULL)
928 return NULL;
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000929 for (index = 0; index < s; index++)
930 {
Fred Drake25e17262000-06-30 17:48:51 +0000931 size_t len = _mbstrlen(str[index]);
932 if (len > INT_MAX) {
933 PyErr_SetString(PyExc_OverflowError,
934 "registry string is too long for a Python string");
935 Py_DECREF(obData);
936 return NULL;
937 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000938 PyList_SetItem(obData,
939 index,
940 PyUnicode_DecodeMBCS(
941 (const char *)str[index],
Fred Drake25e17262000-06-30 17:48:51 +0000942 (int)len,
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000943 NULL)
944 );
945 }
946 free(str);
947
948 break;
949 }
950 case REG_BINARY:
951 /* ALSO handle ALL unknown data types here. Even if we can't
952 support it natively, we should handle the bits. */
953 default:
954 if (retDataSize == 0) {
955 Py_INCREF(Py_None);
956 obData = Py_None;
957 }
958 else
959 obData = Py_BuildValue("s#",
960 (char *)retDataBuf,
961 retDataSize);
962 break;
963 }
964 if (obData == NULL)
965 return NULL;
966 else
967 return obData;
968}
969
970/* The Python methods */
971
972static PyObject *
973PyCloseKey(PyObject *self, PyObject *args)
974{
975 PyObject *obKey;
976 if (!PyArg_ParseTuple(args, "O:CloseKey", &obKey))
977 return NULL;
978 if (!PyHKEY_Close(obKey))
979 return NULL;
980 Py_INCREF(Py_None);
981 return Py_None;
982}
983
984static PyObject *
985PyConnectRegistry(PyObject *self, PyObject *args)
986{
987 HKEY hKey;
988 PyObject *obKey;
989 char *szCompName = NULL;
990 HKEY retKey;
991 long rc;
992 if (!PyArg_ParseTuple(args, "zO:ConnectRegistry", &szCompName, &obKey))
993 return NULL;
994 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
995 return NULL;
Martin v. Löwisbda0dde2006-07-24 10:26:33 +0000996 Py_BEGIN_ALLOW_THREADS
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000997 rc = RegConnectRegistry(szCompName, hKey, &retKey);
Martin v. Löwisbda0dde2006-07-24 10:26:33 +0000998 Py_END_ALLOW_THREADS
Guido van Rossum9f3712c2000-03-28 20:37:15 +0000999 if (rc != ERROR_SUCCESS)
1000 return PyErr_SetFromWindowsErrWithFunction(rc,
1001 "ConnectRegistry");
1002 return PyHKEY_FromHKEY(retKey);
1003}
1004
1005static PyObject *
1006PyCreateKey(PyObject *self, PyObject *args)
1007{
1008 HKEY hKey;
1009 PyObject *obKey;
1010 char *subKey;
1011 HKEY retKey;
1012 long rc;
1013 if (!PyArg_ParseTuple(args, "Oz:CreateKey", &obKey, &subKey))
1014 return NULL;
1015 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1016 return NULL;
1017 rc = RegCreateKey(hKey, subKey, &retKey);
1018 if (rc != ERROR_SUCCESS)
1019 return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey");
1020 return PyHKEY_FromHKEY(retKey);
1021}
1022
1023static PyObject *
1024PyDeleteKey(PyObject *self, PyObject *args)
1025{
1026 HKEY hKey;
1027 PyObject *obKey;
1028 char *subKey;
1029 long rc;
1030 if (!PyArg_ParseTuple(args, "Os:DeleteKey", &obKey, &subKey))
1031 return NULL;
1032 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1033 return NULL;
1034 rc = RegDeleteKey(hKey, subKey );
1035 if (rc != ERROR_SUCCESS)
1036 return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey");
1037 Py_INCREF(Py_None);
1038 return Py_None;
1039}
1040
1041static PyObject *
1042PyDeleteValue(PyObject *self, PyObject *args)
1043{
1044 HKEY hKey;
1045 PyObject *obKey;
1046 char *subKey;
1047 long rc;
1048 if (!PyArg_ParseTuple(args, "Oz:DeleteValue", &obKey, &subKey))
1049 return NULL;
1050 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1051 return NULL;
1052 Py_BEGIN_ALLOW_THREADS
1053 rc = RegDeleteValue(hKey, subKey);
1054 Py_END_ALLOW_THREADS
1055 if (rc !=ERROR_SUCCESS)
1056 return PyErr_SetFromWindowsErrWithFunction(rc,
1057 "RegDeleteValue");
1058 Py_INCREF(Py_None);
1059 return Py_None;
1060}
1061
1062static PyObject *
1063PyEnumKey(PyObject *self, PyObject *args)
1064{
1065 HKEY hKey;
1066 PyObject *obKey;
1067 int index;
1068 long rc;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001069 PyObject *retStr;
Georg Brandl9a928e72006-02-18 23:35:11 +00001070 char tmpbuf[256]; /* max key name length is 255 */
Georg Brandlb2699b22006-02-18 23:44:24 +00001071 DWORD len = sizeof(tmpbuf); /* includes NULL terminator */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001072
1073 if (!PyArg_ParseTuple(args, "Oi:EnumKey", &obKey, &index))
1074 return NULL;
1075 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1076 return NULL;
Tim Peters313fcd42006-02-19 04:05:39 +00001077
Georg Brandl9a928e72006-02-18 23:35:11 +00001078 Py_BEGIN_ALLOW_THREADS
1079 rc = RegEnumKeyEx(hKey, index, tmpbuf, &len, NULL, NULL, NULL, NULL);
1080 Py_END_ALLOW_THREADS
1081 if (rc != ERROR_SUCCESS)
1082 return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx");
Tim Peters313fcd42006-02-19 04:05:39 +00001083
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001084 retStr = PyString_FromStringAndSize(tmpbuf, len);
Georg Brandl9a928e72006-02-18 23:35:11 +00001085 return retStr; /* can be NULL */
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001086}
1087
1088static PyObject *
1089PyEnumValue(PyObject *self, PyObject *args)
1090{
1091 HKEY hKey;
1092 PyObject *obKey;
1093 int index;
1094 long rc;
1095 char *retValueBuf;
1096 char *retDataBuf;
1097 DWORD retValueSize;
1098 DWORD retDataSize;
1099 DWORD typ;
1100 PyObject *obData;
1101 PyObject *retVal;
1102
1103 if (!PyArg_ParseTuple(args, "Oi:EnumValue", &obKey, &index))
1104 return NULL;
1105 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1106 return NULL;
1107
1108 if ((rc = RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, NULL, NULL,
1109 NULL,
1110 &retValueSize, &retDataSize, NULL, NULL))
1111 != ERROR_SUCCESS)
1112 return PyErr_SetFromWindowsErrWithFunction(rc,
1113 "RegQueryInfoKey");
1114 ++retValueSize; /* include null terminators */
1115 ++retDataSize;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001116 retValueBuf = (char *)PyMem_Malloc(retValueSize);
1117 if (retValueBuf == NULL)
1118 return PyErr_NoMemory();
1119 retDataBuf = (char *)PyMem_Malloc(retDataSize);
1120 if (retDataBuf == NULL) {
1121 PyMem_Free(retValueBuf);
1122 return PyErr_NoMemory();
1123 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001124
1125 Py_BEGIN_ALLOW_THREADS
1126 rc = RegEnumValue(hKey,
1127 index,
1128 retValueBuf,
1129 &retValueSize,
1130 NULL,
1131 &typ,
1132 (BYTE *)retDataBuf,
1133 &retDataSize);
1134 Py_END_ALLOW_THREADS
1135
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001136 if (rc != ERROR_SUCCESS) {
1137 retVal = PyErr_SetFromWindowsErrWithFunction(rc,
1138 "PyRegEnumValue");
1139 goto fail;
1140 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001141 obData = Reg2Py(retDataBuf, retDataSize, typ);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001142 if (obData == NULL) {
1143 retVal = NULL;
1144 goto fail;
1145 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001146 retVal = Py_BuildValue("sOi", retValueBuf, obData, typ);
1147 Py_DECREF(obData);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001148 fail:
1149 PyMem_Free(retValueBuf);
1150 PyMem_Free(retDataBuf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001151 return retVal;
1152}
1153
1154static PyObject *
Christian Heimesb39a7562008-01-08 15:46:10 +00001155PyExpandEnvironmentStrings(PyObject *self, PyObject *args)
1156{
1157 Py_UNICODE *retValue = NULL;
1158 Py_UNICODE *src;
1159 DWORD retValueSize;
1160 DWORD rc;
1161 PyObject *o;
1162
1163 if (!PyArg_ParseTuple(args, "u:ExpandEnvironmentStrings", &src))
1164 return NULL;
1165
1166 retValueSize = ExpandEnvironmentStringsW(src, retValue, 0);
1167 if (retValueSize == 0) {
1168 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1169 "ExpandEnvironmentStrings");
1170 }
1171 retValue = (Py_UNICODE *)PyMem_Malloc(retValueSize * sizeof(Py_UNICODE));
1172 if (retValue == NULL) {
1173 return PyErr_NoMemory();
1174 }
1175
1176 rc = ExpandEnvironmentStringsW(src, retValue, retValueSize);
1177 if (rc == 0) {
1178 PyMem_Free(retValue);
1179 return PyErr_SetFromWindowsErrWithFunction(retValueSize,
1180 "ExpandEnvironmentStrings");
1181 }
1182 o = PyUnicode_FromUnicode(retValue, wcslen(retValue));
1183 PyMem_Free(retValue);
1184 return o;
1185}
1186
1187static PyObject *
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001188PyFlushKey(PyObject *self, PyObject *args)
1189{
1190 HKEY hKey;
1191 PyObject *obKey;
1192 long rc;
1193 if (!PyArg_ParseTuple(args, "O:FlushKey", &obKey))
1194 return NULL;
1195 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1196 return NULL;
1197 Py_BEGIN_ALLOW_THREADS
1198 rc = RegFlushKey(hKey);
1199 Py_END_ALLOW_THREADS
1200 if (rc != ERROR_SUCCESS)
1201 return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey");
1202 Py_INCREF(Py_None);
1203 return Py_None;
1204}
1205static PyObject *
1206PyLoadKey(PyObject *self, PyObject *args)
1207{
1208 HKEY hKey;
1209 PyObject *obKey;
1210 char *subKey;
1211 char *fileName;
1212
1213 long rc;
1214 if (!PyArg_ParseTuple(args, "Oss:LoadKey", &obKey, &subKey, &fileName))
1215 return NULL;
1216 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1217 return NULL;
1218 Py_BEGIN_ALLOW_THREADS
1219 rc = RegLoadKey(hKey, subKey, fileName );
1220 Py_END_ALLOW_THREADS
1221 if (rc != ERROR_SUCCESS)
1222 return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey");
1223 Py_INCREF(Py_None);
1224 return Py_None;
1225}
1226
1227static PyObject *
1228PyOpenKey(PyObject *self, PyObject *args)
1229{
1230 HKEY hKey;
1231 PyObject *obKey;
1232
1233 char *subKey;
1234 int res = 0;
1235 HKEY retKey;
1236 long rc;
1237 REGSAM sam = KEY_READ;
1238 if (!PyArg_ParseTuple(args, "Oz|ii:OpenKey", &obKey, &subKey,
1239 &res, &sam))
1240 return NULL;
1241 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1242 return NULL;
1243
1244 Py_BEGIN_ALLOW_THREADS
1245 rc = RegOpenKeyEx(hKey, subKey, res, sam, &retKey);
1246 Py_END_ALLOW_THREADS
1247 if (rc != ERROR_SUCCESS)
1248 return PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx");
1249 return PyHKEY_FromHKEY(retKey);
1250}
1251
1252
1253static PyObject *
1254PyQueryInfoKey(PyObject *self, PyObject *args)
1255{
1256 HKEY hKey;
1257 PyObject *obKey;
1258 long rc;
1259 DWORD nSubKeys, nValues;
1260 FILETIME ft;
1261 LARGE_INTEGER li;
1262 PyObject *l;
1263 PyObject *ret;
1264 if (!PyArg_ParseTuple(args, "O:QueryInfoKey", &obKey))
1265 return NULL;
1266 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1267 return NULL;
1268 if ((rc = RegQueryInfoKey(hKey, NULL, NULL, 0, &nSubKeys, NULL, NULL,
1269 &nValues, NULL, NULL, NULL, &ft))
1270 != ERROR_SUCCESS)
1271 return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey");
1272 li.LowPart = ft.dwLowDateTime;
1273 li.HighPart = ft.dwHighDateTime;
1274 l = PyLong_FromLongLong(li.QuadPart);
1275 if (l == NULL)
1276 return NULL;
1277 ret = Py_BuildValue("iiO", nSubKeys, nValues, l);
1278 Py_DECREF(l);
1279 return ret;
1280}
1281
1282static PyObject *
1283PyQueryValue(PyObject *self, PyObject *args)
1284{
1285 HKEY hKey;
1286 PyObject *obKey;
1287 char *subKey;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001288 long rc;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001289 PyObject *retStr;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001290 char *retBuf;
1291 long bufSize = 0;
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001292
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001293 if (!PyArg_ParseTuple(args, "Oz:QueryValue", &obKey, &subKey))
1294 return NULL;
1295
1296 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1297 return NULL;
1298 if ((rc = RegQueryValue(hKey, subKey, NULL, &bufSize))
1299 != ERROR_SUCCESS)
1300 return PyErr_SetFromWindowsErrWithFunction(rc,
1301 "RegQueryValue");
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001302 retStr = PyString_FromStringAndSize(NULL, bufSize);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001303 if (retStr == NULL)
1304 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001305 retBuf = PyString_AS_STRING(retStr);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001306 if ((rc = RegQueryValue(hKey, subKey, retBuf, &bufSize))
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001307 != ERROR_SUCCESS) {
1308 Py_DECREF(retStr);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001309 return PyErr_SetFromWindowsErrWithFunction(rc,
1310 "RegQueryValue");
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001311 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001312 _PyString_Resize(&retStr, strlen(retBuf));
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001313 return retStr;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001314}
1315
1316static PyObject *
1317PyQueryValueEx(PyObject *self, PyObject *args)
1318{
1319 HKEY hKey;
1320 PyObject *obKey;
1321 char *valueName;
1322
1323 long rc;
1324 char *retBuf;
1325 DWORD bufSize = 0;
1326 DWORD typ;
1327 PyObject *obData;
1328 PyObject *result;
1329
1330 if (!PyArg_ParseTuple(args, "Oz:QueryValueEx", &obKey, &valueName))
1331 return NULL;
1332
1333 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1334 return NULL;
1335 if ((rc = RegQueryValueEx(hKey, valueName,
1336 NULL, NULL, NULL,
1337 &bufSize))
1338 != ERROR_SUCCESS)
1339 return PyErr_SetFromWindowsErrWithFunction(rc,
1340 "RegQueryValueEx");
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001341 retBuf = (char *)PyMem_Malloc(bufSize);
1342 if (retBuf == NULL)
1343 return PyErr_NoMemory();
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001344 if ((rc = RegQueryValueEx(hKey, valueName, NULL,
1345 &typ, (BYTE *)retBuf, &bufSize))
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001346 != ERROR_SUCCESS) {
1347 PyMem_Free(retBuf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001348 return PyErr_SetFromWindowsErrWithFunction(rc,
1349 "RegQueryValueEx");
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001350 }
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001351 obData = Reg2Py(retBuf, bufSize, typ);
Guido van Rossuma6a38ad2003-11-30 22:01:43 +00001352 PyMem_Free((void *)retBuf);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001353 if (obData == NULL)
1354 return NULL;
1355 result = Py_BuildValue("Oi", obData, typ);
1356 Py_DECREF(obData);
1357 return result;
1358}
1359
1360
1361static PyObject *
1362PySaveKey(PyObject *self, PyObject *args)
1363{
1364 HKEY hKey;
1365 PyObject *obKey;
1366 char *fileName;
1367 LPSECURITY_ATTRIBUTES pSA = NULL;
1368
1369 long rc;
1370 if (!PyArg_ParseTuple(args, "Os:SaveKey", &obKey, &fileName))
1371 return NULL;
1372 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1373 return NULL;
1374/* One day we may get security into the core?
1375 if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE))
1376 return NULL;
1377*/
1378 Py_BEGIN_ALLOW_THREADS
1379 rc = RegSaveKey(hKey, fileName, pSA );
1380 Py_END_ALLOW_THREADS
1381 if (rc != ERROR_SUCCESS)
1382 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey");
1383 Py_INCREF(Py_None);
1384 return Py_None;
1385}
1386
1387static PyObject *
1388PySetValue(PyObject *self, PyObject *args)
1389{
1390 HKEY hKey;
1391 PyObject *obKey;
1392 char *subKey;
1393 char *str;
1394 DWORD typ;
1395 DWORD len;
1396 long rc;
1397 PyObject *obStrVal;
1398 PyObject *obSubKey;
1399 if (!PyArg_ParseTuple(args, "OOiO:SetValue",
1400 &obKey,
1401 &obSubKey,
1402 &typ,
1403 &obStrVal))
1404 return NULL;
1405 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1406 return NULL;
1407 if (typ != REG_SZ) {
1408 PyErr_SetString(PyExc_TypeError,
Thomas Hellere1d18f52002-12-20 20:13:35 +00001409 "Type must be _winreg.REG_SZ");
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001410 return NULL;
1411 }
1412 /* XXX - need Unicode support */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001413 str = PyString_AsString(obStrVal);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001414 if (str == NULL)
1415 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001416 len = PyString_Size(obStrVal);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001417 if (obSubKey == Py_None)
1418 subKey = NULL;
1419 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001420 subKey = PyString_AsString(obSubKey);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001421 if (subKey == NULL)
1422 return NULL;
1423 }
1424 Py_BEGIN_ALLOW_THREADS
1425 rc = RegSetValue(hKey, subKey, REG_SZ, str, len+1);
1426 Py_END_ALLOW_THREADS
1427 if (rc != ERROR_SUCCESS)
1428 return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue");
1429 Py_INCREF(Py_None);
1430 return Py_None;
1431}
1432
1433static PyObject *
1434PySetValueEx(PyObject *self, PyObject *args)
1435{
1436 HKEY hKey;
1437 PyObject *obKey;
1438 char *valueName;
1439 PyObject *obRes;
1440 PyObject *value;
1441 BYTE *data;
1442 DWORD len;
1443 DWORD typ;
1444
1445 LONG rc;
1446
1447 if (!PyArg_ParseTuple(args, "OzOiO:SetValueEx",
1448 &obKey,
1449 &valueName,
1450 &obRes,
1451 &typ,
1452 &value))
1453 return NULL;
1454 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1455 return NULL;
1456 if (!Py2Reg(value, typ, &data, &len))
1457 {
1458 if (!PyErr_Occurred())
1459 PyErr_SetString(PyExc_ValueError,
1460 "Could not convert the data to the specified type.");
1461 return NULL;
1462 }
1463 Py_BEGIN_ALLOW_THREADS
1464 rc = RegSetValueEx(hKey, valueName, 0, typ, data, len);
1465 Py_END_ALLOW_THREADS
Guido van Rossumb18618d2000-05-03 23:44:39 +00001466 PyMem_DEL(data);
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001467 if (rc != ERROR_SUCCESS)
1468 return PyErr_SetFromWindowsErrWithFunction(rc,
1469 "RegSetValueEx");
1470 Py_INCREF(Py_None);
1471 return Py_None;
1472}
1473
Mark Hammond8a3c8712008-04-06 01:42:06 +00001474static PyObject *
1475PyDisableReflectionKey(PyObject *self, PyObject *args)
1476{
1477 HKEY hKey;
1478 PyObject *obKey;
1479 HMODULE hMod;
1480 typedef LONG (WINAPI *RDRKFunc)(HKEY);
1481 RDRKFunc pfn = NULL;
1482 LONG rc;
1483
1484 if (!PyArg_ParseTuple(args, "O:DisableReflectionKey", &obKey))
1485 return NULL;
1486 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1487 return NULL;
1488
1489 // Only available on 64bit platforms, so we must load it
1490 // dynamically.
1491 hMod = GetModuleHandle("advapi32.dll");
1492 if (hMod)
1493 pfn = (RDRKFunc)GetProcAddress(hMod,
1494 "RegDisableReflectionKey");
1495 if (!pfn) {
1496 PyErr_SetString(PyExc_NotImplementedError,
1497 "not implemented on this platform");
1498 return NULL;
1499 }
1500 Py_BEGIN_ALLOW_THREADS
1501 rc = (*pfn)(hKey);
1502 Py_END_ALLOW_THREADS
1503 if (rc != ERROR_SUCCESS)
1504 return PyErr_SetFromWindowsErrWithFunction(rc,
1505 "RegDisableReflectionKey");
1506 Py_INCREF(Py_None);
1507 return Py_None;
1508}
1509
1510static PyObject *
1511PyEnableReflectionKey(PyObject *self, PyObject *args)
1512{
1513 HKEY hKey;
1514 PyObject *obKey;
1515 HMODULE hMod;
1516 typedef LONG (WINAPI *RERKFunc)(HKEY);
1517 RERKFunc pfn = NULL;
1518 LONG rc;
1519
1520 if (!PyArg_ParseTuple(args, "O:EnableReflectionKey", &obKey))
1521 return NULL;
1522 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1523 return NULL;
1524
1525 // Only available on 64bit platforms, so we must load it
1526 // dynamically.
1527 hMod = GetModuleHandle("advapi32.dll");
1528 if (hMod)
1529 pfn = (RERKFunc)GetProcAddress(hMod,
1530 "RegEnableReflectionKey");
1531 if (!pfn) {
1532 PyErr_SetString(PyExc_NotImplementedError,
1533 "not implemented on this platform");
1534 return NULL;
1535 }
1536 Py_BEGIN_ALLOW_THREADS
1537 rc = (*pfn)(hKey);
1538 Py_END_ALLOW_THREADS
1539 if (rc != ERROR_SUCCESS)
1540 return PyErr_SetFromWindowsErrWithFunction(rc,
1541 "RegEnableReflectionKey");
1542 Py_INCREF(Py_None);
1543 return Py_None;
1544}
1545
1546static PyObject *
1547PyQueryReflectionKey(PyObject *self, PyObject *args)
1548{
1549 HKEY hKey;
1550 PyObject *obKey;
1551 HMODULE hMod;
1552 typedef LONG (WINAPI *RQRKFunc)(HKEY, BOOL *);
1553 RQRKFunc pfn = NULL;
1554 BOOL result;
1555 LONG rc;
1556
1557 if (!PyArg_ParseTuple(args, "O:QueryReflectionKey", &obKey))
1558 return NULL;
1559 if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
1560 return NULL;
1561
1562 // Only available on 64bit platforms, so we must load it
1563 // dynamically.
1564 hMod = GetModuleHandle("advapi32.dll");
1565 if (hMod)
1566 pfn = (RQRKFunc)GetProcAddress(hMod,
1567 "RegQueryReflectionKey");
1568 if (!pfn) {
1569 PyErr_SetString(PyExc_NotImplementedError,
1570 "not implemented on this platform");
1571 return NULL;
1572 }
1573 Py_BEGIN_ALLOW_THREADS
1574 rc = (*pfn)(hKey, &result);
1575 Py_END_ALLOW_THREADS
1576 if (rc != ERROR_SUCCESS)
1577 return PyErr_SetFromWindowsErrWithFunction(rc,
1578 "RegQueryReflectionKey");
1579 return PyBool_FromLong(rc);
1580}
1581
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001582static struct PyMethodDef winreg_methods[] = {
Neal Norwitz031829d2002-03-31 14:37:44 +00001583 {"CloseKey", PyCloseKey, METH_VARARGS, CloseKey_doc},
1584 {"ConnectRegistry", PyConnectRegistry, METH_VARARGS, ConnectRegistry_doc},
1585 {"CreateKey", PyCreateKey, METH_VARARGS, CreateKey_doc},
1586 {"DeleteKey", PyDeleteKey, METH_VARARGS, DeleteKey_doc},
1587 {"DeleteValue", PyDeleteValue, METH_VARARGS, DeleteValue_doc},
Mark Hammond8a3c8712008-04-06 01:42:06 +00001588 {"DisableReflectionKey", PyDisableReflectionKey, METH_VARARGS, DisableReflectionKey_doc},
1589 {"EnableReflectionKey", PyEnableReflectionKey, METH_VARARGS, EnableReflectionKey_doc},
Neal Norwitz031829d2002-03-31 14:37:44 +00001590 {"EnumKey", PyEnumKey, METH_VARARGS, EnumKey_doc},
1591 {"EnumValue", PyEnumValue, METH_VARARGS, EnumValue_doc},
Christian Heimesb39a7562008-01-08 15:46:10 +00001592 {"ExpandEnvironmentStrings", PyExpandEnvironmentStrings, METH_VARARGS,
1593 ExpandEnvironmentStrings_doc },
Neal Norwitz031829d2002-03-31 14:37:44 +00001594 {"FlushKey", PyFlushKey, METH_VARARGS, FlushKey_doc},
1595 {"LoadKey", PyLoadKey, METH_VARARGS, LoadKey_doc},
1596 {"OpenKey", PyOpenKey, METH_VARARGS, OpenKey_doc},
1597 {"OpenKeyEx", PyOpenKey, METH_VARARGS, OpenKeyEx_doc},
1598 {"QueryValue", PyQueryValue, METH_VARARGS, QueryValue_doc},
1599 {"QueryValueEx", PyQueryValueEx, METH_VARARGS, QueryValueEx_doc},
1600 {"QueryInfoKey", PyQueryInfoKey, METH_VARARGS, QueryInfoKey_doc},
Mark Hammond8a3c8712008-04-06 01:42:06 +00001601 {"QueryReflectionKey",PyQueryReflectionKey,METH_VARARGS, QueryReflectionKey_doc},
Neal Norwitz031829d2002-03-31 14:37:44 +00001602 {"SaveKey", PySaveKey, METH_VARARGS, SaveKey_doc},
1603 {"SetValue", PySetValue, METH_VARARGS, SetValue_doc},
1604 {"SetValueEx", PySetValueEx, METH_VARARGS, SetValueEx_doc},
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001605 NULL,
1606};
1607
1608static void
1609insint(PyObject * d, char * name, long value)
1610{
1611 PyObject *v = PyInt_FromLong(value);
1612 if (!v || PyDict_SetItemString(d, name, v))
1613 PyErr_Clear();
1614 Py_XDECREF(v);
1615}
1616
1617#define ADD_INT(val) insint(d, #val, val)
1618
1619static void
1620inskey(PyObject * d, char * name, HKEY key)
1621{
1622 PyObject *v = PyLong_FromVoidPtr(key);
1623 if (!v || PyDict_SetItemString(d, name, v))
1624 PyErr_Clear();
1625 Py_XDECREF(v);
1626}
1627
1628#define ADD_KEY(val) inskey(d, #val, val)
1629
Mark Hammond8235ea12002-07-19 06:55:41 +00001630PyMODINIT_FUNC init_winreg(void)
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001631{
1632 PyObject *m, *d;
Fred Drake270e19b2000-06-29 16:14:14 +00001633 m = Py_InitModule3("_winreg", winreg_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001634 if (m == NULL)
1635 return;
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001636 d = PyModule_GetDict(m);
1637 PyHKEY_Type.ob_type = &PyType_Type;
1638 PyHKEY_Type.tp_doc = PyHKEY_doc;
1639 Py_INCREF(&PyHKEY_Type);
1640 if (PyDict_SetItemString(d, "HKEYType",
1641 (PyObject *)&PyHKEY_Type) != 0)
1642 return;
1643 Py_INCREF(PyExc_WindowsError);
1644 if (PyDict_SetItemString(d, "error",
1645 PyExc_WindowsError) != 0)
1646 return;
1647
1648 /* Add the relevant constants */
1649 ADD_KEY(HKEY_CLASSES_ROOT);
1650 ADD_KEY(HKEY_CURRENT_USER);
1651 ADD_KEY(HKEY_LOCAL_MACHINE);
1652 ADD_KEY(HKEY_USERS);
1653 ADD_KEY(HKEY_PERFORMANCE_DATA);
1654#ifdef HKEY_CURRENT_CONFIG
1655 ADD_KEY(HKEY_CURRENT_CONFIG);
1656#endif
1657#ifdef HKEY_DYN_DATA
1658 ADD_KEY(HKEY_DYN_DATA);
1659#endif
1660 ADD_INT(KEY_QUERY_VALUE);
1661 ADD_INT(KEY_SET_VALUE);
1662 ADD_INT(KEY_CREATE_SUB_KEY);
1663 ADD_INT(KEY_ENUMERATE_SUB_KEYS);
1664 ADD_INT(KEY_NOTIFY);
1665 ADD_INT(KEY_CREATE_LINK);
1666 ADD_INT(KEY_READ);
1667 ADD_INT(KEY_WRITE);
1668 ADD_INT(KEY_EXECUTE);
1669 ADD_INT(KEY_ALL_ACCESS);
Mark Hammond8a3c8712008-04-06 01:42:06 +00001670#ifdef KEY_WOW64_64KEY
1671 ADD_INT(KEY_WOW64_64KEY);
1672#endif
1673#ifdef KEY_WOW64_32KEY
1674 ADD_INT(KEY_WOW64_32KEY);
1675#endif
Guido van Rossum9f3712c2000-03-28 20:37:15 +00001676 ADD_INT(REG_OPTION_RESERVED);
1677 ADD_INT(REG_OPTION_NON_VOLATILE);
1678 ADD_INT(REG_OPTION_VOLATILE);
1679 ADD_INT(REG_OPTION_CREATE_LINK);
1680 ADD_INT(REG_OPTION_BACKUP_RESTORE);
1681 ADD_INT(REG_OPTION_OPEN_LINK);
1682 ADD_INT(REG_LEGAL_OPTION);
1683 ADD_INT(REG_CREATED_NEW_KEY);
1684 ADD_INT(REG_OPENED_EXISTING_KEY);
1685 ADD_INT(REG_WHOLE_HIVE_VOLATILE);
1686 ADD_INT(REG_REFRESH_HIVE);
1687 ADD_INT(REG_NO_LAZY_FLUSH);
1688 ADD_INT(REG_NOTIFY_CHANGE_NAME);
1689 ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES);
1690 ADD_INT(REG_NOTIFY_CHANGE_LAST_SET);
1691 ADD_INT(REG_NOTIFY_CHANGE_SECURITY);
1692 ADD_INT(REG_LEGAL_CHANGE_FILTER);
1693 ADD_INT(REG_NONE);
1694 ADD_INT(REG_SZ);
1695 ADD_INT(REG_EXPAND_SZ);
1696 ADD_INT(REG_BINARY);
1697 ADD_INT(REG_DWORD);
1698 ADD_INT(REG_DWORD_LITTLE_ENDIAN);
1699 ADD_INT(REG_DWORD_BIG_ENDIAN);
1700 ADD_INT(REG_LINK);
1701 ADD_INT(REG_MULTI_SZ);
1702 ADD_INT(REG_RESOURCE_LIST);
1703 ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
1704 ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
1705}
1706