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