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 |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 11 | integer as the registry handle, a :ref:`handle object <handle-object>` is used |
| 12 | to ensure that the handles are closed correctly, even if the programmer neglects |
| 13 | to explicitly close them. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 14 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 15 | This module offers the following functions: |
| 16 | |
| 17 | |
| 18 | .. function:: CloseKey(hkey) |
| 19 | |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 20 | Closes a previously opened registry key. The *hkey* argument specifies a |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 21 | previously opened key. |
| 22 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 23 | .. note:: |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 24 | |
| 25 | If *hkey* is not closed using this method (or via :meth:`hkey.Close() |
| 26 | <PyHKEY.Close>`), it is closed when the *hkey* object is destroyed by |
| 27 | Python. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 28 | |
| 29 | |
| 30 | .. function:: ConnectRegistry(computer_name, key) |
| 31 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 32 | Establishes a connection to a predefined registry handle on another computer, |
| 33 | and returns a :ref:`handle object <handle-object>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 34 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 35 | *computer_name* is the name of the remote computer, of the form |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 36 | ``r"\\computername"``. If ``None``, the local computer is used. |
| 37 | |
| 38 | *key* is the predefined handle to connect to. |
| 39 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 40 | 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] | 41 | :exc:`WindowsError` exception is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 42 | |
| 43 | |
| 44 | .. function:: CreateKey(key, sub_key) |
| 45 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 46 | Creates or opens the specified key, returning a |
| 47 | :ref:`handle object <handle-object>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 48 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 49 | *key* is an already open key, or one of the predefined |
| 50 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 51 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 52 | *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] | 53 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 54 | If *key* is one of the predefined keys, *sub_key* may be ``None``. In that |
| 55 | 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] | 56 | |
| 57 | If the key already exists, this function opens the existing key. |
| 58 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 59 | 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] | 60 | :exc:`WindowsError` exception is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 61 | |
| 62 | |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 63 | .. function:: CreateKeyEx(key, sub_key, reserved=0, access=KEY_ALL_ACCESS) |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 64 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 65 | Creates or opens the specified key, returning a |
| 66 | :ref:`handle object <handle-object>`. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 67 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 68 | *key* is an already open key, or one of the predefined |
| 69 | :ref:`HKEY_* constants <hkey-constants>`. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 70 | |
| 71 | *sub_key* is a string that names the key this method opens or creates. |
| 72 | |
| 73 | *res* is a reserved integer, and must be zero. The default is zero. |
| 74 | |
| 75 | *sam* is an integer that specifies an access mask that describes the desired |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 76 | security access for the key. Default is :const:`KEY_ALL_ACCESS`. See |
| 77 | :ref:`Access Rights <access-rights>` for other allowed values. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 78 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 79 | If *key* is one of the predefined keys, *sub_key* may be ``None``. In that |
| 80 | 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] | 81 | |
| 82 | If the key already exists, this function opens the existing key. |
| 83 | |
| 84 | The return value is the handle of the opened key. If the function fails, a |
| 85 | :exc:`WindowsError` exception is raised. |
| 86 | |
Georg Brandl | 4c25cf3 | 2010-04-22 07:00:42 +0000 | [diff] [blame] | 87 | .. versionadded:: 3.2 |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 88 | |
| 89 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 90 | .. function:: DeleteKey(key, sub_key) |
| 91 | |
| 92 | Deletes the specified key. |
| 93 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 94 | *key* is an already open key, or one of the predefined |
| 95 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 96 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 97 | *sub_key* is a string that must be a subkey of the key identified by the *key* |
| 98 | 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] | 99 | |
| 100 | *This method can not delete keys with subkeys.* |
| 101 | |
| 102 | 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] | 103 | If the method fails, a :exc:`WindowsError` exception is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 104 | |
| 105 | |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 106 | .. function:: DeleteKeyEx(key, sub_key, access=KEY_ALL_ACCESS, reserved=0) |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 107 | |
| 108 | Deletes the specified key. |
| 109 | |
| 110 | .. note:: |
| 111 | The :func:`DeleteKeyEx` function is implemented with the RegDeleteKeyEx |
| 112 | Windows API function, which is specific to 64-bit versions of Windows. |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 113 | See the `RegDeleteKeyEx documentation |
Benjamin Peterson | cc1f597 | 2010-06-06 02:41:24 +0000 | [diff] [blame] | 114 | <http://msdn.microsoft.com/en-us/library/ms724847%28VS.85%29.aspx>`__. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 115 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 116 | *key* is an already open key, or one of the predefined |
| 117 | :ref:`HKEY_* constants <hkey-constants>`. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 125 | *sam* is an integer that specifies an access mask that describes the desired |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 126 | security access for the key. Default is :const:`KEY_ALL_ACCESS`. See |
| 127 | :ref:`Access Rights <access-rights>` for other allowed values. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 128 | |
| 129 | *This method can not delete keys with subkeys.* |
| 130 | |
| 131 | If the method succeeds, the entire key, including all of its values, is |
| 132 | removed. If the method fails, a :exc:`WindowsError` exception is raised. |
| 133 | |
| 134 | On unsupported Windows versions, :exc:`NotImplementedError` is raised. |
| 135 | |
Georg Brandl | 4c25cf3 | 2010-04-22 07:00:42 +0000 | [diff] [blame] | 136 | .. versionadded:: 3.2 |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 137 | |
| 138 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 139 | .. function:: DeleteValue(key, value) |
| 140 | |
| 141 | Removes a named value from a registry key. |
| 142 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 143 | *key* is an already open key, or one of the predefined |
| 144 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 145 | |
| 146 | *value* is a string that identifies the value to remove. |
| 147 | |
| 148 | |
| 149 | .. function:: EnumKey(key, index) |
| 150 | |
| 151 | Enumerates subkeys of an open registry key, returning a string. |
| 152 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 153 | *key* is an already open key, or one of the predefined |
| 154 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 155 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 156 | *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] | 157 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 158 | The function retrieves the name of one subkey each time it is called. It is |
| 159 | typically called repeatedly until a :exc:`WindowsError` exception is |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 160 | raised, indicating, no more values are available. |
| 161 | |
| 162 | |
| 163 | .. function:: EnumValue(key, index) |
| 164 | |
| 165 | Enumerates values of an open registry key, returning a tuple. |
| 166 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 167 | *key* is an already open key, or one of the predefined |
| 168 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 169 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 170 | *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] | 171 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 172 | The function retrieves the name of one subkey each time it is called. It is |
| 173 | typically called repeatedly, until a :exc:`WindowsError` exception is |
| 174 | raised, indicating no more values. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 175 | |
| 176 | The result is a tuple of 3 items: |
| 177 | |
| 178 | +-------+--------------------------------------------+ |
| 179 | | Index | Meaning | |
| 180 | +=======+============================================+ |
| 181 | | ``0`` | A string that identifies the value name | |
| 182 | +-------+--------------------------------------------+ |
| 183 | | ``1`` | An object that holds the value data, and | |
| 184 | | | whose type depends on the underlying | |
| 185 | | | registry type | |
| 186 | +-------+--------------------------------------------+ |
| 187 | | ``2`` | An integer that identifies the type of the | |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 188 | | | value data (see table in docs for | |
| 189 | | | :meth:`SetValueEx`) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 190 | +-------+--------------------------------------------+ |
| 191 | |
| 192 | |
Ezio Melotti | 985e24d | 2009-09-13 07:54:02 +0000 | [diff] [blame] | 193 | .. function:: ExpandEnvironmentStrings(str) |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 194 | |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 195 | Expands environment variable placeholders ``%NAME%`` in strings like |
| 196 | :const:`REG_EXPAND_SZ`:: |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 197 | |
Ezio Melotti | 985e24d | 2009-09-13 07:54:02 +0000 | [diff] [blame] | 198 | >>> ExpandEnvironmentStrings('%windir%') |
| 199 | 'C:\\Windows' |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 200 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 201 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 202 | .. function:: FlushKey(key) |
| 203 | |
| 204 | Writes all the attributes of a key to the registry. |
| 205 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 206 | *key* is an already open key, or one of the predefined |
| 207 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 208 | |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 209 | 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] | 210 | flushed to disk by the registry using its lazy flusher. Registry changes are |
| 211 | also flushed to disk at system shutdown. Unlike :func:`CloseKey`, the |
| 212 | :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] | 213 | registry. An application should only call :func:`FlushKey` if it requires |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 214 | absolute certainty that registry changes are on disk. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 215 | |
| 216 | .. note:: |
| 217 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 218 | 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] | 219 | isn't. |
| 220 | |
| 221 | |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 222 | .. function:: LoadKey(key, sub_key, file_name) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 223 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 224 | Creates a subkey under the specified key and stores registration information |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 225 | from a specified file into that subkey. |
| 226 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 227 | *key* is a handle returned by :func:`ConnectRegistry` or one of the constants |
| 228 | :const:`HKEY_USERS` or :const:`HKEY_LOCAL_MACHINE`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 229 | |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 230 | *sub_key* is a string that identifies the subkey to load. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 231 | |
| 232 | *file_name* is the name of the file to load registry data from. This file must |
| 233 | have been created with the :func:`SaveKey` function. Under the file allocation |
| 234 | table (FAT) file system, the filename may not have an extension. |
| 235 | |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 236 | A call to :func:`LoadKey` fails if the calling process does not have the |
| 237 | :const:`SE_RESTORE_PRIVILEGE` privilege. Note that privileges are different |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 238 | from permissions -- see the `RegLoadKey documentation |
| 239 | <http://msdn.microsoft.com/en-us/library/ms724889%28v=VS.85%29.aspx>`__ for |
| 240 | more details. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 241 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 242 | If *key* is a handle returned by :func:`ConnectRegistry`, then the path |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 243 | specified in *file_name* is relative to the remote computer. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 244 | |
| 245 | |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 246 | .. function:: OpenKey(key, sub_key, reserved=0, access=KEY_ALL_ACCESS) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 247 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 248 | Opens the specified key, returning a :ref:`handle object <handle-object>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 249 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 250 | *key* is an already open key, or one of the predefined |
| 251 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 252 | |
| 253 | *sub_key* is a string that identifies the sub_key to open. |
| 254 | |
| 255 | *res* is a reserved integer, and must be zero. The default is zero. |
| 256 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 257 | *sam* is an integer that specifies an access mask that describes the desired |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 258 | security access for the key. Default is :const:`KEY_READ`. See :ref:`Access |
| 259 | Rights <access-rights>` for other allowed values. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 260 | |
| 261 | The result is a new handle to the specified key. |
| 262 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 263 | If the function fails, :exc:`WindowsError` is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 264 | |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 265 | .. versionchanged:: 3.2 Allow the use of named arguments. |
| 266 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 267 | |
| 268 | .. function:: OpenKeyEx() |
| 269 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 270 | The functionality of :func:`OpenKeyEx` is provided via :func:`OpenKey`, |
| 271 | by the use of default arguments. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 272 | |
| 273 | |
| 274 | .. function:: QueryInfoKey(key) |
| 275 | |
| 276 | Returns information about a key, as a tuple. |
| 277 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 278 | *key* is an already open key, or one of the predefined |
| 279 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 280 | |
| 281 | The result is a tuple of 3 items: |
| 282 | |
| 283 | +-------+---------------------------------------------+ |
| 284 | | Index | Meaning | |
| 285 | +=======+=============================================+ |
| 286 | | ``0`` | An integer giving the number of sub keys | |
| 287 | | | this key has. | |
| 288 | +-------+---------------------------------------------+ |
| 289 | | ``1`` | An integer giving the number of values this | |
| 290 | | | key has. | |
| 291 | +-------+---------------------------------------------+ |
Georg Brandl | ba956ae | 2007-11-29 17:24:34 +0000 | [diff] [blame] | 292 | | ``2`` | An integer giving when the key was last | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 293 | | | modified (if available) as 100's of | |
| 294 | | | nanoseconds since Jan 1, 1600. | |
| 295 | +-------+---------------------------------------------+ |
| 296 | |
| 297 | |
| 298 | .. function:: QueryValue(key, sub_key) |
| 299 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 300 | Retrieves the unnamed value for a key, as a string. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 301 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 302 | *key* is an already open key, or one of the predefined |
| 303 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 304 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 305 | *sub_key* is a string that holds the name of the subkey with which the value is |
| 306 | associated. If this parameter is ``None`` or empty, the function retrieves the |
| 307 | 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] | 308 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 309 | Values in the registry have name, type, and data components. This method |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 310 | 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] | 311 | underlying API call doesn't return the type, so always use |
| 312 | :func:`QueryValueEx` if possible. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 313 | |
| 314 | |
| 315 | .. function:: QueryValueEx(key, value_name) |
| 316 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 317 | Retrieves the type and data for a specified value name associated with |
| 318 | an open registry key. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 319 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 320 | *key* is an already open key, or one of the predefined |
| 321 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 322 | |
| 323 | *value_name* is a string indicating the value to query. |
| 324 | |
| 325 | The result is a tuple of 2 items: |
| 326 | |
| 327 | +-------+-----------------------------------------+ |
| 328 | | Index | Meaning | |
| 329 | +=======+=========================================+ |
| 330 | | ``0`` | The value of the registry item. | |
| 331 | +-------+-----------------------------------------+ |
| 332 | | ``1`` | An integer giving the registry type for | |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 333 | | | this value (see table in docs for | |
| 334 | | | :meth:`SetValueEx`) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 335 | +-------+-----------------------------------------+ |
| 336 | |
| 337 | |
| 338 | .. function:: SaveKey(key, file_name) |
| 339 | |
| 340 | Saves the specified key, and all its subkeys to the specified file. |
| 341 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 342 | *key* is an already open key, or one of the predefined |
| 343 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 344 | |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 345 | *file_name* is the name of the file to save registry data to. This file |
| 346 | cannot already exist. If this filename includes an extension, it cannot be |
| 347 | used on file allocation table (FAT) file systems by the :meth:`LoadKey` |
| 348 | method. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 349 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 350 | 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] | 351 | *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] | 352 | possess the :const:`SeBackupPrivilege` security privilege. Note that |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 353 | privileges are different than permissions -- see the |
| 354 | `Conflicts Between User Rights and Permissions documentation |
| 355 | <http://msdn.microsoft.com/en-us/library/ms724878%28v=VS.85%29.aspx>`__ |
| 356 | for more details. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 357 | |
| 358 | This function passes NULL for *security_attributes* to the API. |
| 359 | |
| 360 | |
| 361 | .. function:: SetValue(key, sub_key, type, value) |
| 362 | |
| 363 | Associates a value with a specified key. |
| 364 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 365 | *key* is an already open key, or one of the predefined |
| 366 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 367 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 368 | *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] | 369 | |
| 370 | *type* is an integer that specifies the type of the data. Currently this must be |
| 371 | :const:`REG_SZ`, meaning only strings are supported. Use the :func:`SetValueEx` |
| 372 | function for support for other data types. |
| 373 | |
| 374 | *value* is a string that specifies the new value. |
| 375 | |
| 376 | If the key specified by the *sub_key* parameter does not exist, the SetValue |
| 377 | function creates it. |
| 378 | |
| 379 | Value lengths are limited by available memory. Long values (more than 2048 |
| 380 | bytes) should be stored as files with the filenames stored in the configuration |
| 381 | registry. This helps the registry perform efficiently. |
| 382 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 383 | The key identified by the *key* parameter must have been opened with |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 384 | :const:`KEY_SET_VALUE` access. |
| 385 | |
| 386 | |
| 387 | .. function:: SetValueEx(key, value_name, reserved, type, value) |
| 388 | |
| 389 | Stores data in the value field of an open registry key. |
| 390 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 391 | *key* is an already open key, or one of the predefined |
| 392 | :ref:`HKEY_* constants <hkey-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 393 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 394 | *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] | 395 | associated. |
| 396 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 397 | *type* is an integer that specifies the type of the data. See |
| 398 | :ref:`Value Types <value-types>` for the available types. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 399 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 400 | *reserved* can be anything -- zero is always passed to the API. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 401 | |
| 402 | *value* is a string that specifies the new value. |
| 403 | |
| 404 | This method can also set additional value and type information for the specified |
| 405 | key. The key identified by the key parameter must have been opened with |
| 406 | :const:`KEY_SET_VALUE` access. |
| 407 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 408 | To open the key, use the :func:`CreateKey` or :func:`OpenKey` methods. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 409 | |
| 410 | Value lengths are limited by available memory. Long values (more than 2048 |
| 411 | bytes) should be stored as files with the filenames stored in the configuration |
| 412 | registry. This helps the registry perform efficiently. |
| 413 | |
| 414 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 415 | .. function:: DisableReflectionKey(key) |
| 416 | |
| 417 | Disables registry reflection for 32-bit processes running on a 64-bit |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 418 | operating system. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 419 | |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 420 | *key* is an already open key, or one of the predefined :ref:`HKEY_* constants |
| 421 | <hkey-constants>`. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 422 | |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 423 | Will generally raise :exc:`NotImplemented` if executed on a 32-bit operating |
| 424 | system. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 425 | |
| 426 | If the key is not on the reflection list, the function succeeds but has no |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 427 | effect. Disabling reflection for a key does not affect reflection of any |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 428 | subkeys. |
| 429 | |
| 430 | |
| 431 | .. function:: EnableReflectionKey(key) |
| 432 | |
| 433 | Restores registry reflection for the specified disabled key. |
| 434 | |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 435 | *key* is an already open key, or one of the predefined :ref:`HKEY_* constants |
| 436 | <hkey-constants>`. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 437 | |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 438 | Will generally raise :exc:`NotImplemented` if executed on a 32-bit operating |
| 439 | system. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 440 | |
| 441 | Restoring reflection for a key does not affect reflection of any subkeys. |
| 442 | |
| 443 | |
| 444 | .. function:: QueryReflectionKey(key) |
| 445 | |
| 446 | Determines the reflection state for the specified key. |
| 447 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 448 | *key* is an already open key, or one of the predefined |
| 449 | :ref:`HKEY_* constants <hkey-constants>`. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 450 | |
| 451 | Returns ``True`` if reflection is disabled. |
| 452 | |
| 453 | Will generally raise :exc:`NotImplemented` if executed on a 32-bit |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 454 | operating system. |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 455 | |
| 456 | |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 457 | .. _constants: |
| 458 | |
| 459 | Constants |
| 460 | ------------------ |
| 461 | |
| 462 | The following constants are defined for use in many :mod:`_winreg` functions. |
| 463 | |
| 464 | .. _hkey-constants: |
| 465 | |
| 466 | HKEY_* Constants |
| 467 | ++++++++++++++++ |
| 468 | |
| 469 | .. data:: HKEY_CLASSES_ROOT |
| 470 | |
| 471 | Registry entries subordinate to this key define types (or classes) of |
| 472 | documents and the properties associated with those types. Shell and |
| 473 | COM applications use the information stored under this key. |
| 474 | |
| 475 | |
| 476 | .. data:: HKEY_CURRENT_USER |
| 477 | |
| 478 | Registry entries subordinate to this key define the preferences of |
| 479 | the current user. These preferences include the settings of |
| 480 | environment variables, data about program groups, colors, printers, |
| 481 | network connections, and application preferences. |
| 482 | |
| 483 | .. data:: HKEY_LOCAL_MACHINE |
| 484 | |
| 485 | Registry entries subordinate to this key define the physical state |
| 486 | of the computer, including data about the bus type, system memory, |
| 487 | and installed hardware and software. |
| 488 | |
| 489 | .. data:: HKEY_USERS |
| 490 | |
| 491 | Registry entries subordinate to this key define the default user |
| 492 | configuration for new users on the local computer and the user |
| 493 | configuration for the current user. |
| 494 | |
| 495 | .. data:: HKEY_PERFORMANCE_DATA |
| 496 | |
| 497 | Registry entries subordinate to this key allow you to access |
| 498 | performance data. The data is not actually stored in the registry; |
| 499 | the registry functions cause the system to collect the data from |
| 500 | its source. |
| 501 | |
| 502 | |
| 503 | .. data:: HKEY_CURRENT_CONFIG |
| 504 | |
| 505 | Contains information about the current hardware profile of the |
| 506 | local computer system. |
| 507 | |
| 508 | .. data:: HKEY_DYN_DATA |
| 509 | |
| 510 | This key is not used in versions of Windows after 98. |
| 511 | |
| 512 | |
| 513 | .. _access-rights: |
| 514 | |
| 515 | Access Rights |
| 516 | +++++++++++++ |
| 517 | |
| 518 | For more information, see `Registry Key Security and Access |
| 519 | <http://msdn.microsoft.com/en-us/library/ms724878%28v=VS.85%29.aspx>`__. |
| 520 | |
| 521 | .. data:: KEY_ALL_ACCESS |
| 522 | |
| 523 | Combines the STANDARD_RIGHTS_REQUIRED, :const:`KEY_QUERY_VALUE`, |
| 524 | :const:`KEY_SET_VALUE`, :const:`KEY_CREATE_SUB_KEY`, |
| 525 | :const:`KEY_ENUMERATE_SUB_KEYS`, :const:`KEY_NOTIFY`, |
| 526 | and :const:`KEY_CREATE_LINK` access rights. |
| 527 | |
| 528 | .. data:: KEY_WRITE |
| 529 | |
| 530 | Combines the STANDARD_RIGHTS_WRITE, :const:`KEY_SET_VALUE`, and |
| 531 | :const:`KEY_CREATE_SUB_KEY` access rights. |
| 532 | |
| 533 | .. data:: KEY_READ |
| 534 | |
| 535 | Combines the STANDARD_RIGHTS_READ, :const:`KEY_QUERY_VALUE`, |
| 536 | :const:`KEY_ENUMERATE_SUB_KEYS`, and :const:`KEY_NOTIFY` values. |
| 537 | |
| 538 | .. data:: KEY_EXECUTE |
| 539 | |
| 540 | Equivalent to :const:`KEY_READ`. |
| 541 | |
| 542 | .. data:: KEY_QUERY_VALUE |
| 543 | |
| 544 | Required to query the values of a registry key. |
| 545 | |
| 546 | .. data:: KEY_SET_VALUE |
| 547 | |
| 548 | Required to create, delete, or set a registry value. |
| 549 | |
| 550 | .. data:: KEY_CREATE_SUB_KEY |
| 551 | |
| 552 | Required to create a subkey of a registry key. |
| 553 | |
| 554 | .. data:: KEY_ENUMERATE_SUB_KEYS |
| 555 | |
| 556 | Required to enumerate the subkeys of a registry key. |
| 557 | |
| 558 | .. data:: KEY_NOTIFY |
| 559 | |
| 560 | Required to request change notifications for a registry key or for |
| 561 | subkeys of a registry key. |
| 562 | |
| 563 | .. data:: KEY_CREATE_LINK |
| 564 | |
| 565 | Reserved for system use. |
| 566 | |
| 567 | |
| 568 | .. _64-bit-access-rights: |
| 569 | |
| 570 | 64-bit Specific |
| 571 | *************** |
| 572 | |
Georg Brandl | 6faee4e | 2010-09-21 14:48:28 +0000 | [diff] [blame] | 573 | For more information, see `Accessing an Alternate Registry View |
Brian Curtin | 2d067c8 | 2010-05-11 20:35:47 +0000 | [diff] [blame] | 574 | <http://msdn.microsoft.com/en-us/library/aa384129(v=VS.85).aspx>`__. |
| 575 | |
| 576 | .. data:: KEY_WOW64_64KEY |
| 577 | |
| 578 | Indicates that an application on 64-bit Windows should operate on |
| 579 | the 64-bit registry view. |
| 580 | |
| 581 | .. data:: KEY_WOW64_32KEY |
| 582 | |
| 583 | Indicates that an application on 64-bit Windows should operate on |
| 584 | the 32-bit registry view. |
| 585 | |
| 586 | |
| 587 | .. _value-types: |
| 588 | |
| 589 | Value Types |
| 590 | +++++++++++ |
| 591 | |
| 592 | For more information, see `Registry Value Types |
| 593 | <http://msdn.microsoft.com/en-us/library/ms724884%28v=VS.85%29.aspx>`__. |
| 594 | |
| 595 | .. data:: REG_BINARY |
| 596 | |
| 597 | Binary data in any form. |
| 598 | |
| 599 | .. data:: REG_DWORD |
| 600 | |
| 601 | 32-bit number. |
| 602 | |
| 603 | .. data:: REG_DWORD_LITTLE_ENDIAN |
| 604 | |
| 605 | A 32-bit number in little-endian format. |
| 606 | |
| 607 | .. data:: REG_DWORD_BIG_ENDIAN |
| 608 | |
| 609 | A 32-bit number in big-endian format. |
| 610 | |
| 611 | .. data:: REG_EXPAND_SZ |
| 612 | |
| 613 | Null-terminated string containing references to environment |
| 614 | variables (``%PATH%``). |
| 615 | |
| 616 | .. data:: REG_LINK |
| 617 | |
| 618 | A Unicode symbolic link. |
| 619 | |
| 620 | .. data:: REG_MULTI_SZ |
| 621 | |
| 622 | A sequence of null-terminated strings, terminated by two null characters. |
| 623 | (Python handles this termination automatically.) |
| 624 | |
| 625 | .. data:: REG_NONE |
| 626 | |
| 627 | No defined value type. |
| 628 | |
| 629 | .. data:: REG_RESOURCE_LIST |
| 630 | |
| 631 | A device-driver resource list. |
| 632 | |
| 633 | .. data:: REG_FULL_RESOURCE_DESCRIPTOR |
| 634 | |
| 635 | A hardware setting. |
| 636 | |
| 637 | .. data:: REG_RESOURCE_REQUIREMENTS_LIST |
| 638 | |
| 639 | A hardware resource list. |
| 640 | |
| 641 | .. data:: REG_SZ |
| 642 | |
| 643 | A null-terminated string. |
| 644 | |
| 645 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 646 | .. _handle-object: |
| 647 | |
| 648 | Registry Handle Objects |
| 649 | ----------------------- |
| 650 | |
| 651 | This object wraps a Windows HKEY object, automatically closing it when the |
| 652 | object is destroyed. To guarantee cleanup, you can call either the |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 653 | :meth:`~PyHKEY.Close` method on the object, or the :func:`CloseKey` function. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 654 | |
| 655 | All registry functions in this module return one of these objects. |
| 656 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 657 | All registry functions in this module which accept a handle object also accept |
| 658 | an integer, however, use of the handle object is encouraged. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 659 | |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 660 | Handle objects provide semantics for :meth:`__bool__` -- thus :: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 661 | |
| 662 | if handle: |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 663 | print("Yes") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 664 | |
| 665 | will print ``Yes`` if the handle is currently valid (has not been closed or |
| 666 | detached). |
| 667 | |
| 668 | The object also support comparison semantics, so handle objects will compare |
| 669 | true if they both reference the same underlying Windows handle value. |
| 670 | |
Georg Brandl | 22b3431 | 2009-07-26 14:54:51 +0000 | [diff] [blame] | 671 | 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] | 672 | :func:`int` function), in which case the underlying Windows handle value is |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 673 | returned. You can also use the :meth:`~PyHKEY.Detach` method to return the |
| 674 | integer handle, and also disconnect the Windows handle from the handle object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 675 | |
| 676 | |
| 677 | .. method:: PyHKEY.Close() |
| 678 | |
| 679 | Closes the underlying Windows handle. |
| 680 | |
| 681 | If the handle is already closed, no error is raised. |
| 682 | |
| 683 | |
| 684 | .. method:: PyHKEY.Detach() |
| 685 | |
| 686 | Detaches the Windows handle from the handle object. |
| 687 | |
Georg Brandl | 5c10664 | 2007-11-29 17:41:05 +0000 | [diff] [blame] | 688 | The result is an integer that holds the value of the handle before it is |
| 689 | detached. If the handle is already detached or closed, this will return |
| 690 | zero. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 691 | |
| 692 | After calling this function, the handle is effectively invalidated, but the |
Ezio Melotti | bc37298 | 2010-04-25 17:48:01 +0000 | [diff] [blame] | 693 | handle is not closed. You would call this function when you need the |
| 694 | underlying Win32 handle to exist beyond the lifetime of the handle object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 695 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 696 | .. method:: PyHKEY.__enter__() |
| 697 | PyHKEY.__exit__(\*exc_info) |
| 698 | |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 699 | The HKEY object implements :meth:`~object.__enter__` and |
| 700 | :meth:`~object.__exit__` and thus supports the context protocol for the |
| 701 | :keyword:`with` statement:: |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 702 | |
| 703 | with OpenKey(HKEY_LOCAL_MACHINE, "foo") as key: |
Georg Brandl | 8173fb3 | 2010-05-19 21:03:51 +0000 | [diff] [blame] | 704 | ... # work with key |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 705 | |
| 706 | will automatically close *key* when control leaves the :keyword:`with` block. |
| 707 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 708 | |