The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Tie::Registry - Powerful and easy ways to manipulate a registry [on Win32 for now].

SYNOPSIS

  use Tie::Registry;

  $Registry->SomeMethodCall(arg1,...);

  $subKey= $Registry->{"Key\\SubKey\\"};
  $valueData= $Registry->{"Key\\SubKey\\\\ValueName"};
  $Registry->{"Key\\SubKey\\"}= { "NewSubKey" => {...} };
  $Registry->{"Key\\SubKey\\\\ValueName"}= "NewValueData";
  $Registry->{"\\ValueName"}= [ pack("fmt",$data), REG_DATATYPE ];

EXAMPLES

  $Registry->Delimeter("/");
  $diskKey= $Registry->{"LMachine/System/Disk/"};
  $data= $key->{"/Information"};
  $remoteKey= $Registry->{"//ServerA/LMachine/System/"};
  $remoteData= $remoteKey->{"Disk//Information"};
  foreach $entry (  keys(%$key)  ) {
      ...
  }
  foreach $subKey (  $key->SubKeyNames  ) {
      ...
  }
  $key->AllowSave( 1 );
  $key->RegSaveKey( "C:/TEMP/DiskReg", [] );

DESCRIPTION

The Tie::Registry module lets you manipulate the Registry via objects [as in "object oriented"] or via tied hashes. But you will probably mostly use objects which are also references to tied hashes that allow you to mix both access methods [as shown above].

Skip to the SUMMARY section if you just want to dive in and start using the Registry from Perl.

Accessing and manipulating the registry is extremely simple using Tie::Registry. A single, simple expression can return you almost any bit of information stored in the Registry. Tie::Registry also gives you full access to the "raw" underlying API calls so that you can do anything with the Registry in Perl that you could do in C. But the "simple" interface has been carefully designed to handle almost all operations itself without imposing arbitrary limits while providing sensible defaults so you can list only the parameters you care about.

But first, an overview of the Registry itself.

The Registry

The Registry is a forest: a collection of several tree structures. The root of each tree is a key. These root keys are identified by predefined constants whose names start with "HKEY_". Although all keys have a few attributes associated with each [a class, a time stamp, and security information], the most important aspect of keys is that each can contain subkeys and can contain values.

Each subkey has a name: a string which cannot be blank and cannot contain the delimeter character [backslash: '\\'] nor nul ['\0']. Each subkey is also a key and so can contain subkeys and values [and has a class, time stamp, and security information].

Each value has a name: a string which E<can> be blank and can contain any character except for nul, '\0' [including the delimeter character]. Each value also has data associated with it. Each value's data is a contiguous chunk of bytes, which is exactly what a Perl string value is so Perl strings will usually be used to represent value data.

Each value also has a data type which says how to interpret the value data. The primary data types are:

REG_SZ

A null-terminated string.

REG_EXPAND_SZ

A null-terminated string which contains substrings consisting of a percent sign ['%'], an environment variable name, then a percent sign, that should be replaced with the value associate with that environment variable. The system does not automatically do this substitution.

REG_BINARY

Some arbitrary binary value.

REG_MULTI_SZ

Several null-terminated strings concatenated together with an extra trailing '\0' to mark the end of the list.

REG_DWORD

A long [4-byte] integer value. These values are usually returned packed into a 4-character string and expected in the same format.

In the underlying Registry calls, most places which take a subkey name also allow you to pass in a subkey "path" -- a string of several subkey names separated by the delimeter character, backslash ['\\']. For example, doing RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SYSTEM\\DISK",...) is much like opening the "SYSTEM" subkey of HKEY_LOCAL_MACHINE, then opening its "DISK" subkey, then closing the "SYSTEM" subkey.

All of the <Tie::Registry> features allow you to use your own delimeter in place of the system's delimeter, ['\\']. In most of our examples we will use a forward slash ['/'] as our delimeter as it is easier to read and less error prone to use when writing Perl code since you have to type two backslashes for each backslash you want in a string.

You can also connect to the registry of other computers on your network. This will be discussed more later.

Although the Registry does not have a single root key, the Tie::Registry module creates a virtual root key for you which has all of the HKEY_* keys as subkeys.

Tied Hashes Documentation

Before you can use a tied hash, you must create one. One way to do that is via:

    use Tie::Registry qw( %RegHash );

