Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1 | /* |
Georg Brandl | 38feaf0 | 2008-05-25 07:45:51 +0000 | [diff] [blame] | 2 | winreg.c |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 3 | |
| 4 | Windows Registry access module for Python. |
| 5 | |
| 6 | * Simple registry access written by Mark Hammond in win32api |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7 | module circa 1995. |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 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 Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 15 | #include "Python.h" |
| 16 | #include "structmember.h" |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 17 | #include "windows.h" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 18 | |
| 19 | static BOOL PyHKEY_AsHKEY(PyObject *ob, HKEY *pRes, BOOL bNoneOK); |
| 20 | static PyObject *PyHKEY_FromHKEY(HKEY h); |
| 21 | static BOOL PyHKEY_Close(PyObject *obHandle); |
| 22 | |
| 23 | static char errNotAHandle[] = "Object is not a handle"; |
| 24 | |
| 25 | /* The win32api module reports the function name that failed, |
| 26 | but this concept is not in the Python core. |
| 27 | Hopefully it will one day, and in the meantime I dont |
| 28 | want to lose this info... |
| 29 | */ |
| 30 | #define PyErr_SetFromWindowsErrWithFunction(rc, fnname) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 31 | PyErr_SetFromWindowsErr(rc) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 32 | |
| 33 | /* Forward declares */ |
| 34 | |
| 35 | /* Doc strings */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 36 | PyDoc_STRVAR(module_doc, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 37 | "This module provides access to the Windows registry API.\n" |
| 38 | "\n" |
| 39 | "Functions:\n" |
| 40 | "\n" |
| 41 | "CloseKey() - Closes a registry key.\n" |
| 42 | "ConnectRegistry() - Establishes a connection to a predefined registry handle\n" |
| 43 | " on another computer.\n" |
| 44 | "CreateKey() - Creates the specified key, or opens it if it already exists.\n" |
| 45 | "DeleteKey() - Deletes the specified key.\n" |
| 46 | "DeleteValue() - Removes a named value from the specified registry key.\n" |
| 47 | "EnumKey() - Enumerates subkeys of the specified open registry key.\n" |
| 48 | "EnumValue() - Enumerates values of the specified open registry key.\n" |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 49 | "ExpandEnvironmentStrings() - Expand the env strings in a REG_EXPAND_SZ string.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 50 | "FlushKey() - Writes all the attributes of the specified key to the registry.\n" |
| 51 | "LoadKey() - Creates a subkey under HKEY_USER or HKEY_LOCAL_MACHINE and stores\n" |
| 52 | " registration information from a specified file into that subkey.\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 53 | "OpenKey() - Opens the specified key.\n" |
| 54 | "OpenKeyEx() - Alias of OpenKey().\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 55 | "QueryValue() - Retrieves the value associated with the unnamed value for a\n" |
| 56 | " specified key in the registry.\n" |
| 57 | "QueryValueEx() - Retrieves the type and data for a specified value name\n" |
| 58 | " associated with an open registry key.\n" |
| 59 | "QueryInfoKey() - Returns information about the specified key.\n" |
| 60 | "SaveKey() - Saves the specified key, and all its subkeys a file.\n" |
| 61 | "SetValue() - Associates a value with a specified key.\n" |
| 62 | "SetValueEx() - Stores data in the value field of an open registry key.\n" |
| 63 | "\n" |
| 64 | "Special objects:\n" |
| 65 | "\n" |
| 66 | "HKEYType -- type object for HKEY objects\n" |
| 67 | "error -- exception raised for Win32 errors\n" |
| 68 | "\n" |
| 69 | "Integer constants:\n" |
| 70 | "Many constants are defined - see the documentation for each function\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 71 | "to see what constants are used, and where."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 72 | |
| 73 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 74 | PyDoc_STRVAR(CloseKey_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 75 | "CloseKey(hkey)\n" |
| 76 | "Closes a previously opened registry key.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 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öwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 81 | "closed when the hkey object is destroyed by Python."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 82 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 83 | PyDoc_STRVAR(ConnectRegistry_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 84 | "ConnectRegistry(computer_name, key) -> key\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 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" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 88 | " If None, the local computer is used.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 89 | "key is the predefined handle to connect to.\n" |
| 90 | "\n" |
| 91 | "The return value is the handle of the opened key.\n" |
Andrew Svetlov | 616f803 | 2012-10-31 19:29:33 +0200 | [diff] [blame] | 92 | "If the function fails, an OSError exception is raised."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 93 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 94 | PyDoc_STRVAR(CreateKey_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 95 | "CreateKey(key, sub_key) -> key\n" |
| 96 | "Creates or opens the specified key.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 97 | "\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 98 | "key is an already open key, or one of the predefined HKEY_* constants.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 99 | "sub_key is a string that names the key this method opens or creates.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 100 | "\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 101 | "If key is one of the predefined keys, sub_key may be None. In that case,\n" |
| 102 | "the handle returned is the same key handle passed in to the function.\n" |
| 103 | "\n" |
| 104 | "If the key already exists, this function opens the existing key.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 105 | "\n" |
| 106 | "The return value is the handle of the opened key.\n" |
Andrew Svetlov | 616f803 | 2012-10-31 19:29:33 +0200 | [diff] [blame] | 107 | "If the function fails, an OSError exception is raised."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 108 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 109 | PyDoc_STRVAR(CreateKeyEx_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 110 | "CreateKeyEx(key, sub_key, reserved=0, access=KEY_WRITE) -> key\n" |
| 111 | "Creates or opens the specified key.\n" |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 112 | "\n" |
| 113 | "key is an already open key, or one of the predefined HKEY_* constants\n" |
| 114 | "sub_key is a string that names the key this method opens or creates.\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 115 | "reserved is a reserved integer, and must be zero. Default is zero.\n" |
| 116 | "access is an integer that specifies an access mask that describes the \n" |
| 117 | " desired security access for the key. Default is KEY_WRITE.\n" |
| 118 | "\n" |
| 119 | "If key is one of the predefined keys, sub_key may be None. In that case,\n" |
| 120 | "the handle returned is the same key handle passed in to the function.\n" |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 121 | "\n" |
| 122 | "If the key already exists, this function opens the existing key\n" |
| 123 | "\n" |
| 124 | "The return value is the handle of the opened key.\n" |
Andrew Svetlov | 616f803 | 2012-10-31 19:29:33 +0200 | [diff] [blame] | 125 | "If the function fails, an OSError exception is raised."); |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 126 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 127 | PyDoc_STRVAR(DeleteKey_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 128 | "DeleteKey(key, sub_key)\n" |
| 129 | "Deletes the specified key.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 130 | "\n" |
| 131 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 132 | "sub_key is a string that must be a subkey of the key identified by the key\n" |
| 133 | " parameter. This value must not be None, and the key may not have\n" |
| 134 | " subkeys.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 135 | "\n" |
| 136 | "This method can not delete keys with subkeys.\n" |
| 137 | "\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 138 | "If the function succeeds, the entire key, including all of its values,\n" |
Andrew Svetlov | 616f803 | 2012-10-31 19:29:33 +0200 | [diff] [blame] | 139 | "is removed. If the function fails, an OSError exception is raised."); |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 140 | |
| 141 | PyDoc_STRVAR(DeleteKeyEx_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 142 | "DeleteKeyEx(key, sub_key, access=KEY_WOW64_64KEY, reserved=0)\n" |
| 143 | "Deletes the specified key (64-bit OS only).\n" |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 144 | "\n" |
| 145 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 146 | "sub_key is a string that must be a subkey of the key identified by the key\n" |
| 147 | " parameter. This value must not be None, and the key may not have\n" |
| 148 | " subkeys.\n" |
| 149 | "reserved is a reserved integer, and must be zero. Default is zero.\n" |
| 150 | "access is an integer that specifies an access mask that describes the \n" |
| 151 | " desired security access for the key. Default is KEY_WOW64_64KEY.\n" |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 152 | "\n" |
| 153 | "This method can not delete keys with subkeys.\n" |
| 154 | "\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 155 | "If the function succeeds, the entire key, including all of its values,\n" |
Andrew Svetlov | 616f803 | 2012-10-31 19:29:33 +0200 | [diff] [blame] | 156 | "is removed. If the function fails, an OSError exception is raised.\n" |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 157 | "On unsupported Windows versions, NotImplementedError is raised."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 158 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 159 | PyDoc_STRVAR(DeleteValue_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 160 | "DeleteValue(key, value)\n" |
| 161 | "Removes a named value from a registry key.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 162 | "\n" |
| 163 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 164 | "value is a string that identifies the value to remove."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 165 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 166 | PyDoc_STRVAR(EnumKey_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 167 | "EnumKey(key, index) -> string\n" |
| 168 | "Enumerates subkeys of an open registry key.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 169 | "\n" |
| 170 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
| 171 | "index is an integer that identifies the index of the key to retrieve.\n" |
| 172 | "\n" |
| 173 | "The function retrieves the name of one subkey each time it is called.\n" |
Andrew Svetlov | 616f803 | 2012-10-31 19:29:33 +0200 | [diff] [blame] | 174 | "It is typically called repeatedly until an OSError exception is\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 175 | "raised, indicating no more values are available."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 176 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 177 | PyDoc_STRVAR(EnumValue_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 178 | "EnumValue(key, index) -> tuple\n" |
| 179 | "Enumerates values of an open registry key.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 180 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
| 181 | "index is an integer that identifies the index of the value to retrieve.\n" |
| 182 | "\n" |
| 183 | "The function retrieves the name of one subkey each time it is called.\n" |
Andrew Svetlov | 616f803 | 2012-10-31 19:29:33 +0200 | [diff] [blame] | 184 | "It is typically called repeatedly, until an OSError exception\n" |
Mark Hammond | b422f95 | 2000-06-09 06:01:47 +0000 | [diff] [blame] | 185 | "is raised, indicating no more values.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 186 | "\n" |
| 187 | "The result is a tuple of 3 items:\n" |
| 188 | "value_name is a string that identifies the value.\n" |
| 189 | "value_data is an object that holds the value data, and whose type depends\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 190 | " on the underlying registry type.\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 191 | "data_type is an integer that identifies the type of the value data."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 192 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 193 | PyDoc_STRVAR(ExpandEnvironmentStrings_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 194 | "ExpandEnvironmentStrings(string) -> string\n" |
| 195 | "Expand environment vars.\n"); |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 196 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 197 | PyDoc_STRVAR(FlushKey_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 198 | "FlushKey(key)\n" |
| 199 | "Writes all the attributes of a key to the registry.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 200 | "\n" |
| 201 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
| 202 | "\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 203 | "It is not necessary to call FlushKey to change a key. Registry changes are\n" |
| 204 | "flushed to disk by the registry using its lazy flusher. Registry changes are\n" |
| 205 | "also flushed to disk at system shutdown. Unlike CloseKey(), the FlushKey()\n" |
| 206 | "method returns only when all the data has been written to the registry.\n" |
| 207 | "\n" |
| 208 | "An application should only call FlushKey() if it requires absolute certainty\n" |
| 209 | "that registry changes are on disk. If you don't know whether a FlushKey()\n" |
| 210 | "call is required, it probably isn't."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 211 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 212 | PyDoc_STRVAR(LoadKey_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 213 | "LoadKey(key, sub_key, file_name)\n" |
| 214 | "Creates a subkey under the specified key and stores registration information\n" |
| 215 | "from a specified file into that subkey.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 216 | "\n" |
| 217 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 218 | "sub_key is a string that identifies the sub_key to load.\n" |
| 219 | "file_name is the name of the file to load registry data from. This file must\n" |
| 220 | " have been created with the SaveKey() function. Under the file\n" |
| 221 | " allocation table (FAT) file system, the filename may not have an\n" |
| 222 | " extension.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 223 | "\n" |
| 224 | "A call to LoadKey() fails if the calling process does not have the\n" |
| 225 | "SE_RESTORE_PRIVILEGE privilege.\n" |
| 226 | "\n" |
| 227 | "If key is a handle returned by ConnectRegistry(), then the path specified\n" |
| 228 | "in fileName is relative to the remote computer.\n" |
| 229 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 230 | "The docs imply key must be in the HKEY_USER or HKEY_LOCAL_MACHINE tree"); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 231 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 232 | PyDoc_STRVAR(OpenKey_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 233 | "OpenKey(key, sub_key, reserved=0, access=KEY_READ) -> key\n" |
| 234 | "Opens the specified key.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 235 | "\n" |
| 236 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 237 | "sub_key is a string that identifies the sub_key to open.\n" |
| 238 | "reserved is a reserved integer, and must be zero. Default is zero.\n" |
| 239 | "access is an integer that specifies an access mask that describes the desired\n" |
| 240 | " security access for the key. Default is KEY_READ\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 241 | "\n" |
| 242 | "The result is a new handle to the specified key\n" |
Andrew Svetlov | 616f803 | 2012-10-31 19:29:33 +0200 | [diff] [blame] | 243 | "If the function fails, an OSError exception is raised."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 244 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 245 | PyDoc_STRVAR(OpenKeyEx_doc, "See OpenKey()"); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 246 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 247 | PyDoc_STRVAR(QueryInfoKey_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 248 | "QueryInfoKey(key) -> tuple\n" |
| 249 | "Returns information about a key.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 250 | "\n" |
| 251 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
| 252 | "\n" |
| 253 | "The result is a tuple of 3 items:" |
| 254 | "An integer that identifies the number of sub keys this key has.\n" |
| 255 | "An integer that identifies the number of values this key has.\n" |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 256 | "An integer that identifies when the key was last modified (if available)\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 257 | " as 100's of nanoseconds since Jan 1, 1600."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 258 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 259 | PyDoc_STRVAR(QueryValue_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 260 | "QueryValue(key, sub_key) -> string\n" |
| 261 | "Retrieves the unnamed value for a key.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 262 | "\n" |
| 263 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
Mark Hammond | b422f95 | 2000-06-09 06:01:47 +0000 | [diff] [blame] | 264 | "sub_key is a string that holds the name of the subkey with which the value\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 265 | " is associated. If this parameter is None or empty, the function\n" |
| 266 | " retrieves the value set by the SetValue() method for the key\n" |
| 267 | " identified by key." |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 268 | "\n" |
| 269 | "Values in the registry have name, type, and data components. This method\n" |
| 270 | "retrieves the data for a key's first value that has a NULL name.\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 271 | "But the underlying API call doesn't return the type, Lame Lame Lame, DONT USE THIS!!!"); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 272 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 273 | PyDoc_STRVAR(QueryValueEx_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 274 | "QueryValueEx(key, value_name) -> (value, type_id)\n" |
| 275 | "Retrieves the type and data for a specified value name associated with an\n" |
| 276 | "open registry key.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 277 | "\n" |
| 278 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 279 | "value_name is a string indicating the value to query"); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 280 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 281 | PyDoc_STRVAR(SaveKey_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 282 | "SaveKey(key, file_name)\n" |
| 283 | "Saves the specified key, and all its subkeys to the specified file.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 284 | "\n" |
| 285 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 286 | "file_name is the name of the file to save registry data to. This file cannot\n" |
| 287 | " already exist. If this filename includes an extension, it cannot be\n" |
| 288 | " used on file allocation table (FAT) file systems by the LoadKey(),\n" |
| 289 | " ReplaceKey() or RestoreKey() methods.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 290 | "\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 291 | "If key represents a key on a remote computer, the path described by file_name\n" |
| 292 | "is relative to the remote computer.\n" |
| 293 | "\n" |
| 294 | "The caller of this method must possess the SeBackupPrivilege security\n" |
| 295 | "privilege. This function passes NULL for security_attributes to the API."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 296 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 297 | PyDoc_STRVAR(SetValue_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 298 | "SetValue(key, sub_key, type, value)\n" |
| 299 | "Associates a value with a specified key.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 300 | "\n" |
| 301 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
| 302 | "sub_key is a string that names the subkey with which the value is associated.\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 303 | "type is an integer that specifies the type of the data. Currently this must\n" |
| 304 | " be REG_SZ, meaning only strings are supported.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 305 | "value is a string that specifies the new value.\n" |
| 306 | "\n" |
| 307 | "If the key specified by the sub_key parameter does not exist, the SetValue\n" |
| 308 | "function creates it.\n" |
| 309 | "\n" |
| 310 | "Value lengths are limited by available memory. Long values (more than\n" |
| 311 | "2048 bytes) should be stored as files with the filenames stored in \n" |
| 312 | "the configuration registry. This helps the registry perform efficiently.\n" |
| 313 | "\n" |
| 314 | "The key identified by the key parameter must have been opened with\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 315 | "KEY_SET_VALUE access."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 316 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 317 | PyDoc_STRVAR(SetValueEx_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 318 | "SetValueEx(key, value_name, reserved, type, value)\n" |
| 319 | "Stores data in the value field of an open registry key.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 320 | "\n" |
| 321 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 322 | "value_name is a string containing the name of the value to set, or None.\n" |
| 323 | "reserved can be anything - zero is always passed to the API.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 324 | "type is an integer that specifies the type of the data. This should be one of:\n" |
| 325 | " REG_BINARY -- Binary data in any form.\n" |
| 326 | " REG_DWORD -- A 32-bit number.\n" |
| 327 | " REG_DWORD_LITTLE_ENDIAN -- A 32-bit number in little-endian format.\n" |
| 328 | " REG_DWORD_BIG_ENDIAN -- A 32-bit number in big-endian format.\n" |
| 329 | " REG_EXPAND_SZ -- A null-terminated string that contains unexpanded references\n" |
| 330 | " to environment variables (for example, %PATH%).\n" |
| 331 | " REG_LINK -- A Unicode symbolic link.\n" |
Mark Hammond | b422f95 | 2000-06-09 06:01:47 +0000 | [diff] [blame] | 332 | " REG_MULTI_SZ -- An sequence of null-terminated strings, terminated by\n" |
| 333 | " two null characters. Note that Python handles this\n" |
| 334 | " termination automatically.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 335 | " REG_NONE -- No defined value type.\n" |
| 336 | " REG_RESOURCE_LIST -- A device-driver resource list.\n" |
| 337 | " REG_SZ -- A null-terminated string.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 338 | "value is a string that specifies the new value.\n" |
| 339 | "\n" |
| 340 | "This method can also set additional value and type information for the\n" |
| 341 | "specified key. The key identified by the key parameter must have been\n" |
| 342 | "opened with KEY_SET_VALUE access.\n" |
| 343 | "\n" |
| 344 | "To open the key, use the CreateKeyEx() or OpenKeyEx() methods.\n" |
| 345 | "\n" |
| 346 | "Value lengths are limited by available memory. Long values (more than\n" |
| 347 | "2048 bytes) should be stored as files with the filenames stored in \n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 348 | "the configuration registry. This helps the registry perform efficiently."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 349 | |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 350 | PyDoc_STRVAR(DisableReflectionKey_doc, |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 351 | "Disables registry reflection for 32-bit processes running on a 64-bit\n" |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 352 | "Operating System. Will generally raise NotImplemented if executed on\n" |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 353 | "a 32-bit Operating System.\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 354 | "\n" |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 355 | "If the key is not on the reflection list, the function succeeds but has no effect.\n" |
| 356 | "Disabling reflection for a key does not affect reflection of any subkeys."); |
| 357 | |
| 358 | PyDoc_STRVAR(EnableReflectionKey_doc, |
| 359 | "Restores registry reflection for the specified disabled key.\n" |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 360 | "Will generally raise NotImplemented if executed on a 32-bit Operating System.\n" |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 361 | "Restoring reflection for a key does not affect reflection of any subkeys."); |
| 362 | |
| 363 | PyDoc_STRVAR(QueryReflectionKey_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 364 | "QueryReflectionKey(hkey) -> bool\n" |
| 365 | "Determines the reflection state for the specified key.\n" |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 366 | "Will generally raise NotImplemented if executed on a 32-bit Operating System.\n"); |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 367 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 368 | /* PyHKEY docstrings */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 369 | PyDoc_STRVAR(PyHKEY_doc, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 370 | "PyHKEY Object - A Python object, representing a win32 registry key.\n" |
| 371 | "\n" |
Mark Hammond | b422f95 | 2000-06-09 06:01:47 +0000 | [diff] [blame] | 372 | "This object wraps a Windows HKEY object, automatically closing it when\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 373 | "the object is destroyed. To guarantee cleanup, you can call either\n" |
| 374 | "the Close() method on the PyHKEY, or the CloseKey() method.\n" |
| 375 | "\n" |
| 376 | "All functions which accept a handle object also accept an integer - \n" |
| 377 | "however, use of the handle object is encouraged.\n" |
| 378 | "\n" |
| 379 | "Functions:\n" |
| 380 | "Close() - Closes the underlying handle.\n" |
| 381 | "Detach() - Returns the integer Win32 handle, detaching it from the object\n" |
| 382 | "\n" |
| 383 | "Properties:\n" |
| 384 | "handle - The integer Win32 handle.\n" |
| 385 | "\n" |
| 386 | "Operations:\n" |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 387 | "__bool__ - Handles with an open object return true, otherwise false.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 388 | "__int__ - Converting a handle to an integer returns the Win32 handle.\n" |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 389 | "rich comparison - Handle objects are compared using the handle value."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 390 | |
| 391 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 392 | PyDoc_STRVAR(PyHKEY_Close_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 393 | "key.Close()\n" |
| 394 | "Closes the underlying Windows handle.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 395 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 396 | "If the handle is already closed, no error is raised."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 397 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 398 | PyDoc_STRVAR(PyHKEY_Detach_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 399 | "key.Detach() -> int\n" |
| 400 | "Detaches the Windows handle from the handle object.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 401 | "\n" |
| 402 | "The result is the value of the handle before it is detached. If the\n" |
| 403 | "handle is already detached, this will return zero.\n" |
| 404 | "\n" |
| 405 | "After calling this function, the handle is effectively invalidated,\n" |
| 406 | "but the handle is not closed. You would call this function when you\n" |
| 407 | "need the underlying win32 handle to exist beyond the lifetime of the\n" |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 408 | "handle object."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 409 | |
| 410 | |
| 411 | /************************************************************************ |
| 412 | |
| 413 | The PyHKEY object definition |
| 414 | |
| 415 | ************************************************************************/ |
| 416 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 417 | PyObject_VAR_HEAD |
| 418 | HKEY hkey; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 419 | } PyHKEYObject; |
| 420 | |
| 421 | #define PyHKEY_Check(op) ((op)->ob_type == &PyHKEY_Type) |
| 422 | |
| 423 | static char *failMsg = "bad operand type"; |
| 424 | |
| 425 | static PyObject * |
| 426 | PyHKEY_unaryFailureFunc(PyObject *ob) |
| 427 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 428 | PyErr_SetString(PyExc_TypeError, failMsg); |
| 429 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 430 | } |
| 431 | static PyObject * |
| 432 | PyHKEY_binaryFailureFunc(PyObject *ob1, PyObject *ob2) |
| 433 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 434 | PyErr_SetString(PyExc_TypeError, failMsg); |
| 435 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 436 | } |
| 437 | static PyObject * |
| 438 | PyHKEY_ternaryFailureFunc(PyObject *ob1, PyObject *ob2, PyObject *ob3) |
| 439 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 440 | PyErr_SetString(PyExc_TypeError, failMsg); |
| 441 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | static void |
| 445 | PyHKEY_deallocFunc(PyObject *ob) |
| 446 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 447 | /* Can not call PyHKEY_Close, as the ob->tp_type |
| 448 | has already been cleared, thus causing the type |
| 449 | check to fail! |
| 450 | */ |
| 451 | PyHKEYObject *obkey = (PyHKEYObject *)ob; |
| 452 | if (obkey->hkey) |
| 453 | RegCloseKey((HKEY)obkey->hkey); |
| 454 | PyObject_DEL(ob); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 458 | PyHKEY_boolFunc(PyObject *ob) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 459 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 460 | return ((PyHKEYObject *)ob)->hkey != 0; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | static PyObject * |
| 464 | PyHKEY_intFunc(PyObject *ob) |
| 465 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 466 | PyHKEYObject *pyhkey = (PyHKEYObject *)ob; |
| 467 | return PyLong_FromVoidPtr(pyhkey->hkey); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 470 | static PyObject * |
| 471 | PyHKEY_strFunc(PyObject *ob) |
| 472 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 473 | PyHKEYObject *pyhkey = (PyHKEYObject *)ob; |
| 474 | return PyUnicode_FromFormat("<PyHKEY:%p>", pyhkey->hkey); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | static int |
| 478 | PyHKEY_compareFunc(PyObject *ob1, PyObject *ob2) |
| 479 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 480 | PyHKEYObject *pyhkey1 = (PyHKEYObject *)ob1; |
| 481 | PyHKEYObject *pyhkey2 = (PyHKEYObject *)ob2; |
| 482 | return pyhkey1 == pyhkey2 ? 0 : |
| 483 | (pyhkey1 < pyhkey2 ? -1 : 1); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Antoine Pitrou | fbb1c61 | 2010-10-23 17:37:54 +0000 | [diff] [blame] | 486 | static Py_hash_t |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 487 | PyHKEY_hashFunc(PyObject *ob) |
| 488 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 489 | /* Just use the address. |
| 490 | XXX - should we use the handle value? |
| 491 | */ |
| 492 | return _Py_HashPointer(ob); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | |
| 496 | static PyNumberMethods PyHKEY_NumberMethods = |
| 497 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 498 | PyHKEY_binaryFailureFunc, /* nb_add */ |
| 499 | PyHKEY_binaryFailureFunc, /* nb_subtract */ |
| 500 | PyHKEY_binaryFailureFunc, /* nb_multiply */ |
| 501 | PyHKEY_binaryFailureFunc, /* nb_remainder */ |
| 502 | PyHKEY_binaryFailureFunc, /* nb_divmod */ |
| 503 | PyHKEY_ternaryFailureFunc, /* nb_power */ |
| 504 | PyHKEY_unaryFailureFunc, /* nb_negative */ |
| 505 | PyHKEY_unaryFailureFunc, /* nb_positive */ |
| 506 | PyHKEY_unaryFailureFunc, /* nb_absolute */ |
| 507 | PyHKEY_boolFunc, /* nb_bool */ |
| 508 | PyHKEY_unaryFailureFunc, /* nb_invert */ |
| 509 | PyHKEY_binaryFailureFunc, /* nb_lshift */ |
| 510 | PyHKEY_binaryFailureFunc, /* nb_rshift */ |
| 511 | PyHKEY_binaryFailureFunc, /* nb_and */ |
| 512 | PyHKEY_binaryFailureFunc, /* nb_xor */ |
| 513 | PyHKEY_binaryFailureFunc, /* nb_or */ |
| 514 | PyHKEY_intFunc, /* nb_int */ |
| 515 | 0, /* nb_reserved */ |
| 516 | PyHKEY_unaryFailureFunc, /* nb_float */ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 517 | }; |
| 518 | |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 519 | static PyObject *PyHKEY_CloseMethod(PyObject *self, PyObject *args); |
| 520 | static PyObject *PyHKEY_DetachMethod(PyObject *self, PyObject *args); |
| 521 | static PyObject *PyHKEY_Enter(PyObject *self); |
| 522 | static PyObject *PyHKEY_Exit(PyObject *self, PyObject *args); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 523 | |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 524 | static struct PyMethodDef PyHKEY_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 525 | {"Close", PyHKEY_CloseMethod, METH_VARARGS, PyHKEY_Close_doc}, |
| 526 | {"Detach", PyHKEY_DetachMethod, METH_VARARGS, PyHKEY_Detach_doc}, |
| 527 | {"__enter__", (PyCFunction)PyHKEY_Enter, METH_NOARGS, NULL}, |
| 528 | {"__exit__", PyHKEY_Exit, METH_VARARGS, NULL}, |
| 529 | {NULL} |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 530 | }; |
| 531 | |
| 532 | #define OFF(e) offsetof(PyHKEYObject, e) |
| 533 | static PyMemberDef PyHKEY_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 534 | {"handle", T_INT, OFF(hkey), READONLY}, |
| 535 | {NULL} /* Sentinel */ |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 536 | }; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 537 | |
| 538 | /* The type itself */ |
| 539 | PyTypeObject PyHKEY_Type = |
| 540 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 541 | PyVarObject_HEAD_INIT(0, 0) /* fill in type at module init */ |
| 542 | "PyHKEY", |
| 543 | sizeof(PyHKEYObject), |
| 544 | 0, |
| 545 | PyHKEY_deallocFunc, /* tp_dealloc */ |
| 546 | 0, /* tp_print */ |
| 547 | 0, /* tp_getattr */ |
| 548 | 0, /* tp_setattr */ |
| 549 | 0, /* tp_reserved */ |
| 550 | 0, /* tp_repr */ |
| 551 | &PyHKEY_NumberMethods, /* tp_as_number */ |
| 552 | 0, /* tp_as_sequence */ |
| 553 | 0, /* tp_as_mapping */ |
| 554 | PyHKEY_hashFunc, /* tp_hash */ |
| 555 | 0, /* tp_call */ |
| 556 | PyHKEY_strFunc, /* tp_str */ |
| 557 | 0, /* tp_getattro */ |
| 558 | 0, /* tp_setattro */ |
| 559 | 0, /* tp_as_buffer */ |
| 560 | 0, /* tp_flags */ |
| 561 | PyHKEY_doc, /* tp_doc */ |
| 562 | 0, /*tp_traverse*/ |
| 563 | 0, /*tp_clear*/ |
| 564 | 0, /*tp_richcompare*/ |
| 565 | 0, /*tp_weaklistoffset*/ |
| 566 | 0, /*tp_iter*/ |
| 567 | 0, /*tp_iternext*/ |
| 568 | PyHKEY_methods, /*tp_methods*/ |
| 569 | PyHKEY_memberlist, /*tp_members*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 570 | }; |
| 571 | |
| 572 | /************************************************************************ |
| 573 | |
| 574 | The PyHKEY object methods |
| 575 | |
| 576 | ************************************************************************/ |
| 577 | static PyObject * |
| 578 | PyHKEY_CloseMethod(PyObject *self, PyObject *args) |
| 579 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 580 | if (!PyArg_ParseTuple(args, ":Close")) |
| 581 | return NULL; |
| 582 | if (!PyHKEY_Close(self)) |
| 583 | return NULL; |
| 584 | Py_INCREF(Py_None); |
| 585 | return Py_None; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | static PyObject * |
| 589 | PyHKEY_DetachMethod(PyObject *self, PyObject *args) |
| 590 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 591 | void* ret; |
| 592 | PyHKEYObject *pThis = (PyHKEYObject *)self; |
| 593 | if (!PyArg_ParseTuple(args, ":Detach")) |
| 594 | return NULL; |
| 595 | ret = (void*)pThis->hkey; |
| 596 | pThis->hkey = 0; |
| 597 | return PyLong_FromVoidPtr(ret); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 600 | static PyObject * |
| 601 | PyHKEY_Enter(PyObject *self) |
| 602 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 603 | Py_XINCREF(self); |
| 604 | return self; |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | static PyObject * |
| 608 | PyHKEY_Exit(PyObject *self, PyObject *args) |
| 609 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 610 | if (!PyHKEY_Close(self)) |
| 611 | return NULL; |
| 612 | Py_RETURN_NONE; |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 616 | /************************************************************************ |
| 617 | The public PyHKEY API (well, not public yet :-) |
| 618 | ************************************************************************/ |
| 619 | PyObject * |
| 620 | PyHKEY_New(HKEY hInit) |
| 621 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 622 | PyHKEYObject *key = PyObject_NEW(PyHKEYObject, &PyHKEY_Type); |
| 623 | if (key) |
| 624 | key->hkey = hInit; |
| 625 | return (PyObject *)key; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | BOOL |
| 629 | PyHKEY_Close(PyObject *ob_handle) |
| 630 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 631 | LONG rc; |
| 632 | PyHKEYObject *key; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 633 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 634 | if (!PyHKEY_Check(ob_handle)) { |
| 635 | PyErr_SetString(PyExc_TypeError, "bad operand type"); |
| 636 | return FALSE; |
| 637 | } |
| 638 | key = (PyHKEYObject *)ob_handle; |
| 639 | rc = key->hkey ? RegCloseKey((HKEY)key->hkey) : ERROR_SUCCESS; |
| 640 | key->hkey = 0; |
| 641 | if (rc != ERROR_SUCCESS) |
| 642 | PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey"); |
| 643 | return rc == ERROR_SUCCESS; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | BOOL |
| 647 | PyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK) |
| 648 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 649 | if (ob == Py_None) { |
| 650 | if (!bNoneOK) { |
| 651 | PyErr_SetString( |
| 652 | PyExc_TypeError, |
| 653 | "None is not a valid HKEY in this context"); |
| 654 | return FALSE; |
| 655 | } |
| 656 | *pHANDLE = (HKEY)0; |
| 657 | } |
| 658 | else if (PyHKEY_Check(ob)) { |
| 659 | PyHKEYObject *pH = (PyHKEYObject *)ob; |
| 660 | *pHANDLE = pH->hkey; |
| 661 | } |
| 662 | else if (PyLong_Check(ob)) { |
| 663 | /* We also support integers */ |
| 664 | PyErr_Clear(); |
| 665 | *pHANDLE = (HKEY)PyLong_AsVoidPtr(ob); |
| 666 | if (PyErr_Occurred()) |
| 667 | return FALSE; |
| 668 | } |
| 669 | else { |
| 670 | PyErr_SetString( |
| 671 | PyExc_TypeError, |
| 672 | "The object is not a PyHKEY object"); |
| 673 | return FALSE; |
| 674 | } |
| 675 | return TRUE; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | PyObject * |
| 679 | PyHKEY_FromHKEY(HKEY h) |
| 680 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 681 | PyHKEYObject *op; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 682 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 683 | /* Inline PyObject_New */ |
| 684 | op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject)); |
| 685 | if (op == NULL) |
| 686 | return PyErr_NoMemory(); |
| 687 | PyObject_INIT(op, &PyHKEY_Type); |
| 688 | op->hkey = h; |
| 689 | return (PyObject *)op; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | |
| 693 | /************************************************************************ |
| 694 | The module methods |
| 695 | ************************************************************************/ |
| 696 | BOOL |
| 697 | PyWinObject_CloseHKEY(PyObject *obHandle) |
| 698 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 699 | BOOL ok; |
| 700 | if (PyHKEY_Check(obHandle)) { |
| 701 | ok = PyHKEY_Close(obHandle); |
| 702 | } |
Fred Drake | 25e1726 | 2000-06-30 17:48:51 +0000 | [diff] [blame] | 703 | #if SIZEOF_LONG >= SIZEOF_HKEY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 704 | else if (PyLong_Check(obHandle)) { |
| 705 | long rc = RegCloseKey((HKEY)PyLong_AsLong(obHandle)); |
| 706 | ok = (rc == ERROR_SUCCESS); |
| 707 | if (!ok) |
| 708 | PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey"); |
| 709 | } |
Fred Drake | 25e1726 | 2000-06-30 17:48:51 +0000 | [diff] [blame] | 710 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 711 | else if (PyLong_Check(obHandle)) { |
| 712 | long rc = RegCloseKey((HKEY)PyLong_AsVoidPtr(obHandle)); |
| 713 | ok = (rc == ERROR_SUCCESS); |
| 714 | if (!ok) |
| 715 | PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey"); |
| 716 | } |
Fred Drake | 25e1726 | 2000-06-30 17:48:51 +0000 | [diff] [blame] | 717 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 718 | else { |
| 719 | PyErr_SetString( |
| 720 | PyExc_TypeError, |
| 721 | "A handle must be a HKEY object or an integer"); |
| 722 | return FALSE; |
| 723 | } |
| 724 | return ok; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | |
| 728 | /* |
| 729 | Private Helper functions for the registry interfaces |
| 730 | |
| 731 | ** Note that fixupMultiSZ and countString have both had changes |
| 732 | ** made to support "incorrect strings". The registry specification |
| 733 | ** calls for strings to be terminated with 2 null bytes. It seems |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 734 | ** some commercial packages install strings which dont conform, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 735 | ** causing this code to fail - however, "regedit" etc still work |
| 736 | ** with these strings (ie only we dont!). |
| 737 | */ |
| 738 | static void |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 739 | fixupMultiSZ(wchar_t **str, wchar_t *data, int len) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 740 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 741 | wchar_t *P; |
| 742 | int i; |
| 743 | wchar_t *Q; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 744 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 745 | Q = data + len; |
| 746 | for (P = data, i = 0; P < Q && *P != '\0'; P++, i++) { |
| 747 | str[i] = P; |
| 748 | for(; *P != '\0'; P++) |
| 749 | ; |
| 750 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | static int |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 754 | countStrings(wchar_t *data, int len) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 755 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 756 | int strings; |
| 757 | wchar_t *P; |
| 758 | wchar_t *Q = data + len; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 759 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 760 | for (P = data, strings = 0; P < Q && *P != '\0'; P++, strings++) |
| 761 | for (; P < Q && *P != '\0'; P++) |
| 762 | ; |
| 763 | return strings; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | /* Convert PyObject into Registry data. |
| 767 | Allocates space as needed. */ |
| 768 | static BOOL |
| 769 | Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize) |
| 770 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 771 | Py_ssize_t i,j; |
| 772 | switch (typ) { |
| 773 | case REG_DWORD: |
| 774 | if (value != Py_None && !PyLong_Check(value)) |
| 775 | return FALSE; |
| 776 | *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1); |
| 777 | if (*retDataBuf==NULL){ |
| 778 | PyErr_NoMemory(); |
| 779 | return FALSE; |
| 780 | } |
| 781 | *retDataSize = sizeof(DWORD); |
| 782 | if (value == Py_None) { |
| 783 | DWORD zero = 0; |
| 784 | memcpy(*retDataBuf, &zero, sizeof(DWORD)); |
| 785 | } |
| 786 | else { |
Brian Curtin | 12706f2 | 2012-12-27 10:12:45 -0600 | [diff] [blame] | 787 | DWORD d = PyLong_AsUnsignedLong(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 788 | memcpy(*retDataBuf, &d, sizeof(DWORD)); |
| 789 | } |
| 790 | break; |
| 791 | case REG_SZ: |
| 792 | case REG_EXPAND_SZ: |
| 793 | { |
Victor Stinner | be49244 | 2011-11-21 12:43:50 +0100 | [diff] [blame] | 794 | if (value != Py_None) { |
| 795 | Py_ssize_t len; |
| 796 | if (!PyUnicode_Check(value)) |
| 797 | return FALSE; |
| 798 | *retDataBuf = (BYTE*)PyUnicode_AsWideCharString(value, &len); |
| 799 | if (*retDataBuf == NULL) |
| 800 | return FALSE; |
| 801 | *retDataSize = Py_SAFE_DOWNCAST( |
| 802 | (len + 1) * sizeof(wchar_t), |
| 803 | Py_ssize_t, DWORD); |
| 804 | } |
| 805 | else { |
| 806 | *retDataBuf = (BYTE *)PyMem_NEW(wchar_t, 1); |
| 807 | if (*retDataBuf == NULL) { |
| 808 | PyErr_NoMemory(); |
| 809 | return FALSE; |
| 810 | } |
| 811 | ((wchar_t *)*retDataBuf)[0] = L'\0'; |
| 812 | *retDataSize = 1 * sizeof(wchar_t); |
| 813 | } |
| 814 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 815 | } |
| 816 | case REG_MULTI_SZ: |
| 817 | { |
| 818 | DWORD size = 0; |
| 819 | wchar_t *P; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 820 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 821 | if (value == Py_None) |
| 822 | i = 0; |
| 823 | else { |
| 824 | if (!PyList_Check(value)) |
| 825 | return FALSE; |
| 826 | i = PyList_Size(value); |
| 827 | } |
| 828 | for (j = 0; j < i; j++) |
| 829 | { |
| 830 | PyObject *t; |
Victor Stinner | be49244 | 2011-11-21 12:43:50 +0100 | [diff] [blame] | 831 | wchar_t *wstr; |
| 832 | Py_ssize_t len; |
| 833 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 834 | t = PyList_GET_ITEM(value, j); |
| 835 | if (!PyUnicode_Check(t)) |
| 836 | return FALSE; |
Victor Stinner | be49244 | 2011-11-21 12:43:50 +0100 | [diff] [blame] | 837 | wstr = PyUnicode_AsUnicodeAndSize(t, &len); |
| 838 | if (wstr == NULL) |
| 839 | return FALSE; |
| 840 | size += Py_SAFE_DOWNCAST((len + 1) * sizeof(wchar_t), |
Brian Curtin | abb3351 | 2010-08-17 20:08:40 +0000 | [diff] [blame] | 841 | size_t, DWORD); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 842 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 843 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 844 | *retDataSize = size + 2; |
| 845 | *retDataBuf = (BYTE *)PyMem_NEW(char, |
| 846 | *retDataSize); |
| 847 | if (*retDataBuf==NULL){ |
| 848 | PyErr_NoMemory(); |
| 849 | return FALSE; |
| 850 | } |
| 851 | P = (wchar_t *)*retDataBuf; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 852 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 853 | for (j = 0; j < i; j++) |
| 854 | { |
| 855 | PyObject *t; |
Victor Stinner | be49244 | 2011-11-21 12:43:50 +0100 | [diff] [blame] | 856 | wchar_t *wstr; |
| 857 | Py_ssize_t len; |
| 858 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 859 | t = PyList_GET_ITEM(value, j); |
Victor Stinner | be49244 | 2011-11-21 12:43:50 +0100 | [diff] [blame] | 860 | wstr = PyUnicode_AsUnicodeAndSize(t, &len); |
| 861 | if (wstr == NULL) |
| 862 | return FALSE; |
| 863 | wcscpy(P, wstr); |
| 864 | P += (len + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 865 | } |
| 866 | /* And doubly-terminate the list... */ |
| 867 | *P = '\0'; |
| 868 | break; |
| 869 | } |
| 870 | case REG_BINARY: |
| 871 | /* ALSO handle ALL unknown data types here. Even if we can't |
| 872 | support it natively, we should handle the bits. */ |
| 873 | default: |
| 874 | if (value == Py_None) |
| 875 | *retDataSize = 0; |
| 876 | else { |
| 877 | Py_buffer view; |
Neal Norwitz | 1385b89 | 2007-08-26 23:07:13 +0000 | [diff] [blame] | 878 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 879 | if (!PyObject_CheckBuffer(value)) { |
| 880 | PyErr_Format(PyExc_TypeError, |
| 881 | "Objects of type '%s' can not " |
| 882 | "be used as binary registry values", |
| 883 | value->ob_type->tp_name); |
| 884 | return FALSE; |
| 885 | } |
Neal Norwitz | 1385b89 | 2007-08-26 23:07:13 +0000 | [diff] [blame] | 886 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 887 | if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0) |
| 888 | return FALSE; |
Neal Norwitz | 1385b89 | 2007-08-26 23:07:13 +0000 | [diff] [blame] | 889 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 890 | *retDataBuf = (BYTE *)PyMem_NEW(char, view.len); |
| 891 | if (*retDataBuf==NULL){ |
| 892 | PyBuffer_Release(&view); |
| 893 | PyErr_NoMemory(); |
| 894 | return FALSE; |
| 895 | } |
Brian Curtin | abb3351 | 2010-08-17 20:08:40 +0000 | [diff] [blame] | 896 | *retDataSize = Py_SAFE_DOWNCAST(view.len, Py_ssize_t, DWORD); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 897 | memcpy(*retDataBuf, view.buf, view.len); |
| 898 | PyBuffer_Release(&view); |
| 899 | } |
| 900 | break; |
| 901 | } |
| 902 | return TRUE; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | /* Convert Registry data into PyObject*/ |
| 906 | static PyObject * |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 907 | Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 908 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 909 | PyObject *obData; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 910 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 911 | switch (typ) { |
| 912 | case REG_DWORD: |
| 913 | if (retDataSize == 0) |
Brian Curtin | 172e422 | 2012-12-27 14:04:42 -0600 | [diff] [blame] | 914 | obData = PyLong_FromUnsignedLong(0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 915 | else |
Brian Curtin | 172e422 | 2012-12-27 14:04:42 -0600 | [diff] [blame] | 916 | obData = PyLong_FromUnsignedLong(*(int *)retDataBuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 917 | break; |
| 918 | case REG_SZ: |
| 919 | case REG_EXPAND_SZ: |
| 920 | { |
| 921 | /* the buffer may or may not have a trailing NULL */ |
| 922 | wchar_t *data = (wchar_t *)retDataBuf; |
| 923 | int len = retDataSize / 2; |
| 924 | if (retDataSize && data[len-1] == '\0') |
| 925 | retDataSize -= 2; |
| 926 | if (retDataSize <= 0) |
| 927 | data = L""; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 928 | obData = PyUnicode_FromWideChar(data, retDataSize/2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 929 | break; |
| 930 | } |
| 931 | case REG_MULTI_SZ: |
| 932 | if (retDataSize == 0) |
| 933 | obData = PyList_New(0); |
| 934 | else |
| 935 | { |
| 936 | int index = 0; |
| 937 | wchar_t *data = (wchar_t *)retDataBuf; |
| 938 | int len = retDataSize / 2; |
| 939 | int s = countStrings(data, len); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 940 | wchar_t **str = (wchar_t **)PyMem_Malloc(sizeof(wchar_t *)*s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 941 | if (str == NULL) |
| 942 | return PyErr_NoMemory(); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 943 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 944 | fixupMultiSZ(str, data, len); |
| 945 | obData = PyList_New(s); |
Jesus Cea | 782c4cf | 2014-03-13 17:35:32 +0100 | [diff] [blame] | 946 | if (obData == NULL) { |
Victor Stinner | 9cb1ec5 | 2014-03-13 19:08:10 +0100 | [diff] [blame] | 947 | PyMem_Free(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 948 | return NULL; |
Jesus Cea | 782c4cf | 2014-03-13 17:35:32 +0100 | [diff] [blame] | 949 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 950 | for (index = 0; index < s; index++) |
| 951 | { |
| 952 | size_t len = wcslen(str[index]); |
| 953 | if (len > INT_MAX) { |
| 954 | PyErr_SetString(PyExc_OverflowError, |
| 955 | "registry string is too long for a Python string"); |
| 956 | Py_DECREF(obData); |
Victor Stinner | 9cb1ec5 | 2014-03-13 19:08:10 +0100 | [diff] [blame] | 957 | PyMem_Free(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 958 | return NULL; |
| 959 | } |
| 960 | PyList_SetItem(obData, |
| 961 | index, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 962 | PyUnicode_FromWideChar(str[index], len)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 963 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 964 | PyMem_Free(str); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 965 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 966 | break; |
| 967 | } |
| 968 | case REG_BINARY: |
| 969 | /* ALSO handle ALL unknown data types here. Even if we can't |
| 970 | support it natively, we should handle the bits. */ |
| 971 | default: |
| 972 | if (retDataSize == 0) { |
| 973 | Py_INCREF(Py_None); |
| 974 | obData = Py_None; |
| 975 | } |
| 976 | else |
| 977 | obData = PyBytes_FromStringAndSize( |
| 978 | (char *)retDataBuf, retDataSize); |
| 979 | break; |
| 980 | } |
| 981 | return obData; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | /* The Python methods */ |
| 985 | |
| 986 | static PyObject * |
| 987 | PyCloseKey(PyObject *self, PyObject *args) |
| 988 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 989 | PyObject *obKey; |
| 990 | if (!PyArg_ParseTuple(args, "O:CloseKey", &obKey)) |
| 991 | return NULL; |
| 992 | if (!PyHKEY_Close(obKey)) |
| 993 | return NULL; |
| 994 | Py_INCREF(Py_None); |
| 995 | return Py_None; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | static PyObject * |
| 999 | PyConnectRegistry(PyObject *self, PyObject *args) |
| 1000 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1001 | HKEY hKey; |
| 1002 | PyObject *obKey; |
| 1003 | wchar_t *szCompName = NULL; |
| 1004 | HKEY retKey; |
| 1005 | long rc; |
| 1006 | if (!PyArg_ParseTuple(args, "ZO:ConnectRegistry", &szCompName, &obKey)) |
| 1007 | return NULL; |
| 1008 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1009 | return NULL; |
| 1010 | Py_BEGIN_ALLOW_THREADS |
| 1011 | rc = RegConnectRegistryW(szCompName, hKey, &retKey); |
| 1012 | Py_END_ALLOW_THREADS |
| 1013 | if (rc != ERROR_SUCCESS) |
| 1014 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1015 | "ConnectRegistry"); |
| 1016 | return PyHKEY_FromHKEY(retKey); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | static PyObject * |
| 1020 | PyCreateKey(PyObject *self, PyObject *args) |
| 1021 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1022 | HKEY hKey; |
| 1023 | PyObject *obKey; |
| 1024 | wchar_t *subKey; |
| 1025 | HKEY retKey; |
| 1026 | long rc; |
| 1027 | if (!PyArg_ParseTuple(args, "OZ:CreateKey", &obKey, &subKey)) |
| 1028 | return NULL; |
| 1029 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1030 | return NULL; |
| 1031 | rc = RegCreateKeyW(hKey, subKey, &retKey); |
| 1032 | if (rc != ERROR_SUCCESS) |
| 1033 | return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey"); |
| 1034 | return PyHKEY_FromHKEY(retKey); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | static PyObject * |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1038 | PyCreateKeyEx(PyObject *self, PyObject *args, PyObject *kwargs) |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 1039 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1040 | HKEY hKey; |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1041 | PyObject *key; |
| 1042 | wchar_t *sub_key; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1043 | HKEY retKey; |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1044 | int reserved = 0; |
| 1045 | REGSAM access = KEY_WRITE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1046 | long rc; |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1047 | |
| 1048 | char *kwlist[] = {"key", "sub_key", "reserved", "access", NULL}; |
| 1049 | |
| 1050 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OZ|ii:CreateKeyEx", kwlist, |
| 1051 | &key, &sub_key, &reserved, &access)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1052 | return NULL; |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1053 | if (!PyHKEY_AsHKEY(key, &hKey, FALSE)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1054 | return NULL; |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 1055 | |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1056 | rc = RegCreateKeyExW(hKey, sub_key, reserved, NULL, (DWORD)NULL, |
| 1057 | access, NULL, &retKey, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1058 | if (rc != ERROR_SUCCESS) |
| 1059 | return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKeyEx"); |
| 1060 | return PyHKEY_FromHKEY(retKey); |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | static PyObject * |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1064 | PyDeleteKey(PyObject *self, PyObject *args) |
| 1065 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1066 | HKEY hKey; |
| 1067 | PyObject *obKey; |
| 1068 | wchar_t *subKey; |
| 1069 | long rc; |
| 1070 | if (!PyArg_ParseTuple(args, "Ou:DeleteKey", &obKey, &subKey)) |
| 1071 | return NULL; |
| 1072 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1073 | return NULL; |
| 1074 | rc = RegDeleteKeyW(hKey, subKey ); |
| 1075 | if (rc != ERROR_SUCCESS) |
| 1076 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey"); |
| 1077 | Py_INCREF(Py_None); |
| 1078 | return Py_None; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | static PyObject * |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1082 | PyDeleteKeyEx(PyObject *self, PyObject *args, PyObject *kwargs) |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 1083 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1084 | HKEY hKey; |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1085 | PyObject *key; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1086 | HMODULE hMod; |
| 1087 | typedef LONG (WINAPI *RDKEFunc)(HKEY, const wchar_t*, REGSAM, int); |
| 1088 | RDKEFunc pfn = NULL; |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1089 | wchar_t *sub_key; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1090 | long rc; |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1091 | int reserved = 0; |
| 1092 | REGSAM access = KEY_WOW64_64KEY; |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 1093 | |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1094 | char *kwlist[] = {"key", "sub_key", "access", "reserved", NULL}; |
| 1095 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Ou|ii:DeleteKeyEx", kwlist, |
| 1096 | &key, &sub_key, &access, &reserved)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1097 | return NULL; |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1098 | if (!PyHKEY_AsHKEY(key, &hKey, FALSE)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1099 | return NULL; |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 1100 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1101 | /* Only available on 64bit platforms, so we must load it |
| 1102 | dynamically. */ |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 1103 | hMod = GetModuleHandleW(L"advapi32.dll"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1104 | if (hMod) |
| 1105 | pfn = (RDKEFunc)GetProcAddress(hMod, |
| 1106 | "RegDeleteKeyExW"); |
| 1107 | if (!pfn) { |
| 1108 | PyErr_SetString(PyExc_NotImplementedError, |
| 1109 | "not implemented on this platform"); |
| 1110 | return NULL; |
| 1111 | } |
| 1112 | Py_BEGIN_ALLOW_THREADS |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1113 | rc = (*pfn)(hKey, sub_key, access, reserved); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1114 | Py_END_ALLOW_THREADS |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 1115 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1116 | if (rc != ERROR_SUCCESS) |
| 1117 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKeyEx"); |
| 1118 | Py_INCREF(Py_None); |
| 1119 | return Py_None; |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | static PyObject * |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1123 | PyDeleteValue(PyObject *self, PyObject *args) |
| 1124 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1125 | HKEY hKey; |
| 1126 | PyObject *obKey; |
| 1127 | wchar_t *subKey; |
| 1128 | long rc; |
| 1129 | if (!PyArg_ParseTuple(args, "OZ:DeleteValue", &obKey, &subKey)) |
| 1130 | return NULL; |
| 1131 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1132 | return NULL; |
| 1133 | Py_BEGIN_ALLOW_THREADS |
| 1134 | rc = RegDeleteValueW(hKey, subKey); |
| 1135 | Py_END_ALLOW_THREADS |
| 1136 | if (rc !=ERROR_SUCCESS) |
| 1137 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1138 | "RegDeleteValue"); |
| 1139 | Py_INCREF(Py_None); |
| 1140 | return Py_None; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | static PyObject * |
| 1144 | PyEnumKey(PyObject *self, PyObject *args) |
| 1145 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1146 | HKEY hKey; |
| 1147 | PyObject *obKey; |
| 1148 | int index; |
| 1149 | long rc; |
| 1150 | PyObject *retStr; |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1151 | |
| 1152 | /* The Windows docs claim that the max key name length is 255 |
| 1153 | * characters, plus a terminating nul character. However, |
| 1154 | * empirical testing demonstrates that it is possible to |
| 1155 | * create a 256 character key that is missing the terminating |
| 1156 | * nul. RegEnumKeyEx requires a 257 character buffer to |
| 1157 | * retrieve such a key name. */ |
| 1158 | wchar_t tmpbuf[257]; |
Kristján Valur Jónsson | 984dfa7 | 2012-04-02 15:23:29 +0000 | [diff] [blame] | 1159 | DWORD len = sizeof(tmpbuf)/sizeof(wchar_t); /* includes NULL terminator */ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1160 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1161 | if (!PyArg_ParseTuple(args, "Oi:EnumKey", &obKey, &index)) |
| 1162 | return NULL; |
| 1163 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1164 | return NULL; |
Tim Peters | 313fcd4 | 2006-02-19 04:05:39 +0000 | [diff] [blame] | 1165 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1166 | Py_BEGIN_ALLOW_THREADS |
| 1167 | rc = RegEnumKeyExW(hKey, index, tmpbuf, &len, NULL, NULL, NULL, NULL); |
| 1168 | Py_END_ALLOW_THREADS |
| 1169 | if (rc != ERROR_SUCCESS) |
| 1170 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx"); |
Tim Peters | 313fcd4 | 2006-02-19 04:05:39 +0000 | [diff] [blame] | 1171 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1172 | retStr = PyUnicode_FromWideChar(tmpbuf, len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1173 | return retStr; /* can be NULL */ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | static PyObject * |
| 1177 | PyEnumValue(PyObject *self, PyObject *args) |
| 1178 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1179 | HKEY hKey; |
| 1180 | PyObject *obKey; |
| 1181 | int index; |
| 1182 | long rc; |
| 1183 | wchar_t *retValueBuf; |
Amaury Forgeot d'Arc | f2b69df | 2010-08-16 22:11:29 +0000 | [diff] [blame] | 1184 | BYTE *tmpBuf; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1185 | BYTE *retDataBuf; |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1186 | DWORD retValueSize, bufValueSize; |
| 1187 | DWORD retDataSize, bufDataSize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1188 | DWORD typ; |
| 1189 | PyObject *obData; |
| 1190 | PyObject *retVal; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1191 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1192 | if (!PyArg_ParseTuple(args, "Oi:EnumValue", &obKey, &index)) |
| 1193 | return NULL; |
| 1194 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1195 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1196 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1197 | if ((rc = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, NULL, NULL, |
| 1198 | NULL, |
| 1199 | &retValueSize, &retDataSize, NULL, NULL)) |
| 1200 | != ERROR_SUCCESS) |
| 1201 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1202 | "RegQueryInfoKey"); |
| 1203 | ++retValueSize; /* include null terminators */ |
| 1204 | ++retDataSize; |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1205 | bufDataSize = retDataSize; |
| 1206 | bufValueSize = retValueSize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1207 | retValueBuf = (wchar_t *)PyMem_Malloc(sizeof(wchar_t) * retValueSize); |
| 1208 | if (retValueBuf == NULL) |
| 1209 | return PyErr_NoMemory(); |
| 1210 | retDataBuf = (BYTE *)PyMem_Malloc(retDataSize); |
| 1211 | if (retDataBuf == NULL) { |
| 1212 | PyMem_Free(retValueBuf); |
| 1213 | return PyErr_NoMemory(); |
| 1214 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1215 | |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1216 | while (1) { |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1217 | Py_BEGIN_ALLOW_THREADS |
| 1218 | rc = RegEnumValueW(hKey, |
| 1219 | index, |
| 1220 | retValueBuf, |
| 1221 | &retValueSize, |
| 1222 | NULL, |
| 1223 | &typ, |
| 1224 | (BYTE *)retDataBuf, |
| 1225 | &retDataSize); |
| 1226 | Py_END_ALLOW_THREADS |
| 1227 | |
| 1228 | if (rc != ERROR_MORE_DATA) |
| 1229 | break; |
| 1230 | |
| 1231 | bufDataSize *= 2; |
Amaury Forgeot d'Arc | f2b69df | 2010-08-16 22:11:29 +0000 | [diff] [blame] | 1232 | tmpBuf = (BYTE *)PyMem_Realloc(retDataBuf, bufDataSize); |
Brian Curtin | 9b7e2d1 | 2010-06-08 20:57:52 +0000 | [diff] [blame] | 1233 | if (tmpBuf == NULL) { |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1234 | PyErr_NoMemory(); |
| 1235 | retVal = NULL; |
| 1236 | goto fail; |
| 1237 | } |
Brian Curtin | 9b7e2d1 | 2010-06-08 20:57:52 +0000 | [diff] [blame] | 1238 | retDataBuf = tmpBuf; |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1239 | retDataSize = bufDataSize; |
| 1240 | retValueSize = bufValueSize; |
| 1241 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1242 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1243 | if (rc != ERROR_SUCCESS) { |
| 1244 | retVal = PyErr_SetFromWindowsErrWithFunction(rc, |
| 1245 | "PyRegEnumValue"); |
| 1246 | goto fail; |
| 1247 | } |
| 1248 | obData = Reg2Py(retDataBuf, retDataSize, typ); |
| 1249 | if (obData == NULL) { |
| 1250 | retVal = NULL; |
| 1251 | goto fail; |
| 1252 | } |
| 1253 | retVal = Py_BuildValue("uOi", retValueBuf, obData, typ); |
| 1254 | Py_DECREF(obData); |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1255 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1256 | PyMem_Free(retValueBuf); |
| 1257 | PyMem_Free(retDataBuf); |
| 1258 | return retVal; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | static PyObject * |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1262 | PyExpandEnvironmentStrings(PyObject *self, PyObject *args) |
| 1263 | { |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 1264 | wchar_t *retValue = NULL; |
| 1265 | wchar_t *src; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1266 | DWORD retValueSize; |
| 1267 | DWORD rc; |
| 1268 | PyObject *o; |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1269 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1270 | if (!PyArg_ParseTuple(args, "u:ExpandEnvironmentStrings", &src)) |
| 1271 | return NULL; |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1272 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1273 | retValueSize = ExpandEnvironmentStringsW(src, retValue, 0); |
| 1274 | if (retValueSize == 0) { |
| 1275 | return PyErr_SetFromWindowsErrWithFunction(retValueSize, |
| 1276 | "ExpandEnvironmentStrings"); |
| 1277 | } |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 1278 | retValue = (wchar_t *)PyMem_Malloc(retValueSize * sizeof(wchar_t)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1279 | if (retValue == NULL) { |
| 1280 | return PyErr_NoMemory(); |
| 1281 | } |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1282 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1283 | rc = ExpandEnvironmentStringsW(src, retValue, retValueSize); |
| 1284 | if (rc == 0) { |
| 1285 | PyMem_Free(retValue); |
| 1286 | return PyErr_SetFromWindowsErrWithFunction(retValueSize, |
| 1287 | "ExpandEnvironmentStrings"); |
| 1288 | } |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 1289 | o = PyUnicode_FromWideChar(retValue, wcslen(retValue)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1290 | PyMem_Free(retValue); |
| 1291 | return o; |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | static PyObject * |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1295 | PyFlushKey(PyObject *self, PyObject *args) |
| 1296 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1297 | HKEY hKey; |
| 1298 | PyObject *obKey; |
| 1299 | long rc; |
| 1300 | if (!PyArg_ParseTuple(args, "O:FlushKey", &obKey)) |
| 1301 | return NULL; |
| 1302 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1303 | return NULL; |
| 1304 | Py_BEGIN_ALLOW_THREADS |
| 1305 | rc = RegFlushKey(hKey); |
| 1306 | Py_END_ALLOW_THREADS |
| 1307 | if (rc != ERROR_SUCCESS) |
| 1308 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey"); |
| 1309 | Py_INCREF(Py_None); |
| 1310 | return Py_None; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1311 | } |
| 1312 | static PyObject * |
| 1313 | PyLoadKey(PyObject *self, PyObject *args) |
| 1314 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1315 | HKEY hKey; |
| 1316 | PyObject *obKey; |
| 1317 | wchar_t *subKey; |
| 1318 | wchar_t *fileName; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1319 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1320 | long rc; |
| 1321 | if (!PyArg_ParseTuple(args, "Ouu:LoadKey", &obKey, &subKey, &fileName)) |
| 1322 | return NULL; |
| 1323 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1324 | return NULL; |
| 1325 | Py_BEGIN_ALLOW_THREADS |
| 1326 | rc = RegLoadKeyW(hKey, subKey, fileName ); |
| 1327 | Py_END_ALLOW_THREADS |
| 1328 | if (rc != ERROR_SUCCESS) |
| 1329 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey"); |
| 1330 | Py_INCREF(Py_None); |
| 1331 | return Py_None; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | static PyObject * |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1335 | PyOpenKey(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1336 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1337 | HKEY hKey; |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1338 | PyObject *key; |
| 1339 | wchar_t *sub_key; |
| 1340 | int reserved = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1341 | HKEY retKey; |
| 1342 | long rc; |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1343 | REGSAM access = KEY_READ; |
| 1344 | |
| 1345 | char *kwlist[] = {"key", "sub_key", "reserved", "access", NULL}; |
| 1346 | |
| 1347 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OZ|ii:OpenKey", kwlist, |
| 1348 | &key, &sub_key, &reserved, &access)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1349 | return NULL; |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1350 | if (!PyHKEY_AsHKEY(key, &hKey, FALSE)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1351 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1352 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1353 | Py_BEGIN_ALLOW_THREADS |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1354 | rc = RegOpenKeyExW(hKey, sub_key, reserved, access, &retKey); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1355 | Py_END_ALLOW_THREADS |
| 1356 | if (rc != ERROR_SUCCESS) |
| 1357 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx"); |
| 1358 | return PyHKEY_FromHKEY(retKey); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | |
| 1362 | static PyObject * |
| 1363 | PyQueryInfoKey(PyObject *self, PyObject *args) |
| 1364 | { |
| 1365 | HKEY hKey; |
| 1366 | PyObject *obKey; |
| 1367 | long rc; |
| 1368 | DWORD nSubKeys, nValues; |
| 1369 | FILETIME ft; |
| 1370 | LARGE_INTEGER li; |
| 1371 | PyObject *l; |
| 1372 | PyObject *ret; |
| 1373 | if (!PyArg_ParseTuple(args, "O:QueryInfoKey", &obKey)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1374 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1375 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1376 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1377 | if ((rc = RegQueryInfoKey(hKey, NULL, NULL, 0, &nSubKeys, NULL, NULL, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1378 | &nValues, NULL, NULL, NULL, &ft)) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1379 | != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1380 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey"); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1381 | li.LowPart = ft.dwLowDateTime; |
| 1382 | li.HighPart = ft.dwHighDateTime; |
| 1383 | l = PyLong_FromLongLong(li.QuadPart); |
| 1384 | if (l == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1385 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1386 | ret = Py_BuildValue("iiO", nSubKeys, nValues, l); |
| 1387 | Py_DECREF(l); |
| 1388 | return ret; |
| 1389 | } |
| 1390 | |
| 1391 | static PyObject * |
| 1392 | PyQueryValue(PyObject *self, PyObject *args) |
| 1393 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1394 | HKEY hKey; |
| 1395 | PyObject *obKey; |
| 1396 | wchar_t *subKey; |
| 1397 | long rc; |
| 1398 | PyObject *retStr; |
| 1399 | wchar_t *retBuf; |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1400 | DWORD bufSize = 0; |
| 1401 | DWORD retSize = 0; |
| 1402 | wchar_t *tmp; |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1403 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1404 | if (!PyArg_ParseTuple(args, "OZ:QueryValue", &obKey, &subKey)) |
| 1405 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1406 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1407 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1408 | return NULL; |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1409 | |
| 1410 | rc = RegQueryValueW(hKey, subKey, NULL, &retSize); |
| 1411 | if (rc == ERROR_MORE_DATA) |
| 1412 | retSize = 256; |
| 1413 | else if (rc != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1414 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1415 | "RegQueryValue"); |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1416 | |
| 1417 | bufSize = retSize; |
| 1418 | retBuf = (wchar_t *) PyMem_Malloc(bufSize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1419 | if (retBuf == NULL) |
| 1420 | return PyErr_NoMemory(); |
Guido van Rossum | a8c360e | 2007-07-17 20:50:43 +0000 | [diff] [blame] | 1421 | |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1422 | while (1) { |
| 1423 | retSize = bufSize; |
| 1424 | rc = RegQueryValueW(hKey, subKey, retBuf, &retSize); |
| 1425 | if (rc != ERROR_MORE_DATA) |
| 1426 | break; |
| 1427 | |
| 1428 | bufSize *= 2; |
| 1429 | tmp = (wchar_t *) PyMem_Realloc(retBuf, bufSize); |
| 1430 | if (tmp == NULL) { |
| 1431 | PyMem_Free(retBuf); |
| 1432 | return PyErr_NoMemory(); |
| 1433 | } |
| 1434 | retBuf = tmp; |
| 1435 | } |
| 1436 | |
| 1437 | if (rc != ERROR_SUCCESS) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1438 | PyMem_Free(retBuf); |
| 1439 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1440 | "RegQueryValue"); |
| 1441 | } |
Guido van Rossum | a8c360e | 2007-07-17 20:50:43 +0000 | [diff] [blame] | 1442 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1443 | retStr = PyUnicode_FromWideChar(retBuf, wcslen(retBuf)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1444 | PyMem_Free(retBuf); |
| 1445 | return retStr; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | static PyObject * |
| 1449 | PyQueryValueEx(PyObject *self, PyObject *args) |
| 1450 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1451 | HKEY hKey; |
| 1452 | PyObject *obKey; |
| 1453 | wchar_t *valueName; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1454 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1455 | long rc; |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1456 | BYTE *retBuf, *tmp; |
| 1457 | DWORD bufSize = 0, retSize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1458 | DWORD typ; |
| 1459 | PyObject *obData; |
| 1460 | PyObject *result; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1461 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1462 | if (!PyArg_ParseTuple(args, "OZ:QueryValueEx", &obKey, &valueName)) |
| 1463 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1464 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1465 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1466 | return NULL; |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1467 | |
| 1468 | rc = RegQueryValueExW(hKey, valueName, NULL, NULL, NULL, &bufSize); |
| 1469 | if (rc == ERROR_MORE_DATA) |
| 1470 | bufSize = 256; |
| 1471 | else if (rc != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1472 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1473 | "RegQueryValueEx"); |
| 1474 | retBuf = (BYTE *)PyMem_Malloc(bufSize); |
| 1475 | if (retBuf == NULL) |
| 1476 | return PyErr_NoMemory(); |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1477 | |
| 1478 | while (1) { |
| 1479 | retSize = bufSize; |
| 1480 | rc = RegQueryValueExW(hKey, valueName, NULL, &typ, |
| 1481 | (BYTE *)retBuf, &retSize); |
| 1482 | if (rc != ERROR_MORE_DATA) |
| 1483 | break; |
| 1484 | |
| 1485 | bufSize *= 2; |
| 1486 | tmp = (char *) PyMem_Realloc(retBuf, bufSize); |
| 1487 | if (tmp == NULL) { |
| 1488 | PyMem_Free(retBuf); |
| 1489 | return PyErr_NoMemory(); |
| 1490 | } |
| 1491 | retBuf = tmp; |
| 1492 | } |
| 1493 | |
| 1494 | if (rc != ERROR_SUCCESS) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1495 | PyMem_Free(retBuf); |
| 1496 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1497 | "RegQueryValueEx"); |
| 1498 | } |
| 1499 | obData = Reg2Py(retBuf, bufSize, typ); |
| 1500 | PyMem_Free(retBuf); |
| 1501 | if (obData == NULL) |
| 1502 | return NULL; |
| 1503 | result = Py_BuildValue("Oi", obData, typ); |
| 1504 | Py_DECREF(obData); |
| 1505 | return result; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | |
| 1509 | static PyObject * |
| 1510 | PySaveKey(PyObject *self, PyObject *args) |
| 1511 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1512 | HKEY hKey; |
| 1513 | PyObject *obKey; |
| 1514 | wchar_t *fileName; |
| 1515 | LPSECURITY_ATTRIBUTES pSA = NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1516 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1517 | long rc; |
| 1518 | if (!PyArg_ParseTuple(args, "Ou:SaveKey", &obKey, &fileName)) |
| 1519 | return NULL; |
| 1520 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1521 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1522 | /* One day we may get security into the core? |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1523 | if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE)) |
| 1524 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1525 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1526 | Py_BEGIN_ALLOW_THREADS |
| 1527 | rc = RegSaveKeyW(hKey, fileName, pSA ); |
| 1528 | Py_END_ALLOW_THREADS |
| 1529 | if (rc != ERROR_SUCCESS) |
| 1530 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey"); |
| 1531 | Py_INCREF(Py_None); |
| 1532 | return Py_None; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | static PyObject * |
| 1536 | PySetValue(PyObject *self, PyObject *args) |
| 1537 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1538 | HKEY hKey; |
| 1539 | PyObject *obKey; |
| 1540 | wchar_t *subKey; |
| 1541 | wchar_t *str; |
| 1542 | DWORD typ; |
| 1543 | DWORD len; |
| 1544 | long rc; |
| 1545 | if (!PyArg_ParseTuple(args, "OZiu#:SetValue", |
| 1546 | &obKey, |
| 1547 | &subKey, |
| 1548 | &typ, |
| 1549 | &str, |
| 1550 | &len)) |
| 1551 | return NULL; |
| 1552 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1553 | return NULL; |
| 1554 | if (typ != REG_SZ) { |
| 1555 | PyErr_SetString(PyExc_TypeError, |
| 1556 | "Type must be winreg.REG_SZ"); |
| 1557 | return NULL; |
| 1558 | } |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1559 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1560 | Py_BEGIN_ALLOW_THREADS |
| 1561 | rc = RegSetValueW(hKey, subKey, REG_SZ, str, len+1); |
| 1562 | Py_END_ALLOW_THREADS |
| 1563 | if (rc != ERROR_SUCCESS) |
| 1564 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue"); |
| 1565 | Py_INCREF(Py_None); |
| 1566 | return Py_None; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | static PyObject * |
| 1570 | PySetValueEx(PyObject *self, PyObject *args) |
| 1571 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1572 | HKEY hKey; |
| 1573 | PyObject *obKey; |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 1574 | wchar_t *valueName; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1575 | PyObject *obRes; |
| 1576 | PyObject *value; |
| 1577 | BYTE *data; |
| 1578 | DWORD len; |
| 1579 | DWORD typ; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1580 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1581 | LONG rc; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1582 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1583 | if (!PyArg_ParseTuple(args, "OZOiO:SetValueEx", |
| 1584 | &obKey, |
| 1585 | &valueName, |
| 1586 | &obRes, |
| 1587 | &typ, |
| 1588 | &value)) |
| 1589 | return NULL; |
| 1590 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1591 | return NULL; |
| 1592 | if (!Py2Reg(value, typ, &data, &len)) |
| 1593 | { |
| 1594 | if (!PyErr_Occurred()) |
| 1595 | PyErr_SetString(PyExc_ValueError, |
| 1596 | "Could not convert the data to the specified type."); |
| 1597 | return NULL; |
| 1598 | } |
| 1599 | Py_BEGIN_ALLOW_THREADS |
| 1600 | rc = RegSetValueExW(hKey, valueName, 0, typ, data, len); |
| 1601 | Py_END_ALLOW_THREADS |
| 1602 | PyMem_DEL(data); |
| 1603 | if (rc != ERROR_SUCCESS) |
| 1604 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1605 | "RegSetValueEx"); |
| 1606 | Py_INCREF(Py_None); |
| 1607 | return Py_None; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1610 | static PyObject * |
| 1611 | PyDisableReflectionKey(PyObject *self, PyObject *args) |
| 1612 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1613 | HKEY hKey; |
| 1614 | PyObject *obKey; |
| 1615 | HMODULE hMod; |
| 1616 | typedef LONG (WINAPI *RDRKFunc)(HKEY); |
| 1617 | RDRKFunc pfn = NULL; |
| 1618 | LONG rc; |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1619 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1620 | if (!PyArg_ParseTuple(args, "O:DisableReflectionKey", &obKey)) |
| 1621 | return NULL; |
| 1622 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1623 | return NULL; |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1624 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1625 | /* Only available on 64bit platforms, so we must load it |
| 1626 | dynamically.*/ |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 1627 | hMod = GetModuleHandleW(L"advapi32.dll"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1628 | if (hMod) |
| 1629 | pfn = (RDRKFunc)GetProcAddress(hMod, |
| 1630 | "RegDisableReflectionKey"); |
| 1631 | if (!pfn) { |
| 1632 | PyErr_SetString(PyExc_NotImplementedError, |
| 1633 | "not implemented on this platform"); |
| 1634 | return NULL; |
| 1635 | } |
| 1636 | Py_BEGIN_ALLOW_THREADS |
| 1637 | rc = (*pfn)(hKey); |
| 1638 | Py_END_ALLOW_THREADS |
| 1639 | if (rc != ERROR_SUCCESS) |
| 1640 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1641 | "RegDisableReflectionKey"); |
| 1642 | Py_INCREF(Py_None); |
| 1643 | return Py_None; |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | static PyObject * |
| 1647 | PyEnableReflectionKey(PyObject *self, PyObject *args) |
| 1648 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1649 | HKEY hKey; |
| 1650 | PyObject *obKey; |
| 1651 | HMODULE hMod; |
| 1652 | typedef LONG (WINAPI *RERKFunc)(HKEY); |
| 1653 | RERKFunc pfn = NULL; |
| 1654 | LONG rc; |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1655 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1656 | if (!PyArg_ParseTuple(args, "O:EnableReflectionKey", &obKey)) |
| 1657 | return NULL; |
| 1658 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1659 | return NULL; |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1660 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1661 | /* Only available on 64bit platforms, so we must load it |
| 1662 | dynamically.*/ |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 1663 | hMod = GetModuleHandleW(L"advapi32.dll"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1664 | if (hMod) |
| 1665 | pfn = (RERKFunc)GetProcAddress(hMod, |
| 1666 | "RegEnableReflectionKey"); |
| 1667 | if (!pfn) { |
| 1668 | PyErr_SetString(PyExc_NotImplementedError, |
| 1669 | "not implemented on this platform"); |
| 1670 | return NULL; |
| 1671 | } |
| 1672 | Py_BEGIN_ALLOW_THREADS |
| 1673 | rc = (*pfn)(hKey); |
| 1674 | Py_END_ALLOW_THREADS |
| 1675 | if (rc != ERROR_SUCCESS) |
| 1676 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1677 | "RegEnableReflectionKey"); |
| 1678 | Py_INCREF(Py_None); |
| 1679 | return Py_None; |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
| 1682 | static PyObject * |
| 1683 | PyQueryReflectionKey(PyObject *self, PyObject *args) |
| 1684 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1685 | HKEY hKey; |
| 1686 | PyObject *obKey; |
| 1687 | HMODULE hMod; |
| 1688 | typedef LONG (WINAPI *RQRKFunc)(HKEY, BOOL *); |
| 1689 | RQRKFunc pfn = NULL; |
| 1690 | BOOL result; |
| 1691 | LONG rc; |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1692 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1693 | if (!PyArg_ParseTuple(args, "O:QueryReflectionKey", &obKey)) |
| 1694 | return NULL; |
| 1695 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1696 | return NULL; |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1697 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1698 | /* Only available on 64bit platforms, so we must load it |
| 1699 | dynamically.*/ |
Martin v. Löwis | 50590f1 | 2012-01-14 17:54:09 +0100 | [diff] [blame] | 1700 | hMod = GetModuleHandleW(L"advapi32.dll"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1701 | if (hMod) |
| 1702 | pfn = (RQRKFunc)GetProcAddress(hMod, |
| 1703 | "RegQueryReflectionKey"); |
| 1704 | if (!pfn) { |
| 1705 | PyErr_SetString(PyExc_NotImplementedError, |
| 1706 | "not implemented on this platform"); |
| 1707 | return NULL; |
| 1708 | } |
| 1709 | Py_BEGIN_ALLOW_THREADS |
| 1710 | rc = (*pfn)(hKey, &result); |
| 1711 | Py_END_ALLOW_THREADS |
| 1712 | if (rc != ERROR_SUCCESS) |
| 1713 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1714 | "RegQueryReflectionKey"); |
| 1715 | return PyBool_FromLong(result); |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1718 | static struct PyMethodDef winreg_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1719 | {"CloseKey", PyCloseKey, METH_VARARGS, CloseKey_doc}, |
| 1720 | {"ConnectRegistry", PyConnectRegistry, METH_VARARGS, ConnectRegistry_doc}, |
| 1721 | {"CreateKey", PyCreateKey, METH_VARARGS, CreateKey_doc}, |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1722 | {"CreateKeyEx", (PyCFunction)PyCreateKeyEx, |
| 1723 | METH_VARARGS | METH_KEYWORDS, CreateKeyEx_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1724 | {"DeleteKey", PyDeleteKey, METH_VARARGS, DeleteKey_doc}, |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1725 | {"DeleteKeyEx", (PyCFunction)PyDeleteKeyEx, |
| 1726 | METH_VARARGS | METH_KEYWORDS, DeleteKeyEx_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1727 | {"DeleteValue", PyDeleteValue, METH_VARARGS, DeleteValue_doc}, |
| 1728 | {"DisableReflectionKey", PyDisableReflectionKey, METH_VARARGS, DisableReflectionKey_doc}, |
| 1729 | {"EnableReflectionKey", PyEnableReflectionKey, METH_VARARGS, EnableReflectionKey_doc}, |
| 1730 | {"EnumKey", PyEnumKey, METH_VARARGS, EnumKey_doc}, |
| 1731 | {"EnumValue", PyEnumValue, METH_VARARGS, EnumValue_doc}, |
| 1732 | {"ExpandEnvironmentStrings", PyExpandEnvironmentStrings, METH_VARARGS, |
| 1733 | ExpandEnvironmentStrings_doc }, |
| 1734 | {"FlushKey", PyFlushKey, METH_VARARGS, FlushKey_doc}, |
| 1735 | {"LoadKey", PyLoadKey, METH_VARARGS, LoadKey_doc}, |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1736 | {"OpenKey", (PyCFunction)PyOpenKey, METH_VARARGS | METH_KEYWORDS, |
| 1737 | OpenKey_doc}, |
| 1738 | {"OpenKeyEx", (PyCFunction)PyOpenKey, METH_VARARGS | METH_KEYWORDS, |
| 1739 | OpenKeyEx_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1740 | {"QueryValue", PyQueryValue, METH_VARARGS, QueryValue_doc}, |
| 1741 | {"QueryValueEx", PyQueryValueEx, METH_VARARGS, QueryValueEx_doc}, |
| 1742 | {"QueryInfoKey", PyQueryInfoKey, METH_VARARGS, QueryInfoKey_doc}, |
| 1743 | {"QueryReflectionKey",PyQueryReflectionKey,METH_VARARGS, QueryReflectionKey_doc}, |
| 1744 | {"SaveKey", PySaveKey, METH_VARARGS, SaveKey_doc}, |
| 1745 | {"SetValue", PySetValue, METH_VARARGS, SetValue_doc}, |
| 1746 | {"SetValueEx", PySetValueEx, METH_VARARGS, SetValueEx_doc}, |
| 1747 | NULL, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1748 | }; |
| 1749 | |
| 1750 | static void |
| 1751 | insint(PyObject * d, char * name, long value) |
| 1752 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1753 | PyObject *v = PyLong_FromLong(value); |
| 1754 | if (!v || PyDict_SetItemString(d, name, v)) |
| 1755 | PyErr_Clear(); |
| 1756 | Py_XDECREF(v); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | #define ADD_INT(val) insint(d, #val, val) |
| 1760 | |
| 1761 | static void |
| 1762 | inskey(PyObject * d, char * name, HKEY key) |
| 1763 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1764 | PyObject *v = PyLong_FromVoidPtr(key); |
| 1765 | if (!v || PyDict_SetItemString(d, name, v)) |
| 1766 | PyErr_Clear(); |
| 1767 | Py_XDECREF(v); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | #define ADD_KEY(val) inskey(d, #val, val) |
| 1771 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1772 | |
| 1773 | static struct PyModuleDef winregmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1774 | PyModuleDef_HEAD_INIT, |
| 1775 | "winreg", |
| 1776 | module_doc, |
| 1777 | -1, |
| 1778 | winreg_methods, |
| 1779 | NULL, |
| 1780 | NULL, |
| 1781 | NULL, |
| 1782 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1783 | }; |
| 1784 | |
| 1785 | PyMODINIT_FUNC PyInit_winreg(void) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1786 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1787 | PyObject *m, *d; |
| 1788 | m = PyModule_Create(&winregmodule); |
| 1789 | if (m == NULL) |
| 1790 | return NULL; |
| 1791 | d = PyModule_GetDict(m); |
| 1792 | PyHKEY_Type.tp_doc = PyHKEY_doc; |
| 1793 | if (PyType_Ready(&PyHKEY_Type) < 0) |
| 1794 | return NULL; |
| 1795 | Py_INCREF(&PyHKEY_Type); |
| 1796 | if (PyDict_SetItemString(d, "HKEYType", |
| 1797 | (PyObject *)&PyHKEY_Type) != 0) |
| 1798 | return NULL; |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1799 | Py_INCREF(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1800 | if (PyDict_SetItemString(d, "error", |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1801 | PyExc_OSError) != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1802 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1803 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1804 | /* Add the relevant constants */ |
| 1805 | ADD_KEY(HKEY_CLASSES_ROOT); |
| 1806 | ADD_KEY(HKEY_CURRENT_USER); |
| 1807 | ADD_KEY(HKEY_LOCAL_MACHINE); |
| 1808 | ADD_KEY(HKEY_USERS); |
| 1809 | ADD_KEY(HKEY_PERFORMANCE_DATA); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1810 | #ifdef HKEY_CURRENT_CONFIG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1811 | ADD_KEY(HKEY_CURRENT_CONFIG); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1812 | #endif |
| 1813 | #ifdef HKEY_DYN_DATA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1814 | ADD_KEY(HKEY_DYN_DATA); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1815 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1816 | ADD_INT(KEY_QUERY_VALUE); |
| 1817 | ADD_INT(KEY_SET_VALUE); |
| 1818 | ADD_INT(KEY_CREATE_SUB_KEY); |
| 1819 | ADD_INT(KEY_ENUMERATE_SUB_KEYS); |
| 1820 | ADD_INT(KEY_NOTIFY); |
| 1821 | ADD_INT(KEY_CREATE_LINK); |
| 1822 | ADD_INT(KEY_READ); |
| 1823 | ADD_INT(KEY_WRITE); |
| 1824 | ADD_INT(KEY_EXECUTE); |
| 1825 | ADD_INT(KEY_ALL_ACCESS); |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1826 | #ifdef KEY_WOW64_64KEY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1827 | ADD_INT(KEY_WOW64_64KEY); |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1828 | #endif |
| 1829 | #ifdef KEY_WOW64_32KEY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1830 | ADD_INT(KEY_WOW64_32KEY); |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1831 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1832 | ADD_INT(REG_OPTION_RESERVED); |
| 1833 | ADD_INT(REG_OPTION_NON_VOLATILE); |
| 1834 | ADD_INT(REG_OPTION_VOLATILE); |
| 1835 | ADD_INT(REG_OPTION_CREATE_LINK); |
| 1836 | ADD_INT(REG_OPTION_BACKUP_RESTORE); |
| 1837 | ADD_INT(REG_OPTION_OPEN_LINK); |
| 1838 | ADD_INT(REG_LEGAL_OPTION); |
| 1839 | ADD_INT(REG_CREATED_NEW_KEY); |
| 1840 | ADD_INT(REG_OPENED_EXISTING_KEY); |
| 1841 | ADD_INT(REG_WHOLE_HIVE_VOLATILE); |
| 1842 | ADD_INT(REG_REFRESH_HIVE); |
| 1843 | ADD_INT(REG_NO_LAZY_FLUSH); |
| 1844 | ADD_INT(REG_NOTIFY_CHANGE_NAME); |
| 1845 | ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES); |
| 1846 | ADD_INT(REG_NOTIFY_CHANGE_LAST_SET); |
| 1847 | ADD_INT(REG_NOTIFY_CHANGE_SECURITY); |
| 1848 | ADD_INT(REG_LEGAL_CHANGE_FILTER); |
| 1849 | ADD_INT(REG_NONE); |
| 1850 | ADD_INT(REG_SZ); |
| 1851 | ADD_INT(REG_EXPAND_SZ); |
| 1852 | ADD_INT(REG_BINARY); |
| 1853 | ADD_INT(REG_DWORD); |
| 1854 | ADD_INT(REG_DWORD_LITTLE_ENDIAN); |
| 1855 | ADD_INT(REG_DWORD_BIG_ENDIAN); |
| 1856 | ADD_INT(REG_LINK); |
| 1857 | ADD_INT(REG_MULTI_SZ); |
| 1858 | ADD_INT(REG_RESOURCE_LIST); |
| 1859 | ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR); |
| 1860 | ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST); |
| 1861 | return m; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1864 | |