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