⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch10.htm

📁 《Perl 5 Unreleased》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<HTML>



<HEAD>

   <TITLE>Chapter 10 -- Win32 Modules on Windows NT </TITLE>

   <META NAME="GENERATOR" CONTENT="Mozilla/3.0b5aGold (WinNT; I) [Netscape]">

</HEAD>

<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">

<H1><FONT COLOR=#FF0000>Chapter 10</FONT></H1>

<H1><B><FONT SIZE=5 COLOR=#FF0000>Win32 Modules on Windows NT </FONT></B>

</H1>

<P>

<HR WIDTH="100%"></P>

<P>

<H3 ALIGN=CENTER><FONT COLOR="#000000"><FONT SIZE=+2>CONTENTS<A NAME="CONTENTS"></A>

</FONT></FONT></H3>

<UL>

<LI><A HREF="#TheWin32Module" >The Win32 Module</A>

<LI><A HREF="#TheProcessModuleExtensions" >The Process Module Extensions</A>

<LI><A HREF="#TheKillMethod" >The Kill Method</A>

<UL>

<LI><A HREF="#TheResumeandSuspendMethods" >The Resume and Suspend Methods</A>

<LI><A HREF="#TheGetExitCodeFunction" >The GetExitCode() Function</A>

<LI><A HREF="#TheWaitTimeoutFunction" >The Wait($Timeout) Function</A>

<LI><A HREF="#ClassPriority" >Class Priority</A>

</UL>

<LI><A HREF="#TheWin32SemaphoreUtilities" >The Win32::Semaphore Utilities</A>

<LI><A HREF="#UsingMutexwithWin32Mutex" >Using Mutex with Win32::Mutex</A>

<LI><A HREF="#UsingWin32ChangeNotification" >Using Win32::ChangeNotification</A>

<LI><A HREF="#TheWin32EventlogModule" >The Win32::Eventlog Module</A>

<UL>

<LI><A HREF="#ReadingEvents" >Reading Events</A>

<LI><A HREF="#ReportingtheContentsofanEvent" >Reporting the Contents of an Event</A>

</UL>

<LI><A HREF="#TheWin32RegistryModule" >The Win32::Registry Module</A>

<LI><A HREF="#TheWin32NetAdminModule" >The Win32::NetAdmin Module</A>

<UL>

<LI><A HREF="#AddingaUser" >Adding a User</A>

<LI><A HREF="#CreatingorDeletingaGroup" >Creating or Deleting a Group</A>

<LI><A HREF="#CommentsAboutaGroup" >Comments About a Group</A>

<LI><A HREF="#AddingUserstoaGroup" >Adding Users to a Group</A>

<LI><A HREF="#RemovingUsersfromaGroup" >Removing Users from a Group</A>

<LI><A HREF="#CheckingGroupMembership" >Checking Group Membership</A>

<LI><A HREF="#ListingGroupMembers" >Listing Group Members</A>

</UL>

<LI><A HREF="#UsingtheWin32ServiceModule" >Using the Win32::Service Module</A>

<LI><A HREF="#UsingtheWin32NetResourceModule" >Using the Win32::NetResource Module</A>

<LI><A HREF="#HandlingInputfromMultipleSources" >Handling Input from Multiple Sources</A>

<LI><A HREF="#Summary" >Summary</A>

</UL>

<HR>

<P>

In the previous chapter I covered details on getting and installing

Perl for Windows NT. In this chapter I cover the methods and functions

of some of the <TT><FONT FACE="Courier">Win32</FONT></TT> modules

that come with the NT Perl package.

<P>

Although this chapter covers details of how to make calls into

the Windows 32 bit API, I do not go into the details of how the

module itself works as it interacts with the API. Dealing with

that topic would be well beyond the scope of this book. To get

a good understanding of how to program an NT application, check

out Jason Loveman's <I>Moving to Windows NT Programming</I>, Sams

Publishing. This book may seem a bit dated at first, but it provides

an excellent tutorial on how to get going from a Windows 3.<I>x</I>

environment to NT. The other book that is a must-have is <I>The

Win32 API Desktop Reference</I> by Jim McCord, also from Sams

Publishing. This book is a comprehensive reference manual with

a bit of instruction on how to program without the benefit of

the Microsoft Foundation Classes.<P>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR VALIGN=TOP><TD ><B>Note</B></TD></TR>

<TR VALIGN=TOP><TD >

<BLOCKQUOTE>

Remember from the previous chapter that to execute a Perl script, you must type the <TT><FONT FACE="Courier">perl</FONT></TT> command on the prompt with the name of the Perl script:<BR>

<TT><FONT FACE="Courier">c:\dos&gt; perl myscript.pl</FONT></TT>

</BLOCKQUOTE>



</TD></TR>

</TABLE></CENTER>

<P>

<H2><A NAME="TheWin32Module"><FONT SIZE=5 COLOR=#FF0000>The Win32

Module</FONT></A></H2>

<P>

The Win32 module contains extensions for working specifically

