Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1 | /* |
Fred Drake | 270e19b | 2000-06-29 16:14:14 +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 |
| 7 | module circa 1995. |
| 8 | * Bill Tutt expanded the support significantly not long after. |
| 9 | * Numerous other people have submitted patches since then. |
| 10 | * Ripped from win32api module 03-Feb-2000 by Mark Hammond, and |
| 11 | basic Unicode support added. |
| 12 | |
| 13 | */ |
| 14 | |
Guido van 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) \ |
| 31 | PyErr_SetFromWindowsErr(rc) |
| 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" |
| 49 | "FlushKey() - Writes all the attributes of the specified key to the registry.\n" |
| 50 | "LoadKey() - Creates a subkey under HKEY_USER or HKEY_LOCAL_MACHINE and stores\n" |
| 51 | " registration information from a specified file into that subkey.\n" |
| 52 | "OpenKey() - Alias for <om win32api.RegOpenKeyEx>\n" |
| 53 | "OpenKeyEx() - Opens the specified key.\n" |
| 54 | "QueryValue() - Retrieves the value associated with the unnamed value for a\n" |
| 55 | " specified key in the registry.\n" |
| 56 | "QueryValueEx() - Retrieves the type and data for a specified value name\n" |
| 57 | " associated with an open registry key.\n" |
| 58 | "QueryInfoKey() - Returns information about the specified key.\n" |
| 59 | "SaveKey() - Saves the specified key, and all its subkeys a file.\n" |
| 60 | "SetValue() - Associates a value with a specified key.\n" |
| 61 | "SetValueEx() - Stores data in the value field of an open registry key.\n" |
| 62 | "\n" |
| 63 | "Special objects:\n" |
| 64 | "\n" |
| 65 | "HKEYType -- type object for HKEY objects\n" |
| 66 | "error -- exception raised for Win32 errors\n" |
| 67 | "\n" |
| 68 | "Integer constants:\n" |
| 69 | "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] | 70 | "to see what constants are used, and where."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 71 | |
| 72 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 73 | PyDoc_STRVAR(CloseKey_doc, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 74 | "CloseKey(hkey) - Closes a previously opened registry key.\n" |
| 75 | "\n" |
| 76 | "The hkey argument specifies a previously opened key.\n" |
| 77 | "\n" |
| 78 | "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] | 79 | "closed when the hkey object is destroyed by Python."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 80 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 81 | PyDoc_STRVAR(ConnectRegistry_doc, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 82 | "key = ConnectRegistry(computer_name, key) - " |
| 83 | "Establishes a connection to a predefined registry handle on another computer.\n" |
| 84 | "\n" |
| 85 | "computer_name is the name of the remote computer, of the form \\\\computername.\n" |
| 86 | " If None, the local computer is used.\n" |
| 87 | "key is the predefined handle to connect to.\n" |
| 88 | "\n" |
| 89 | "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] | 90 | "If the function fails, an EnvironmentError exception is raised."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 91 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 92 | PyDoc_STRVAR(CreateKey_doc, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 93 | "key = CreateKey(key, sub_key) - Creates or opens the specified key.\n" |
| 94 | "\n" |
| 95 | "key is an already open key, or one of the predefined HKEY_* constants\n" |
| 96 | "sub_key is a string that names the key this method opens or creates.\n" |
| 97 | " If key is one of the predefined keys, sub_key may be None. In that case,\n" |
| 98 | " the handle returned is the same key handle passed in to the function.\n" |
| 99 | "\n" |
| 100 | "If the key already exists, this function opens the existing key\n" |
| 101 | "\n" |
| 102 | "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] | 103 | "If the function fails, an exception is raised."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 104 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 105 | PyDoc_STRVAR(DeleteKey_doc, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 106 | "DeleteKey(key, sub_key) - Deletes the specified key.\n" |
| 107 | "\n" |
| 108 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
| 109 | "sub_key is a string that must be a subkey of the key identified by the key parameter.\n" |
| 110 | " This value must not be None, and the key may not have subkeys.\n" |
| 111 | "\n" |
| 112 | "This method can not delete keys with subkeys.\n" |
| 113 | "\n" |
| 114 | "If the method succeeds, the entire key, including all of its values,\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 115 | "is removed. If the method fails, an EnvironmentError exception is raised."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 116 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 117 | PyDoc_STRVAR(DeleteValue_doc, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 118 | "DeleteValue(key, value) - Removes a named value from a registry key.\n" |
| 119 | "\n" |
| 120 | "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] | 121 | "value is a string that identifies the value to remove."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 122 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 123 | PyDoc_STRVAR(EnumKey_doc, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 124 | "string = EnumKey(key, index) - Enumerates subkeys of an open registry key.\n" |
| 125 | "\n" |
| 126 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
| 127 | "index is an integer that identifies the index of the key to retrieve.\n" |
| 128 | "\n" |
| 129 | "The function retrieves the name of one subkey each time it is called.\n" |
Mark Hammond | b422f95 | 2000-06-09 06:01:47 +0000 | [diff] [blame] | 130 | "It is typically called repeatedly until an EnvironmentError exception is\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 131 | "raised, indicating no more values are available."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 132 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 133 | PyDoc_STRVAR(EnumValue_doc, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 134 | "tuple = EnumValue(key, index) - Enumerates values of an open registry key.\n" |
| 135 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
| 136 | "index is an integer that identifies the index of the value to retrieve.\n" |
| 137 | "\n" |
| 138 | "The function retrieves the name of one subkey each time it is called.\n" |
Mark Hammond | b422f95 | 2000-06-09 06:01:47 +0000 | [diff] [blame] | 139 | "It is typically called repeatedly, until an EnvironmentError exception\n" |
| 140 | "is raised, indicating no more values.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 141 | "\n" |
| 142 | "The result is a tuple of 3 items:\n" |
| 143 | "value_name is a string that identifies the value.\n" |
| 144 | "value_data is an object that holds the value data, and whose type depends\n" |
| 145 | " on the underlying registry type.\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 146 | "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] | 147 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 148 | PyDoc_STRVAR(FlushKey_doc, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 149 | "FlushKey(key) - Writes all the attributes of a key to the registry.\n" |
| 150 | "\n" |
| 151 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
| 152 | "\n" |
| 153 | "It is not necessary to call RegFlushKey to change a key.\n" |
| 154 | "Registry changes are flushed to disk by the registry using its lazy flusher.\n" |
| 155 | "Registry changes are also flushed to disk at system shutdown.\n" |
| 156 | "Unlike CloseKey(), the FlushKey() method returns only when all the data has\n" |
| 157 | "been written to the registry.\n" |
| 158 | "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] | 159 | "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] | 160 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 161 | PyDoc_STRVAR(LoadKey_doc, |
Mark Hammond | b422f95 | 2000-06-09 06:01:47 +0000 | [diff] [blame] | 162 | "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] | 163 | "and stores registration information from a specified file into that subkey.\n" |
| 164 | "\n" |
| 165 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
| 166 | "sub_key is a string that identifies the sub_key to load\n" |
| 167 | "file_name is the name of the file to load registry data from.\n" |
| 168 | " This file must have been created with the SaveKey() function.\n" |
| 169 | " Under the file allocation table (FAT) file system, the filename may not\n" |
| 170 | "have an extension.\n" |
| 171 | "\n" |
| 172 | "A call to LoadKey() fails if the calling process does not have the\n" |
| 173 | "SE_RESTORE_PRIVILEGE privilege.\n" |
| 174 | "\n" |
| 175 | "If key is a handle returned by ConnectRegistry(), then the path specified\n" |
| 176 | "in fileName is relative to the remote computer.\n" |
| 177 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 178 | "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] | 179 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 180 | PyDoc_STRVAR(OpenKey_doc, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 181 | "key = OpenKey(key, sub_key, res = 0, sam = KEY_READ) - Opens the specified key.\n" |
| 182 | "\n" |
| 183 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
| 184 | "sub_key is a string that identifies the sub_key to open\n" |
| 185 | "res is a reserved integer, and must be zero. Default is zero.\n" |
| 186 | "sam is an integer that specifies an access mask that describes the desired\n" |
| 187 | " security access for the key. Default is KEY_READ\n" |
| 188 | "\n" |
| 189 | "The result is a new handle to the specified key\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 190 | "If the function fails, an EnvironmentError exception is raised."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 191 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 192 | PyDoc_STRVAR(OpenKeyEx_doc, "See OpenKey()"); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 193 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 194 | PyDoc_STRVAR(QueryInfoKey_doc, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 195 | "tuple = QueryInfoKey(key) - Returns information about a key.\n" |
| 196 | "\n" |
| 197 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
| 198 | "\n" |
| 199 | "The result is a tuple of 3 items:" |
| 200 | "An integer that identifies the number of sub keys this key has.\n" |
| 201 | "An integer that identifies the number of values this key has.\n" |
| 202 | "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] | 203 | " as 100's of nanoseconds since Jan 1, 1600."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 204 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 205 | PyDoc_STRVAR(QueryValue_doc, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 206 | "string = QueryValue(key, sub_key) - retrieves the unnamed value for a key.\n" |
| 207 | "\n" |
| 208 | "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] | 209 | "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] | 210 | " is associated. If this parameter is None or empty, the function retrieves\n" |
| 211 | " the value set by the SetValue() method for the key identified by key." |
| 212 | "\n" |
| 213 | "Values in the registry have name, type, and data components. This method\n" |
| 214 | "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] | 215 | "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] | 216 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 217 | PyDoc_STRVAR(QueryValueEx_doc, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 218 | "value,type_id = QueryValueEx(key, value_name) - Retrieves the type and data for a specified value name associated with an open registry key.\n" |
| 219 | "\n" |
| 220 | "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] | 221 | "value_name is a string indicating the value to query"); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 222 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 223 | PyDoc_STRVAR(SaveKey_doc, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 224 | "SaveKey(key, file_name) - Saves the specified key, and all its subkeys to the specified file.\n" |
| 225 | "\n" |
| 226 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
| 227 | "file_name is the name of the file to save registry data to.\n" |
| 228 | " This file cannot already exist. If this filename includes an extension,\n" |
| 229 | " it cannot be used on file allocation table (FAT) file systems by the\n" |
| 230 | " LoadKey(), ReplaceKey() or RestoreKey() methods.\n" |
| 231 | "\n" |
| 232 | "If key represents a key on a remote computer, the path described by\n" |
| 233 | "file_name is relative to the remote computer.\n" |
| 234 | "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] | 235 | "This function passes NULL for security_attributes to the API."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 236 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 237 | PyDoc_STRVAR(SetValue_doc, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 238 | "SetValue(key, sub_key, type, value) - Associates a value with a specified key.\n" |
| 239 | "\n" |
| 240 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
| 241 | "sub_key is a string that names the subkey with which the value is associated.\n" |
| 242 | "type is an integer that specifies the type of the data. Currently this\n" |
| 243 | " must be REG_SZ, meaning only strings are supported.\n" |
| 244 | "value is a string that specifies the new value.\n" |
| 245 | "\n" |
| 246 | "If the key specified by the sub_key parameter does not exist, the SetValue\n" |
| 247 | "function creates it.\n" |
| 248 | "\n" |
| 249 | "Value lengths are limited by available memory. Long values (more than\n" |
| 250 | "2048 bytes) should be stored as files with the filenames stored in \n" |
| 251 | "the configuration registry. This helps the registry perform efficiently.\n" |
| 252 | "\n" |
| 253 | "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] | 254 | "KEY_SET_VALUE access."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 255 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 256 | PyDoc_STRVAR(SetValueEx_doc, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 257 | "SetValueEx(key, value_name, reserved, type, value) - Stores data in the value field of an open registry key.\n" |
| 258 | "\n" |
| 259 | "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] | 260 | "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] | 261 | "type is an integer that specifies the type of the data. This should be one of:\n" |
| 262 | " REG_BINARY -- Binary data in any form.\n" |
| 263 | " REG_DWORD -- A 32-bit number.\n" |
| 264 | " REG_DWORD_LITTLE_ENDIAN -- A 32-bit number in little-endian format.\n" |
| 265 | " REG_DWORD_BIG_ENDIAN -- A 32-bit number in big-endian format.\n" |
| 266 | " REG_EXPAND_SZ -- A null-terminated string that contains unexpanded references\n" |
| 267 | " to environment variables (for example, %PATH%).\n" |
| 268 | " REG_LINK -- A Unicode symbolic link.\n" |
Mark Hammond | b422f95 | 2000-06-09 06:01:47 +0000 | [diff] [blame] | 269 | " REG_MULTI_SZ -- An sequence of null-terminated strings, terminated by\n" |
| 270 | " two null characters. Note that Python handles this\n" |
| 271 | " termination automatically.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 272 | " REG_NONE -- No defined value type.\n" |
| 273 | " REG_RESOURCE_LIST -- A device-driver resource list.\n" |
| 274 | " REG_SZ -- A null-terminated string.\n" |
| 275 | "reserved can be anything - zero is always passed to the API.\n" |
| 276 | "value is a string that specifies the new value.\n" |
| 277 | "\n" |
| 278 | "This method can also set additional value and type information for the\n" |
| 279 | "specified key. The key identified by the key parameter must have been\n" |
| 280 | "opened with KEY_SET_VALUE access.\n" |
| 281 | "\n" |
| 282 | "To open the key, use the CreateKeyEx() or OpenKeyEx() methods.\n" |
| 283 | "\n" |
| 284 | "Value lengths are limited by available memory. Long values (more than\n" |
| 285 | "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] | 286 | "the configuration registry. This helps the registry perform efficiently."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 287 | |
| 288 | /* PyHKEY docstrings */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 289 | PyDoc_STRVAR(PyHKEY_doc, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 290 | "PyHKEY Object - A Python object, representing a win32 registry key.\n" |
| 291 | "\n" |
Mark Hammond | b422f95 | 2000-06-09 06:01:47 +0000 | [diff] [blame] | 292 | "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] | 293 | "the object is destroyed. To guarantee cleanup, you can call either\n" |
| 294 | "the Close() method on the PyHKEY, or the CloseKey() method.\n" |
| 295 | "\n" |
| 296 | "All functions which accept a handle object also accept an integer - \n" |
| 297 | "however, use of the handle object is encouraged.\n" |
| 298 | "\n" |
| 299 | "Functions:\n" |
| 300 | "Close() - Closes the underlying handle.\n" |
| 301 | "Detach() - Returns the integer Win32 handle, detaching it from the object\n" |
| 302 | "\n" |
| 303 | "Properties:\n" |
| 304 | "handle - The integer Win32 handle.\n" |
| 305 | "\n" |
| 306 | "Operations:\n" |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 307 | "__bool__ - Handles with an open object return true, otherwise false.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 308 | "__int__ - Converting a handle to an integer returns the Win32 handle.\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 309 | "__cmp__ - Handle objects are compared using the handle value."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 310 | |
| 311 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 312 | PyDoc_STRVAR(PyHKEY_Close_doc, |
Mark Hammond | b422f95 | 2000-06-09 06:01:47 +0000 | [diff] [blame] | 313 | "key.Close() - Closes the underlying Windows handle.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 314 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 315 | "If the handle is already closed, no error is raised."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 316 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 317 | PyDoc_STRVAR(PyHKEY_Detach_doc, |
Mark Hammond | b422f95 | 2000-06-09 06:01:47 +0000 | [diff] [blame] | 318 | "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] | 319 | "\n" |
| 320 | "The result is the value of the handle before it is detached. If the\n" |
| 321 | "handle is already detached, this will return zero.\n" |
| 322 | "\n" |
| 323 | "After calling this function, the handle is effectively invalidated,\n" |
| 324 | "but the handle is not closed. You would call this function when you\n" |
| 325 | "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] | 326 | "handle object.\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 327 | "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] | 328 | |
| 329 | |
| 330 | /************************************************************************ |
| 331 | |
| 332 | The PyHKEY object definition |
| 333 | |
| 334 | ************************************************************************/ |
| 335 | typedef struct { |
| 336 | PyObject_VAR_HEAD |
| 337 | HKEY hkey; |
| 338 | } PyHKEYObject; |
| 339 | |
| 340 | #define PyHKEY_Check(op) ((op)->ob_type == &PyHKEY_Type) |
| 341 | |
| 342 | static char *failMsg = "bad operand type"; |
| 343 | |
| 344 | static PyObject * |
| 345 | PyHKEY_unaryFailureFunc(PyObject *ob) |
| 346 | { |
| 347 | PyErr_SetString(PyExc_TypeError, failMsg); |
| 348 | return NULL; |
| 349 | } |
| 350 | static PyObject * |
| 351 | PyHKEY_binaryFailureFunc(PyObject *ob1, PyObject *ob2) |
| 352 | { |
| 353 | PyErr_SetString(PyExc_TypeError, failMsg); |
| 354 | return NULL; |
| 355 | } |
| 356 | static PyObject * |
| 357 | PyHKEY_ternaryFailureFunc(PyObject *ob1, PyObject *ob2, PyObject *ob3) |
| 358 | { |
| 359 | PyErr_SetString(PyExc_TypeError, failMsg); |
| 360 | return NULL; |
| 361 | } |
| 362 | |
| 363 | static void |
| 364 | PyHKEY_deallocFunc(PyObject *ob) |
| 365 | { |
| 366 | /* Can not call PyHKEY_Close, as the ob->tp_type |
| 367 | has already been cleared, thus causing the type |
| 368 | check to fail! |
| 369 | */ |
| 370 | PyHKEYObject *obkey = (PyHKEYObject *)ob; |
| 371 | if (obkey->hkey) |
| 372 | RegCloseKey((HKEY)obkey->hkey); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 373 | PyObject_DEL(ob); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 377 | PyHKEY_boolFunc(PyObject *ob) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 378 | { |
| 379 | return ((PyHKEYObject *)ob)->hkey != 0; |
| 380 | } |
| 381 | |
| 382 | static PyObject * |
| 383 | PyHKEY_intFunc(PyObject *ob) |
| 384 | { |
| 385 | PyHKEYObject *pyhkey = (PyHKEYObject *)ob; |
| 386 | return PyLong_FromVoidPtr(pyhkey->hkey); |
| 387 | } |
| 388 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 389 | static PyObject * |
| 390 | PyHKEY_strFunc(PyObject *ob) |
| 391 | { |
| 392 | PyHKEYObject *pyhkey = (PyHKEYObject *)ob; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 393 | return PyUnicode_FromFormat("<PyHKEY:%p>", pyhkey->hkey); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | static int |
| 397 | PyHKEY_compareFunc(PyObject *ob1, PyObject *ob2) |
| 398 | { |
| 399 | PyHKEYObject *pyhkey1 = (PyHKEYObject *)ob1; |
| 400 | PyHKEYObject *pyhkey2 = (PyHKEYObject *)ob2; |
| 401 | return pyhkey1 == pyhkey2 ? 0 : |
| 402 | (pyhkey1 < pyhkey2 ? -1 : 1); |
| 403 | } |
| 404 | |
| 405 | static long |
| 406 | PyHKEY_hashFunc(PyObject *ob) |
| 407 | { |
| 408 | /* Just use the address. |
| 409 | XXX - should we use the handle value? |
| 410 | */ |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 411 | return _Py_HashPointer(ob); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | |
| 415 | static PyNumberMethods PyHKEY_NumberMethods = |
| 416 | { |
| 417 | PyHKEY_binaryFailureFunc, /* nb_add */ |
| 418 | PyHKEY_binaryFailureFunc, /* nb_subtract */ |
| 419 | PyHKEY_binaryFailureFunc, /* nb_multiply */ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 420 | PyHKEY_binaryFailureFunc, /* nb_remainder */ |
| 421 | PyHKEY_binaryFailureFunc, /* nb_divmod */ |
| 422 | PyHKEY_ternaryFailureFunc, /* nb_power */ |
| 423 | PyHKEY_unaryFailureFunc, /* nb_negative */ |
| 424 | PyHKEY_unaryFailureFunc, /* nb_positive */ |
| 425 | PyHKEY_unaryFailureFunc, /* nb_absolute */ |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 426 | PyHKEY_boolFunc, /* nb_bool */ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 427 | PyHKEY_unaryFailureFunc, /* nb_invert */ |
| 428 | PyHKEY_binaryFailureFunc, /* nb_lshift */ |
| 429 | PyHKEY_binaryFailureFunc, /* nb_rshift */ |
| 430 | PyHKEY_binaryFailureFunc, /* nb_and */ |
| 431 | PyHKEY_binaryFailureFunc, /* nb_xor */ |
| 432 | PyHKEY_binaryFailureFunc, /* nb_or */ |
Guido van Rossum | a8c360e | 2007-07-17 20:50:43 +0000 | [diff] [blame] | 433 | NULL, /* nb_coerce */ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 434 | PyHKEY_intFunc, /* nb_int */ |
| 435 | PyHKEY_unaryFailureFunc, /* nb_long */ |
| 436 | PyHKEY_unaryFailureFunc, /* nb_float */ |
| 437 | PyHKEY_unaryFailureFunc, /* nb_oct */ |
| 438 | PyHKEY_unaryFailureFunc, /* nb_hex */ |
| 439 | }; |
| 440 | |
| 441 | |
| 442 | /* fwd declare __getattr__ */ |
Tim Peters | c3d12ac | 2005-12-24 06:03:06 +0000 | [diff] [blame] | 443 | static PyObject *PyHKEY_getattr(PyObject *self, const char *name); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 444 | |
| 445 | /* The type itself */ |
| 446 | PyTypeObject PyHKEY_Type = |
| 447 | { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 448 | PyVarObject_HEAD_INIT(0, 0) /* fill in type at module init */ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 449 | "PyHKEY", |
| 450 | sizeof(PyHKEYObject), |
| 451 | 0, |
| 452 | PyHKEY_deallocFunc, /* tp_dealloc */ |
Guido van Rossum | 346f1a8 | 2007-08-07 19:58:47 +0000 | [diff] [blame] | 453 | 0, /* tp_print */ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 454 | PyHKEY_getattr, /* tp_getattr */ |
| 455 | 0, /* tp_setattr */ |
| 456 | PyHKEY_compareFunc, /* tp_compare */ |
| 457 | 0, /* tp_repr */ |
| 458 | &PyHKEY_NumberMethods, /* tp_as_number */ |
| 459 | 0, /* tp_as_sequence */ |
| 460 | 0, /* tp_as_mapping */ |
| 461 | PyHKEY_hashFunc, /* tp_hash */ |
| 462 | 0, /* tp_call */ |
| 463 | PyHKEY_strFunc, /* tp_str */ |
| 464 | 0, /* tp_getattro */ |
| 465 | 0, /* tp_setattro */ |
| 466 | 0, /* tp_as_buffer */ |
| 467 | 0, /* tp_flags */ |
| 468 | PyHKEY_doc, /* tp_doc */ |
| 469 | }; |
| 470 | |
| 471 | #define OFF(e) offsetof(PyHKEYObject, e) |
| 472 | |
Neal Norwitz | 8dfc4a9 | 2007-08-11 06:39:53 +0000 | [diff] [blame] | 473 | static PyMemberDef PyHKEY_memberlist[] = { |
| 474 | {"handle", T_INT, OFF(hkey), READONLY}, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 475 | {NULL} /* Sentinel */ |
| 476 | }; |
| 477 | |
| 478 | /************************************************************************ |
| 479 | |
| 480 | The PyHKEY object methods |
| 481 | |
| 482 | ************************************************************************/ |
| 483 | static PyObject * |
| 484 | PyHKEY_CloseMethod(PyObject *self, PyObject *args) |
| 485 | { |
| 486 | if (!PyArg_ParseTuple(args, ":Close")) |
| 487 | return NULL; |
| 488 | if (!PyHKEY_Close(self)) |
| 489 | return NULL; |
| 490 | Py_INCREF(Py_None); |
| 491 | return Py_None; |
| 492 | } |
| 493 | |
| 494 | static PyObject * |
| 495 | PyHKEY_DetachMethod(PyObject *self, PyObject *args) |
| 496 | { |
| 497 | void* ret; |
| 498 | PyHKEYObject *pThis = (PyHKEYObject *)self; |
| 499 | if (!PyArg_ParseTuple(args, ":Detach")) |
| 500 | return NULL; |
| 501 | ret = (void*)pThis->hkey; |
| 502 | pThis->hkey = 0; |
| 503 | return PyLong_FromVoidPtr(ret); |
| 504 | } |
| 505 | |
| 506 | static struct PyMethodDef PyHKEY_methods[] = { |
Neal Norwitz | 031829d | 2002-03-31 14:37:44 +0000 | [diff] [blame] | 507 | {"Close", PyHKEY_CloseMethod, METH_VARARGS, PyHKEY_Close_doc}, |
| 508 | {"Detach", PyHKEY_DetachMethod, METH_VARARGS, PyHKEY_Detach_doc}, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 509 | {NULL} |
| 510 | }; |
| 511 | |
| 512 | /*static*/ PyObject * |
Tim Peters | c3d12ac | 2005-12-24 06:03:06 +0000 | [diff] [blame] | 513 | PyHKEY_getattr(PyObject *self, const char *name) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 514 | { |
| 515 | PyObject *res; |
| 516 | |
| 517 | res = Py_FindMethod(PyHKEY_methods, self, name); |
| 518 | if (res != NULL) |
| 519 | return res; |
| 520 | PyErr_Clear(); |
| 521 | if (strcmp(name, "handle") == 0) |
| 522 | return PyLong_FromVoidPtr(((PyHKEYObject *)self)->hkey); |
Neal Norwitz | 8dfc4a9 | 2007-08-11 06:39:53 +0000 | [diff] [blame] | 523 | PyErr_Format(PyExc_AttributeError, |
| 524 | "'%.50s' object has no attribute '%.400s'", |
| 525 | Py_Type(self)->tp_name, name); |
| 526 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | /************************************************************************ |
| 530 | The public PyHKEY API (well, not public yet :-) |
| 531 | ************************************************************************/ |
| 532 | PyObject * |
| 533 | PyHKEY_New(HKEY hInit) |
| 534 | { |
| 535 | PyHKEYObject *key = PyObject_NEW(PyHKEYObject, &PyHKEY_Type); |
| 536 | if (key) |
| 537 | key->hkey = hInit; |
| 538 | return (PyObject *)key; |
| 539 | } |
| 540 | |
| 541 | BOOL |
| 542 | PyHKEY_Close(PyObject *ob_handle) |
| 543 | { |
| 544 | LONG rc; |
| 545 | PyHKEYObject *key; |
| 546 | |
| 547 | if (!PyHKEY_Check(ob_handle)) { |
| 548 | PyErr_SetString(PyExc_TypeError, "bad operand type"); |
| 549 | return FALSE; |
| 550 | } |
| 551 | key = (PyHKEYObject *)ob_handle; |
| 552 | rc = key->hkey ? RegCloseKey((HKEY)key->hkey) : ERROR_SUCCESS; |
| 553 | key->hkey = 0; |
| 554 | if (rc != ERROR_SUCCESS) |
| 555 | PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey"); |
| 556 | return rc == ERROR_SUCCESS; |
| 557 | } |
| 558 | |
| 559 | BOOL |
| 560 | PyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK) |
| 561 | { |
| 562 | if (ob == Py_None) { |
| 563 | if (!bNoneOK) { |
| 564 | PyErr_SetString( |
| 565 | PyExc_TypeError, |
| 566 | "None is not a valid HKEY in this context"); |
| 567 | return FALSE; |
| 568 | } |
| 569 | *pHANDLE = (HKEY)0; |
| 570 | } |
| 571 | else if (PyHKEY_Check(ob)) { |
| 572 | PyHKEYObject *pH = (PyHKEYObject *)ob; |
| 573 | *pHANDLE = pH->hkey; |
| 574 | } |
Neal Norwitz | 1fe5f38 | 2007-08-31 04:32:55 +0000 | [diff] [blame] | 575 | else if (PyLong_Check(ob)) { |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 576 | /* We also support integers */ |
| 577 | PyErr_Clear(); |
| 578 | *pHANDLE = (HKEY)PyLong_AsVoidPtr(ob); |
| 579 | if (PyErr_Occurred()) |
| 580 | return FALSE; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 581 | } |
| 582 | else { |
| 583 | PyErr_SetString( |
| 584 | PyExc_TypeError, |
| 585 | "The object is not a PyHKEY object"); |
| 586 | return FALSE; |
| 587 | } |
| 588 | return TRUE; |
| 589 | } |
| 590 | |
| 591 | PyObject * |
| 592 | PyHKEY_FromHKEY(HKEY h) |
| 593 | { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 594 | PyHKEYObject *op; |
| 595 | |
Guido van Rossum | e3a8e7e | 2002-08-19 19:26:42 +0000 | [diff] [blame] | 596 | /* Inline PyObject_New */ |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 597 | op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject)); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 598 | if (op == NULL) |
| 599 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 600 | PyObject_INIT(op, &PyHKEY_Type); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 601 | op->hkey = h; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 602 | return (PyObject *)op; |
| 603 | } |
| 604 | |
| 605 | |
| 606 | /************************************************************************ |
| 607 | The module methods |
| 608 | ************************************************************************/ |
| 609 | BOOL |
| 610 | PyWinObject_CloseHKEY(PyObject *obHandle) |
| 611 | { |
| 612 | BOOL ok; |
| 613 | if (PyHKEY_Check(obHandle)) { |
| 614 | ok = PyHKEY_Close(obHandle); |
| 615 | } |
Fred Drake | 25e1726 | 2000-06-30 17:48:51 +0000 | [diff] [blame] | 616 | #if SIZEOF_LONG >= SIZEOF_HKEY |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 617 | else if (PyInt_Check(obHandle)) { |
| 618 | long rc = RegCloseKey((HKEY)PyInt_AsLong(obHandle)); |
| 619 | ok = (rc == ERROR_SUCCESS); |
| 620 | if (!ok) |
| 621 | PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey"); |
| 622 | } |
Fred Drake | 25e1726 | 2000-06-30 17:48:51 +0000 | [diff] [blame] | 623 | #else |
| 624 | else if (PyLong_Check(obHandle)) { |
| 625 | long rc = RegCloseKey((HKEY)PyLong_AsVoidPtr(obHandle)); |
| 626 | ok = (rc == ERROR_SUCCESS); |
| 627 | if (!ok) |
| 628 | PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey"); |
| 629 | } |
| 630 | #endif |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 631 | else { |
| 632 | PyErr_SetString( |
| 633 | PyExc_TypeError, |
| 634 | "A handle must be a HKEY object or an integer"); |
| 635 | return FALSE; |
| 636 | } |
| 637 | return ok; |
| 638 | } |
| 639 | |
| 640 | |
| 641 | /* |
| 642 | Private Helper functions for the registry interfaces |
| 643 | |
| 644 | ** Note that fixupMultiSZ and countString have both had changes |
| 645 | ** made to support "incorrect strings". The registry specification |
| 646 | ** calls for strings to be terminated with 2 null bytes. It seems |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 647 | ** some commercial packages install strings which dont conform, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 648 | ** causing this code to fail - however, "regedit" etc still work |
| 649 | ** with these strings (ie only we dont!). |
| 650 | */ |
| 651 | static void |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 652 | fixupMultiSZ(wchar_t **str, wchar_t *data, int len) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 653 | { |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 654 | wchar_t *P; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 655 | int i; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 656 | wchar_t *Q; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 657 | |
| 658 | Q = data + len; |
| 659 | for (P = data, i = 0; P < Q && *P != '\0'; P++, i++) { |
| 660 | str[i] = P; |
| 661 | for(; *P != '\0'; P++) |
| 662 | ; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | static int |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 667 | countStrings(wchar_t *data, int len) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 668 | { |
| 669 | int strings; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 670 | wchar_t *P; |
| 671 | wchar_t *Q = data + len; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 672 | |
| 673 | for (P = data, strings = 0; P < Q && *P != '\0'; P++, strings++) |
| 674 | for (; P < Q && *P != '\0'; P++) |
| 675 | ; |
| 676 | return strings; |
| 677 | } |
| 678 | |
| 679 | /* Convert PyObject into Registry data. |
| 680 | Allocates space as needed. */ |
| 681 | static BOOL |
| 682 | Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize) |
| 683 | { |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 684 | switch (typ) { |
| 685 | case REG_DWORD: |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 686 | if (value != Py_None && !PyLong_Check(value)) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 687 | return FALSE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 688 | *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 689 | if (*retDataBuf==NULL){ |
| 690 | PyErr_NoMemory(); |
| 691 | return FALSE; |
| 692 | } |
| 693 | *retDataSize = sizeof(DWORD); |
| 694 | if (value == Py_None) { |
| 695 | DWORD zero = 0; |
| 696 | memcpy(*retDataBuf, &zero, sizeof(DWORD)); |
| 697 | } |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 698 | else { |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 699 | DWORD d = PyLong_AsLong(value); |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 700 | memcpy(*retDataBuf, &d, sizeof(DWORD)); |
| 701 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 702 | break; |
| 703 | case REG_SZ: |
| 704 | case REG_EXPAND_SZ: |
| 705 | { |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 706 | if (value == Py_None) |
| 707 | *retDataSize = 1; |
| 708 | else { |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 709 | if (!PyUnicode_Check(value)) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 710 | return FALSE; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 711 | |
| 712 | *retDataSize = 2 + PyUnicode_GET_DATA_SIZE(value); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 713 | } |
| 714 | *retDataBuf = (BYTE *)PyMem_NEW(DWORD, *retDataSize); |
| 715 | if (*retDataBuf==NULL){ |
| 716 | PyErr_NoMemory(); |
| 717 | return FALSE; |
| 718 | } |
| 719 | if (value == Py_None) |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 720 | wcscpy((wchar_t *)*retDataBuf, L""); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 721 | else |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 722 | wcscpy((wchar_t *)*retDataBuf, |
| 723 | PyUnicode_AS_UNICODE(value)); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 724 | break; |
| 725 | } |
| 726 | case REG_MULTI_SZ: |
| 727 | { |
| 728 | DWORD size = 0; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 729 | wchar_t *P; |
| 730 | int i,j; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 731 | |
| 732 | if (value == Py_None) |
| 733 | i = 0; |
| 734 | else { |
| 735 | if (!PyList_Check(value)) |
| 736 | return FALSE; |
| 737 | i = PyList_Size(value); |
| 738 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 739 | for (j = 0; j < i; j++) |
| 740 | { |
| 741 | PyObject *t; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 742 | t = PyList_GET_ITEM(value, j); |
| 743 | if (!PyUnicode_Check(t)) |
| 744 | return FALSE; |
| 745 | size += 2 + PyUnicode_GET_DATA_SIZE(t); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 748 | *retDataSize = size + 2; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 749 | *retDataBuf = (BYTE *)PyMem_NEW(char, |
| 750 | *retDataSize); |
| 751 | if (*retDataBuf==NULL){ |
| 752 | PyErr_NoMemory(); |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 753 | return FALSE; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 754 | } |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 755 | P = (wchar_t *)*retDataBuf; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 756 | |
| 757 | for (j = 0; j < i; j++) |
| 758 | { |
| 759 | PyObject *t; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 760 | t = PyList_GET_ITEM(value, j); |
| 761 | wcscpy(P, PyUnicode_AS_UNICODE(t)); |
| 762 | P += 1 + wcslen( |
| 763 | PyUnicode_AS_UNICODE(t)); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 764 | } |
| 765 | /* And doubly-terminate the list... */ |
| 766 | *P = '\0'; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 767 | break; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 768 | } |
| 769 | case REG_BINARY: |
| 770 | /* ALSO handle ALL unknown data types here. Even if we can't |
| 771 | support it natively, we should handle the bits. */ |
| 772 | default: |
| 773 | if (value == Py_None) |
| 774 | *retDataSize = 0; |
| 775 | else { |
Neal Norwitz | 1385b89 | 2007-08-26 23:07:13 +0000 | [diff] [blame] | 776 | PyBuffer view; |
| 777 | |
| 778 | if (!PyObject_CheckBuffer(value)) { |
Tim Peters | 313fcd4 | 2006-02-19 04:05:39 +0000 | [diff] [blame] | 779 | PyErr_Format(PyExc_TypeError, |
Mark Hammond | 4e80bb5 | 2000-07-28 03:44:41 +0000 | [diff] [blame] | 780 | "Objects of type '%s' can not " |
Tim Peters | 313fcd4 | 2006-02-19 04:05:39 +0000 | [diff] [blame] | 781 | "be used as binary registry values", |
Mark Hammond | 4e80bb5 | 2000-07-28 03:44:41 +0000 | [diff] [blame] | 782 | value->ob_type->tp_name); |
| 783 | return FALSE; |
| 784 | } |
Neal Norwitz | 1385b89 | 2007-08-26 23:07:13 +0000 | [diff] [blame] | 785 | |
| 786 | if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0) |
| 787 | return FALSE; |
| 788 | |
| 789 | *retDataBuf = (BYTE *)PyMem_NEW(char, view.len); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 790 | if (*retDataBuf==NULL){ |
Neal Norwitz | 1385b89 | 2007-08-26 23:07:13 +0000 | [diff] [blame] | 791 | PyObject_ReleaseBuffer(value, &view); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 792 | PyErr_NoMemory(); |
| 793 | return FALSE; |
| 794 | } |
Neal Norwitz | 1385b89 | 2007-08-26 23:07:13 +0000 | [diff] [blame] | 795 | *retDataSize = view.len; |
| 796 | memcpy(*retDataBuf, view.buf, view.len); |
| 797 | PyObject_ReleaseBuffer(value, &view); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 798 | } |
| 799 | break; |
| 800 | } |
| 801 | return TRUE; |
| 802 | } |
| 803 | |
| 804 | /* Convert Registry data into PyObject*/ |
| 805 | static PyObject * |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 806 | Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 807 | { |
| 808 | PyObject *obData; |
| 809 | |
| 810 | switch (typ) { |
| 811 | case REG_DWORD: |
| 812 | if (retDataSize == 0) |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 813 | obData = PyInt_FromLong(0); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 814 | else |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 815 | obData = PyInt_FromLong(*(int *)retDataBuf); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 816 | break; |
| 817 | case REG_SZ: |
| 818 | case REG_EXPAND_SZ: |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 819 | { |
| 820 | /* the buffer may or may not have a trailing NULL */ |
| 821 | wchar_t *data = (wchar_t *)retDataBuf; |
| 822 | int len = retDataSize / 2; |
| 823 | if (retDataSize && data[len-1] == '\0') |
| 824 | retDataSize -= 2; |
| 825 | if (retDataSize <= 0) |
| 826 | data = L""; |
| 827 | obData = PyUnicode_FromUnicode(data, retDataSize/2); |
| 828 | break; |
| 829 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 830 | case REG_MULTI_SZ: |
| 831 | if (retDataSize == 0) |
| 832 | obData = PyList_New(0); |
| 833 | else |
| 834 | { |
| 835 | int index = 0; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 836 | wchar_t *data = (wchar_t *)retDataBuf; |
| 837 | int len = retDataSize / 2; |
| 838 | int s = countStrings(data, len); |
| 839 | wchar_t **str = (wchar_t **)malloc(sizeof(wchar_t *)*s); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 840 | if (str == NULL) |
| 841 | return PyErr_NoMemory(); |
| 842 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 843 | fixupMultiSZ(str, data, len); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 844 | obData = PyList_New(s); |
Fred Drake | 25e1726 | 2000-06-30 17:48:51 +0000 | [diff] [blame] | 845 | if (obData == NULL) |
| 846 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 847 | for (index = 0; index < s; index++) |
| 848 | { |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 849 | size_t len = wcslen(str[index]); |
Fred Drake | 25e1726 | 2000-06-30 17:48:51 +0000 | [diff] [blame] | 850 | if (len > INT_MAX) { |
| 851 | PyErr_SetString(PyExc_OverflowError, |
| 852 | "registry string is too long for a Python string"); |
| 853 | Py_DECREF(obData); |
| 854 | return NULL; |
| 855 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 856 | PyList_SetItem(obData, |
| 857 | index, |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 858 | PyUnicode_FromUnicode(str[index], len)); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 859 | } |
| 860 | free(str); |
| 861 | |
| 862 | break; |
| 863 | } |
| 864 | case REG_BINARY: |
| 865 | /* ALSO handle ALL unknown data types here. Even if we can't |
| 866 | support it natively, we should handle the bits. */ |
| 867 | default: |
| 868 | if (retDataSize == 0) { |
| 869 | Py_INCREF(Py_None); |
| 870 | obData = Py_None; |
| 871 | } |
| 872 | else |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 873 | obData = PyBytes_FromStringAndSize( |
| 874 | (char *)retDataBuf, retDataSize); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 875 | break; |
| 876 | } |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 877 | return obData; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | /* The Python methods */ |
| 881 | |
| 882 | static PyObject * |
| 883 | PyCloseKey(PyObject *self, PyObject *args) |
| 884 | { |
| 885 | PyObject *obKey; |
| 886 | if (!PyArg_ParseTuple(args, "O:CloseKey", &obKey)) |
| 887 | return NULL; |
| 888 | if (!PyHKEY_Close(obKey)) |
| 889 | return NULL; |
| 890 | Py_INCREF(Py_None); |
| 891 | return Py_None; |
| 892 | } |
| 893 | |
| 894 | static PyObject * |
| 895 | PyConnectRegistry(PyObject *self, PyObject *args) |
| 896 | { |
| 897 | HKEY hKey; |
| 898 | PyObject *obKey; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 899 | wchar_t *szCompName = NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 900 | HKEY retKey; |
| 901 | long rc; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 902 | if (!PyArg_ParseTuple(args, "ZO:ConnectRegistry", &szCompName, &obKey)) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 903 | return NULL; |
| 904 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 905 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 906 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 907 | rc = RegConnectRegistryW(szCompName, hKey, &retKey); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 908 | Py_END_ALLOW_THREADS |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 909 | if (rc != ERROR_SUCCESS) |
| 910 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 911 | "ConnectRegistry"); |
| 912 | return PyHKEY_FromHKEY(retKey); |
| 913 | } |
| 914 | |
| 915 | static PyObject * |
| 916 | PyCreateKey(PyObject *self, PyObject *args) |
| 917 | { |
| 918 | HKEY hKey; |
| 919 | PyObject *obKey; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 920 | wchar_t *subKey; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 921 | HKEY retKey; |
| 922 | long rc; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 923 | if (!PyArg_ParseTuple(args, "OZ:CreateKey", &obKey, &subKey)) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 924 | return NULL; |
| 925 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 926 | return NULL; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 927 | rc = RegCreateKeyW(hKey, subKey, &retKey); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 928 | if (rc != ERROR_SUCCESS) |
| 929 | return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey"); |
| 930 | return PyHKEY_FromHKEY(retKey); |
| 931 | } |
| 932 | |
| 933 | static PyObject * |
| 934 | PyDeleteKey(PyObject *self, PyObject *args) |
| 935 | { |
| 936 | HKEY hKey; |
| 937 | PyObject *obKey; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 938 | wchar_t *subKey; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 939 | long rc; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 940 | if (!PyArg_ParseTuple(args, "Ou:DeleteKey", &obKey, &subKey)) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 941 | return NULL; |
| 942 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 943 | return NULL; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 944 | rc = RegDeleteKeyW(hKey, subKey ); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 945 | if (rc != ERROR_SUCCESS) |
| 946 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey"); |
| 947 | Py_INCREF(Py_None); |
| 948 | return Py_None; |
| 949 | } |
| 950 | |
| 951 | static PyObject * |
| 952 | PyDeleteValue(PyObject *self, PyObject *args) |
| 953 | { |
| 954 | HKEY hKey; |
| 955 | PyObject *obKey; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 956 | wchar_t *subKey; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 957 | long rc; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 958 | if (!PyArg_ParseTuple(args, "OZ:DeleteValue", &obKey, &subKey)) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 959 | return NULL; |
| 960 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 961 | return NULL; |
| 962 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 963 | rc = RegDeleteValueW(hKey, subKey); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 964 | Py_END_ALLOW_THREADS |
| 965 | if (rc !=ERROR_SUCCESS) |
| 966 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 967 | "RegDeleteValue"); |
| 968 | Py_INCREF(Py_None); |
| 969 | return Py_None; |
| 970 | } |
| 971 | |
| 972 | static PyObject * |
| 973 | PyEnumKey(PyObject *self, PyObject *args) |
| 974 | { |
| 975 | HKEY hKey; |
| 976 | PyObject *obKey; |
| 977 | int index; |
| 978 | long rc; |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 979 | PyObject *retStr; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 980 | wchar_t tmpbuf[256]; /* max key name length is 255 */ |
Georg Brandl | b2699b2 | 2006-02-18 23:44:24 +0000 | [diff] [blame] | 981 | DWORD len = sizeof(tmpbuf); /* includes NULL terminator */ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 982 | |
| 983 | if (!PyArg_ParseTuple(args, "Oi:EnumKey", &obKey, &index)) |
| 984 | return NULL; |
| 985 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 986 | return NULL; |
Tim Peters | 313fcd4 | 2006-02-19 04:05:39 +0000 | [diff] [blame] | 987 | |
Georg Brandl | 9a928e7 | 2006-02-18 23:35:11 +0000 | [diff] [blame] | 988 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 989 | rc = RegEnumKeyExW(hKey, index, tmpbuf, &len, NULL, NULL, NULL, NULL); |
Georg Brandl | 9a928e7 | 2006-02-18 23:35:11 +0000 | [diff] [blame] | 990 | Py_END_ALLOW_THREADS |
| 991 | if (rc != ERROR_SUCCESS) |
| 992 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx"); |
Tim Peters | 313fcd4 | 2006-02-19 04:05:39 +0000 | [diff] [blame] | 993 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 994 | retStr = PyUnicode_FromUnicode(tmpbuf, len); |
Georg Brandl | 9a928e7 | 2006-02-18 23:35:11 +0000 | [diff] [blame] | 995 | return retStr; /* can be NULL */ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | static PyObject * |
| 999 | PyEnumValue(PyObject *self, PyObject *args) |
| 1000 | { |
| 1001 | HKEY hKey; |
| 1002 | PyObject *obKey; |
| 1003 | int index; |
| 1004 | long rc; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1005 | wchar_t *retValueBuf; |
| 1006 | BYTE *retDataBuf; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1007 | DWORD retValueSize; |
| 1008 | DWORD retDataSize; |
| 1009 | DWORD typ; |
| 1010 | PyObject *obData; |
| 1011 | PyObject *retVal; |
| 1012 | |
| 1013 | if (!PyArg_ParseTuple(args, "Oi:EnumValue", &obKey, &index)) |
| 1014 | return NULL; |
| 1015 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1016 | return NULL; |
| 1017 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1018 | if ((rc = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, NULL, NULL, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1019 | NULL, |
| 1020 | &retValueSize, &retDataSize, NULL, NULL)) |
| 1021 | != ERROR_SUCCESS) |
| 1022 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1023 | "RegQueryInfoKey"); |
| 1024 | ++retValueSize; /* include null terminators */ |
| 1025 | ++retDataSize; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1026 | retValueBuf = (wchar_t *)PyMem_Malloc(sizeof(wchar_t) * retValueSize); |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1027 | if (retValueBuf == NULL) |
| 1028 | return PyErr_NoMemory(); |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1029 | retDataBuf = (BYTE *)PyMem_Malloc(retDataSize); |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1030 | if (retDataBuf == NULL) { |
| 1031 | PyMem_Free(retValueBuf); |
| 1032 | return PyErr_NoMemory(); |
| 1033 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1034 | |
| 1035 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1036 | rc = RegEnumValueW(hKey, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1037 | index, |
| 1038 | retValueBuf, |
| 1039 | &retValueSize, |
| 1040 | NULL, |
| 1041 | &typ, |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1042 | retDataBuf, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1043 | &retDataSize); |
| 1044 | Py_END_ALLOW_THREADS |
| 1045 | |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1046 | if (rc != ERROR_SUCCESS) { |
| 1047 | retVal = PyErr_SetFromWindowsErrWithFunction(rc, |
| 1048 | "PyRegEnumValue"); |
| 1049 | goto fail; |
| 1050 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1051 | obData = Reg2Py(retDataBuf, retDataSize, typ); |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1052 | if (obData == NULL) { |
| 1053 | retVal = NULL; |
| 1054 | goto fail; |
| 1055 | } |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1056 | retVal = Py_BuildValue("uOi", retValueBuf, obData, typ); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1057 | Py_DECREF(obData); |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1058 | fail: |
| 1059 | PyMem_Free(retValueBuf); |
| 1060 | PyMem_Free(retDataBuf); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1061 | return retVal; |
| 1062 | } |
| 1063 | |
| 1064 | static PyObject * |
| 1065 | PyFlushKey(PyObject *self, PyObject *args) |
| 1066 | { |
| 1067 | HKEY hKey; |
| 1068 | PyObject *obKey; |
| 1069 | long rc; |
| 1070 | if (!PyArg_ParseTuple(args, "O:FlushKey", &obKey)) |
| 1071 | return NULL; |
| 1072 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1073 | return NULL; |
| 1074 | Py_BEGIN_ALLOW_THREADS |
| 1075 | rc = RegFlushKey(hKey); |
| 1076 | Py_END_ALLOW_THREADS |
| 1077 | if (rc != ERROR_SUCCESS) |
| 1078 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey"); |
| 1079 | Py_INCREF(Py_None); |
| 1080 | return Py_None; |
| 1081 | } |
| 1082 | static PyObject * |
| 1083 | PyLoadKey(PyObject *self, PyObject *args) |
| 1084 | { |
| 1085 | HKEY hKey; |
| 1086 | PyObject *obKey; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1087 | wchar_t *subKey; |
| 1088 | wchar_t *fileName; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1089 | |
| 1090 | long rc; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1091 | if (!PyArg_ParseTuple(args, "Ouu:LoadKey", &obKey, &subKey, &fileName)) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1092 | return NULL; |
| 1093 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1094 | return NULL; |
| 1095 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1096 | rc = RegLoadKeyW(hKey, subKey, fileName ); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1097 | Py_END_ALLOW_THREADS |
| 1098 | if (rc != ERROR_SUCCESS) |
| 1099 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey"); |
| 1100 | Py_INCREF(Py_None); |
| 1101 | return Py_None; |
| 1102 | } |
| 1103 | |
| 1104 | static PyObject * |
| 1105 | PyOpenKey(PyObject *self, PyObject *args) |
| 1106 | { |
| 1107 | HKEY hKey; |
| 1108 | PyObject *obKey; |
| 1109 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1110 | wchar_t *subKey; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1111 | int res = 0; |
| 1112 | HKEY retKey; |
| 1113 | long rc; |
| 1114 | REGSAM sam = KEY_READ; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1115 | if (!PyArg_ParseTuple(args, "OZ|ii:OpenKey", &obKey, &subKey, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1116 | &res, &sam)) |
| 1117 | return NULL; |
| 1118 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1119 | return NULL; |
| 1120 | |
| 1121 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1122 | rc = RegOpenKeyExW(hKey, subKey, res, sam, &retKey); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1123 | Py_END_ALLOW_THREADS |
| 1124 | if (rc != ERROR_SUCCESS) |
| 1125 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx"); |
| 1126 | return PyHKEY_FromHKEY(retKey); |
| 1127 | } |
| 1128 | |
| 1129 | |
| 1130 | static PyObject * |
| 1131 | PyQueryInfoKey(PyObject *self, PyObject *args) |
| 1132 | { |
| 1133 | HKEY hKey; |
| 1134 | PyObject *obKey; |
| 1135 | long rc; |
| 1136 | DWORD nSubKeys, nValues; |
| 1137 | FILETIME ft; |
| 1138 | LARGE_INTEGER li; |
| 1139 | PyObject *l; |
| 1140 | PyObject *ret; |
| 1141 | if (!PyArg_ParseTuple(args, "O:QueryInfoKey", &obKey)) |
| 1142 | return NULL; |
| 1143 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1144 | return NULL; |
| 1145 | if ((rc = RegQueryInfoKey(hKey, NULL, NULL, 0, &nSubKeys, NULL, NULL, |
| 1146 | &nValues, NULL, NULL, NULL, &ft)) |
| 1147 | != ERROR_SUCCESS) |
| 1148 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey"); |
| 1149 | li.LowPart = ft.dwLowDateTime; |
| 1150 | li.HighPart = ft.dwHighDateTime; |
| 1151 | l = PyLong_FromLongLong(li.QuadPart); |
| 1152 | if (l == NULL) |
| 1153 | return NULL; |
| 1154 | ret = Py_BuildValue("iiO", nSubKeys, nValues, l); |
| 1155 | Py_DECREF(l); |
| 1156 | return ret; |
| 1157 | } |
| 1158 | |
| 1159 | static PyObject * |
| 1160 | PyQueryValue(PyObject *self, PyObject *args) |
| 1161 | { |
| 1162 | HKEY hKey; |
| 1163 | PyObject *obKey; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1164 | wchar_t *subKey; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1165 | long rc; |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1166 | PyObject *retStr; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1167 | wchar_t *retBuf; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1168 | long bufSize = 0; |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1169 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1170 | if (!PyArg_ParseTuple(args, "OZ:QueryValue", &obKey, &subKey)) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1171 | return NULL; |
| 1172 | |
| 1173 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1174 | return NULL; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1175 | if ((rc = RegQueryValueW(hKey, subKey, NULL, &bufSize)) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1176 | != ERROR_SUCCESS) |
| 1177 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1178 | "RegQueryValue"); |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1179 | retBuf = (wchar_t *)PyMem_Malloc(bufSize); |
Guido van Rossum | a8c360e | 2007-07-17 20:50:43 +0000 | [diff] [blame] | 1180 | if (retBuf == NULL) |
| 1181 | return PyErr_NoMemory(); |
| 1182 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1183 | if ((rc = RegQueryValueW(hKey, subKey, retBuf, &bufSize)) |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1184 | != ERROR_SUCCESS) { |
Guido van Rossum | a8c360e | 2007-07-17 20:50:43 +0000 | [diff] [blame] | 1185 | PyMem_Free(retBuf); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1186 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1187 | "RegQueryValue"); |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1188 | } |
Guido van Rossum | a8c360e | 2007-07-17 20:50:43 +0000 | [diff] [blame] | 1189 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1190 | retStr = PyUnicode_FromUnicode(retBuf, wcslen(retBuf)); |
Guido van Rossum | a8c360e | 2007-07-17 20:50:43 +0000 | [diff] [blame] | 1191 | PyMem_Free(retBuf); |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1192 | return retStr; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | static PyObject * |
| 1196 | PyQueryValueEx(PyObject *self, PyObject *args) |
| 1197 | { |
| 1198 | HKEY hKey; |
| 1199 | PyObject *obKey; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1200 | wchar_t *valueName; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1201 | |
| 1202 | long rc; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1203 | BYTE *retBuf; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1204 | DWORD bufSize = 0; |
| 1205 | DWORD typ; |
| 1206 | PyObject *obData; |
| 1207 | PyObject *result; |
| 1208 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1209 | if (!PyArg_ParseTuple(args, "OZ:QueryValueEx", &obKey, &valueName)) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1210 | return NULL; |
| 1211 | |
| 1212 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1213 | return NULL; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1214 | if ((rc = RegQueryValueExW(hKey, valueName, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1215 | NULL, NULL, NULL, |
| 1216 | &bufSize)) |
| 1217 | != ERROR_SUCCESS) |
| 1218 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1219 | "RegQueryValueEx"); |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1220 | retBuf = (BYTE *)PyMem_Malloc(bufSize); |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1221 | if (retBuf == NULL) |
| 1222 | return PyErr_NoMemory(); |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1223 | if ((rc = RegQueryValueExW(hKey, valueName, NULL, |
| 1224 | &typ, retBuf, &bufSize)) |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1225 | != ERROR_SUCCESS) { |
| 1226 | PyMem_Free(retBuf); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1227 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1228 | "RegQueryValueEx"); |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1229 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1230 | obData = Reg2Py(retBuf, bufSize, typ); |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1231 | PyMem_Free(retBuf); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1232 | if (obData == NULL) |
| 1233 | return NULL; |
| 1234 | result = Py_BuildValue("Oi", obData, typ); |
| 1235 | Py_DECREF(obData); |
| 1236 | return result; |
| 1237 | } |
| 1238 | |
| 1239 | |
| 1240 | static PyObject * |
| 1241 | PySaveKey(PyObject *self, PyObject *args) |
| 1242 | { |
| 1243 | HKEY hKey; |
| 1244 | PyObject *obKey; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1245 | wchar_t *fileName; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1246 | LPSECURITY_ATTRIBUTES pSA = NULL; |
| 1247 | |
| 1248 | long rc; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1249 | if (!PyArg_ParseTuple(args, "Ou:SaveKey", &obKey, &fileName)) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1250 | return NULL; |
| 1251 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1252 | return NULL; |
| 1253 | /* One day we may get security into the core? |
| 1254 | if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE)) |
| 1255 | return NULL; |
| 1256 | */ |
| 1257 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1258 | rc = RegSaveKeyW(hKey, fileName, pSA ); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1259 | Py_END_ALLOW_THREADS |
| 1260 | if (rc != ERROR_SUCCESS) |
| 1261 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey"); |
| 1262 | Py_INCREF(Py_None); |
| 1263 | return Py_None; |
| 1264 | } |
| 1265 | |
| 1266 | static PyObject * |
| 1267 | PySetValue(PyObject *self, PyObject *args) |
| 1268 | { |
| 1269 | HKEY hKey; |
| 1270 | PyObject *obKey; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1271 | wchar_t *subKey; |
| 1272 | wchar_t *str; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1273 | DWORD typ; |
| 1274 | DWORD len; |
| 1275 | long rc; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1276 | if (!PyArg_ParseTuple(args, "OZiu#:SetValue", |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1277 | &obKey, |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1278 | &subKey, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1279 | &typ, |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1280 | &str, |
| 1281 | &len)) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1282 | return NULL; |
| 1283 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1284 | return NULL; |
| 1285 | if (typ != REG_SZ) { |
| 1286 | PyErr_SetString(PyExc_TypeError, |
Thomas Heller | e1d18f5 | 2002-12-20 20:13:35 +0000 | [diff] [blame] | 1287 | "Type must be _winreg.REG_SZ"); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1288 | return NULL; |
| 1289 | } |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1290 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1291 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1292 | rc = RegSetValueW(hKey, subKey, REG_SZ, str, len+1); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1293 | Py_END_ALLOW_THREADS |
| 1294 | if (rc != ERROR_SUCCESS) |
| 1295 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue"); |
| 1296 | Py_INCREF(Py_None); |
| 1297 | return Py_None; |
| 1298 | } |
| 1299 | |
| 1300 | static PyObject * |
| 1301 | PySetValueEx(PyObject *self, PyObject *args) |
| 1302 | { |
| 1303 | HKEY hKey; |
| 1304 | PyObject *obKey; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1305 | Py_UNICODE *valueName; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1306 | PyObject *obRes; |
| 1307 | PyObject *value; |
| 1308 | BYTE *data; |
| 1309 | DWORD len; |
| 1310 | DWORD typ; |
| 1311 | |
| 1312 | LONG rc; |
| 1313 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1314 | if (!PyArg_ParseTuple(args, "OZOiO:SetValueEx", |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1315 | &obKey, |
| 1316 | &valueName, |
| 1317 | &obRes, |
| 1318 | &typ, |
| 1319 | &value)) |
| 1320 | return NULL; |
| 1321 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1322 | return NULL; |
| 1323 | if (!Py2Reg(value, typ, &data, &len)) |
| 1324 | { |
| 1325 | if (!PyErr_Occurred()) |
| 1326 | PyErr_SetString(PyExc_ValueError, |
| 1327 | "Could not convert the data to the specified type."); |
| 1328 | return NULL; |
| 1329 | } |
| 1330 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1331 | rc = RegSetValueExW(hKey, valueName, 0, typ, data, len); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1332 | Py_END_ALLOW_THREADS |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1333 | PyMem_DEL(data); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1334 | if (rc != ERROR_SUCCESS) |
| 1335 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1336 | "RegSetValueEx"); |
| 1337 | Py_INCREF(Py_None); |
| 1338 | return Py_None; |
| 1339 | } |
| 1340 | |
| 1341 | static struct PyMethodDef winreg_methods[] = { |
Neal Norwitz | 031829d | 2002-03-31 14:37:44 +0000 | [diff] [blame] | 1342 | {"CloseKey", PyCloseKey, METH_VARARGS, CloseKey_doc}, |
| 1343 | {"ConnectRegistry", PyConnectRegistry, METH_VARARGS, ConnectRegistry_doc}, |
| 1344 | {"CreateKey", PyCreateKey, METH_VARARGS, CreateKey_doc}, |
| 1345 | {"DeleteKey", PyDeleteKey, METH_VARARGS, DeleteKey_doc}, |
| 1346 | {"DeleteValue", PyDeleteValue, METH_VARARGS, DeleteValue_doc}, |
| 1347 | {"EnumKey", PyEnumKey, METH_VARARGS, EnumKey_doc}, |
| 1348 | {"EnumValue", PyEnumValue, METH_VARARGS, EnumValue_doc}, |
| 1349 | {"FlushKey", PyFlushKey, METH_VARARGS, FlushKey_doc}, |
| 1350 | {"LoadKey", PyLoadKey, METH_VARARGS, LoadKey_doc}, |
| 1351 | {"OpenKey", PyOpenKey, METH_VARARGS, OpenKey_doc}, |
| 1352 | {"OpenKeyEx", PyOpenKey, METH_VARARGS, OpenKeyEx_doc}, |
| 1353 | {"QueryValue", PyQueryValue, METH_VARARGS, QueryValue_doc}, |
| 1354 | {"QueryValueEx", PyQueryValueEx, METH_VARARGS, QueryValueEx_doc}, |
| 1355 | {"QueryInfoKey", PyQueryInfoKey, METH_VARARGS, QueryInfoKey_doc}, |
| 1356 | {"SaveKey", PySaveKey, METH_VARARGS, SaveKey_doc}, |
| 1357 | {"SetValue", PySetValue, METH_VARARGS, SetValue_doc}, |
| 1358 | {"SetValueEx", PySetValueEx, METH_VARARGS, SetValueEx_doc}, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1359 | NULL, |
| 1360 | }; |
| 1361 | |
| 1362 | static void |
| 1363 | insint(PyObject * d, char * name, long value) |
| 1364 | { |
| 1365 | PyObject *v = PyInt_FromLong(value); |
| 1366 | if (!v || PyDict_SetItemString(d, name, v)) |
| 1367 | PyErr_Clear(); |
| 1368 | Py_XDECREF(v); |
| 1369 | } |
| 1370 | |
| 1371 | #define ADD_INT(val) insint(d, #val, val) |
| 1372 | |
| 1373 | static void |
| 1374 | inskey(PyObject * d, char * name, HKEY key) |
| 1375 | { |
| 1376 | PyObject *v = PyLong_FromVoidPtr(key); |
| 1377 | if (!v || PyDict_SetItemString(d, name, v)) |
| 1378 | PyErr_Clear(); |
| 1379 | Py_XDECREF(v); |
| 1380 | } |
| 1381 | |
| 1382 | #define ADD_KEY(val) inskey(d, #val, val) |
| 1383 | |
Mark Hammond | 8235ea1 | 2002-07-19 06:55:41 +0000 | [diff] [blame] | 1384 | PyMODINIT_FUNC init_winreg(void) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1385 | { |
| 1386 | PyObject *m, *d; |
Fred Drake | 270e19b | 2000-06-29 16:14:14 +0000 | [diff] [blame] | 1387 | m = Py_InitModule3("_winreg", winreg_methods, module_doc); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 1388 | if (m == NULL) |
| 1389 | return; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1390 | d = PyModule_GetDict(m); |
Martin v. Löwis | 95c95ce | 2007-07-22 14:41:55 +0000 | [diff] [blame] | 1391 | Py_Type(&PyHKEY_Type) = &PyType_Type; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1392 | PyHKEY_Type.tp_doc = PyHKEY_doc; |
| 1393 | Py_INCREF(&PyHKEY_Type); |
| 1394 | if (PyDict_SetItemString(d, "HKEYType", |
| 1395 | (PyObject *)&PyHKEY_Type) != 0) |
| 1396 | return; |
| 1397 | Py_INCREF(PyExc_WindowsError); |
| 1398 | if (PyDict_SetItemString(d, "error", |
| 1399 | PyExc_WindowsError) != 0) |
| 1400 | return; |
| 1401 | |
| 1402 | /* Add the relevant constants */ |
| 1403 | ADD_KEY(HKEY_CLASSES_ROOT); |
| 1404 | ADD_KEY(HKEY_CURRENT_USER); |
| 1405 | ADD_KEY(HKEY_LOCAL_MACHINE); |
| 1406 | ADD_KEY(HKEY_USERS); |
| 1407 | ADD_KEY(HKEY_PERFORMANCE_DATA); |
| 1408 | #ifdef HKEY_CURRENT_CONFIG |
| 1409 | ADD_KEY(HKEY_CURRENT_CONFIG); |
| 1410 | #endif |
| 1411 | #ifdef HKEY_DYN_DATA |
| 1412 | ADD_KEY(HKEY_DYN_DATA); |
| 1413 | #endif |
| 1414 | ADD_INT(KEY_QUERY_VALUE); |
| 1415 | ADD_INT(KEY_SET_VALUE); |
| 1416 | ADD_INT(KEY_CREATE_SUB_KEY); |
| 1417 | ADD_INT(KEY_ENUMERATE_SUB_KEYS); |
| 1418 | ADD_INT(KEY_NOTIFY); |
| 1419 | ADD_INT(KEY_CREATE_LINK); |
| 1420 | ADD_INT(KEY_READ); |
| 1421 | ADD_INT(KEY_WRITE); |
| 1422 | ADD_INT(KEY_EXECUTE); |
| 1423 | ADD_INT(KEY_ALL_ACCESS); |
| 1424 | ADD_INT(REG_OPTION_RESERVED); |
| 1425 | ADD_INT(REG_OPTION_NON_VOLATILE); |
| 1426 | ADD_INT(REG_OPTION_VOLATILE); |
| 1427 | ADD_INT(REG_OPTION_CREATE_LINK); |
| 1428 | ADD_INT(REG_OPTION_BACKUP_RESTORE); |
| 1429 | ADD_INT(REG_OPTION_OPEN_LINK); |
| 1430 | ADD_INT(REG_LEGAL_OPTION); |
| 1431 | ADD_INT(REG_CREATED_NEW_KEY); |
| 1432 | ADD_INT(REG_OPENED_EXISTING_KEY); |
| 1433 | ADD_INT(REG_WHOLE_HIVE_VOLATILE); |
| 1434 | ADD_INT(REG_REFRESH_HIVE); |
| 1435 | ADD_INT(REG_NO_LAZY_FLUSH); |
| 1436 | ADD_INT(REG_NOTIFY_CHANGE_NAME); |
| 1437 | ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES); |
| 1438 | ADD_INT(REG_NOTIFY_CHANGE_LAST_SET); |
| 1439 | ADD_INT(REG_NOTIFY_CHANGE_SECURITY); |
| 1440 | ADD_INT(REG_LEGAL_CHANGE_FILTER); |
| 1441 | ADD_INT(REG_NONE); |
| 1442 | ADD_INT(REG_SZ); |
| 1443 | ADD_INT(REG_EXPAND_SZ); |
| 1444 | ADD_INT(REG_BINARY); |
| 1445 | ADD_INT(REG_DWORD); |
| 1446 | ADD_INT(REG_DWORD_LITTLE_ENDIAN); |
| 1447 | ADD_INT(REG_DWORD_BIG_ENDIAN); |
| 1448 | ADD_INT(REG_LINK); |
| 1449 | ADD_INT(REG_MULTI_SZ); |
| 1450 | ADD_INT(REG_RESOURCE_LIST); |
| 1451 | ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR); |
| 1452 | ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST); |
| 1453 | } |
| 1454 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame^] | 1455 | |