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