📄 ch26.htm
字号:
<P>If you are still having problems, consider re-installing Windowsor 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 theRegistry, and you know how to make a Registry backup. Let's lookat how to use the Registry. To make Registry access as easy aspossible, 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 designedto make Registry access as easy as possible. You do not need in-depthknowledge of the Registry to use the methods. The examples inthis 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 followingsections are collected into one script file called ELST01.PL onthe CD-ROM that accompanies this book. When creating your ownscripts you merely need to cut and paste the lines of code thatyou're interested in. You won't need to hunt through four or fivefiles.<P>The next few sections discuss how to do specific Registry tasksusing the <TT>DmmReg</TT> module.You see how to use the following methods:<UL><LI><TT>openKey()</TT>-This constructormethod will open an existing key. It returns the undefined valueif the requested key can't be found in the Registry.<LI><TT>createKey()</TT>-This is anotherconstructor method. It will create a new key and optionally assigna value to the default name in one step.<LI><TT>getValue()</TT>-This methodlets you find the value half of a key's name-value pair.<LI><TT>setValue()</TT>-This methodlets you create or modify a key's name-value pair.<LI><TT>getKeys()</TT>-This methodreturns an array that contains a list of subkeys for a given key.<LI><TT>getValues()</TT>-This methodreturns 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" >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'sname. For example, if you want to determine if a file associationexists for <TT>.pl</TT> files, checkfor the existeNCe of the <TT>HKEY_CLASSES_ROOT\.pl</TT>key like this:<P><IMG SRC="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>variableto 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 holdthe 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 forPerl 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 subkeyname is passed as the only argument to the openKey method.<P>If you need to open a key that is deeper in the hierarchy, simplyadd the braNChes to the argument of the openKey method.<BLOCKQUOTE><PRE>$handle = HKEY_USERS->openKey('Default\Software\Microsoft\Userinformation'); </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 existingone. You specify the name of the new key, and you optionally specifya value for the default name-value pair. For example, if you wantedto create a Registry key that holds the name of the last datafile 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 argumentis 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 RegistryEditor. 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 scripthas 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"><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. Youcan also tell Windows that the key should be kept in memory andnot written to disk-a volatile key. However, this level of detailis more involved than this brief introducton can cover. Pleaseread <I>Special Edition Using the Windows 95 Registry</I> if youneed 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 writtenin the last section, you do this:<P><IMG SRC="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 bethe 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 objectrefereNCe.<BR>Call the <I><FONT FACE="MCPdigital-I">getValue()</FONT> method.The argument to <FONT FACE="MCPdigital-I">getValue()</FONT> isthe name of the value to be retrieved. In this instaNCe, the defaultvalue 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 returnsan array that holds the data type of the value and the value itself.SiNCe you only need the value in this example, an array slicewas used. You place parentheses around the entire fuNCtion callto ensure that the return value is evaluated in an array context.Then, regular subscripting notation selects the second elementof the returned array. The second element is assigned to <TT>$value</TT>.<P>The <TT>DmmReg</TT> module is designedto work with strings, the most popular type of data stored inthe Registry. While you can work with other data types, like binarydata, 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-valuepair 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 theexample shown in "Creating a New Key." Perhaps, insteadof 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 tostore. You can use the following script as a template:<P><IMG SRC="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 bethe 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 objectrefereNCe.<BR>Call the <I><FONT FACE="MCPdigital-I">setValue()</FONT> methodoNCe 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 + -