Georg Brandl | 38feaf0 | 2008-05-25 07:45:51 +0000 | [diff] [blame] | 1 | :mod:`winreg` -- Windows registry access |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2 | ========================================= |
| 3 | |
Georg Brandl | 38feaf0 | 2008-05-25 07:45:51 +0000 | [diff] [blame] | 4 | .. module:: winreg |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 5 | :platform: Windows |
| 6 | :synopsis: Routines and objects for manipulating the Windows registry. |
| 7 | .. sectionauthor:: Mark Hammond <MarkH@ActiveState.com> |
| 8 | |
| 9 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 10 | These functions expose the Windows registry API to Python. Instead of using an |
| 11 | integer as the registry handle, a handle object is used to ensure that the |
| 12 | handles are closed correctly, even if the programmer neglects to explicitly |
| 13 | close them. |
| 14 | |
| 15 | This module exposes a very low-level interface to the Windows registry; it is |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 16 | expected that in the future a new ``winreg`` module will be created offering a |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 17 | higher-level interface to the registry API. |
| 18 | |
| 19 | This module offers the following functions: |
| 20 | |
| 21 | |
| 22 | .. function:: CloseKey(hkey) |
| 23 | |
| 24 | Closes a previously opened registry key. The hkey argument specifies a |
| 25 | previously opened key. |
| 26 | |
| 27 | Note that if *hkey* is not closed using this method (or via |
| 28 | :meth:`handle.Close`), it is closed when the *hkey* object is destroyed by |
| 29 | Python. |
| 30 | |
| 31 | |
| 32 | .. function:: ConnectRegistry(computer_name, key) |
| 33 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 34 | Establishes a connection to a predefined registry handle on another computer, |
| 35 | and returns a :ref:`handle object <handle-object>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 36 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 37 | *computer_name* is the name of the remote computer, of the form |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 38 | ``r"\\computername"``. If ``None``, the local computer is used. |
| 39 | |
| 40 | *key* is the predefined handle to connect to. |
| 41 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 42 | The return value is the handle of the opened key. If the function fails, a |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 43 | :exc:`WindowsError` exception is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 44 | |
| 45 | |
| 46 | .. function:: CreateKey(key, sub_key) |
| 47 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 48 | Creates or opens the specified key, returning a |
| 49 | :ref:`handle object <handle-object>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 50 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 51 | *key* is an already open key, or one of the predefined :const:`HKEY_\*` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 52 | constants. |
| 53 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 54 | *sub_key* is a string that names the key this method opens or creates. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 55 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 56 | If *key* is one of the predefined keys, *sub_key* may be ``None``. In that |
| 57 | case, the handle returned is the same key handle passed in to the function. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 58 | |
| 59 | If the key already exists, this function opens the existing key. |
| 60 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 61 | The return value is the handle of the opened key. If the function fails, a |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 62 | :exc:`WindowsError` exception is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 63 | |
| 64 | |
Brian Curtin | d855948 | 2010-04-24 17:21:31 +0000 | [diff] [blame] | 65 | .. function:: CreateKeyEx(key, sub_key[, res[, sam]]) |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 66 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 67 | Creates or opens the specified key, returning a |
| 68 | :ref:`handle object <handle-object>`. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 69 | |
| 70 | *key* is an already open key, or one of the predefined :const:`HKEY_\*` |
| 71 | constants. |
| 72 | |
| 73 | *sub_key* is a string that names the key this method opens or creates. |
| 74 | |
| 75 | *res* is a reserved integer, and must be zero. The default is zero. |
| 76 | |
| 77 | *sam* is an integer that specifies an access mask that describes the desired |
| 78 | security access for the key. Default is :const:`KEY_ALL_ACCESS` |
| 79 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 80 | If *key* is one of the predefined keys, *sub_key* may be ``None``. In that |
| 81 | case, the handle returned is the same key handle passed in to the function. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 82 | |
| 83 | If the key already exists, this function opens the existing key. |
| 84 | |
| 85 | The return value is the handle of the opened key. If the function fails, a |
| 86 | :exc:`WindowsError` exception is raised. |
| 87 | |
Georg Brandl | 4c25cf3 | 2010-04-22 07:00:42 +0000 | [diff] [blame] | 88 | .. versionadded:: 3.2 |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 89 | |
| 90 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 91 | .. function:: DeleteKey(key, sub_key) |
| 92 | |
| 93 | Deletes the specified key. |
| 94 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 95 | *key* is an already open key, or any one of the predefined :const:`HKEY_\*` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 96 | constants. |
| 97 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 98 | *sub_key* is a string that must be a subkey of the key identified by the *key* |
| 99 | parameter. This value must not be ``None``, and the key may not have subkeys. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 100 | |
| 101 | *This method can not delete keys with subkeys.* |
| 102 | |
| 103 | If the method succeeds, the entire key, including all of its values, is removed. |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 104 | If the method fails, a :exc:`WindowsError` exception is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 105 | |
| 106 | |
Brian Curtin | d855948 | 2010-04-24 17:21:31 +0000 | [diff] [blame] | 107 | .. function:: DeleteKeyEx(key, sub_key[, sam[, res]]) |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 108 | |
| 109 | Deletes the specified key. |
| 110 | |
| 111 | .. note:: |
| 112 | The :func:`DeleteKeyEx` function is implemented with the RegDeleteKeyEx |
| 113 | Windows API function, which is specific to 64-bit versions of Windows. |
| 114 | See http://msdn.microsoft.com/en-us/library/ms724847%28VS.85%29.aspx |
| 115 | |
| 116 | *key* is an already open key, or any one of the predefined :const:`HKEY_\*` |
| 117 | constants. |
| 118 | |
| 119 | *sub_key* is a string that must be a subkey of the key identified by the |
| 120 | *key* parameter. This value must not be ``None``, and the key may not have |
| 121 | subkeys. |
| 122 | |
| 123 | *res* is a reserved integer, and must be zero. The default is zero. |
| 124 | |
| 125 | *sam* is an integer that specifies an access mask that describes the |
| 126 | desired security access for the key. Default is :const:`KEY_WOW64_64KEY` |
| 127 | |
| 128 | *This method can not delete keys with subkeys.* |
| 129 | |
| 130 | If the method succeeds, the entire key, including all of its values, is |
| 131 | removed. If the method fails, a :exc:`WindowsError` exception is raised. |
| 132 | |
| 133 | On unsupported Windows versions, :exc:`NotImplementedError` is raised. |
| 134 | |
Georg Brandl | 4c25cf3 | 2010-04-22 07:00:42 +0000 | [diff] [blame] | 135 | .. versionadded:: 3.2 |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 136 | |
| 137 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 138 | .. function:: DeleteValue(key, value) |
| 139 | |
| 140 | Removes a named value from a registry key. |
| 141 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 142 | *key* is an already open key, or one of the predefined :const:`HKEY_\*` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 143 | constants. |
| 144 | |
| 145 | *value* is a string that identifies the value to remove. |
| 146 | |
| 147 | |
| 148 | .. function:: EnumKey(key, index) |
| 149 | |
| 150 | Enumerates subkeys of an open registry key, returning a string. |
| 151 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 152 | *key* is an already open key, or any one of the predefined :const:`HKEY_\*` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 153 | constants. |
| 154 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 155 | *index* is an integer that identifies the index of the key to retrieve. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 156 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 157 | The function retrieves the name of one subkey each time it is called. It is |
| 158 | typically called repeatedly until a :exc:`WindowsError` exception is |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 159 | raised, indicating, no more values are available. |
| 160 | |
| 161 | |
| 162 | .. function:: EnumValue(key, index) |
| 163 | |
| 164 | Enumerates values of an open registry key, returning a tuple. |
| 165 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 166 | *key* is an already open key, or any one of the predefined :const:`HKEY_\*` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 167 | constants. |
| 168 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 169 | *index* is an integer that identifies the index of the value to retrieve. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 170 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 171 | The function retrieves the name of one subkey each time it is called. It is |
| 172 | typically called repeatedly, until a :exc:`WindowsError` exception is |
| 173 | raised, indicating no more values. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 174 | |
| 175 | The result is a tuple of 3 items: |
| 176 | |
| 177 | +-------+--------------------------------------------+ |
| 178 | | Index | Meaning | |
| 179 | +=======+============================================+ |
| 180 | | ``0`` | A string that identifies the value name | |
| 181 | +-------+--------------------------------------------+ |
| 182 | | ``1`` | An object that holds the value data, and | |
| 183 | | | whose type depends on the underlying | |
| 184 | | | registry type | |
| 185 | +-------+--------------------------------------------+ |
| 186 | | ``2`` | An integer that identifies the type of the | |
| 187 | | | value data | |
| 188 | +-------+--------------------------------------------+ |
| 189 | |
| 190 | |
Ezio Melotti | 985e24d | 2009-09-13 07:54:02 +0000 | [diff] [blame] | 191 | .. function:: ExpandEnvironmentStrings(str) |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 192 | |
Ezio Melotti | 985e24d | 2009-09-13 07:54:02 +0000 | [diff] [blame] | 193 | Expands environment strings %NAME% in unicode string like :const:`REG_EXPAND_SZ`:: |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 194 | |
Ezio Melotti | 985e24d | 2009-09-13 07:54:02 +0000 | [diff] [blame] | 195 | >>> ExpandEnvironmentStrings('%windir%') |
| 196 | 'C:\\Windows' |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 197 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 198 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 199 | .. function:: FlushKey(key) |
| 200 | |
| 201 | Writes all the attributes of a key to the registry. |
| 202 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 203 | *key* is an already open key, or one of the predefined :const:`HKEY_\*` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 204 | constants. |
| 205 | |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 206 | It is not necessary to call :func:`FlushKey` to change a key. Registry changes are |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 207 | flushed to disk by the registry using its lazy flusher. Registry changes are |
| 208 | also flushed to disk at system shutdown. Unlike :func:`CloseKey`, the |
| 209 | :func:`FlushKey` method returns only when all the data has been written to the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 210 | registry. An application should only call :func:`FlushKey` if it requires |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 211 | absolute certainty that registry changes are on disk. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 212 | |
| 213 | .. note:: |
| 214 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 215 | If you don't know whether a :func:`FlushKey` call is required, it probably |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 216 | isn't. |
| 217 | |
| 218 | |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 219 | .. function:: LoadKey(key, sub_key, file_name) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 220 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 221 | Creates a subkey under the specified key and stores registration information |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 222 | from a specified file into that subkey. |
| 223 | |
| 224 | *key* is an already open key, or any of the predefined :const:`HKEY_\*` |
| 225 | constants. |
| 226 | |
| 227 | *sub_key* is a string that identifies the sub_key to load. |
| 228 | |
| 229 | *file_name* is the name of the file to load registry data from. This file must |
| 230 | have been created with the :func:`SaveKey` function. Under the file allocation |
| 231 | table (FAT) file system, the filename may not have an extension. |
| 232 | |
| 233 | A call to LoadKey() fails if the calling process does not have the |
| 234 | :const:`SE_RESTORE_PRIVILEGE` privilege. Note that privileges are different than |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 235 | permissions -- see the Win32 documentation for more details. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 236 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 237 | If *key* is a handle returned by :func:`ConnectRegistry`, then the path |
| 238 | specified in *fileName* is relative to the remote computer. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 239 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 240 | The Win32 documentation implies *key* must be in the :const:`HKEY_USER` or |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 241 | :const:`HKEY_LOCAL_MACHINE` tree. This may or may not be true. |
| 242 | |
| 243 | |
Brian Curtin | d855948 | 2010-04-24 17:21:31 +0000 | [diff] [blame] | 244 | .. function:: OpenKey(key, sub_key[, res[, sam]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 245 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 246 | Opens the specified key, returning a :ref:`handle object <handle-object>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 247 | |
| 248 | *key* is an already open key, or any one of the predefined :const:`HKEY_\*` |
| 249 | constants. |
| 250 | |
| 251 | *sub_key* is a string that identifies the sub_key to open. |
| 252 | |
| 253 | *res* is a reserved integer, and must be zero. The default is zero. |
| 254 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 255 | *sam* is an integer that specifies an access mask that describes the desired |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 256 | security access for the key. Default is :const:`KEY_READ`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 257 | |
| 258 | The result is a new handle to the specified key. |
| 259 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 260 | If the function fails, :exc:`WindowsError` is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 261 | |
| 262 | |
| 263 | .. function:: OpenKeyEx() |
| 264 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 265 | The functionality of :func:`OpenKeyEx` is provided via :func:`OpenKey`, |
| 266 | by the use of default arguments. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 267 | |
| 268 | |
| 269 | .. function:: QueryInfoKey(key) |
| 270 | |
| 271 | Returns information about a key, as a tuple. |
| 272 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 273 | *key* is an already open key, or one of the predefined :const:`HKEY_\*` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 274 | constants. |
| 275 | |
| 276 | The result is a tuple of 3 items: |
| 277 | |
| 278 | +-------+---------------------------------------------+ |
| 279 | | Index | Meaning | |
| 280 | +=======+=============================================+ |
| 281 | | ``0`` | An integer giving the number of sub keys | |
| 282 | | | this key has. | |
| 283 | +-------+---------------------------------------------+ |
| 284 | | ``1`` | An integer giving the number of values this | |
| 285 | | | key has. | |
| 286 | +-------+---------------------------------------------+ |
Georg Brandl | ba956ae | 2007-11-29 17:24:34 +0000 | [diff] [blame] | 287 | | ``2`` | An integer giving when the key was last | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 288 | | | modified (if available) as 100's of | |
| 289 | | | nanoseconds since Jan 1, 1600. | |
| 290 | +-------+---------------------------------------------+ |
| 291 | |
| 292 | |
| 293 | .. function:: QueryValue(key, sub_key) |
| 294 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 295 | Retrieves the unnamed value for a key, as a string. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 296 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 297 | *key* is an already open key, or one of the predefined :const:`HKEY_\*` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 298 | constants. |
| 299 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 300 | *sub_key* is a string that holds the name of the subkey with which the value is |
| 301 | associated. If this parameter is ``None`` or empty, the function retrieves the |
| 302 | value set by the :func:`SetValue` method for the key identified by *key*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 303 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 304 | Values in the registry have name, type, and data components. This method |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 305 | retrieves the data for a key's first value that has a NULL name. But the |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 306 | underlying API call doesn't return the type, so always use |
| 307 | :func:`QueryValueEx` if possible. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 308 | |
| 309 | |
| 310 | .. function:: QueryValueEx(key, value_name) |
| 311 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 312 | Retrieves the type and data for a specified value name associated with |
| 313 | an open registry key. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 314 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 315 | *key* is an already open key, or one of the predefined :const:`HKEY_\*` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 316 | constants. |
| 317 | |
| 318 | *value_name* is a string indicating the value to query. |
| 319 | |
| 320 | The result is a tuple of 2 items: |
| 321 | |
| 322 | +-------+-----------------------------------------+ |
| 323 | | Index | Meaning | |
| 324 | +=======+=========================================+ |
| 325 | | ``0`` | The value of the registry item. | |
| 326 | +-------+-----------------------------------------+ |
| 327 | | ``1`` | An integer giving the registry type for | |
| 328 | | | this value. | |
| 329 | +-------+-----------------------------------------+ |
| 330 | |
| 331 | |
| 332 | .. function:: SaveKey(key, file_name) |
| 333 | |
| 334 | Saves the specified key, and all its subkeys to the specified file. |
| 335 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 336 | *key* is an already open key, or one of the predefined :const:`HKEY_\*` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 337 | constants. |
| 338 | |
| 339 | *file_name* is the name of the file to save registry data to. This file cannot |
| 340 | already exist. If this filename includes an extension, it cannot be used on file |
| 341 | allocation table (FAT) file systems by the :meth:`LoadKey`, :meth:`ReplaceKey` |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 342 | or :meth:`RestoreKey` methods. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 343 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 344 | If *key* represents a key on a remote computer, the path described by |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 345 | *file_name* is relative to the remote computer. The caller of this method must |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 346 | possess the :const:`SeBackupPrivilege` security privilege. Note that |
| 347 | privileges are different than permissions -- see the Win32 documentation for |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 348 | more details. |
| 349 | |
| 350 | This function passes NULL for *security_attributes* to the API. |
| 351 | |
| 352 | |
| 353 | .. function:: SetValue(key, sub_key, type, value) |
| 354 | |
| 355 | Associates a value with a specified key. |
| 356 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 357 | *key* is an already open key, or one of the predefined :const:`HKEY_\*` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 358 | constants. |
| 359 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 360 | *sub_key* is a string that names the subkey with which the value is associated. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 361 | |
| 362 | *type* is an integer that specifies the type of the data. Currently this must be |
| 363 | :const:`REG_SZ`, meaning only strings are supported. Use the :func:`SetValueEx` |
| 364 | function for support for other data types. |
| 365 | |
| 366 | *value* is a string that specifies the new value. |
| 367 | |
| 368 | If the key specified by the *sub_key* parameter does not exist, the SetValue |
| 369 | function creates it. |
| 370 | |
| 371 | Value lengths are limited by available memory. Long values (more than 2048 |
| 372 | bytes) should be stored as files with the filenames stored in the configuration |
| 373 | registry. This helps the registry perform efficiently. |
| 374 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 375 | The key identified by the *key* parameter must have been opened with |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 376 | :const:`KEY_SET_VALUE` access. |
| 377 | |
| 378 | |
| 379 | .. function:: SetValueEx(key, value_name, reserved, type, value) |
| 380 | |
| 381 | Stores data in the value field of an open registry key. |
| 382 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 383 | *key* is an already open key, or one of the predefined :const:`HKEY_\*` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 384 | constants. |
| 385 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 386 | *value_name* is a string that names the subkey with which the value is |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 387 | associated. |
| 388 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 389 | *type* is an integer that specifies the type of the data. This should be one |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 390 | of the following constants defined in this module: |
| 391 | |
| 392 | +----------------------------------+---------------------------------------------+ |
| 393 | | Constant | Meaning | |
| 394 | +==================================+=============================================+ |
| 395 | | :const:`REG_BINARY` | Binary data in any form. | |
| 396 | +----------------------------------+---------------------------------------------+ |
| 397 | | :const:`REG_DWORD` | A 32-bit number. | |
| 398 | +----------------------------------+---------------------------------------------+ |
| 399 | | :const:`REG_DWORD_LITTLE_ENDIAN` | A 32-bit number in little-endian format. | |
| 400 | +----------------------------------+---------------------------------------------+ |
| 401 | | :const:`REG_DWORD_BIG_ENDIAN` | A 32-bit number in big-endian format. | |
| 402 | +----------------------------------+---------------------------------------------+ |
| 403 | | :const:`REG_EXPAND_SZ` | Null-terminated string containing | |
| 404 | | | references to environment variables | |
| 405 | | | (``%PATH%``). | |
| 406 | +----------------------------------+---------------------------------------------+ |
| 407 | | :const:`REG_LINK` | A Unicode symbolic link. | |
| 408 | +----------------------------------+---------------------------------------------+ |
| 409 | | :const:`REG_MULTI_SZ` | A sequence of null-terminated strings, | |
| 410 | | | terminated by two null characters. (Python | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 411 | | | handles this termination automatically.) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 412 | +----------------------------------+---------------------------------------------+ |
| 413 | | :const:`REG_NONE` | No defined value type. | |
| 414 | +----------------------------------+---------------------------------------------+ |
| 415 | | :const:`REG_RESOURCE_LIST` | A device-driver resource list. | |
| 416 | +----------------------------------+---------------------------------------------+ |
| 417 | | :const:`REG_SZ` | A null-terminated string. | |
| 418 | +----------------------------------+---------------------------------------------+ |
| 419 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 420 | *reserved* can be anything -- zero is always passed to the API. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 421 | |
| 422 | *value* is a string that specifies the new value. |
| 423 | |
| 424 | This method can also set additional value and type information for the specified |
| 425 | key. The key identified by the key parameter must have been opened with |
| 426 | :const:`KEY_SET_VALUE` access. |
| 427 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 428 | To open the key, use the :func:`CreateKey` or :func:`OpenKey` methods. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 429 | |
| 430 | Value lengths are limited by available memory. Long values (more than 2048 |
| 431 | bytes) should be stored as files with the filenames stored in the configuration |
| 432 | registry. This helps the registry perform efficiently. |
| 433 | |
| 434 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 435 | .. function:: DisableReflectionKey(key) |
| 436 | |
| 437 | Disables registry reflection for 32-bit processes running on a 64-bit |
| 438 | Operating System. |
| 439 | |
| 440 | *key* is an already open key, or one of the predefined :const:`HKEY_\*` |
| 441 | constants. |
| 442 | |
| 443 | Will generally raise :exc:`NotImplemented` if executed on a 32-bit |
| 444 | Operating System. |
| 445 | |
| 446 | If the key is not on the reflection list, the function succeeds but has no |
| 447 | effect. Disabling reflection for a key does not affect reflection of any |
| 448 | subkeys. |
| 449 | |
| 450 | |
| 451 | .. function:: EnableReflectionKey(key) |
| 452 | |
| 453 | Restores registry reflection for the specified disabled key. |
| 454 | |
| 455 | *key* is an already open key, or one of the predefined :const:`HKEY_\*` |
| 456 | constants. |
| 457 | |
| 458 | Will generally raise :exc:`NotImplemented` if executed on a 32-bit |
| 459 | Operating System. |
| 460 | |
| 461 | Restoring reflection for a key does not affect reflection of any subkeys. |
| 462 | |
| 463 | |
| 464 | .. function:: QueryReflectionKey(key) |
| 465 | |
| 466 | Determines the reflection state for the specified key. |
| 467 | |
| 468 | *key* is an already open key, or one of the predefined :const:`HKEY_\*` |
| 469 | constants. |
| 470 | |
| 471 | Returns ``True`` if reflection is disabled. |
| 472 | |
| 473 | Will generally raise :exc:`NotImplemented` if executed on a 32-bit |
| 474 | Operating System. |
| 475 | |
| 476 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 477 | .. _handle-object: |
| 478 | |
| 479 | Registry Handle Objects |
| 480 | ----------------------- |
| 481 | |
| 482 | This object wraps a Windows HKEY object, automatically closing it when the |
| 483 | object is destroyed. To guarantee cleanup, you can call either the |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 484 | :meth:`Close` method on the object, or the :func:`CloseKey` function. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 485 | |
| 486 | All registry functions in this module return one of these objects. |
| 487 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 488 | All registry functions in this module which accept a handle object also accept |
| 489 | an integer, however, use of the handle object is encouraged. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 490 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 491 | Handle objects provide semantics for :meth:`__bool__` -- thus :: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 492 | |
| 493 | if handle: |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 494 | print("Yes") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 495 | |
| 496 | will print ``Yes`` if the handle is currently valid (has not been closed or |
| 497 | detached). |
| 498 | |
| 499 | The object also support comparison semantics, so handle objects will compare |
| 500 | true if they both reference the same underlying Windows handle value. |
| 501 | |
Georg Brandl | 22b3431 | 2009-07-26 14:54:51 +0000 | [diff] [blame] | 502 | Handle objects can be converted to an integer (e.g., using the built-in |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 503 | :func:`int` function), in which case the underlying Windows handle value is |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 504 | returned. You can also use the :meth:`Detach` method to return the integer |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 505 | handle, and also disconnect the Windows handle from the handle object. |
| 506 | |
| 507 | |
| 508 | .. method:: PyHKEY.Close() |
| 509 | |
| 510 | Closes the underlying Windows handle. |
| 511 | |
| 512 | If the handle is already closed, no error is raised. |
| 513 | |
| 514 | |
| 515 | .. method:: PyHKEY.Detach() |
| 516 | |
| 517 | Detaches the Windows handle from the handle object. |
| 518 | |
Georg Brandl | 5c10664 | 2007-11-29 17:41:05 +0000 | [diff] [blame] | 519 | The result is an integer that holds the value of the handle before it is |
| 520 | detached. If the handle is already detached or closed, this will return |
| 521 | zero. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 522 | |
| 523 | After calling this function, the handle is effectively invalidated, but the |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame^] | 524 | handle is not closed. You would call this function when you need the |
| 525 | underlying Win32 handle to exist beyond the lifetime of the handle object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 526 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 527 | .. method:: PyHKEY.__enter__() |
| 528 | PyHKEY.__exit__(\*exc_info) |
| 529 | |
| 530 | The HKEY object implements :meth:`__enter__` and :meth:`__exit__` and thus |
| 531 | supports the context protocol for the :keyword:`with` statement:: |
| 532 | |
| 533 | with OpenKey(HKEY_LOCAL_MACHINE, "foo") as key: |
| 534 | # ... work with key ... |
| 535 | |
| 536 | will automatically close *key* when control leaves the :keyword:`with` block. |
| 537 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 538 | |