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