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