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