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