📄 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 using
the Registry Editor as shown in Figure D.6.
<P>
<A HREF="fd-6.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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. SiNCe
you 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 to
retrieve 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" 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 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>
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> and
display 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>
Persistent
Recent<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," you
saw that each Registry key can have name-value pairs associated
with it. You use the <TT>getValues()</TT>
method to get a list of these pairs.
<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 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>
Open the <I><FONT FACE="MCPdigital-I">HKEY_LOCAL_MACHINE\SOFTWARE\A
Perl Test Script </FONT>subkey.<BR>
Call the <I><FONT FACE="MCPdigital-I">getValues()</FONT> method
to populate the <FONT FACE="MCPdigital-I">%pairs</FONT> hash.
<BR>
Iterate over <I><FONT FACE="MCPdigital-I">%pairs</FONT> to print
the 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.dat
Date : 07-01-1996
Message 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 storing
configuration information that needs to be persistent:
<UL>
<LI><B>To create a file association</B>-You can associate an executable
file with a data file so that when the data file is double-clicked,
the application is started. You can also associate different context
menu options (like Open and Print) with each file extension.
<LI><B>To specify an icon</B>-You can use the Registry to determine
which icon is displayed in folders for each file extension.
<LI><B>To enable the 'new' context menu option</B>-You can let
the 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 creating
Registry keys and name-value pairs, so the code to do each task
will 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 code
will define extension for both Perl scripts and Perl modules.
The default value is used by Windows as a pointer to another Registry
key where additional information is stored. Step 2 will create
this 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 default
value of this key will be used as the file's type in the file's
property list.<BR>
<BR>
<TT>$handle = HKEY_CLASSES_ROOT->createKey('A
Perl 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> extension
is shown here. Change 'A Perl File' to 'A Perl Module' to create
context 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 pointing
to Microsoft Word, you should start whatever editor you normally
use.
<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 experiment
with different icon numbers to find one that you like. Icon number
27 in the <TT>shell32.dll</TT> file
looks 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, one
of 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> extension
key.<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 will
look like Figure D.7.
<P>
<A HREF="fd-7.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/fd-7.gif"><B>Figure D.7 : </B><I>The new sub-menu with options to create
Perl 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. The
Registry is used to store all types of information about the hardware
and 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>). These
keys are at the top of a hierarchical structure similar to a directory
tree.
<P>
The Registry information is stored on two files, <TT>user.dat</TT>
and <TT>system.dat</TT>. When the
system is booted, these files are read into memory and the Registry
is created. You read about sing the Registry Editor to export
and 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 their
default icons; and added Perl file types to the <TT>new</TT>
option of the context menu.
<HR>
<CENTER><P><A HREF="ch25.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch25.htm"><IMG SRC="pc.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A>
<A HREF="#CONTENTS"><IMG SRC="cc.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/cc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A>
<A HREF="index-1.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/index-1.htm"><IMG SRC="hb.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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 + -