Perl in a Nutshell

Perl in a NutshellSearch this book
Previous: 19.12 Win32::ProcessChapter 19
Win32 Modules and Extensions
Next: 19.14 Win32::Semaphore
 

19.13 Win32::Registry

This module provides access to the Windows Registry, the database that stores information about all parts of your system and software. Many operating system and application behaviors are controlled by Registry data. The Win32::Registry module gives you a way to access and update registry information with Perl. (Warning: Always be careful when making changes to the registry. If vital system information gets changed by mistake, your system could become inoperable. Always make certain you have a backup of your registry before you start to make modifications.)

The Registry module automatically creates objects for the top-level registry trees. These objects are created in the main:: namespace, and each key that you open or create is accessed via one of these root objects. The four top-level objects are:

$HKEY_CLASSES_ROOT 
$HKEY_CURRENT_USER 
$HKEY_LOCAL_MACHINE 
$HKEY_USERS
If you are outside of the main (default) namespace, you should package declare the keys, i.e., $main::HKEY_USERS.

The Open method creates new key objects for subtrees or subkeys under another open key object. Initially, a new key is opened from one of the main key objects, for example:

use Win32::Registry;
$p = "SOFTWARE\Microsoft\Windows NT\CurrentVersion";
$HKEY_LOCAL_MACHINE->Open($p, $CurrVer) || die "Open $!";
This example creates a key object $CurrVer for the CurrentVersion key for Windows NT. This key contains several values for the version of the operating system. With the new key open, you can read or change the values it contains (every key has at least one unnamed, default value), or open and create subkeys. The Open method can only create key objects for existing keys.

Registry values are represented in Win32::Registry functions by three elements: the name of the value, the data type of the value, and the value itself. There are several different data types for the values. Win32::Registry defines the following constants for these types:

REG_SZ		String data
REG_DWORD		Unsigned four-byte integer
REG_MULTI_SZ	Multiple strings delimited with NULL
REG_EXPAND_SZ	Strings that expand (e.g., based on environment variables)
REG_BINARY	Binary data (no particular format is assumed)

19.13.1 Methods

The following methods can be used on key objects, either the preopened main keys or subkeys that you have already opened.


Previous: 19.12 Win32::ProcessPerl in a NutshellNext: 19.14 Win32::Semaphore
19.12 Win32::ProcessBook Index19.14 Win32::Semaphore