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