Приглашаем посетить
Мода (modnaya.ru)

[Chapter 16] 16.5 Opening and Reading Registry Values

PreviousChapter 16
System Information
Next
 

16.5 Opening and Reading Registry Values

Let's start our exploration of the Registry by finding out how to get information out of it. As an example, let's see what we can find out about the current build version of Windows NT on our system. If you're using this book on a Windows 95 system, you'll need to change the Windows NT key to Windows:

use Win32::Registry;
$p = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion";
$main::HKEY_LOCAL_MACHINE->Open($p, $CurrVer) || 
        die "Open: $!";
$CurrVer->GetValues(\%vals); # get sub keys and value -hash ref
foreach $k (keys %vals) {
    $key = $vals{$k};
    print "$$key[0] = $$key[2]\n"; # see below for explanation
}

Running this script on one of our systems produces the following output:

CurrentBuildNumber: 1381
CSDVersion = Service Pack 3
CurrentBuild = 1.511.1 () (Obsolete data - do not use)
RegisteredOrganization = Axiom Technologies
CurrentType = Uniprocessor Free
InstallDate = Ц?L3
RegisteredOwner = Erik Olson
CurrentVersion = 4.0
SystemRoot = D:\NT
CurrentBuildNumber = 1381
SoftwareType = SYSTEM
ProductId = 50036419013877247607
SourcePath = E:\I386
PathName = D:\NT

Let's see what's going on here. The first line of the script employs the use operator to include the Win32::Registry package. We then have a variable $p containing a Registry path relative to HKEY_LOCAL_MACHINE. The third line uses $main::HKEY_LOCAL_MACHINE (one of the Registry keys declared in registry.pm that we mentioned) to open the CurrentVersion key. If the Open method succeeds, $CurrVer will contain the Registry object corresponding to the CurrentVersion key.

Line four uses the $CurrVer key to call the GetValues method. GetValues takes a reference to a hash as a parameter and populates that hash with all of the values under $CurrVer. Each hash element consists of a key with the name of the Registry value and a value containing a reference to a three-element list. The list contains the value name, the data type of the value, and the value data. The remaining lines of the example iterate over each value using the foreach operator and print its value name and data value. For example:

foreach $k (keys %vals) {          # iterate over keys
    $key = $vals{$k};              # get ref to list
    print "$$key[0] = $$key[2]\n"; # dereference as list
}

We've seen how the Open method will open a Registry key relative to one of the main subtrees (or another key). We can also use the Create method to open a key, creating it if it doesn't exist. Create won't create more than one level deep, so we need to have a handle to the parent key before calling create. Here's an example that creates a new key under the HKEY_CURRENT_USER\SOFTWARE hive:

use Win32::Registry;
$main::HKEY_CURRENT_USER->Open("SOFTWARE", $Software) || 
        die "Open: $!";
$Software->Create("ERIKO", $eriko) || 
        die "Create: $!"; # new key is in $eriko

In order to create a key under SOFTWARE, we first need to obtain the key to SOFTWARE. We do so by using Open again, this time with the HKEY_CURRENT_USER subtree. After we have the SOFTWARE key open, we can create keys directly beneath it.


PreviousHomeNext
16.4 The RegistryBook Index16.6 Setting Registry Values