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 |
| 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" |
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 { |
| 386 | PyObject_VAR_HEAD |
| 387 | HKEY hkey; |
| 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 | { |
| 397 | PyErr_SetString(PyExc_TypeError, failMsg); |
| 398 | return NULL; |
| 399 | } |
| 400 | static PyObject * |
| 401 | PyHKEY_binaryFailureFunc(PyObject *ob1, PyObject *ob2) |
| 402 | { |
| 403 | PyErr_SetString(PyExc_TypeError, failMsg); |
| 404 | return NULL; |
| 405 | } |
| 406 | static PyObject * |
| 407 | PyHKEY_ternaryFailureFunc(PyObject *ob1, PyObject *ob2, PyObject *ob3) |
| 408 | { |
| 409 | PyErr_SetString(PyExc_TypeError, failMsg); |
| 410 | return NULL; |
| 411 | } |
| 412 | |
| 413 | static void |
| 414 | PyHKEY_deallocFunc(PyObject *ob) |
| 415 | { |
| 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); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 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 | { |
| 429 | return ((PyHKEYObject *)ob)->hkey != 0; |
| 430 | } |
| 431 | |
| 432 | static PyObject * |
| 433 | PyHKEY_intFunc(PyObject *ob) |
| 434 | { |
| 435 | PyHKEYObject *pyhkey = (PyHKEYObject *)ob; |
| 436 | return PyLong_FromVoidPtr(pyhkey->hkey); |
| 437 | } |
| 438 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 439 | static PyObject * |
| 440 | PyHKEY_strFunc(PyObject *ob) |
| 441 | { |
| 442 | PyHKEYObject *pyhkey = (PyHKEYObject *)ob; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 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 | { |
| 449 | PyHKEYObject *pyhkey1 = (PyHKEYObject *)ob1; |
| 450 | PyHKEYObject *pyhkey2 = (PyHKEYObject *)ob2; |
| 451 | return pyhkey1 == pyhkey2 ? 0 : |
| 452 | (pyhkey1 < pyhkey2 ? -1 : 1); |
| 453 | } |
| 454 | |
| 455 | static long |
| 456 | PyHKEY_hashFunc(PyObject *ob) |
| 457 | { |
| 458 | /* Just use the address. |
| 459 | XXX - should we use the handle value? |
| 460 | */ |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 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 | { |
| 467 | PyHKEY_binaryFailureFunc, /* nb_add */ |
| 468 | PyHKEY_binaryFailureFunc, /* nb_subtract */ |
| 469 | PyHKEY_binaryFailureFunc, /* nb_multiply */ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 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 */ |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 476 | PyHKEY_boolFunc, /* nb_bool */ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 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 */ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 483 | PyHKEY_intFunc, /* nb_int */ |
Mark Dickinson | 8055afd | 2009-01-17 10:04:45 +0000 | [diff] [blame] | 484 | 0, /* nb_reserved */ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 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[] = { |
| 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} |
| 499 | }; |
| 500 | |
| 501 | #define OFF(e) offsetof(PyHKEYObject, e) |
| 502 | static PyMemberDef PyHKEY_memberlist[] = { |
| 503 | {"handle", T_INT, OFF(hkey), READONLY}, |
| 504 | {NULL} /* Sentinel */ |
| 505 | }; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 506 | |
| 507 | /* The type itself */ |
| 508 | PyTypeObject PyHKEY_Type = |
| 509 | { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 510 | PyVarObject_HEAD_INIT(0, 0) /* fill in type at module init */ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 511 | "PyHKEY", |
| 512 | sizeof(PyHKEYObject), |
| 513 | 0, |
| 514 | PyHKEY_deallocFunc, /* tp_dealloc */ |
Guido van Rossum | 346f1a8 | 2007-08-07 19:58:47 +0000 | [diff] [blame] | 515 | 0, /* tp_print */ |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 516 | 0, /* tp_getattr */ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 517 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 518 | 0, /* tp_reserved */ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 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 */ |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 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 | { |
| 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; |
| 555 | } |
| 556 | |
| 557 | static PyObject * |
| 558 | PyHKEY_DetachMethod(PyObject *self, PyObject *args) |
| 559 | { |
| 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); |
| 567 | } |
| 568 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 569 | static PyObject * |
| 570 | PyHKEY_Enter(PyObject *self) |
| 571 | { |
| 572 | Py_XINCREF(self); |
| 573 | return self; |
| 574 | } |
| 575 | |
| 576 | static PyObject * |
| 577 | PyHKEY_Exit(PyObject *self, PyObject *args) |
| 578 | { |
| 579 | if (!PyHKEY_Close(self)) |
| 580 | return NULL; |
| 581 | Py_RETURN_NONE; |
| 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 | { |
| 591 | PyHKEYObject *key = PyObject_NEW(PyHKEYObject, &PyHKEY_Type); |
| 592 | if (key) |
| 593 | key->hkey = hInit; |
| 594 | return (PyObject *)key; |
| 595 | } |
| 596 | |
| 597 | BOOL |
| 598 | PyHKEY_Close(PyObject *ob_handle) |
| 599 | { |
| 600 | LONG rc; |
| 601 | PyHKEYObject *key; |
| 602 | |
| 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; |
| 613 | } |
| 614 | |
| 615 | BOOL |
| 616 | PyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK) |
| 617 | { |
| 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 | } |
Neal Norwitz | 1fe5f38 | 2007-08-31 04:32:55 +0000 | [diff] [blame] | 631 | else if (PyLong_Check(ob)) { |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 632 | /* We also support integers */ |
| 633 | PyErr_Clear(); |
| 634 | *pHANDLE = (HKEY)PyLong_AsVoidPtr(ob); |
| 635 | if (PyErr_Occurred()) |
| 636 | return FALSE; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 637 | } |
| 638 | else { |
| 639 | PyErr_SetString( |
| 640 | PyExc_TypeError, |
| 641 | "The object is not a PyHKEY object"); |
| 642 | return FALSE; |
| 643 | } |
| 644 | return TRUE; |
| 645 | } |
| 646 | |
| 647 | PyObject * |
| 648 | PyHKEY_FromHKEY(HKEY h) |
| 649 | { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 650 | PyHKEYObject *op; |
| 651 | |
Guido van Rossum | e3a8e7e | 2002-08-19 19:26:42 +0000 | [diff] [blame] | 652 | /* Inline PyObject_New */ |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 653 | op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject)); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 654 | if (op == NULL) |
| 655 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 656 | PyObject_INIT(op, &PyHKEY_Type); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 657 | op->hkey = h; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 658 | return (PyObject *)op; |
| 659 | } |
| 660 | |
| 661 | |
| 662 | /************************************************************************ |
| 663 | The module methods |
| 664 | ************************************************************************/ |
| 665 | BOOL |
| 666 | PyWinObject_CloseHKEY(PyObject *obHandle) |
| 667 | { |
| 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 |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 673 | else if (PyLong_Check(obHandle)) { |
| 674 | long rc = RegCloseKey((HKEY)PyLong_AsLong(obHandle)); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 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 |
| 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 | } |
| 686 | #endif |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +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; |
| 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 | { |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 710 | wchar_t *P; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 711 | int i; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 712 | wchar_t *Q; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 713 | |
| 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 | } |
| 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 | { |
| 725 | int strings; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 726 | wchar_t *P; |
| 727 | wchar_t *Q = data + len; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 728 | |
| 729 | for (P = data, strings = 0; P < Q && *P != '\0'; P++, strings++) |
| 730 | for (; P < Q && *P != '\0'; P++) |
| 731 | ; |
| 732 | return strings; |
| 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 | { |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 740 | Py_ssize_t i,j; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 741 | switch (typ) { |
| 742 | case REG_DWORD: |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 743 | if (value != Py_None && !PyLong_Check(value)) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 744 | return FALSE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 745 | *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 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 | } |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 755 | else { |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 756 | DWORD d = PyLong_AsLong(value); |
Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 757 | memcpy(*retDataBuf, &d, sizeof(DWORD)); |
| 758 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 759 | break; |
| 760 | case REG_SZ: |
| 761 | case REG_EXPAND_SZ: |
| 762 | { |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 763 | if (value == Py_None) |
| 764 | *retDataSize = 1; |
| 765 | else { |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 766 | if (!PyUnicode_Check(value)) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 767 | return FALSE; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 768 | |
| 769 | *retDataSize = 2 + PyUnicode_GET_DATA_SIZE(value); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 770 | } |
| 771 | *retDataBuf = (BYTE *)PyMem_NEW(DWORD, *retDataSize); |
| 772 | if (*retDataBuf==NULL){ |
| 773 | PyErr_NoMemory(); |
| 774 | return FALSE; |
| 775 | } |
| 776 | if (value == Py_None) |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 777 | wcscpy((wchar_t *)*retDataBuf, L""); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 778 | else |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 779 | wcscpy((wchar_t *)*retDataBuf, |
| 780 | PyUnicode_AS_UNICODE(value)); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 781 | break; |
| 782 | } |
| 783 | case REG_MULTI_SZ: |
| 784 | { |
| 785 | DWORD size = 0; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 786 | wchar_t *P; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 787 | |
| 788 | if (value == Py_None) |
| 789 | i = 0; |
| 790 | else { |
| 791 | if (!PyList_Check(value)) |
| 792 | return FALSE; |
| 793 | i = PyList_Size(value); |
| 794 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 795 | for (j = 0; j < i; j++) |
| 796 | { |
| 797 | PyObject *t; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 798 | t = PyList_GET_ITEM(value, j); |
| 799 | if (!PyUnicode_Check(t)) |
| 800 | return FALSE; |
| 801 | size += 2 + PyUnicode_GET_DATA_SIZE(t); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 804 | *retDataSize = size + 2; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 805 | *retDataBuf = (BYTE *)PyMem_NEW(char, |
| 806 | *retDataSize); |
| 807 | if (*retDataBuf==NULL){ |
| 808 | PyErr_NoMemory(); |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 809 | return FALSE; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 810 | } |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 811 | P = (wchar_t *)*retDataBuf; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 812 | |
| 813 | for (j = 0; j < i; j++) |
| 814 | { |
| 815 | PyObject *t; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 816 | t = PyList_GET_ITEM(value, j); |
| 817 | wcscpy(P, PyUnicode_AS_UNICODE(t)); |
| 818 | P += 1 + wcslen( |
| 819 | PyUnicode_AS_UNICODE(t)); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 820 | } |
| 821 | /* And doubly-terminate the list... */ |
| 822 | *P = '\0'; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 823 | break; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 824 | } |
| 825 | case REG_BINARY: |
| 826 | /* ALSO handle ALL unknown data types here. Even if we can't |
| 827 | support it natively, we should handle the bits. */ |
| 828 | default: |
| 829 | if (value == Py_None) |
| 830 | *retDataSize = 0; |
| 831 | else { |
Thomas Heller | 39763a1 | 2007-09-24 14:43:56 +0000 | [diff] [blame] | 832 | Py_buffer view; |
Neal Norwitz | 1385b89 | 2007-08-26 23:07:13 +0000 | [diff] [blame] | 833 | |
| 834 | if (!PyObject_CheckBuffer(value)) { |
Tim Peters | 313fcd4 | 2006-02-19 04:05:39 +0000 | [diff] [blame] | 835 | PyErr_Format(PyExc_TypeError, |
Mark Hammond | 4e80bb5 | 2000-07-28 03:44:41 +0000 | [diff] [blame] | 836 | "Objects of type '%s' can not " |
Tim Peters | 313fcd4 | 2006-02-19 04:05:39 +0000 | [diff] [blame] | 837 | "be used as binary registry values", |
Mark Hammond | 4e80bb5 | 2000-07-28 03:44:41 +0000 | [diff] [blame] | 838 | value->ob_type->tp_name); |
| 839 | return FALSE; |
| 840 | } |
Neal Norwitz | 1385b89 | 2007-08-26 23:07:13 +0000 | [diff] [blame] | 841 | |
| 842 | if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0) |
| 843 | return FALSE; |
| 844 | |
| 845 | *retDataBuf = (BYTE *)PyMem_NEW(char, view.len); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 846 | if (*retDataBuf==NULL){ |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 847 | PyBuffer_Release(&view); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 848 | PyErr_NoMemory(); |
| 849 | return FALSE; |
| 850 | } |
Neal Norwitz | 1385b89 | 2007-08-26 23:07:13 +0000 | [diff] [blame] | 851 | *retDataSize = view.len; |
| 852 | memcpy(*retDataBuf, view.buf, view.len); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 853 | PyBuffer_Release(&view); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 854 | } |
| 855 | break; |
| 856 | } |
| 857 | return TRUE; |
| 858 | } |
| 859 | |
| 860 | /* Convert Registry data into PyObject*/ |
| 861 | static PyObject * |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 862 | Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 863 | { |
| 864 | PyObject *obData; |
| 865 | |
| 866 | switch (typ) { |
| 867 | case REG_DWORD: |
| 868 | if (retDataSize == 0) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 869 | obData = PyLong_FromLong(0); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 870 | else |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 871 | obData = PyLong_FromLong(*(int *)retDataBuf); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 872 | break; |
| 873 | case REG_SZ: |
| 874 | case REG_EXPAND_SZ: |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 875 | { |
| 876 | /* the buffer may or may not have a trailing NULL */ |
| 877 | wchar_t *data = (wchar_t *)retDataBuf; |
| 878 | int len = retDataSize / 2; |
| 879 | if (retDataSize && data[len-1] == '\0') |
| 880 | retDataSize -= 2; |
| 881 | if (retDataSize <= 0) |
| 882 | data = L""; |
| 883 | obData = PyUnicode_FromUnicode(data, retDataSize/2); |
| 884 | break; |
| 885 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 886 | case REG_MULTI_SZ: |
| 887 | if (retDataSize == 0) |
| 888 | obData = PyList_New(0); |
| 889 | else |
| 890 | { |
| 891 | int index = 0; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 892 | wchar_t *data = (wchar_t *)retDataBuf; |
| 893 | int len = retDataSize / 2; |
| 894 | int s = countStrings(data, len); |
| 895 | wchar_t **str = (wchar_t **)malloc(sizeof(wchar_t *)*s); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 896 | if (str == NULL) |
| 897 | return PyErr_NoMemory(); |
| 898 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 899 | fixupMultiSZ(str, data, len); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 900 | obData = PyList_New(s); |
Fred Drake | 25e1726 | 2000-06-30 17:48:51 +0000 | [diff] [blame] | 901 | if (obData == NULL) |
| 902 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 903 | for (index = 0; index < s; index++) |
| 904 | { |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 905 | size_t len = wcslen(str[index]); |
Fred Drake | 25e1726 | 2000-06-30 17:48:51 +0000 | [diff] [blame] | 906 | if (len > INT_MAX) { |
| 907 | PyErr_SetString(PyExc_OverflowError, |
| 908 | "registry string is too long for a Python string"); |
| 909 | Py_DECREF(obData); |
| 910 | return NULL; |
| 911 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 912 | PyList_SetItem(obData, |
| 913 | index, |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 914 | PyUnicode_FromUnicode(str[index], len)); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 915 | } |
| 916 | free(str); |
| 917 | |
| 918 | break; |
| 919 | } |
| 920 | case REG_BINARY: |
| 921 | /* ALSO handle ALL unknown data types here. Even if we can't |
| 922 | support it natively, we should handle the bits. */ |
| 923 | default: |
| 924 | if (retDataSize == 0) { |
| 925 | Py_INCREF(Py_None); |
| 926 | obData = Py_None; |
| 927 | } |
| 928 | else |
Gregory P. Smith | 0a608fd | 2008-09-06 21:34:51 +0000 | [diff] [blame] | 929 | obData = PyBytes_FromStringAndSize( |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 930 | (char *)retDataBuf, retDataSize); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 931 | break; |
| 932 | } |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 933 | return obData; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | /* The Python methods */ |
| 937 | |
| 938 | static PyObject * |
| 939 | PyCloseKey(PyObject *self, PyObject *args) |
| 940 | { |
| 941 | PyObject *obKey; |
| 942 | if (!PyArg_ParseTuple(args, "O:CloseKey", &obKey)) |
| 943 | return NULL; |
| 944 | if (!PyHKEY_Close(obKey)) |
| 945 | return NULL; |
| 946 | Py_INCREF(Py_None); |
| 947 | return Py_None; |
| 948 | } |
| 949 | |
| 950 | static PyObject * |
| 951 | PyConnectRegistry(PyObject *self, PyObject *args) |
| 952 | { |
| 953 | HKEY hKey; |
| 954 | PyObject *obKey; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 955 | wchar_t *szCompName = NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 956 | HKEY retKey; |
| 957 | long rc; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 958 | if (!PyArg_ParseTuple(args, "ZO:ConnectRegistry", &szCompName, &obKey)) |
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; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 962 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 963 | rc = RegConnectRegistryW(szCompName, hKey, &retKey); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 964 | Py_END_ALLOW_THREADS |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 965 | if (rc != ERROR_SUCCESS) |
| 966 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 967 | "ConnectRegistry"); |
| 968 | return PyHKEY_FromHKEY(retKey); |
| 969 | } |
| 970 | |
| 971 | static PyObject * |
| 972 | PyCreateKey(PyObject *self, PyObject *args) |
| 973 | { |
| 974 | HKEY hKey; |
| 975 | PyObject *obKey; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 976 | wchar_t *subKey; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 977 | HKEY retKey; |
| 978 | long rc; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 979 | if (!PyArg_ParseTuple(args, "OZ:CreateKey", &obKey, &subKey)) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 980 | return NULL; |
| 981 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 982 | return NULL; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 983 | rc = RegCreateKeyW(hKey, subKey, &retKey); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 984 | if (rc != ERROR_SUCCESS) |
| 985 | return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey"); |
| 986 | return PyHKEY_FromHKEY(retKey); |
| 987 | } |
| 988 | |
| 989 | static PyObject * |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 990 | PyCreateKeyEx(PyObject *self, PyObject *args) |
| 991 | { |
| 992 | HKEY hKey; |
| 993 | PyObject *obKey; |
| 994 | wchar_t *subKey; |
| 995 | HKEY retKey; |
| 996 | int res = 0; |
| 997 | REGSAM sam = KEY_WRITE; |
| 998 | long rc; |
| 999 | if (!PyArg_ParseTuple(args, "OZ|ii:CreateKeyEx", &obKey, &subKey, |
| 1000 | &res, &sam)) |
| 1001 | return NULL; |
| 1002 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1003 | return NULL; |
| 1004 | |
| 1005 | rc = RegCreateKeyExW(hKey, subKey, res, NULL, (DWORD)NULL, |
| 1006 | sam, NULL, &retKey, NULL); |
| 1007 | if (rc != ERROR_SUCCESS) |
| 1008 | return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKeyEx"); |
| 1009 | return PyHKEY_FromHKEY(retKey); |
| 1010 | } |
| 1011 | |
| 1012 | static PyObject * |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1013 | PyDeleteKey(PyObject *self, PyObject *args) |
| 1014 | { |
| 1015 | HKEY hKey; |
| 1016 | PyObject *obKey; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1017 | wchar_t *subKey; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1018 | long rc; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1019 | if (!PyArg_ParseTuple(args, "Ou:DeleteKey", &obKey, &subKey)) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1020 | return NULL; |
| 1021 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1022 | return NULL; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1023 | rc = RegDeleteKeyW(hKey, subKey ); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1024 | if (rc != ERROR_SUCCESS) |
| 1025 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey"); |
| 1026 | Py_INCREF(Py_None); |
| 1027 | return Py_None; |
| 1028 | } |
| 1029 | |
| 1030 | static PyObject * |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 1031 | PyDeleteKeyEx(PyObject *self, PyObject *args) |
| 1032 | { |
| 1033 | HKEY hKey; |
| 1034 | PyObject *obKey; |
| 1035 | HMODULE hMod; |
| 1036 | typedef LONG (WINAPI *RDKEFunc)(HKEY, const wchar_t*, REGSAM, int); |
| 1037 | RDKEFunc pfn = NULL; |
| 1038 | wchar_t *subKey; |
| 1039 | long rc; |
| 1040 | int res = 0; |
| 1041 | REGSAM sam = KEY_WOW64_64KEY; |
| 1042 | |
| 1043 | if (!PyArg_ParseTuple(args, "Ou|ii:DeleteKeyEx", |
| 1044 | &obKey, &subKey, &sam, &res)) |
| 1045 | return NULL; |
| 1046 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1047 | return NULL; |
| 1048 | |
| 1049 | /* Only available on 64bit platforms, so we must load it |
| 1050 | dynamically. */ |
| 1051 | hMod = GetModuleHandle("advapi32.dll"); |
| 1052 | if (hMod) |
| 1053 | pfn = (RDKEFunc)GetProcAddress(hMod, |
| 1054 | "RegDeleteKeyExW"); |
| 1055 | if (!pfn) { |
| 1056 | PyErr_SetString(PyExc_NotImplementedError, |
| 1057 | "not implemented on this platform"); |
| 1058 | return NULL; |
| 1059 | } |
| 1060 | Py_BEGIN_ALLOW_THREADS |
| 1061 | rc = (*pfn)(hKey, subKey, sam, res); |
| 1062 | Py_END_ALLOW_THREADS |
| 1063 | |
| 1064 | if (rc != ERROR_SUCCESS) |
| 1065 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKeyEx"); |
| 1066 | Py_INCREF(Py_None); |
| 1067 | return Py_None; |
| 1068 | } |
| 1069 | |
| 1070 | static PyObject * |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1071 | PyDeleteValue(PyObject *self, PyObject *args) |
| 1072 | { |
| 1073 | HKEY hKey; |
| 1074 | PyObject *obKey; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1075 | wchar_t *subKey; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1076 | long rc; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1077 | if (!PyArg_ParseTuple(args, "OZ:DeleteValue", &obKey, &subKey)) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1078 | return NULL; |
| 1079 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1080 | return NULL; |
| 1081 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1082 | rc = RegDeleteValueW(hKey, subKey); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1083 | Py_END_ALLOW_THREADS |
| 1084 | if (rc !=ERROR_SUCCESS) |
| 1085 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1086 | "RegDeleteValue"); |
| 1087 | Py_INCREF(Py_None); |
| 1088 | return Py_None; |
| 1089 | } |
| 1090 | |
| 1091 | static PyObject * |
| 1092 | PyEnumKey(PyObject *self, PyObject *args) |
| 1093 | { |
| 1094 | HKEY hKey; |
| 1095 | PyObject *obKey; |
| 1096 | int index; |
| 1097 | long rc; |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1098 | PyObject *retStr; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1099 | wchar_t tmpbuf[256]; /* max key name length is 255 */ |
Georg Brandl | b2699b2 | 2006-02-18 23:44:24 +0000 | [diff] [blame] | 1100 | DWORD len = sizeof(tmpbuf); /* includes NULL terminator */ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1101 | |
| 1102 | if (!PyArg_ParseTuple(args, "Oi:EnumKey", &obKey, &index)) |
| 1103 | return NULL; |
| 1104 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1105 | return NULL; |
Tim Peters | 313fcd4 | 2006-02-19 04:05:39 +0000 | [diff] [blame] | 1106 | |
Georg Brandl | 9a928e7 | 2006-02-18 23:35:11 +0000 | [diff] [blame] | 1107 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1108 | rc = RegEnumKeyExW(hKey, index, tmpbuf, &len, NULL, NULL, NULL, NULL); |
Georg Brandl | 9a928e7 | 2006-02-18 23:35:11 +0000 | [diff] [blame] | 1109 | Py_END_ALLOW_THREADS |
| 1110 | if (rc != ERROR_SUCCESS) |
| 1111 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx"); |
Tim Peters | 313fcd4 | 2006-02-19 04:05:39 +0000 | [diff] [blame] | 1112 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1113 | retStr = PyUnicode_FromUnicode(tmpbuf, len); |
Georg Brandl | 9a928e7 | 2006-02-18 23:35:11 +0000 | [diff] [blame] | 1114 | return retStr; /* can be NULL */ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | static PyObject * |
| 1118 | PyEnumValue(PyObject *self, PyObject *args) |
| 1119 | { |
| 1120 | HKEY hKey; |
| 1121 | PyObject *obKey; |
| 1122 | int index; |
| 1123 | long rc; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1124 | wchar_t *retValueBuf; |
| 1125 | BYTE *retDataBuf; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1126 | DWORD retValueSize; |
| 1127 | DWORD retDataSize; |
| 1128 | DWORD typ; |
| 1129 | PyObject *obData; |
| 1130 | PyObject *retVal; |
| 1131 | |
| 1132 | if (!PyArg_ParseTuple(args, "Oi:EnumValue", &obKey, &index)) |
| 1133 | return NULL; |
| 1134 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1135 | return NULL; |
| 1136 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1137 | if ((rc = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, NULL, NULL, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1138 | NULL, |
| 1139 | &retValueSize, &retDataSize, NULL, NULL)) |
| 1140 | != ERROR_SUCCESS) |
| 1141 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1142 | "RegQueryInfoKey"); |
| 1143 | ++retValueSize; /* include null terminators */ |
| 1144 | ++retDataSize; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1145 | retValueBuf = (wchar_t *)PyMem_Malloc(sizeof(wchar_t) * retValueSize); |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1146 | if (retValueBuf == NULL) |
| 1147 | return PyErr_NoMemory(); |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1148 | retDataBuf = (BYTE *)PyMem_Malloc(retDataSize); |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1149 | if (retDataBuf == NULL) { |
| 1150 | PyMem_Free(retValueBuf); |
| 1151 | return PyErr_NoMemory(); |
| 1152 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1153 | |
| 1154 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1155 | rc = RegEnumValueW(hKey, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1156 | index, |
| 1157 | retValueBuf, |
| 1158 | &retValueSize, |
| 1159 | NULL, |
| 1160 | &typ, |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1161 | retDataBuf, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1162 | &retDataSize); |
| 1163 | Py_END_ALLOW_THREADS |
| 1164 | |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1165 | if (rc != ERROR_SUCCESS) { |
| 1166 | retVal = PyErr_SetFromWindowsErrWithFunction(rc, |
| 1167 | "PyRegEnumValue"); |
| 1168 | goto fail; |
| 1169 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1170 | obData = Reg2Py(retDataBuf, retDataSize, typ); |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1171 | if (obData == NULL) { |
| 1172 | retVal = NULL; |
| 1173 | goto fail; |
| 1174 | } |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1175 | retVal = Py_BuildValue("uOi", retValueBuf, obData, typ); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1176 | Py_DECREF(obData); |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1177 | fail: |
| 1178 | PyMem_Free(retValueBuf); |
| 1179 | PyMem_Free(retDataBuf); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1180 | return retVal; |
| 1181 | } |
| 1182 | |
| 1183 | static PyObject * |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1184 | PyExpandEnvironmentStrings(PyObject *self, PyObject *args) |
| 1185 | { |
| 1186 | Py_UNICODE *retValue = NULL; |
| 1187 | Py_UNICODE *src; |
| 1188 | DWORD retValueSize; |
| 1189 | DWORD rc; |
| 1190 | PyObject *o; |
| 1191 | |
| 1192 | if (!PyArg_ParseTuple(args, "u:ExpandEnvironmentStrings", &src)) |
| 1193 | return NULL; |
| 1194 | |
| 1195 | retValueSize = ExpandEnvironmentStringsW(src, retValue, 0); |
| 1196 | if (retValueSize == 0) { |
| 1197 | return PyErr_SetFromWindowsErrWithFunction(retValueSize, |
| 1198 | "ExpandEnvironmentStrings"); |
| 1199 | } |
| 1200 | retValue = (Py_UNICODE *)PyMem_Malloc(retValueSize * sizeof(Py_UNICODE)); |
| 1201 | if (retValue == NULL) { |
| 1202 | return PyErr_NoMemory(); |
| 1203 | } |
| 1204 | |
| 1205 | rc = ExpandEnvironmentStringsW(src, retValue, retValueSize); |
| 1206 | if (rc == 0) { |
| 1207 | PyMem_Free(retValue); |
| 1208 | return PyErr_SetFromWindowsErrWithFunction(retValueSize, |
| 1209 | "ExpandEnvironmentStrings"); |
| 1210 | } |
| 1211 | o = PyUnicode_FromUnicode(retValue, wcslen(retValue)); |
| 1212 | PyMem_Free(retValue); |
| 1213 | return o; |
| 1214 | } |
| 1215 | |
| 1216 | static PyObject * |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1217 | PyFlushKey(PyObject *self, PyObject *args) |
| 1218 | { |
| 1219 | HKEY hKey; |
| 1220 | PyObject *obKey; |
| 1221 | long rc; |
| 1222 | if (!PyArg_ParseTuple(args, "O:FlushKey", &obKey)) |
| 1223 | return NULL; |
| 1224 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1225 | return NULL; |
| 1226 | Py_BEGIN_ALLOW_THREADS |
| 1227 | rc = RegFlushKey(hKey); |
| 1228 | Py_END_ALLOW_THREADS |
| 1229 | if (rc != ERROR_SUCCESS) |
| 1230 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey"); |
| 1231 | Py_INCREF(Py_None); |
| 1232 | return Py_None; |
| 1233 | } |
| 1234 | static PyObject * |
| 1235 | PyLoadKey(PyObject *self, PyObject *args) |
| 1236 | { |
| 1237 | HKEY hKey; |
| 1238 | PyObject *obKey; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1239 | wchar_t *subKey; |
| 1240 | wchar_t *fileName; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1241 | |
| 1242 | long rc; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1243 | if (!PyArg_ParseTuple(args, "Ouu:LoadKey", &obKey, &subKey, &fileName)) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1244 | return NULL; |
| 1245 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1246 | return NULL; |
| 1247 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1248 | rc = RegLoadKeyW(hKey, subKey, fileName ); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1249 | Py_END_ALLOW_THREADS |
| 1250 | if (rc != ERROR_SUCCESS) |
| 1251 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey"); |
| 1252 | Py_INCREF(Py_None); |
| 1253 | return Py_None; |
| 1254 | } |
| 1255 | |
| 1256 | static PyObject * |
| 1257 | PyOpenKey(PyObject *self, PyObject *args) |
| 1258 | { |
| 1259 | HKEY hKey; |
| 1260 | PyObject *obKey; |
| 1261 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1262 | wchar_t *subKey; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1263 | int res = 0; |
| 1264 | HKEY retKey; |
| 1265 | long rc; |
| 1266 | REGSAM sam = KEY_READ; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1267 | if (!PyArg_ParseTuple(args, "OZ|ii:OpenKey", &obKey, &subKey, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1268 | &res, &sam)) |
| 1269 | return NULL; |
| 1270 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1271 | return NULL; |
| 1272 | |
| 1273 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1274 | rc = RegOpenKeyExW(hKey, subKey, res, sam, &retKey); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1275 | Py_END_ALLOW_THREADS |
| 1276 | if (rc != ERROR_SUCCESS) |
| 1277 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx"); |
| 1278 | return PyHKEY_FromHKEY(retKey); |
| 1279 | } |
| 1280 | |
| 1281 | |
| 1282 | static PyObject * |
| 1283 | PyQueryInfoKey(PyObject *self, PyObject *args) |
| 1284 | { |
| 1285 | HKEY hKey; |
| 1286 | PyObject *obKey; |
| 1287 | long rc; |
| 1288 | DWORD nSubKeys, nValues; |
| 1289 | FILETIME ft; |
| 1290 | LARGE_INTEGER li; |
| 1291 | PyObject *l; |
| 1292 | PyObject *ret; |
| 1293 | if (!PyArg_ParseTuple(args, "O:QueryInfoKey", &obKey)) |
| 1294 | return NULL; |
| 1295 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1296 | return NULL; |
| 1297 | if ((rc = RegQueryInfoKey(hKey, NULL, NULL, 0, &nSubKeys, NULL, NULL, |
| 1298 | &nValues, NULL, NULL, NULL, &ft)) |
| 1299 | != ERROR_SUCCESS) |
| 1300 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey"); |
| 1301 | li.LowPart = ft.dwLowDateTime; |
| 1302 | li.HighPart = ft.dwHighDateTime; |
| 1303 | l = PyLong_FromLongLong(li.QuadPart); |
| 1304 | if (l == NULL) |
| 1305 | return NULL; |
| 1306 | ret = Py_BuildValue("iiO", nSubKeys, nValues, l); |
| 1307 | Py_DECREF(l); |
| 1308 | return ret; |
| 1309 | } |
| 1310 | |
| 1311 | static PyObject * |
| 1312 | PyQueryValue(PyObject *self, PyObject *args) |
| 1313 | { |
| 1314 | HKEY hKey; |
| 1315 | PyObject *obKey; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1316 | wchar_t *subKey; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1317 | long rc; |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1318 | PyObject *retStr; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1319 | wchar_t *retBuf; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1320 | long bufSize = 0; |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1321 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1322 | if (!PyArg_ParseTuple(args, "OZ:QueryValue", &obKey, &subKey)) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1323 | return NULL; |
| 1324 | |
| 1325 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1326 | return NULL; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1327 | if ((rc = RegQueryValueW(hKey, subKey, NULL, &bufSize)) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1328 | != ERROR_SUCCESS) |
| 1329 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1330 | "RegQueryValue"); |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1331 | retBuf = (wchar_t *)PyMem_Malloc(bufSize); |
Guido van Rossum | a8c360e | 2007-07-17 20:50:43 +0000 | [diff] [blame] | 1332 | if (retBuf == NULL) |
| 1333 | return PyErr_NoMemory(); |
| 1334 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1335 | if ((rc = RegQueryValueW(hKey, subKey, retBuf, &bufSize)) |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1336 | != ERROR_SUCCESS) { |
Guido van Rossum | a8c360e | 2007-07-17 20:50:43 +0000 | [diff] [blame] | 1337 | PyMem_Free(retBuf); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1338 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1339 | "RegQueryValue"); |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1340 | } |
Guido van Rossum | a8c360e | 2007-07-17 20:50:43 +0000 | [diff] [blame] | 1341 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1342 | retStr = PyUnicode_FromUnicode(retBuf, wcslen(retBuf)); |
Guido van Rossum | a8c360e | 2007-07-17 20:50:43 +0000 | [diff] [blame] | 1343 | PyMem_Free(retBuf); |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1344 | return retStr; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | static PyObject * |
| 1348 | PyQueryValueEx(PyObject *self, PyObject *args) |
| 1349 | { |
| 1350 | HKEY hKey; |
| 1351 | PyObject *obKey; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1352 | wchar_t *valueName; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1353 | |
| 1354 | long rc; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1355 | BYTE *retBuf; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1356 | DWORD bufSize = 0; |
| 1357 | DWORD typ; |
| 1358 | PyObject *obData; |
| 1359 | PyObject *result; |
| 1360 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1361 | if (!PyArg_ParseTuple(args, "OZ:QueryValueEx", &obKey, &valueName)) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1362 | return NULL; |
| 1363 | |
| 1364 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1365 | return NULL; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1366 | if ((rc = RegQueryValueExW(hKey, valueName, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1367 | NULL, NULL, NULL, |
| 1368 | &bufSize)) |
| 1369 | != ERROR_SUCCESS) |
| 1370 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1371 | "RegQueryValueEx"); |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1372 | retBuf = (BYTE *)PyMem_Malloc(bufSize); |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1373 | if (retBuf == NULL) |
| 1374 | return PyErr_NoMemory(); |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1375 | if ((rc = RegQueryValueExW(hKey, valueName, NULL, |
| 1376 | &typ, retBuf, &bufSize)) |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1377 | != ERROR_SUCCESS) { |
| 1378 | PyMem_Free(retBuf); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1379 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1380 | "RegQueryValueEx"); |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1381 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1382 | obData = Reg2Py(retBuf, bufSize, typ); |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1383 | PyMem_Free(retBuf); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1384 | if (obData == NULL) |
| 1385 | return NULL; |
| 1386 | result = Py_BuildValue("Oi", obData, typ); |
| 1387 | Py_DECREF(obData); |
| 1388 | return result; |
| 1389 | } |
| 1390 | |
| 1391 | |
| 1392 | static PyObject * |
| 1393 | PySaveKey(PyObject *self, PyObject *args) |
| 1394 | { |
| 1395 | HKEY hKey; |
| 1396 | PyObject *obKey; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1397 | wchar_t *fileName; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1398 | LPSECURITY_ATTRIBUTES pSA = NULL; |
| 1399 | |
| 1400 | long rc; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1401 | if (!PyArg_ParseTuple(args, "Ou:SaveKey", &obKey, &fileName)) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1402 | return NULL; |
| 1403 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1404 | return NULL; |
| 1405 | /* One day we may get security into the core? |
| 1406 | if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE)) |
| 1407 | return NULL; |
| 1408 | */ |
| 1409 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1410 | rc = RegSaveKeyW(hKey, fileName, pSA ); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1411 | Py_END_ALLOW_THREADS |
| 1412 | if (rc != ERROR_SUCCESS) |
| 1413 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey"); |
| 1414 | Py_INCREF(Py_None); |
| 1415 | return Py_None; |
| 1416 | } |
| 1417 | |
| 1418 | static PyObject * |
| 1419 | PySetValue(PyObject *self, PyObject *args) |
| 1420 | { |
| 1421 | HKEY hKey; |
| 1422 | PyObject *obKey; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1423 | wchar_t *subKey; |
| 1424 | wchar_t *str; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1425 | DWORD typ; |
| 1426 | DWORD len; |
| 1427 | long rc; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1428 | if (!PyArg_ParseTuple(args, "OZiu#:SetValue", |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1429 | &obKey, |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1430 | &subKey, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1431 | &typ, |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1432 | &str, |
| 1433 | &len)) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1434 | return NULL; |
| 1435 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1436 | return NULL; |
| 1437 | if (typ != REG_SZ) { |
| 1438 | PyErr_SetString(PyExc_TypeError, |
Georg Brandl | 38feaf0 | 2008-05-25 07:45:51 +0000 | [diff] [blame] | 1439 | "Type must be winreg.REG_SZ"); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1440 | return NULL; |
| 1441 | } |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1442 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1443 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1444 | rc = RegSetValueW(hKey, subKey, REG_SZ, str, len+1); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1445 | Py_END_ALLOW_THREADS |
| 1446 | if (rc != ERROR_SUCCESS) |
| 1447 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue"); |
| 1448 | Py_INCREF(Py_None); |
| 1449 | return Py_None; |
| 1450 | } |
| 1451 | |
| 1452 | static PyObject * |
| 1453 | PySetValueEx(PyObject *self, PyObject *args) |
| 1454 | { |
| 1455 | HKEY hKey; |
| 1456 | PyObject *obKey; |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1457 | Py_UNICODE *valueName; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1458 | PyObject *obRes; |
| 1459 | PyObject *value; |
| 1460 | BYTE *data; |
| 1461 | DWORD len; |
| 1462 | DWORD typ; |
| 1463 | |
| 1464 | LONG rc; |
| 1465 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1466 | if (!PyArg_ParseTuple(args, "OZOiO:SetValueEx", |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1467 | &obKey, |
| 1468 | &valueName, |
| 1469 | &obRes, |
| 1470 | &typ, |
| 1471 | &value)) |
| 1472 | return NULL; |
| 1473 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1474 | return NULL; |
| 1475 | if (!Py2Reg(value, typ, &data, &len)) |
| 1476 | { |
| 1477 | if (!PyErr_Occurred()) |
| 1478 | PyErr_SetString(PyExc_ValueError, |
| 1479 | "Could not convert the data to the specified type."); |
| 1480 | return NULL; |
| 1481 | } |
| 1482 | Py_BEGIN_ALLOW_THREADS |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1483 | rc = RegSetValueExW(hKey, valueName, 0, typ, data, len); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1484 | Py_END_ALLOW_THREADS |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1485 | PyMem_DEL(data); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1486 | if (rc != ERROR_SUCCESS) |
| 1487 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1488 | "RegSetValueEx"); |
| 1489 | Py_INCREF(Py_None); |
| 1490 | return Py_None; |
| 1491 | } |
| 1492 | |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1493 | static PyObject * |
| 1494 | PyDisableReflectionKey(PyObject *self, PyObject *args) |
| 1495 | { |
| 1496 | HKEY hKey; |
| 1497 | PyObject *obKey; |
| 1498 | HMODULE hMod; |
| 1499 | typedef LONG (WINAPI *RDRKFunc)(HKEY); |
| 1500 | RDRKFunc pfn = NULL; |
| 1501 | LONG rc; |
| 1502 | |
| 1503 | if (!PyArg_ParseTuple(args, "O:DisableReflectionKey", &obKey)) |
| 1504 | return NULL; |
| 1505 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1506 | return NULL; |
| 1507 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 1508 | /* Only available on 64bit platforms, so we must load it |
| 1509 | dynamically.*/ |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1510 | hMod = GetModuleHandle("advapi32.dll"); |
| 1511 | if (hMod) |
| 1512 | pfn = (RDRKFunc)GetProcAddress(hMod, |
| 1513 | "RegDisableReflectionKey"); |
| 1514 | if (!pfn) { |
| 1515 | PyErr_SetString(PyExc_NotImplementedError, |
| 1516 | "not implemented on this platform"); |
| 1517 | return NULL; |
| 1518 | } |
| 1519 | Py_BEGIN_ALLOW_THREADS |
| 1520 | rc = (*pfn)(hKey); |
| 1521 | Py_END_ALLOW_THREADS |
| 1522 | if (rc != ERROR_SUCCESS) |
| 1523 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1524 | "RegDisableReflectionKey"); |
| 1525 | Py_INCREF(Py_None); |
| 1526 | return Py_None; |
| 1527 | } |
| 1528 | |
| 1529 | static PyObject * |
| 1530 | PyEnableReflectionKey(PyObject *self, PyObject *args) |
| 1531 | { |
| 1532 | HKEY hKey; |
| 1533 | PyObject *obKey; |
| 1534 | HMODULE hMod; |
| 1535 | typedef LONG (WINAPI *RERKFunc)(HKEY); |
| 1536 | RERKFunc pfn = NULL; |
| 1537 | LONG rc; |
| 1538 | |
| 1539 | if (!PyArg_ParseTuple(args, "O:EnableReflectionKey", &obKey)) |
| 1540 | return NULL; |
| 1541 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1542 | return NULL; |
| 1543 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 1544 | /* Only available on 64bit platforms, so we must load it |
| 1545 | dynamically.*/ |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1546 | hMod = GetModuleHandle("advapi32.dll"); |
| 1547 | if (hMod) |
| 1548 | pfn = (RERKFunc)GetProcAddress(hMod, |
| 1549 | "RegEnableReflectionKey"); |
| 1550 | if (!pfn) { |
| 1551 | PyErr_SetString(PyExc_NotImplementedError, |
| 1552 | "not implemented on this platform"); |
| 1553 | return NULL; |
| 1554 | } |
| 1555 | Py_BEGIN_ALLOW_THREADS |
| 1556 | rc = (*pfn)(hKey); |
| 1557 | Py_END_ALLOW_THREADS |
| 1558 | if (rc != ERROR_SUCCESS) |
| 1559 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1560 | "RegEnableReflectionKey"); |
| 1561 | Py_INCREF(Py_None); |
| 1562 | return Py_None; |
| 1563 | } |
| 1564 | |
| 1565 | static PyObject * |
| 1566 | PyQueryReflectionKey(PyObject *self, PyObject *args) |
| 1567 | { |
| 1568 | HKEY hKey; |
| 1569 | PyObject *obKey; |
| 1570 | HMODULE hMod; |
| 1571 | typedef LONG (WINAPI *RQRKFunc)(HKEY, BOOL *); |
| 1572 | RQRKFunc pfn = NULL; |
| 1573 | BOOL result; |
| 1574 | LONG rc; |
| 1575 | |
| 1576 | if (!PyArg_ParseTuple(args, "O:QueryReflectionKey", &obKey)) |
| 1577 | return NULL; |
| 1578 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1579 | return NULL; |
| 1580 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 1581 | /* Only available on 64bit platforms, so we must load it |
| 1582 | dynamically.*/ |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1583 | hMod = GetModuleHandle("advapi32.dll"); |
| 1584 | if (hMod) |
| 1585 | pfn = (RQRKFunc)GetProcAddress(hMod, |
| 1586 | "RegQueryReflectionKey"); |
| 1587 | if (!pfn) { |
| 1588 | PyErr_SetString(PyExc_NotImplementedError, |
| 1589 | "not implemented on this platform"); |
| 1590 | return NULL; |
| 1591 | } |
| 1592 | Py_BEGIN_ALLOW_THREADS |
| 1593 | rc = (*pfn)(hKey, &result); |
| 1594 | Py_END_ALLOW_THREADS |
| 1595 | if (rc != ERROR_SUCCESS) |
| 1596 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1597 | "RegQueryReflectionKey"); |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 1598 | return PyBool_FromLong(result); |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1601 | static struct PyMethodDef winreg_methods[] = { |
Neal Norwitz | 031829d | 2002-03-31 14:37:44 +0000 | [diff] [blame] | 1602 | {"CloseKey", PyCloseKey, METH_VARARGS, CloseKey_doc}, |
| 1603 | {"ConnectRegistry", PyConnectRegistry, METH_VARARGS, ConnectRegistry_doc}, |
| 1604 | {"CreateKey", PyCreateKey, METH_VARARGS, CreateKey_doc}, |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 1605 | {"CreateKeyEx", PyCreateKeyEx, METH_VARARGS, CreateKeyEx_doc}, |
Neal Norwitz | 031829d | 2002-03-31 14:37:44 +0000 | [diff] [blame] | 1606 | {"DeleteKey", PyDeleteKey, METH_VARARGS, DeleteKey_doc}, |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 1607 | {"DeleteKeyEx", PyDeleteKeyEx, METH_VARARGS, DeleteKeyEx_doc}, |
Neal Norwitz | 031829d | 2002-03-31 14:37:44 +0000 | [diff] [blame] | 1608 | {"DeleteValue", PyDeleteValue, METH_VARARGS, DeleteValue_doc}, |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1609 | {"DisableReflectionKey", PyDisableReflectionKey, METH_VARARGS, DisableReflectionKey_doc}, |
| 1610 | {"EnableReflectionKey", PyEnableReflectionKey, METH_VARARGS, EnableReflectionKey_doc}, |
Neal Norwitz | 031829d | 2002-03-31 14:37:44 +0000 | [diff] [blame] | 1611 | {"EnumKey", PyEnumKey, METH_VARARGS, EnumKey_doc}, |
| 1612 | {"EnumValue", PyEnumValue, METH_VARARGS, EnumValue_doc}, |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1613 | {"ExpandEnvironmentStrings", PyExpandEnvironmentStrings, METH_VARARGS, |
| 1614 | ExpandEnvironmentStrings_doc }, |
Neal Norwitz | 031829d | 2002-03-31 14:37:44 +0000 | [diff] [blame] | 1615 | {"FlushKey", PyFlushKey, METH_VARARGS, FlushKey_doc}, |
| 1616 | {"LoadKey", PyLoadKey, METH_VARARGS, LoadKey_doc}, |
| 1617 | {"OpenKey", PyOpenKey, METH_VARARGS, OpenKey_doc}, |
| 1618 | {"OpenKeyEx", PyOpenKey, METH_VARARGS, OpenKeyEx_doc}, |
| 1619 | {"QueryValue", PyQueryValue, METH_VARARGS, QueryValue_doc}, |
| 1620 | {"QueryValueEx", PyQueryValueEx, METH_VARARGS, QueryValueEx_doc}, |
| 1621 | {"QueryInfoKey", PyQueryInfoKey, METH_VARARGS, QueryInfoKey_doc}, |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1622 | {"QueryReflectionKey",PyQueryReflectionKey,METH_VARARGS, QueryReflectionKey_doc}, |
Neal Norwitz | 031829d | 2002-03-31 14:37:44 +0000 | [diff] [blame] | 1623 | {"SaveKey", PySaveKey, METH_VARARGS, SaveKey_doc}, |
| 1624 | {"SetValue", PySetValue, METH_VARARGS, SetValue_doc}, |
| 1625 | {"SetValueEx", PySetValueEx, METH_VARARGS, SetValueEx_doc}, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1626 | NULL, |
| 1627 | }; |
| 1628 | |
| 1629 | static void |
| 1630 | insint(PyObject * d, char * name, long value) |
| 1631 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1632 | PyObject *v = PyLong_FromLong(value); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1633 | if (!v || PyDict_SetItemString(d, name, v)) |
| 1634 | PyErr_Clear(); |
| 1635 | Py_XDECREF(v); |
| 1636 | } |
| 1637 | |
| 1638 | #define ADD_INT(val) insint(d, #val, val) |
| 1639 | |
| 1640 | static void |
| 1641 | inskey(PyObject * d, char * name, HKEY key) |
| 1642 | { |
| 1643 | PyObject *v = PyLong_FromVoidPtr(key); |
| 1644 | if (!v || PyDict_SetItemString(d, name, v)) |
| 1645 | PyErr_Clear(); |
| 1646 | Py_XDECREF(v); |
| 1647 | } |
| 1648 | |
| 1649 | #define ADD_KEY(val) inskey(d, #val, val) |
| 1650 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1651 | |
| 1652 | static struct PyModuleDef winregmodule = { |
| 1653 | PyModuleDef_HEAD_INIT, |
| 1654 | "winreg", |
| 1655 | module_doc, |
| 1656 | -1, |
| 1657 | winreg_methods, |
| 1658 | NULL, |
| 1659 | NULL, |
| 1660 | NULL, |
| 1661 | NULL |
| 1662 | }; |
| 1663 | |
| 1664 | PyMODINIT_FUNC PyInit_winreg(void) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1665 | { |
| 1666 | PyObject *m, *d; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1667 | m = PyModule_Create(&winregmodule); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 1668 | if (m == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1669 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1670 | d = PyModule_GetDict(m); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1671 | PyHKEY_Type.tp_doc = PyHKEY_doc; |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 1672 | if (PyType_Ready(&PyHKEY_Type) < 0) |
| 1673 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1674 | Py_INCREF(&PyHKEY_Type); |
| 1675 | if (PyDict_SetItemString(d, "HKEYType", |
| 1676 | (PyObject *)&PyHKEY_Type) != 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1677 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1678 | Py_INCREF(PyExc_WindowsError); |
| 1679 | if (PyDict_SetItemString(d, "error", |
| 1680 | PyExc_WindowsError) != 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1681 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1682 | |
| 1683 | /* Add the relevant constants */ |
| 1684 | ADD_KEY(HKEY_CLASSES_ROOT); |
| 1685 | ADD_KEY(HKEY_CURRENT_USER); |
| 1686 | ADD_KEY(HKEY_LOCAL_MACHINE); |
| 1687 | ADD_KEY(HKEY_USERS); |
| 1688 | ADD_KEY(HKEY_PERFORMANCE_DATA); |
| 1689 | #ifdef HKEY_CURRENT_CONFIG |
| 1690 | ADD_KEY(HKEY_CURRENT_CONFIG); |
| 1691 | #endif |
| 1692 | #ifdef HKEY_DYN_DATA |
| 1693 | ADD_KEY(HKEY_DYN_DATA); |
| 1694 | #endif |
| 1695 | ADD_INT(KEY_QUERY_VALUE); |
| 1696 | ADD_INT(KEY_SET_VALUE); |
| 1697 | ADD_INT(KEY_CREATE_SUB_KEY); |
| 1698 | ADD_INT(KEY_ENUMERATE_SUB_KEYS); |
| 1699 | ADD_INT(KEY_NOTIFY); |
| 1700 | ADD_INT(KEY_CREATE_LINK); |
| 1701 | ADD_INT(KEY_READ); |
| 1702 | ADD_INT(KEY_WRITE); |
| 1703 | ADD_INT(KEY_EXECUTE); |
| 1704 | ADD_INT(KEY_ALL_ACCESS); |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1705 | #ifdef KEY_WOW64_64KEY |
| 1706 | ADD_INT(KEY_WOW64_64KEY); |
| 1707 | #endif |
| 1708 | #ifdef KEY_WOW64_32KEY |
| 1709 | ADD_INT(KEY_WOW64_32KEY); |
| 1710 | #endif |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1711 | ADD_INT(REG_OPTION_RESERVED); |
| 1712 | ADD_INT(REG_OPTION_NON_VOLATILE); |
| 1713 | ADD_INT(REG_OPTION_VOLATILE); |
| 1714 | ADD_INT(REG_OPTION_CREATE_LINK); |
| 1715 | ADD_INT(REG_OPTION_BACKUP_RESTORE); |
| 1716 | ADD_INT(REG_OPTION_OPEN_LINK); |
| 1717 | ADD_INT(REG_LEGAL_OPTION); |
| 1718 | ADD_INT(REG_CREATED_NEW_KEY); |
| 1719 | ADD_INT(REG_OPENED_EXISTING_KEY); |
| 1720 | ADD_INT(REG_WHOLE_HIVE_VOLATILE); |
| 1721 | ADD_INT(REG_REFRESH_HIVE); |
| 1722 | ADD_INT(REG_NO_LAZY_FLUSH); |
| 1723 | ADD_INT(REG_NOTIFY_CHANGE_NAME); |
| 1724 | ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES); |
| 1725 | ADD_INT(REG_NOTIFY_CHANGE_LAST_SET); |
| 1726 | ADD_INT(REG_NOTIFY_CHANGE_SECURITY); |
| 1727 | ADD_INT(REG_LEGAL_CHANGE_FILTER); |
| 1728 | ADD_INT(REG_NONE); |
| 1729 | ADD_INT(REG_SZ); |
| 1730 | ADD_INT(REG_EXPAND_SZ); |
| 1731 | ADD_INT(REG_BINARY); |
| 1732 | ADD_INT(REG_DWORD); |
| 1733 | ADD_INT(REG_DWORD_LITTLE_ENDIAN); |
| 1734 | ADD_INT(REG_DWORD_BIG_ENDIAN); |
| 1735 | ADD_INT(REG_LINK); |
| 1736 | ADD_INT(REG_MULTI_SZ); |
| 1737 | ADD_INT(REG_RESOURCE_LIST); |
| 1738 | ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR); |
| 1739 | ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1740 | return m; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1743 | |