which gives you access to %RegHash which has been tied to the virtual root key of the Registry. There are also several ways you can tie a hash variable to any other key of the Registry, which are discussed later.

Note that you will most likely use $Registry which is a reference to %RegHash [that is, $Registry= \%RegHash] instead of using %RegHash directly. So you would use $Registry->{Key} rather than $RegHash{Key} and use keys %{$Registry} rather than keys %RegHash, for example.

For each hash which has been tied to a Registry key, the Perl keys function will return a list containing the name of each of the key's subkeys with a delimeter character appended to it and containing the name of each of the key's values with a delimeter prepended to it. For example:

    keys( %{ $Registry->{"HKEY_CLASSES_ROOT\\batfile\\"} } )

might yield the following list value:

    ( "DefaultIcon\\",  # The subkey named "DefaultIcon"
      "shell\\",        # The subkey named "shell"
      "shellex\\",      # The subkey named "shellex"
      "\\",             # The default value [named ""]
      "\\EditFlags" )   # The value named "EditFlags"

For the virtual root key, short-hand subkey names are used as shown below. You can use the short-hand name, the regular HKEY_* name, or any numeric value to access these keys, but the short-hand names are all that will be returned by the keys function.

"Classes" for HKEY_CLASSES_ROOT

Contains mappings between file name extensions and the uses for such files along with configuration information for COM [MicroSoft's Common Object Model] objects. Usually a link to the "SOFTWARE\\Classes" subkey of the HKEY_LOCAL_MACHINE key.

"CUser" for HKEY_CURRENT_USER

Contains information specific to the currently logged-in user. Mostly software configuration information. Usually a link to a subkey of the HKEY_USERS key.

"LMachine" for HKEY_LOCAL_MACHINE

Contains all manner of information about the computer.

"Users" for HKEY_USERS

Contains one subkey, ".DEFAULT", which gets copied to a new subkey whenever a new user is added. Also contains a subkey for each user of the system, though only the one for the current user is usually loaded at any given time.

"PerfData" for HKEY_PERFORMANCE_DATA

Used to access data about system performance. Access via this key is "special" and all but the most carefully constructed calls will fail, usually with ERROR_INSUFFICIENT_BUFFER. For example, you can't enumerate key names without also enumerating values which require huge buffers but the exact buffer size required cannot be determined beforehand because RegQueryInfoKey() E<always> fails with ERROR_INSUFFICIENT_BUFFER for HKEY_PERFORMANCE_DATA no matter how it is called. So it is currently not very useful to tie a hash to this key. You can use it to create an object to use for making carefully constructed calls to the underlying Reg*() routines.

"CConfig" for HKEY_CURRENT_CONFIG

Contains minimal information about the computer's current configuration.

"DynData" for HKEY_DYN_DATA

Dynamic data. We have found no documentation for this key.

A tied hash is much like a regular hash variable in Perl -- you give it a key string inside braces, [{ and }], and it gives you back a value [or lets you set a value]. For Tie::Registry hashes, there are two types of values that will be returned.

SubKeys

If you give it a string which represents a subkey, then it will give you back a reference to a hash which has been tied to that subkey. It can't return the hash itself, so it returns a reference to it. It also blesses that reference so that it is also an object so you can use it to call method functions.

Values

If you give it a string which is a value name, then it will give you back a string which is the data for that value. Alternately, you can request that it give you both the data value string and the data value type [we discuss how to request this later]. In this case, it would return a reference to an array where the value data string is element [0] and the value data type is element [1].

The key string which you use in the tied hash must be intepreted to determine whether it is a value name or a key name or a path that combines several of these or even other things. There are two simple rules that make this interpretation easy and unambiguous: Put a delimeter after each key name. Put a delimeter in front of each value name.

Exactly how the key string will be intepreted is governed by the following cases, in the order listed. These cases are designed to "do what you mean"; most of the time you won't have to think about them, especially if you follow the two simple rules above. After the list of cases we give several examples which should be clear enough so feel free to skip to them unless you are worried about the details.

Remote machines

If the hash is tied to the virtual root of the registry [or the virtual root of a remote machine's registry], then we treat hash key strings which start with the delimeter character specially.

If the hash key string starts with two delimeters in a row, then those should be immediately followed by the name of a remote machine whose registry we wish to connect to. That can be followed by a delimeter and more subkey names, etc. If the machine name is not following by anything, then a virtual root for the remote machine's registry is created, a hash is tied to it, and a reference to that hash it is returned.

Hash key string starts with the delimeter

If the hash is tied to a virtual root key, then the leading delimeter is ignored. It should be followed by a valid Registry root key name [either a short-hand name like "LMachine", an HKEY_* value, or a numeric value]. This alternate notation is allowed to be more consistant with the Open() method function.

For all other Registry keys, the leading delimeter indicates that the rest of the string is a value name. The leading delimeter is stripped and the rest of the string [which can be empty and can contain more delimeters] is used as a value name with no further parsing.

Exact match with direct subkey name followed by delimeter

If you have already called the Perl keys function on the tied hash [or have already called MemberNames on the object] and the hash key string exactly matches one of the strings returned, then no further parsing is done. In other words, if the key string exactly matches the name of a direct subkey with a delimeter appended, then a reference to a hash tied to that subkey is returned.

This is only important if you have selected a delimeter other than the system default delimeter and one of the subkey names contains the delimeter you have chosen. This rule allows you to deal with subkeys which contain your chosen delimeter in their name as long as you only traverse subkeys one level at a time and always enumerate the list of members before doing so.

The main advantage of this is that Perl code which recursively traverses a hash will work on hashes tied to Registry keys even if a non-default delimeter has been selected.

Hash key string contains two delimeters in a row

If the hash key string contains two delimeters in a row, then the string is split between those two delimeters. The first part is interpreted as a subkey name or a path of subkey names separated by delimeters. The second part is interpreted as a value name.

Hash key string ends with a delimeter

If the key string ends with a delimeter, then it is treated as a subkey name or path of subkey names separated by delimeters.

Hash key string contains a delimeter

If the key string contains a delimeter, then it is split after the last delimeter. The first part is treated as a subkey name or path of subkey names separated by delimeters. The second part is ambiguous and is treated as outlined in the next item.

Hash key string contains no delimeters

If the hash key string contains no delimeters, then it is ambiguous.

If you are reading from the hash [fetching], then we first use the key string as a value name. If there is a value with a matching name in the Registry key which the hash is tied to, then the value data string [and possibly the value data type] is returned. Otherwise, we retry by using the hash key string as a subkey name. If there is a subkey with a matching name, then we return a reference to a hash tied to that subkey. Otherwise we return undef.

If you are writing to the hash [storing], then we use the key string as a subkey name only if the value you are storing is a reference to a hash value. Otherwise we use the key string as a value name.

Examples

Here are some examples showing different ways of accessing Registry information using references to tied hashes:

Canonical value fetch
    $tip18= $Registry->{"HKEY_LOCAL_MACHINE\\Software\\Microsoft\\"
               . "Windows\\CurrentVersion\\Explorer\\Tips\\\\18"};

Should return the text of important tip number 18. Note that two backslashes, "\\", are required to get a single backslash into a Perl double-quoted string. Note that "\\" is appended to each key name ["HKEY_LOCAL_MACHINE" through "Tips"] and "\\" is prepended to the value name, "18".

Changing your delimeter
    $Registry->Delimeter("/");
    $tip18= $Registry->{"HKEY_LOCAL_MACHINE/Software/Microsoft/"
               . "Windows/CurrentVersion/Explorer/Tips//18"};

This usually makes things easier to read when working in Perl. All remaining examples will assume the delimeter has been changed as above.

Using intermediate keys
    $ms= $Registry->{"LMachine/Software/Microsoft/"};
    $tips= $ms->{"Windows/CurrentVersion/Explorer/Tips/"};
    $tip18= $winlogon->{"/18"};

Same as above but lets you efficiently re-access those intermediate keys.

Chaining in a single statement
    $tip18= $Registry->{"LMachine/Software/Microsoft/"}->
              {"Windows/CurrentVersion/Explorer/Tips/"}->{"/18"};

Like above, this creates intermediate key objects then uses them to access other data. Once this statement finishes, the intermediate key objects are destroying. Several handles into the Registry are opened and closed by this statement so it is less efficient but there are times when this will be useful.

Even less efficient example of chaining
    $tip18= $Registry->{"LMachine/Software/Microsoft"}->
              {"Windows/CurrentVersion/Explorer/Tips"}->{"/18"};

Because we left off the trailing delimeters, Tie::Registry doesn't know whether final names, "Microsoft" and "Tips", are subkey names or value names. So this statement ends up executing the same code as the next one.

What the above really does
    $tip18= $Registry->{"LMachine/Software/"}->{"Microsoft"}->
              {"Windows/CurrentVersion/Explorer/"}->{"Tips"}->{"/18"};

With more chains to go through, more temporary objects are created and later destroyed than in our first chaining example. Also, when "Microsoft" is looked up, Tie::Registry first tries to open it as a value and fails then tries it as a subkey. The same is true for when it looks up "Tips".

Getting all of the tips
    $tips= $Registry->{"LMachine/Software/Microsoft/"}->
              {"Windows/CurrentVersion/Explorer/Tips/"}
      or  die "Can't find the Windows tips: $^E\n";
    foreach(  keys %$tips  ) {
        print "$_: ", $tips->{$_}, "\n";
    }

First notice that we actually check for failure for the first time. Note that your version of Perl may not set $^E properly [see the BUGS section]. We are assuming that the "Tips" key contains no subkeys. Otherwise the print statement would show something like "Tie::Registry=HASH(0xc03ebc)" for each subkey.

Deleting items

You can use the Perl delete function to delete a value from a Registry key or to delete a subkey as long that subkey contains no subkeys of its own. See "More Examples", below, for more information.

Storing items

You can use the Perl assignment operator [=] to create new keys, create new values, or replace values. The values you store should be in the same format as the values you would fetch from a tied hash. For example, you can use a single assignment statement to copy an entire Registry tree. The following statement:

    $Registry->{"LMachine/Software/Classes/Tie_Registry/"}=
      $Registry->{"LMachine/Software/Classes/batfile/"};

creates a "Tie_Registry" subkey under the "Software\\Classes" subkey of the HKEY_LOCAL_MACHINE key. Then it populates it with copies of all of the subkeys and values in the "batfile" subkey and all of its subkeys. Note that you need to have called $Registry->ArrayValues(1) for the proper value data type information to be copied. Note also that this release of Tie::Registry does not copy key attributes such as class name and security information [this is planned for a future release].

The following statement creates a whole subtree in the Registry:

    $Registry->{"LMachine/Software/FooCorp/"}= {
        "FooWriter/" => {
            "/Version" => "4.032",
            "Startup/" => {
                "/Title" => "Foo Writer Deluxe ][",
                "/WindowSize" => [ pack("LL",$wid,$ht), REG_BINARY ],
                "/TaskBarIcon" => [ "0x0001", REG_DWORD ],
            },
            "Compatibility/" => {
                "/AutoConvert" => "Always",
                "/Default Palette" => "Windows Colors",
            },
        },
        "/License", => "0123-9C8EF1-09-FC",
    };

Note that all but the last Registry key used on the left-hand side of the assignment ["FooCorp/"] must already exist for this statement to succeed.

By using the leading a trailing delimeters on each subkey name and value name, Tie::Registry will tell you if you try to assign subkey information to a value or visa-versa.

More examples

Adding a new tip
    $tips= $Registry->{"LMachine/Software/Microsoft/"}->
              {"Windows/CurrentVersion/Explorer/Tips/"}
      or  die "Can't find the Windows tips: $^E\n";
    $tips{'/186'}= "Be very careful when making changes to the Registry!";
Deleting our new tip
    $tips= $Registry->{"LMachine/Software/Microsoft/"}->
              {"Windows/CurrentVersion/Explorer/Tips/"}
      or  die "Can't find the Windows tips: $^E\n";
    $tip186= delete $tips{'/186'};

Note that Perl's delete function returns the value that was deleted.

Adding a new tip differently
    $Registry->{"LMachine/Software/Microsoft/" .
                "Windows/CurrentVersion/Explorer/Tips//186"}=
      "Be very careful when making changes to the Registry!";
Deleting differently
    $tip186= delete $Registry->{"LMachine/Software/Microsoft/Windows/" .
                                "CurrentVersion/Explorer/Tips//186"};

Note that this only deletes the tail of what we looked up, the "186" value, not any of the keys listed.

Deleting a key
    $tips= delete $Registry->{"CUser/Software/Microsoft/Windows/" .
                              "CurrentVersion/Explorer/Tips/"};

WARNING: This will delete all information about the current user's tip preferences. Actually executing this command would probably cause the user to see the Welcome screen the next time they log in and may cause more serious problems. This statement is shown as an example only and should not be used when experimenting.

This deletes the "Tips" key and the values it contains. The delete function will return a reference to a hash [not a tied hash] containing the value names and value data that were deleted.

The information to be returned is copied from the Registry into a regular Perl hash before the key is deleted. If the key has many subkeys, this copying could take a significant amount of memory and/or processor time. So you can disable this process by calling the FastDelete member function:

    $prevSetting= $regKey->FastDelete(1);

which will cause all subsequent delete operations to simply return a true value if they succeed.

Undeleting a key
    $Registry->{"LMachine/Software/Microsoft/Windows/" .
                "CurrentVersion/Explorer/Tips/"}= $tips;

This adds back what we just deleted. Note that this version of Tie::Registry will use defaults for the key attributes [such as class and security] and not restore the previous attributes.

Not deleting a key
    $res= delete $Registry->{"CUser/Software/Microsoft/Windows/"}
    defined($res)  ||  die "Can't delete URL key: $^E\n";

WARNING: Actually executing this command could cause serious problems. This statement is shown as an example only and should not be used when experimenting.

Since the "Windows" key should contain subkeys, that delete statement should make no changes to the Registry, return undef, and set $^E to "Access is denied" [but see the BUGS section about $^E].

Not deleting again
    $tips= $Registry->{"CUser/Software/Microsoft/Windows/" .
                       "CurrentVersion/Explorer/Tips/"};
    delete $tips;

The Perl delete function requires that its argument be an expression that ends in a hash element lookup [or hash slice], which is not the case here. The delete function doesn't know which hash $tips came from and so can't delete it.

Objects Documentation

The following member functions are defined for use on Tie::Registry objects:

new

The new method creates a new Tie::Registry object. new is just a synonym for Open so see Open below for information on what arguments to pass in. Examples:

    $machKey= new Tie::Registry "LMachine"
      ||  die "Can't access HKEY_LOCAL_MACHINE key: $^E\n";
    $userKey= Tie::Registry->new("CUser")
      ||  die "Can't access HKEY_CURRENT_USER key: $^E\n";
Open =item $subKey= $key->Open( $sSubKey, $rhOptions )

The Open method opens a Registry key and returns a new Tie::Registry object associated with that Registry key. If you wish to use that object as a tied hash [not just as an object], then use the TiedRef method function after Open.

sSubKey is a string specifying a subkey to be opened. Alternately sSubKey can be a reference to an array value containing the list of increasingly deep subkeys specifying the path to the subkey to be opened.

$rhOptions is an optional reference to a hash containing extra options. The Open method supports two options, "Delimeter" and "Access", and $rhOptions should have only have zero or more of these strings as keys.

The "Delimeter" option specifies what string [usually a single character] will be used as the delimeter to be appended to subkey names and prepended to value names. If this option is not specified, the new key [$subKey] inherits the delimeter of the old key [$key].

The "Access" option specifies what level of access to the Registry key you wish to have once it has been opened. If this option is not specified, the new key [$subKey] is opened with the same access level used when the old key [$key] was opened. The virtual root of the Registry pretends it was opened with access KEY_READ|KEY_WRITE so this is the default access when opening keys directory via $Registry.

If the "Access" option value is a string that starts with "KEY_", then it should match E<one> of the predefined access levels [probably "KEY_READ", "KEY_WRITE", or "KEY_ALL_ACCESS"] exported by the Win32API::Registry module. Otherwise, a numeric value is expected. For maximum flexibility, include use Win32API::Registry qw(:KEY_);, for example, near the top of your script so you can specify more complicated access levels such as KEY_READ|KEY_WRITE.

If sSubKey does not begin with the delimeter [or sSubKey is an array reference], then the path to the subkey to be opened will be relative to the path of the original key [$key]. If sSubKey begins with a single delimeter, then the path to the subkey to be opened will be relative to the virtual root of the Registry on whichever machine the original key resides. If sSubKey begins with two consectutive delimeters, then those must be followed by a machine name which causes the Connect method function to be called.

Examples:

    $machKey= $Registry->Open( "LMachine", {Access=>KEY_READ,Delimeter=>"/"} )
      ||  die "Can't open HKEY_LOCAL_MACHINE key: $^E\n";
    $swKey= $machKey->Open( "Software" );
    $logonKey= $swKey->Open( "Microsoft/Windows NT/CurrentVersion/Winlogon/" );
    $NTversKey= $swKey->Open( ["Microsoft","Windows NT","CurrentVersion"] );
    $versKey= $swKey->Open( qw(Microsoft Windows CurrentVersion) );

    $remoteKey= $Registry->Open( "//HostA/LMachine/System/", {Delimeter=>"/"} )
      ||  die "Can't connect to HostA or can't open subkey: $^E\n";
Connect =item $remoteKey= $Registry->Connect( $sMachineName, $sKeyPath, $rhOptions )

The Connect method connects to the Registry of a remote machine, and opens a key within it, then returns a new Tie::Registry object associated with that remote Registry key. If you wish to use that object as a tied hash [not just as an object], then use the TiedRef method function after Connect.

sMachineName is the name of the remote machine. You don't have to preceed the machine name with two delimeter characters.

sKeyPath is a string specifying the remote key to be opened. Alternately sKeyPath can be a reference to an array value containing the list of increasingly deep keys specifying the path to the key to be opened.

$rhOptions is an optional reference to a hash containing extra options. The Connect method supports two options, "Delimeter" and "Access". See the Open method documentation for more information on these options.

sKeyPath is already relative to the virtual root of the Registry of the remote machine. A single leading delimeter on sKeyPath will be ignored and is not required.

sKeyPath can be empty in which case Connect will return an object representing the virtual root key of the remote Registry. Each subsequent use of Open on this virtual root key will call the system RegConnectRegistry function.

The Connect method can be called via any Tie::Registry object, not just $Registry. Attributes such as the desired level of access and the delimeter will be inherited from the object used.

Examples:

    $remMachKey= $Registry->Connect( "HostA", "LMachine", {Delimeter->"/"} )
      ||  die "Can't connect to HostA's HKEY_LOCAL_MACHINE key: $^E\n";

    $remVersKey= $remMachKey->Connect( "www.microsoft.com",
                   "LMachine/Software/Microsoft/Inetsrv/CurrentVersion/",
                   { Access->KEY_READ, Delimeter->"/" } )
      ||  die "Can't check what version of IIS Microsoft is running: $^E\n";

    $remVersKey= $remMachKey->Connect( "www",
                   qw(LMachine Software Microsoft Inetsrv CurrentVersion) )
      ||  die "Can't check what version of IIS we are running: $^E\n";
    
ObjectRef

Documentation under construction.

Flush

Documentation under construction.

GetValue

Documentation under construction.

ValueNames

Documentation under construction.

SubKeyNames

Documentation under construction.

SubKeyClasses

Documentation under construction.

SubKeyTimes

Documentation under construction.

MemberNames

Documentation under construction.

Information

Documentation under construction.

Delimeter

Documentation under construction.

Handle

Documentation under construction.

Path

Documentation under construction.

Machine

Documentation under construction.

Access

Documentation under construction.

OS_Delimeter

Documentation under construction.

Roots

Documentation under construction.

Tie

Documentation under construction.

TiedRef

Documentation under construction.

ArrayValues

Documentation under construction.

FastDelete

Documentation under construction.

SetValue

Documentation under construction.

StoreKey

Documentation under construction.

CreateKey

Documentation under construction.

Load

Documentation under construction.

UnLoad

Documentation under construction.

AllowSave

Documentation under construction.

AllowLoad

Documentation under construction.

SUMMARY

Most things can be done most easily via tied hashes. Skip down to the the "Tied Hashes Summary" to get started quickly.

Objects Summary

Here are quick examples that document the most common functionality of all of the method function [except for a few almost useless ones].

    # Just another way of saying Open():
    $key= new Tie::Registry "LMachine\\Software\\",
      { Access=>KEY_READ|KEY_WRITE, Delimeter=>"\\" };

    # Open a Registry key:
    $subKey= $key->Open( "SubKey/SubSubKey/",
      { Access=>KEY_ALL_ACCESS, Delimeter=>"/" } );

    # Connect to a remote Registry key:
    $remKey= $Registry->Connect( "MachineName", "LMachine/",
      { Access=>KEY_READ, Delimeter=>"/" } );

    # Get value data:
    $valueString= $key->GetValue("ValueName");
    ( $valueString, $valueType )= $key->GetValue("ValueName");

    # Get list of value names:
    @valueNames= $key->ValueNames;

    # Get list of subkey names:
    @subKeyNames= $key->SubKeyNames;

    # Get combined list of value names (with leading delimeters)
    # and subkey names (with trailing delimeters):
    @memberNames= $key->MemberNames;

    # Get all information about a key:
    %keyInfo= $key->Information;
    # keys(%keyInfo)= qw( Class LastWrite
    #   CntSubKeys MaxSubKeyLen MaxSubClassLen
    #   CntValues MaxValNameLen MaxValDataLen SecurityLen );

    # Get selected information about a key:
    ( $class, $cntSubKeys )= $key->Information( "Class", "CntSubKeys" );

    # Get and/or set delimeter:
    $delim= $key->Delimeter;
    $oldDelim= $key->Delimeter( $newDelim );

    # Get "path" for an open key:
    $path= $key->Path;
    # For example, "/CUser/Control Panel/Mouse/"
    # or "//NetServer/LMachine/System/DISK/".

    # Get name of machine where key is from:
    $mach= $key->Machine;
    # Will usually be "" indicating key is on local machine.

    # Get referenced to a tied hash for the object
    # so you can use the simpler tied hash interface:
    $key= $key->TiedRef;

    # Control whether hashes tied to this object return
    # array references when asked for value data:
    $oldBool= $key->ArrayValues( $newBool );

    # Control whether delete via hashes tied to this
    # object simply return a true value to save resources:
    $oldBool= $key->FastDelete( $newBool );

    # Add or set a value:
    $key->SetValue( "ValueName", $valueDataString );
    $key->SetValue( "ValueName", pack($format,$valueData), "REG_BINARY" );

    # Add or set a key:
    $key->CreateKey( "SubKeyName" );
    $key->CreateKey( "SubKeyName",
      { Access=>"KEY_ALL_ACCESS", Class=>"ClassName",
        Delimeter=>"/", Volatile=>1, Backup=>1 } );

    # Load an off-line Registry hive file into the on-line Registry:
    $newKey= $Registry->Load( "C:/Path/To/Hive/FileName" );
    $newKey= $key->Load( "C:/Path/To/Hive/FileName", "NewSubKeyName" );
    # Unload a Registry hive file loaded via the Load() method:
    $newKey->UnLoad;

    # (Dis)Allow yourself to load Registry hive files:
    $success= $Registry->AllowLoad( $bool );

    # (Dis)Allow yourself to save a Registry key to a hive file:
    $success= $Registry->AllowSave( $bool );

    # Save a Registry key to a new hive file:
    $key->RegSaveKey( "C:/Path/To/Hive/FileName", [] );

Other Useful Methods

See Win32API::Registry for more information on these methods. These methods are provided for coding convenience and are identical to the Win32API::Registry functions except that these don't take a handle to a Registry key, instead getting the handle from the invoking object [$key].

    $key->RegGetKeySecurity( $iSecInfo, $sSecDesc, $lenSecDesc );
    $key->RegLoadKey( $sSubKeyName, $sPathToFile );
    $key->RegNotifyChangeKeyValue(
      $bWatchSubtree, $iNotifyFilter, $hEvent, $bAsync );
    $key->RegQueryMultipleValues(
      $structValueEnts, $cntValueEnts, $Buffer, $lenBuffer );
    $key->RegReplaceKey( $sSubKeyName, $sPathToNewFile, $sPathToBackupFile );
    $key->RegRestoreKey( $sPathToFile, $iFlags );
    $key->RegSetKeySecurity( $iSecInfo, $sSecDesc );
    $key->RegUnLoadKey( $sSubKeyName );

Tied Hashes Summary

For fast learners, this may be the only section you need to read. Always append one delimeter to the end of each Registry key name and prepend one delimeter to the front of each Registry value name.

Opening keys

    use Tie::Registry;
    $Registry->Delimeter("/");                  # Set delimeter to "/".
    $swKey= $Registry->{"LMachine/Software/"};
    $winKey= $swKey->{"Microsoft/Windows/CurrentVersion/"};
    $userKey= $Registry->
      {"CUser/Software/Microsoft/Windows/CurrentVersion/"};
    $remoteKey= $Registry->{"//HostName/LMachine/"};

Reading values

    $progDir= $winKey->{"/ProgramFilesDir"};    # "C:\\Program Files"
    $tip21= $winKey->{"Explorer/Tips//21"};     # Text of tip #21.

    $winKey->ArrayValues(1);
    ( $devPath, $type )= $winKey->{"/DevicePath"};
    # $devPath eq "%SystemRoot%\\inf"
    # $type eq "REG_EXPAND_SZ"  [if you have SetDualVar.pm installed]
    # $type == REG_EXPAND_SZ  [if you did "use Win32API::Registry qw(REG_)"]

Setting values

    $winKey->{"Setup//SourcePath"}= "\\\\SwServer\\SwShare\\Windows";
    # Simple.  Assumes data type of REG_SZ.

    $winKey->{"Setup//Installation Sources"}=
      [ "D:\x00\\\\SwServer\\SwShare\\Windows\0\0", "REG_MULTI_SZ" ];
    # "\x00" and "\0" used to mark ends of each string and end of list.

    $userKey->{"Explorer/Tips//DisplayInitialTipWindow"}=
      [ pack("L",0), "REG_DWORD" ];
    $userKey->{"Explorer/Tips//Next"}= [ pack("S",3), "REG_BINARY" ];
    $userKey->{"Explorer/Tips//Show"}= [ pack("L",0), "REG_BINARY" ];

Adding keys

    $swKey->{"FooCorp/"}= {
        "FooWriter/" => {
            "/Version" => "4.032",
            "Startup/" => {
                "/Title" => "Foo Writer Deluxe ][",
                "/WindowSize" => [ pack("LL",$wid,$ht), REG_BINARY ],
                "/TaskBarIcon" => [ "0x0001", REG_DWORD ],
            },
            "Compatibility/" => {
                "/AutoConvert" => "Always",
                "/Default Palette" => "Windows Colors",
            },
        },
        "/License", => "0123-9C8EF1-09-FC",
    };

Listing all subkeys and values

    @members= keys( %{$swKey} );
    @subKeys= grep(  m#^/#,  keys( %{$swKey->{"Classes/batfile/"}} )  );
    # @subKeys= ( "/", "/EditFlags" );
    @valueNames= grep(  ! m#^/#,  keys( %{$swKey->{"Classes/batfile/"}} )  );
    # @valueNames= ( "DefaultIcon/", "shell/", "shellex/" );

Deleting values or keys with no subkeys

    $oldValue= delete $userKey->{"Explorer/Tips//Next"};

    $oldValues= delete $userKey->{"Explorer/Tips/"};
    # $oldValues will be reference to hash containing deleted keys values.

Closing keys

    undef $swKey;               # Explicit way to close a key.
    $winKey= "Anything else";   # Implicitly closes a key.
    exit 0;                     # Implicitly closes all keys.

AUTHOR

Tye McQueen, tye@metronet.com, see http://www.metronet.com/~tye/.

SEE ALSO

Win32API::Registry(3) - Provides access to Reg*(), HKEY_*, KEY_*, REG_* [required].

Win32::WinError(3) - Defines ERROR_* values [optional].

SetDualVar(3) - For returning REG_* values as combined string/integer [optional].

BUGS

Because Tie::Registry requires Win32API::Registry which uses the standard Perl tools for building extensions and these are not supported with the ActiveState version of Perl, Tie::Registry cannot be used with the ActiveState version of Perl. Sorry. The ActiveState version and standard version of Perl are merging so you may want to switch to the standard version of Perl soon.

Because Perl hashes are case sensitive, certain lookups are also case sensistive. In particular, the "Classes", "CUser", "LMachine", "Users", "PerfData", "CConfig", and "DynData" root keys must always be entered without changing between upper and lower case letters. Also, the special rule for matching subkey names that contain the user-selected delimeter only works if case is matched. All other key name and value name lookups should be case insensitive because the underlying Reg*() calls ignore case.

3 POD Errors

The following errors were encountered while parsing the POD:

Around line 1338:

Unknown E content in E<can>

Around line 1469:

Unknown E content in E<always>

Around line 1909:

Unknown E content in E<one>