blob: 4e3cebb77d035b241defa2110f27071ca357cc91 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`_winreg` -- Windows registry access
3=========================================
4
5.. module:: _winreg
6 :platform: Windows
7 :synopsis: Routines and objects for manipulating the Windows registry.
8.. sectionauthor:: Mark Hammond <MarkH@ActiveState.com>
9
Georg Brandlecd0ad32008-05-25 07:46:33 +000010.. note::
11 The :mod:`_winreg` module has been renamed to :mod:`winreg` in Python 3.0.
12 The :term:`2to3` tool will automatically adapt imports when converting your
13 sources to 3.0.
14
Georg Brandl8ec7f652007-08-15 14:28:01 +000015
16.. versionadded:: 2.0
17
18These functions expose the Windows registry API to Python. Instead of using an
19integer as the registry handle, a handle object is used to ensure that the
20handles are closed correctly, even if the programmer neglects to explicitly
21close them.
22
23This module exposes a very low-level interface to the Windows registry; it is
24expected that in the future a new ``winreg`` module will be created offering a
25higher-level interface to the registry API.
26
27This module offers the following functions:
28
29
30.. function:: CloseKey(hkey)
31
32 Closes a previously opened registry key. The hkey argument specifies a
33 previously opened key.
34
35 Note that if *hkey* is not closed using this method (or via
36 :meth:`handle.Close`), it is closed when the *hkey* object is destroyed by
37 Python.
38
39
40.. function:: ConnectRegistry(computer_name, key)
41
42 Establishes a connection to a predefined registry handle on another computer,
43 and returns a :dfn:`handle object`
44
45 *computer_name* is the name of the remote computer, of the form
46 ``r"\\computername"``. If ``None``, the local computer is used.
47
48 *key* is the predefined handle to connect to.
49
50 The return value is the handle of the opened key. If the function fails, an
51 :exc:`EnvironmentError` exception is raised.
52
53
54.. function:: CreateKey(key, sub_key)
55
56 Creates or opens the specified key, returning a :dfn:`handle object`
57
58 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
59 constants.
60
61 *sub_key* is a string that names the key this method opens or creates.
62
63 If *key* is one of the predefined keys, *sub_key* may be ``None``. In that
64 case, the handle returned is the same key handle passed in to the function.
65
66 If the key already exists, this function opens the existing key.
67
68 The return value is the handle of the opened key. If the function fails, an
69 :exc:`EnvironmentError` exception is raised.
70
71
72.. function:: DeleteKey(key, sub_key)
73
74 Deletes the specified key.
75
76 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
77 constants.
78
79 *sub_key* is a string that must be a subkey of the key identified by the *key*
80 parameter. This value must not be ``None``, and the key may not have subkeys.
81
82 *This method can not delete keys with subkeys.*
83
84 If the method succeeds, the entire key, including all of its values, is removed.
85 If the method fails, an :exc:`EnvironmentError` exception is raised.
86
87
88.. function:: DeleteValue(key, value)
89
90 Removes a named value from a registry key.
91
92 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
93 constants.
94
95 *value* is a string that identifies the value to remove.
96
97
98.. function:: EnumKey(key, index)
99
100 Enumerates subkeys of an open registry key, returning a string.
101
102 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
103 constants.
104
105 *index* is an integer that identifies the index of the key to retrieve.
106
107 The function retrieves the name of one subkey each time it is called. It is
108 typically called repeatedly until an :exc:`EnvironmentError` exception is
109 raised, indicating, no more values are available.
110
111
112.. function:: EnumValue(key, index)
113
114 Enumerates values of an open registry key, returning a tuple.
115
116 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
117 constants.
118
119 *index* is an integer that identifies the index of the value to retrieve.
120
121 The function retrieves the name of one subkey each time it is called. It is
122 typically called repeatedly, until an :exc:`EnvironmentError` exception is
123 raised, indicating no more values.
124
125 The result is a tuple of 3 items:
126
127 +-------+--------------------------------------------+
128 | Index | Meaning |
129 +=======+============================================+
130 | ``0`` | A string that identifies the value name |
131 +-------+--------------------------------------------+
132 | ``1`` | An object that holds the value data, and |
133 | | whose type depends on the underlying |
134 | | registry type |
135 +-------+--------------------------------------------+
136 | ``2`` | An integer that identifies the type of the |
137 | | value data |
138 +-------+--------------------------------------------+
139
140
Christian Heimesb39a7562008-01-08 15:46:10 +0000141.. function:: ExpandEnvironmentStrings(unicode)
142
143 Expands environment strings %NAME% in unicode string like const:`REG_EXPAND_SZ`::
Georg Brandl502d6312008-01-08 16:18:26 +0000144
145 >>> ExpandEnvironmentStrings(u"%windir%")
146 u"C:\\Windows"
147
148 .. versionadded:: 2.6
Christian Heimesb39a7562008-01-08 15:46:10 +0000149
150
Georg Brandl8ec7f652007-08-15 14:28:01 +0000151.. function:: FlushKey(key)
152
153 Writes all the attributes of a key to the registry.
154
155 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
156 constants.
157
Georg Brandl51174092008-05-09 06:10:43 +0000158 It is not necessary to call :func:`FlushKey` to change a key. Registry changes are
Georg Brandl8ec7f652007-08-15 14:28:01 +0000159 flushed to disk by the registry using its lazy flusher. Registry changes are
160 also flushed to disk at system shutdown. Unlike :func:`CloseKey`, the
161 :func:`FlushKey` method returns only when all the data has been written to the
162 registry. An application should only call :func:`FlushKey` if it requires
163 absolute certainty that registry changes are on disk.
164
165 .. note::
166
167 If you don't know whether a :func:`FlushKey` call is required, it probably
168 isn't.
169
170
Georg Brandl51174092008-05-09 06:10:43 +0000171.. function:: LoadKey(key, sub_key, file_name)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000172
173 Creates a subkey under the specified key and stores registration information
174 from a specified file into that subkey.
175
176 *key* is an already open key, or any of the predefined :const:`HKEY_\*`
177 constants.
178
179 *sub_key* is a string that identifies the sub_key to load.
180
181 *file_name* is the name of the file to load registry data from. This file must
182 have been created with the :func:`SaveKey` function. Under the file allocation
183 table (FAT) file system, the filename may not have an extension.
184
185 A call to LoadKey() fails if the calling process does not have the
186 :const:`SE_RESTORE_PRIVILEGE` privilege. Note that privileges are different than
187 permissions - see the Win32 documentation for more details.
188
189 If *key* is a handle returned by :func:`ConnectRegistry`, then the path
190 specified in *fileName* is relative to the remote computer.
191
192 The Win32 documentation implies *key* must be in the :const:`HKEY_USER` or
193 :const:`HKEY_LOCAL_MACHINE` tree. This may or may not be true.
194
195
196.. function:: OpenKey(key, sub_key[, res=0][, sam=KEY_READ])
197
198 Opens the specified key, returning a :dfn:`handle object`
199
200 *key* is an already open key, or any one of the predefined :const:`HKEY_\*`
201 constants.
202
203 *sub_key* is a string that identifies the sub_key to open.
204
205 *res* is a reserved integer, and must be zero. The default is zero.
206
207 *sam* is an integer that specifies an access mask that describes the desired
208 security access for the key. Default is :const:`KEY_READ`
209
210 The result is a new handle to the specified key.
211
212 If the function fails, :exc:`EnvironmentError` is raised.
213
214
215.. function:: OpenKeyEx()
216
217 The functionality of :func:`OpenKeyEx` is provided via :func:`OpenKey`, by the
218 use of default arguments.
219
220
221.. function:: QueryInfoKey(key)
222
223 Returns information about a key, as a tuple.
224
225 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
226 constants.
227
228 The result is a tuple of 3 items:
229
230 +-------+---------------------------------------------+
231 | Index | Meaning |
232 +=======+=============================================+
233 | ``0`` | An integer giving the number of sub keys |
234 | | this key has. |
235 +-------+---------------------------------------------+
236 | ``1`` | An integer giving the number of values this |
237 | | key has. |
238 +-------+---------------------------------------------+
239 | ``2`` | A long integer giving when the key was last |
240 | | modified (if available) as 100's of |
241 | | nanoseconds since Jan 1, 1600. |
242 +-------+---------------------------------------------+
243
244
245.. function:: QueryValue(key, sub_key)
246
247 Retrieves the unnamed value for a key, as a string
248
249 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
250 constants.
251
252 *sub_key* is a string that holds the name of the subkey with which the value is
253 associated. If this parameter is ``None`` or empty, the function retrieves the
254 value set by the :func:`SetValue` method for the key identified by *key*.
255
256 Values in the registry have name, type, and data components. This method
257 retrieves the data for a key's first value that has a NULL name. But the
258 underlying API call doesn't return the type, Lame Lame Lame, DO NOT USE THIS!!!
259
260
261.. function:: QueryValueEx(key, value_name)
262
263 Retrieves the type and data for a specified value name associated with an open
264 registry key.
265
266 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
267 constants.
268
269 *value_name* is a string indicating the value to query.
270
271 The result is a tuple of 2 items:
272
273 +-------+-----------------------------------------+
274 | Index | Meaning |
275 +=======+=========================================+
276 | ``0`` | The value of the registry item. |
277 +-------+-----------------------------------------+
278 | ``1`` | An integer giving the registry type for |
279 | | this value. |
280 +-------+-----------------------------------------+
281
282
283.. function:: SaveKey(key, file_name)
284
285 Saves the specified key, and all its subkeys to the specified file.
286
287 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
288 constants.
289
290 *file_name* is the name of the file to save registry data to. This file cannot
291 already exist. If this filename includes an extension, it cannot be used on file
292 allocation table (FAT) file systems by the :meth:`LoadKey`, :meth:`ReplaceKey`
293 or :meth:`RestoreKey` methods.
294
295 If *key* represents a key on a remote computer, the path described by
296 *file_name* is relative to the remote computer. The caller of this method must
297 possess the :const:`SeBackupPrivilege` security privilege. Note that
298 privileges are different than permissions - see the Win32 documentation for
299 more details.
300
301 This function passes NULL for *security_attributes* to the API.
302
303
304.. function:: SetValue(key, sub_key, type, value)
305
306 Associates a value with a specified key.
307
308 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
309 constants.
310
311 *sub_key* is a string that names the subkey with which the value is associated.
312
313 *type* is an integer that specifies the type of the data. Currently this must be
314 :const:`REG_SZ`, meaning only strings are supported. Use the :func:`SetValueEx`
315 function for support for other data types.
316
317 *value* is a string that specifies the new value.
318
319 If the key specified by the *sub_key* parameter does not exist, the SetValue
320 function creates it.
321
322 Value lengths are limited by available memory. Long values (more than 2048
323 bytes) should be stored as files with the filenames stored in the configuration
324 registry. This helps the registry perform efficiently.
325
326 The key identified by the *key* parameter must have been opened with
327 :const:`KEY_SET_VALUE` access.
328
329
330.. function:: SetValueEx(key, value_name, reserved, type, value)
331
332 Stores data in the value field of an open registry key.
333
334 *key* is an already open key, or one of the predefined :const:`HKEY_\*`
335 constants.
336
337 *value_name* is a string that names the subkey with which the value is
338 associated.
339
340 *type* is an integer that specifies the type of the data. This should be one
341 of the following constants defined in this module:
342
343 +----------------------------------+---------------------------------------------+
344 | Constant | Meaning |
345 +==================================+=============================================+
346 | :const:`REG_BINARY` | Binary data in any form. |
347 +----------------------------------+---------------------------------------------+
348 | :const:`REG_DWORD` | A 32-bit number. |
349 +----------------------------------+---------------------------------------------+
350 | :const:`REG_DWORD_LITTLE_ENDIAN` | A 32-bit number in little-endian format. |
351 +----------------------------------+---------------------------------------------+
352 | :const:`REG_DWORD_BIG_ENDIAN` | A 32-bit number in big-endian format. |
353 +----------------------------------+---------------------------------------------+
354 | :const:`REG_EXPAND_SZ` | Null-terminated string containing |
355 | | references to environment variables |
356 | | (``%PATH%``). |
357 +----------------------------------+---------------------------------------------+
358 | :const:`REG_LINK` | A Unicode symbolic link. |
359 +----------------------------------+---------------------------------------------+
360 | :const:`REG_MULTI_SZ` | A sequence of null-terminated strings, |
361 | | terminated by two null characters. (Python |
362 | | handles this termination automatically.) |
363 +----------------------------------+---------------------------------------------+
364 | :const:`REG_NONE` | No defined value type. |
365 +----------------------------------+---------------------------------------------+
366 | :const:`REG_RESOURCE_LIST` | A device-driver resource list. |
367 +----------------------------------+---------------------------------------------+
368 | :const:`REG_SZ` | A null-terminated string. |
369 +----------------------------------+---------------------------------------------+
370
371 *reserved* can be anything - zero is always passed to the API.
372
373 *value* is a string that specifies the new value.
374
375 This method can also set additional value and type information for the specified
376 key. The key identified by the key parameter must have been opened with
377 :const:`KEY_SET_VALUE` access.
378
379 To open the key, use the :func:`CreateKeyEx` or :func:`OpenKey` methods.
380
381 Value lengths are limited by available memory. Long values (more than 2048
382 bytes) should be stored as files with the filenames stored in the configuration
383 registry. This helps the registry perform efficiently.
384
385
386.. _handle-object:
387
388Registry Handle Objects
389-----------------------
390
391This object wraps a Windows HKEY object, automatically closing it when the
392object is destroyed. To guarantee cleanup, you can call either the
393:meth:`Close` method on the object, or the :func:`CloseKey` function.
394
395All registry functions in this module return one of these objects.
396
397All registry functions in this module which accept a handle object also accept
398an integer, however, use of the handle object is encouraged.
399
400Handle objects provide semantics for :meth:`__nonzero__` - thus ::
401
402 if handle:
403 print "Yes"
404
405will print ``Yes`` if the handle is currently valid (has not been closed or
406detached).
407
408The object also support comparison semantics, so handle objects will compare
409true if they both reference the same underlying Windows handle value.
410
411Handle objects can be converted to an integer (e.g., using the builtin
412:func:`int` function), in which case the underlying Windows handle value is
413returned. You can also use the :meth:`Detach` method to return the integer
414handle, and also disconnect the Windows handle from the handle object.
415
416
417.. method:: PyHKEY.Close()
418
419 Closes the underlying Windows handle.
420
421 If the handle is already closed, no error is raised.
422
423
424.. method:: PyHKEY.Detach()
425
426 Detaches the Windows handle from the handle object.
427
428 The result is an integer (or long on 64 bit Windows) that holds the value of the
429 handle before it is detached. If the handle is already detached or closed, this
430 will return zero.
431
432 After calling this function, the handle is effectively invalidated, but the
433 handle is not closed. You would call this function when you need the
434 underlying Win32 handle to exist beyond the lifetime of the handle object.
435
Christian Heimesb39a7562008-01-08 15:46:10 +0000436.. method:: PyHKEY.__enter__()
Georg Brandl502d6312008-01-08 16:18:26 +0000437 PyHKEY.__exit__(\*exc_info)
438
439 The HKEY object implements :meth:`__enter__` and :meth:`__exit__` and thus
440 supports the context protocol for the :keyword:`with` statement::
441
442 with OpenKey(HKEY_LOCAL_MACHINE, "foo") as key:
443 # ... work with key ...
444
445 will automatically close *key* when control leaves the :keyword:`with` block.
446
447 .. versionadded:: 2.6
Christian Heimesb39a7562008-01-08 15:46:10 +0000448