with the Windows NT system. Here are some miscellaneous functions

that exist only under the Windows NT hierarchy-you'll find them

quite useful when writing Perl scripts under NT:

<UL>

<LI><TT><FONT FACE="Courier">Win32::GetLastError</FONT></TT>.

Returns the last error value generated by a call to a Win32 API

function.

<LI><TT><FONT FACE="Courier">Win32::PerlVersion</FONT></TT>. Returns

a string indicating the version of NT Perl.

<LI><TT><FONT FACE="Courier">Win32::LoginName</FONT></TT>. Returns

the name of the user running the current Perl process.

<LI><TT><FONT FACE="Courier">Win32::NodeName</FONT></TT>. Returns

the NT node name of the current machine.

<LI><TT><FONT FACE="Courier">Win32::DomainName</FONT></TT>. Returns

the domain name of the computer that the owner of the current

Perl process is logged into.

<LI><TT><FONT FACE="Courier">Win32::FsType</FONT></TT>. Returns

a string with the name of the underlying file system.

<LI><TT><FONT FACE="Courier">Win32::GetCwd</FONT></TT>. Returns

the current drive only. This function does not return a path.

<LI><TT><FONT FACE="Courier">Win32::GetOSVersion</FONT></TT>.

Returns an array in the form (<TT><FONT FACE="Courier">$description,

$major, $minor, $build, $id</FONT></TT>), which contains the following

items: an arbitrary descriptive string, followed by the major,

minor, and build numbers of the operating system. The <TT><FONT FACE="Courier">$id</FONT></TT>

is a digit with a code of the actual operating system. The values

of <TT><FONT FACE="Courier">$id</FONT></TT> are <TT><FONT FACE="Courier">0</FONT></TT>

for generic Win32, <TT><FONT FACE="Courier">1</FONT></TT> for

Windows 95, and <TT><FONT FACE="Courier">2</FONT></TT> for Windows

NT.

<LI><TT><FONT FACE="Courier">Win32::FormatMessage ERRORCODE</FONT></TT>.

Converts the Win32 error code to a printable string.

<LI><TT><FONT FACE="Courier">Win32::Spawn</FONT></TT>. Starts

a process. (See the following section on this module for more

information.)

<LI><TT><FONT FACE="Courier">Win32::InitiateSystemShutdown</FONT></TT>.

Starts shutdown. (See the following section on this module for

more information.)

<LI><TT><FONT FACE="Courier">Win32::AbortSystemShutdown</FONT></TT>.

Stops shutdown. (See the following section on this module for

more information.)

</UL>

<H5>Spawning Processes</H5>

<BLOCKQUOTE>

The <TT><FONT FACE="Courier">Win32::Spawn</FONT></TT> command

is used to start a process. The syntax for this command is

</BLOCKQUOTE>

<BLOCKQUOTE>

<TT><FONT FACE="Courier">Win32::Spawn( $command, $args, $pid);</FONT></TT>

</BLOCKQUOTE>

<BLOCKQUOTE>

A new process is started and runs the command in <TT><FONT FACE="Courier">$command</FONT></TT>.

Any command-line arguments passed into the process are specified

in <TT><FONT FACE="Courier">$args</FONT></TT>. The process ID

is returned in <TT><FONT FACE="Courier">$pid</FONT></TT>.

</BLOCKQUOTE>

<H5>Starting System Shutdown</H5>

<BLOCKQUOTE>

The <TT><FONT FACE="Courier">Win32::InitiateSystemShutdown</FONT></TT>

command is used to shut down a machine. The syntax for this command

is

</BLOCKQUOTE>

<BLOCKQUOTE>

<TT><FONT FACE="Courier">Win32::InitiateSystemShutdown (<BR>

&nbsp;&nbsp;&nbsp;&nbsp;$machine, $message, $timeout, $forceClose,

$reboot);</FONT></TT>

</BLOCKQUOTE>

<BLOCKQUOTE>

The machine name is specified in <TT><FONT FACE="Courier">$machine</FONT></TT>,

and a <TT><FONT FACE="Courier">NULL</FONT></TT> value identifies

the current machine. The <TT><FONT FACE="Courier">$message</FONT></TT>

value is sent to all the users logged into the machine at this

time. The <TT><FONT FACE="Courier">$timeout</FONT></TT> value

is used to notify users of imminent shutdown. The <TT><FONT FACE="Courier">$forceClose</FONT></TT>

value is set to <TT><FONT FACE="Courier">1</FONT></TT> if you

want the system to close all open files without prompting the

user. (This is useful during off times when the user might not

be at his or her desk.) The machine will reboot if <TT><FONT FACE="Courier">$reboot</FONT></TT>

is set to <TT><FONT FACE="Courier">true</FONT></TT>.

</BLOCKQUOTE>

<H5>Aborting System Shutdown</H5>

<BLOCKQUOTE>

The <TT><FONT FACE="Courier">Win32::AbortSystemShutdown</FONT></TT>

