📄 ch26.htm
字号:
<P>
If you are still having problems, consider re-installing Windows
or calling an expert for help.
<H2><A NAME="UsingtheRegistry"><FONT SIZE=5 COLOR=#FF0000>
Using the Registry</FONT></A></H2>
<P>
At this point, you have some background information about the
Registry, and you know how to make a Registry backup. Let's look
at how to use the Registry. To make Registry access as easy as
possible, I have created an object-oriented module, called <TT>DmmReg.pm</TT>,
for Registry access.<BR>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Note</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
The module was called <TT>DmmReg</TT> because there is already a module called <TT>Registry.pm</TT> iNCluded with Perl for Win32. However, that module has little documentation and I wanted to create something special for this book.
</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
The <TT>DmmReg</TT> module was designed
to make Registry access as easy as possible. You do not need in-depth
knowledge of the Registry to use the methods. The examples in
this chapter show you how to open and create keys, read key values,
and list subkeys.<BR>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Tip</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
On the other hand, you might feel more comfortable changing the Registry if you know more. If so, read Que's <TT><I>Special Edition Using the Windows 95 Registry</I></TT> by Jerry Honeycutt.
</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
All of the snippets of code that are discussed in the following
sections are collected into one script file called ELST01.PL on
the CD-ROM that accompanies this book. When creating your own
scripts you merely need to cut and paste the lines of code that
you're interested in. You won't need to hunt through four or five
files.
<P>
The next few sections discuss how to do specific Registry tasks
using the <TT>DmmReg</TT> module.
You see how to use the following methods:
<UL>
<LI><TT>openKey()</TT>-This constructor
method will open an existing key. It returns the undefined value
if the requested key can't be found in the Registry.
<LI><TT>createKey()</TT>-This is another
constructor method. It will create a new key and optionally assign
a value to the default name in one step.
<LI><TT>getValue()</TT>-This method
lets you find the value half of a key's name-value pair.
<LI><TT>setValue()</TT>-This method
lets you create or modify a key's name-value pair.
<LI><TT>getKeys()</TT>-This method
returns an array that contains a list of subkeys for a given key.
<LI><TT>getValues()</TT>-This method
returns a hash that contains name-value entries for a given key.
</UL>
<P>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Tip</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
In order to avoid a bit of potential confusion, let me clarify one thing. The <TT>DmmReg</TT> module has <TT><I>two</I></TT> constructor fuNCtions: <TT>createKey()</TT> and <TT>openKey()</TT>. Both fuNCtions will return an object refereNCe. If you aren't
sure what constructor fuNCtions are, see <A HREF="ch14.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch14.htm" >Chapter 14</A>, "What Are Objects?".
</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<H3><A NAME="OpeninganExistingKey">
Opening an Existing Key</A></H3>
<P>
To open an existing Registry key, you need only know the key's
name. For example, if you want to determine if a file association
exists for <TT>.pl</TT> files, check
for the existeNCe of the <TT>HKEY_CLASSES_ROOT\.pl</TT>
key like this:
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>
<BLOCKQUOTE>
<I>Specify that this script will use the <FONT FACE="MCPdigital-I">DmmReg</FONT>
module.<BR>
Specify that strict variable checking should be done.<BR>
Declare the <I><FONT FACE="MCPdigital-I">$handle </FONT>variable
to be local to the file.<BR>
Create an object of type <I><FONT FACE="MCPdigital-I">HKEY_CLASSES_ROOT</FONT>
and open the subkey called <FONT FACE="MCPdigital-I">.pl.</FONT>
The <FONT FACE="MCPdigital-I">$handle</FONT> object will hold
the object refereNCe.<BR>
Display a message indicating the existeNCe of the subkey.</I></I></I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
use DmmReg;
use strict;
my($handle);
$handle = HKEY_CLASSES_ROOT->openKey('.pl');
print("There " .
(defined($handle)? "is an" : "is no") .
" association for .pl files\.n");
</PRE>
</BLOCKQUOTE>
<P>
If your system does not have any file associations defined for
Perl scripts, this program displays:
<BLOCKQUOTE>
<PRE>
There is no association for .pl files.
</PRE>
</BLOCKQUOTE>
<P>
The name of the root key is used as the class name and the subkey
name is passed as the only argument to the openKey method.
<P>
If you need to open a key that is deeper in the hierarchy, simply
add the braNChes to the argument of the openKey method.
<BLOCKQUOTE>
<PRE>
$handle = HKEY_USERS->openKey('Default\Software\Microsoft\User
information');
</PRE>
</BLOCKQUOTE>
<P>
You can also see from this second example that the <TT>DmmReg</TT>
module lets you create more than one type of object. Actually,
you can create a different object for each of the six root keys.
Each class has exactly the same methods and fuNCtionality.
<H3><A NAME="CreatingaNewKey">
Creating a New Key</A></H3>
<P>
Creating a new key is almost as simple as opening an existing
one. You specify the name of the new key, and you optionally specify
a value for the default name-value pair. For example, if you wanted
to create a Registry key that holds the name of the last data
file that your script opened you could do it like this:
<BLOCKQUOTE>
<PRE>
$h = HKEY_LOCAL_MACHINE->createKey(
'SOFTWARE\A Perl Test Script\Last Data File',
'C:\TEST.DAT');
</PRE>
</BLOCKQUOTE>
<P>
The first argument is the name of the key and the second argument
is the data that will be assigned to the default name.<BR>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Note</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
The most confusing aspect of the Registry and its keys is that each key can have both subkeys and name-value pairs associated with it. The default name is represented by an empty string. The <TT>createKey()</TT> method lets you create a new key and assign
a value to its default name in one step.
</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
You can verify that the assignment worked by using the Registry
Editor. The new key and its default value is shown in Figure D.5.
Some programmers refer to this type of information as <I>persistent</I>
because the Registry key will be around even after your script
has ended. If the key specified as the parameter to the <TT>createKey()</TT>
method already exists, then that key will be opened.
<P>
<A HREF="fd-5.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/fd-5.gif"><B>Figure D.5 : </B><I>Creating persistent information in the Registry</I>.</A>
<P>
As with the <TT>openKey()</TT> method,
you can specify limited access rights when opening a key. You
can also tell Windows that the key should be kept in memory and
not written to disk-a volatile key. However, this level of detail
is more involved than this brief introducton can cover. Please
read <I>Special Edition Using the Windows 95 Registry</I> if you
need more advaNCed information.
<H3><A NAME="FindingaKeysValue">
Finding a Key's Value</A></H3>
<P>
You can find out a key's value by using the <TT>getValue()</TT>
method in the <TT>DmmReg</TT> module.
For example, to read the name of the data file that was written
in the last section, you do this:
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>
<BLOCKQUOTE>
<I>Specify that this script will use the <FONT FACE="MCPdigital-I">DmmReg
</FONT>module.<BR>
Specify that strict variable checking should be done.<BR>
Declare the <I><FONT FACE="MCPdigital-I">$handle </FONT>and <FONT FACE="MCPdigital-I">$keyName</FONT>
variables to be local to the file.<BR>
Initialize <I><FONT FACE="MCPdigital-I">$keyName</FONT> to be
the name of the key we're interested in.<BR>
Call the <I><FONT FACE="MCPdigital-I">openKey()</FONT> method,
<FONT FACE="MCPdigital-I">$handle</FONT> will hold the object
refereNCe.<BR>
Call the <I><FONT FACE="MCPdigital-I">getValue()</FONT> method.
The argument to <FONT FACE="MCPdigital-I">getValue()</FONT> is
the name of the value to be retrieved. In this instaNCe, the default
value is sought.<BR>
Print the data associated with the default value.</I></I></I></I></I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
use DmmReg;
use strict;
my($handle);
my($keyName) = 'SOFTWARE\A Perl Test Script\Last Data File';
my($value)
$handle = HKEY_LOCAL_MACHINE->openKey($keyName);
$value = ($handle->getValue(''))[1];
print("The data file was named $value\n");
</PRE>
</BLOCKQUOTE>
<P>
This program displays:
<BLOCKQUOTE>
<PRE>
The data file was named C:\TEST.DAT
</PRE>
</BLOCKQUOTE>
<P>
You may find the call to the <TT>getValue()</TT>
method to be a little confusing. Let's take a closer look at it:
<BLOCKQUOTE>
<PRE>
$data = ($handle->getValue(''))[1];
</PRE>
</BLOCKQUOTE>
<P>
The <TT>getValue()</TT> method returns
an array that holds the data type of the value and the value itself.
SiNCe you only need the value in this example, an array slice
was used. You place parentheses around the entire fuNCtion call
to ensure that the return value is evaluated in an array context.
Then, regular subscripting notation selects the second element
of the returned array. The second element is assigned to <TT>$value</TT>.
<P>
The <TT>DmmReg</TT> module is designed
to work with strings, the most popular type of data stored in
the Registry. While you can work with other data types, like binary
data, you'll need to look at more advaNCed books to find out how.
<H3><A NAME="SettingaKeysNameValuePairs">
Setting a Key's Name-Value Pairs</A></H3>
<P>
You've already seen how to set the value of the default name-value
pair by using the <TT>createKey() </TT>method.
In this section, you use the <TT>setValue()</TT>
method to explicitly set any name-value pair. Let's build on the
example shown in "Creating a New Key." Perhaps, instead
of just saving one data file, you need to save more than one.
Maybe you have the names of a message file and a data file to
store. You can use the following script as a template:
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>
<BLOCKQUOTE>
<I>Specify that this script will use the <FONT FACE="MCPdigital-I">DmmReg
</FONT>module.<BR>
Specify that strict variable checking should be done.<BR>
Declare the <I><FONT FACE="MCPdigital-I">$handle</FONT> and <FONT FACE="MCPdigital-I">$keyName</FONT>
variables to be local to the file.<BR>
Initialize <I><FONT FACE="MCPdigital-I">$keyName</FONT> to be
the name of the key we're interested in.<BR>
Call the <I><FONT FACE="MCPdigital-I">createKey()</FONT> method,
<FONT FACE="MCPdigital-I">$handle</FONT> will hold the object
refereNCe.<BR>
Call the <I><FONT FACE="MCPdigital-I">setValue()</FONT> method
oNCe for each name-value pair that needs to be stored.</I></I></I></I></I>
</BLOCKQUOTE>
<BLOCKQUOTE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -