📄 ch26.htm
字号:
<PRE>use DmmReg;use strict;my($handle);my($keyName) = 'SOFTWARE\A Perl Test Script';$handle = HKEY_LOCAL_MACHINE->createKey($keyName);$handle->setValue('Data File', 'c:\perl5\test.dat');$handle->setValue('Date', '07-01-1996');$handle->setValue('Message File', 'c:\perl5\friday.log');</PRE></BLOCKQUOTE><P>After this script is run, you can see the name-value pairs usingthe Registry Editor as shown in Figure D.6.<P><A HREF="fd-6.gif"><B>Figure D.6 : </B><I>A Registry key with four name-value pairs</I>.</A><P>Notice that the default name-value pair is no longer valued. SiNCeyou are using specifying names with the <TT>setValue()</TT>method, the default name is no longer needed.<H3><A NAME="GettingaListofSubkeys">Getting a List of Subkeys</A></H3><P>The <TT>getKeys()</TT> method of the<TT>DmmReg</TT> module is used toretrieve a list of subkeys for any specified key. For example,if you need to find all of the subkeys for the <TT>HKEY_CURRENT_USER\Network</TT>key use the following code.<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 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>Open the <I><FONT FACE="MCPdigital-I">HKEY_CURRENT_USER\Network</FONT>subkey.<BR>Check the status of the <I><FONT FACE="MCPdigital-I">openKey()</FONT>method, die if an error occured.<BR>Call the <I><FONT FACE="MCPdigital-I">getKeys()</FONT> method.<BR>Iterate over<I><FONT FACE="MCPdigital-I"> @subKeys</FONT> anddisplay the subkeys.</I></I></I></I></I></I></BLOCKQUOTE><BLOCKQUOTE><PRE>use DmmReg;use strict;my($handle);my($keyName) = 'Network';my(@subKeys);my($subKey);$handle = HKEY_CURRENT_USER->openKey('Network');die("Unable to open $keyName") unless defined($handle);$handle->getKeys(\@subKeys);foreach $subKey (sort(@subKeys)) { print("$subKey\n");}</PRE></BLOCKQUOTE><P>This program displays:<BLOCKQUOTE><PRE>PersistentRecent<BR></PRE></BLOCKQUOTE><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Caution</B></TD></TR><TR><TD><BLOCKQUOTE>There is a bug-that I have not been able to correct-that will not let you get a list of keys starting from one of the six root keys. SiNCe the first level of subkeys do not change, use the Registry Editor to find them.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H3><A NAME="GettingaListofNameValuePairs">Getting a List of Name-Value Pairs</A></H3><P>Earlier, in "Setting a Key's Name-Value Pairs," yousaw that each Registry key can have name-value pairs associatedwith it. You use the <TT>getValues()</TT>method to get a list of these pairs.<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 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>Open the <I><FONT FACE="MCPdigital-I">HKEY_LOCAL_MACHINE\SOFTWARE\APerl Test Script </FONT>subkey.<BR>Call the <I><FONT FACE="MCPdigital-I">getValues()</FONT> methodto populate the <FONT FACE="MCPdigital-I">%pairs</FONT> hash.<BR>Iterate over <I><FONT FACE="MCPdigital-I">%pairs</FONT> to printthe name-value pairs.</I></I></I></I></I></BLOCKQUOTE><BLOCKQUOTE><PRE>use DmmReg;use strict;my($handle);my($keyName) = 'SOFTWARE\A Perl Test Script';my($name);my(%pairs);$handle = HKEY_LOCAL_MACHINE->openKey($keyName);$handle->getValues(\%pairs);foreach $name (sort(keys(%pairs))) { printf("%-12.12s: @{$pairs{$name}}[1]\n", $name);}</PRE></BLOCKQUOTE><P>This program displays:<BLOCKQUOTE><PRE>Data File : c:\perl5\test.datDate : 07-01-1996Message File: c:\perl5\friday.log</PRE></BLOCKQUOTE><H2><A NAME="SomeCommonUsesfortheRegistry"><FONT SIZE=5 COLOR=#FF0000>Some Common Uses for the Registry</FONT></A></H2><P>There are several common uses for the Registry besides storingconfiguration information that needs to be persistent:<UL><LI><B>To create a file association</B>-You can associate an executablefile with a data file so that when the data file is double-clicked,the application is started. You can also associate different contextmenu options (like Open and Print) with each file extension.<LI><B>To specify an icon</B>-You can use the Registry to determinewhich icon is displayed in folders for each file extension.<LI><B>To enable the 'new' context menu option</B>-You can letthe user create new data files by using the new context menu option.</UL><P>By this time, you understand all of the coNCepts involved in creatingRegistry keys and name-value pairs, so the code to do each taskwill be presented with very few comments.<H3><A NAME="CreatingaFileAssociation">Creating a File Association</A></H3><P>There are three steps to creating file associations:<OL><LI>Tell Windows about the file extension. These lines of codewill define extension for both Perl scripts and Perl modules.The default value is used by Windows as a pointer to another Registrykey where additional information is stored. Step 2 will createthis secondary key.<BR><BR><TT>$handle = HKEY_CLASSES_ROOT->createKey('.pl','A Perl File');<BR>$handle = HKEY_CLASSES_ROOT->createKey('.pm', 'A Perl Module');</TT><LI>Create a key for the file extension description. The defaultvalue of this key will be used as the file's type in the file'sproperty list.<BR><BR><TT>$handle = HKEY_CLASSES_ROOT->createKey('APerl File', 'Perl Script');<BR>$handle = HKEY_CLASSES_ROOT->createKey('A Perl Module', 'Perl<BR>Module');</TT><LI>Create a key for each context menu option that you are creating.The keys for the <TT>.pl</TT> extensionis shown here. Change 'A Perl File' to 'A Perl Module' to createcontext menu options for <TT>.pm</TT>files.</OL><BLOCKQUOTE><PRE>$handle = HKEY_CLASSES_ROOT->createKey('A Perl File\Shell\Open\Command','C:\MSOFFICE7\WINWORD\WINWORD.EXE %1');$handle = HKEY_CLASSES_ROOT->createKey('A Perl File\Shell\Edit\Command','C:\MSOFFICE7\WINWORD\WINWORD.EXE %1');$handle = HKEY_CLASSES_ROOT->createKey('A Perl File\Shell\Print\Command','C:\MSOFFICE7\WINWORD\WINWORD.EXE /p %1');</PRE></BLOCKQUOTE><P>For simplicity's sake, I have all of my associations pointingto Microsoft Word, you should start whatever editor you normallyuse.<H3><A NAME="SettingtheIconforaFileExtension">Setting the Icon for a File Extension</A></H3><P>You specify the icon for a file extension by creating a <TT>DefaultIcon</TT>subkey under the extension description key like this:<BLOCKQUOTE><PRE>$handle = HKEY_CLASSES_ROOT->createKey('A Perl File\DefaultIcon', 'C:\WINDOWS\SYSTEM\SHELL32.DLL,27');</PRE></BLOCKQUOTE><P>The default value of the <TT>DefaultIcon</TT>key indicates which DLL and icon number to use. You can experimentwith different icon numbers to find one that you like. Icon number27 in the <TT>shell32.dll</TT> filelooks like a monitor that is displaying a starburst.<H3><A NAME="EnablingthenewContextMenuOption">Enabling the 'new' Context Menu Option</A></H3><P>If you right-click while inside a folder or on the desktop, oneof the context menu options is <TT>new</TT>.You can add your own file types to the <TT>new</TT>sub-menu by following these steps:<OL><LI>Open the <TT>.pl</TT> extensionkey.<BR><BR><TT>$handle = HKEY_CLASSES_ROOT->openKey('.pl');</TT><LI>Create a subkey called <TT>ShellNew</TT>.<BR><BR><TT>$handle = HKEY_CLASSES_ROOT->createKey('.pl\ShellNew','');</TT><LI>Create a name-value pair with a name of <TT>NullFile</TT>.</OL><BLOCKQUOTE><PRE>$handle->setValue('NullFile', '');</PRE></BLOCKQUOTE><P>If you follow these steps for both the .pl and .pm extensions,your <TT>new</TT> context menu willlook like Figure D.7.<P><A HREF="fd-7.gif"><B>Figure D.7 : </B><I>The new sub-menu with options to createPerl files</I>.</A><H2><A NAME="Summary"><FONT SIZE=5 COLOR=#FF0000>Summary</FONT></A></H2><P>This chapter briefly introduced you to the Windows Registry. TheRegistry is used to store all types of information about the hardwareand software that are installed on your computer system.<P>You learned that there are three root keys (<TT>HKEY_DYN_DATA</TT>,<TT>HKEY_LOCAL_MACHINE</TT>, and <TT>HKEY_USERS</TT>)and three shortcut keys (<TT>HKEY_CLASSES_ROOT</TT>,<TT>HKEY_CURRENT_CONFIG</TT>, and<TT>HKEY_CURRENT_USER</TT>). Thesekeys are at the top of a hierarchical structure similar to a directorytree.<P>The Registry information is stored on two files, <TT>user.dat</TT>and <TT>system.dat</TT>. When thesystem is booted, these files are read into memory and the Registryis created. You read about sing the Registry Editor to exportand import the Registry information for backup and recovery.<P>Then, you saw how to use the <TT>DmmReg</TT>module to access and modify Registry keys and name-value pairs.Examples were shown that create file association for <TT>.pl</TT>and <TT>.pm</TT> files; changed theirdefault icons; and added Perl file types to the <TT>new</TT>option of the context menu.<HR><CENTER><P><A HREF="ch25.htm"><IMG SRC="pc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="#CONTENTS"><IMG SRC="cc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="index.htm"><IMG SRC="hb.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><HR WIDTH="100%"></P></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -