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