command attempts to stop the shutdown process on a given machine.

The syntax for this command is

</BLOCKQUOTE>

<BLOCKQUOTE>

<TT><FONT FACE="Courier">Win32::AbortSystemShutdown ($machine);</FONT></TT>

</BLOCKQUOTE>

<BLOCKQUOTE>

The abort request might not always work because the <TT><FONT FACE="Courier">$timeout</FONT></TT>

may happen before you get this command going. (I could not stop

the shutdown/reboot process even with a very long timeout.) Stopping

a shutdown process in the middle may leave your system in an indeterminate

state. Once you get the ball rolling on a shutdown, let it go

through all the way. However, if you must stop an accidental shutdown

command, this command can be a valuable resource.

</BLOCKQUOTE>

<BLOCKQUOTE>

Several modules exist under the <TT><FONT FACE="Courier">Win32::

extensions</FONT></TT> to enable you to work with processes, interprocess

communications networks, and user resources. I cover these modules

as well. One thing to keep in mind is that this code is in test

mode at this time, and improvements will be made as the code develops.

Also, these extensions are almost always NT-specific and might

not work on Windows 95. This means that the code you write with

these modules will not be portable to platforms other than Windows

NT systems.

</BLOCKQUOTE>

<BLOCKQUOTE>

Listing 10.1 illustrates how to use some of the functions in the

Win32 module.

</BLOCKQUOTE>

<HR>

<BLOCKQUOTE>

<B>Listing 10.1. Using the Win32 modules.<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<TT><FONT FACE="Courier">&nbsp;1 use Win32;<BR>

&nbsp;2<BR>

&nbsp;3 $x = Win32::PerlVersion&nbsp;&nbsp;;<BR>

&nbsp;4 print &quot; Version number =&nbsp;&nbsp;$x \n &quot;;

<BR>

&nbsp;5<BR>

&nbsp;6 $x = Win32::LoginName;<BR>

&nbsp;7<BR>

&nbsp;8 print &quot; \n Login Name $x&nbsp;&nbsp;&quot;;<BR>

&nbsp;9<BR>

10&nbsp;&nbsp;$x = Win32::NodeName;<BR>

11 print &quot; \n&nbsp;&nbsp;Node Name = $x &quot;;<BR>

12<BR>

13&nbsp;&nbsp;$x = Win32::DomainName;<BR>

14 print &quot; \n&nbsp;&nbsp;Domain Name = $x &quot;;<BR>

15<BR>

16&nbsp;&nbsp;$x = Win32::FsType;<BR>

17 print &quot; \n&nbsp;&nbsp;File system type = $x &quot;;<BR>

18<BR>

19&nbsp;&nbsp;$x = Win32::GetCwd;<BR>

20 print &quot; \n&nbsp;&nbsp;Current Drive = $x &quot;;<BR>

21<BR>

22&nbsp;&nbsp;$x = Win32::GetOSVersion;<BR>

23 print &quot; \n&nbsp;&nbsp;Current OS version = $x &quot;;

<BR>

24<BR>

25 $x = Win32::GetTickCount ;<BR>

26 print &quot; \n Tick count =&nbsp;&nbsp;$x &quot;;<BR>

27 print &quot;\n This is an NT box &quot;&nbsp;&nbsp;if ( Win32::IsWinNT);

<BR>

28 print &quot;\n This is a 95 box &quot;&nbsp;&nbsp;if ( Win32::IsWin95);</FONT></TT>

</BLOCKQUOTE>

<HR>

<H2><A NAME="TheProcessModuleExtensions"><FONT SIZE=5 COLOR=#FF0000>The

Process Module Extensions</FONT></A></H2>

<P>

The <TT><FONT FACE="Courier">Win32::Process</FONT></TT> module

extensions enable you to create different processes and start

applications using Perl scripts. The <TT><FONT FACE="Courier">Create</FONT></TT>

function lets you create a <TT><FONT FACE="Courier">Process</FONT></TT>

object that will run an application. The syntax for this call

is

<BLOCKQUOTE>

<TT><FONT FACE="Courier">Create( $pObject,<BR>

&nbsp;&nbsp;&nbsp;&nbsp;$ApplicationName,<BR>

&nbsp;&nbsp;&nbsp;&nbsp;$CommandLine,<BR>

&nbsp;&nbsp;&nbsp;&nbsp;$InheritHandles,<BR>

&nbsp;&nbsp;&nbsp;&nbsp;$CreateOptions,<BR>

&nbsp;&nbsp;&nbsp;&nbsp;$CurrentDir)</FONT></TT>

</BLOCKQUOTE>

<P>

<TT><FONT FACE="Courier">$pObject</FONT></TT> is the returned

reference to an object from this <TT><FONT FACE="Courier">Create</FONT></TT>

call. The <TT><FONT FACE="Courier">Create</FONT></TT> function

is the constructor for the <TT><FONT FACE="Courier">Process</FONT></TT>

class. Subsequent methods are called using this object as a reference.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -