📄 ch10.htm
字号:
<P>
<TT><FONT FACE="Courier">$ApplicationName</FONT></TT> is the full
pathname of the application you want to run in this <TT><FONT FACE="Courier">Process</FONT></TT>
object. The path can be specified using forward slashes or back
slashes. For example, to fire up <TT><FONT FACE="Courier">C:\dos\winzip.exe</FONT></TT>
on a system, you can use either one of these two pathnames:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">"c:/dos/winzip.exe"<BR>
<BR>
"c:\\dos\\winzip.exe"</FONT></TT>
</BLOCKQUOTE>
<P>
<TT><FONT FACE="Courier">$CommandLine</FONT></TT> is the list
of arguments passed at the command line to the application in
<TT><FONT FACE="Courier">$ApplicationName</FONT></TT>. The new
application will inherit any open file handles if <TT><FONT FACE="Courier">$InheritHandles</FONT></TT>
is set to <TT><FONT FACE="Courier">1</FONT></TT>. If you do not
want the called application to inherit file handles, set <TT><FONT FACE="Courier">$InheritHandles</FONT></TT>
to <TT><FONT FACE="Courier">0</FONT></TT>. The <TT><FONT FACE="Courier">$CurrentDir</FONT></TT>
variable is set to the working directory for executable programs.
<P>
The <TT><FONT FACE="Courier">$CreateOptions</FONT></TT> variable
can take a combination of the following values:<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD WIDTH=273><TT><FONT FACE="Courier">CREATE_DEFAULT_ERROR_MODE</FONT></TT>
</TD><TD WIDTH=317>Gives the new process the default error mode.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=273><TT><FONT FACE="Courier">CREATE_NEW_CONSOLE</FONT></TT>
</TD><TD WIDTH=317>The new process has a new console. This cannot be used with <TT><FONT FACE="Courier">DETAchED_PROCESS</FONT></TT>.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=273><TT><FONT FACE="Courier">CREATE_NEW_PROCESS_GROUP</FONT></TT>
</TD><TD WIDTH=317>The new process is the root of a new process group.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=273><TT><FONT FACE="Courier">CREATE_SEPARATE_WOW_VDM</FONT></TT>
</TD><TD WIDTH=317>The new process runs in its own 16-bit Virtual DOS Machine (VDM).
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=273><TT><FONT FACE="Courier">CREATE_SUSPENDED</FONT></TT>
</TD><TD WIDTH=317>Starts the new process in a suspended state. The process can be forced to execute with the <TT><FONT FACE="Courier">Resume</FONT></TT> method.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=273><TT><FONT FACE="Courier">CREATE_UNICODE_ENVIRONMENT</FONT></TT>
</TD><TD WIDTH=317>The new process environment contains support for Unicode characters.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=273><TT><FONT FACE="Courier">DEBUG_PROCESS</FONT></TT>
</TD><TD WIDTH=317>The calling process is being used as a debugger, and the new process is being debugged.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=273><TT><FONT FACE="Courier">DEBUG_ONLY_THIS_PROCESS</FONT></TT>
</TD><TD WIDTH=317>The new process is not debugged, even if the calling process is running under the debugger.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=273><TT><FONT FACE="Courier">DETAchED_PROCESS</FONT></TT>
</TD><TD WIDTH=317>The new process does not have access to the console of the calling process.
</TD></TR>
</TABLE></CENTER>
<P>
<P>
Once the <TT><FONT FACE="Courier">Process</FONT></TT> object has
been created, you can apply methods to it. The following methods
exist for the <TT><FONT FACE="Courier">Win32::Process</FONT></TT>
object.
<UL>
<LI><TT><FONT FACE="Courier">Kill($ExitCode)</FONT></TT>
<LI><TT><FONT FACE="Courier">Resume()</FONT></TT>
<LI><TT><FONT FACE="Courier">Suspend()</FONT></TT>
<LI><TT><FONT FACE="Courier">GetExitCode()</FONT></TT>
<LI><TT><FONT FACE="Courier">Wait($Timeout)</FONT></TT>
</UL>
<H3><A NAME="TheKillMethod">The <TT><FONT SIZE=4 FACE="Courier">Kill</FONT></TT><FONT SIZE=4>
Method</FONT></A></H3>
<P>
This method kills the process associated with the <TT><FONT FACE="Courier">Process</FONT></TT>
object. The syntax for this method is
<BLOCKQUOTE>
<TT><FONT FACE="Courier">Kill( $ExitCode).</FONT></TT>
</BLOCKQUOTE>
<P>
The <TT><FONT FACE="Courier">$ExitCode</FONT></TT> is the value
of the code that the process should return.
<H3><A NAME="TheResumeandSuspendMethods">The <TT><FONT SIZE=4 FACE="Courier">Resume</FONT></TT><FONT SIZE=4>
and </FONT><TT><FONT SIZE=4 FACE="Courier">Suspend</FONT></TT><FONT SIZE=4>
Methods</FONT></A></H3>
<P>
The <TT><FONT FACE="Courier">Suspend()</FONT></TT> method stops
a process from executing. To restart the thread, call the <TT><FONT FACE="Courier">Resume()</FONT></TT>
method.
<P>
The <TT><FONT FACE="Courier">Resume</FONT></TT> call can be used
to resume a process that was created with the <TT><FONT FACE="Courier">CREATE_SUSPENDED</FONT></TT>
flag.
<H3><A NAME="TheGetExitCodeFunction">The <TT><FONT SIZE=4 FACE="Courier">GetExitCode()</FONT></TT><FONT SIZE=4>
Function</FONT></A></H3>
<P>
The <TT><FONT FACE="Courier">GetExitCode()</FONT></TT> function
forces the <TT><FONT FACE="Courier">pObject</FONT></TT> to terminate
with its exit code set to the value of <TT><FONT FACE="Courier">$ExitCode</FONT></TT>.
The syntax for this function is
<BLOCKQUOTE>
<TT><FONT FACE="Courier">GetExitCode( $ExitCode );</FONT></TT>
</BLOCKQUOTE>
<P>
You can use <TT><FONT FACE="Courier">GetExitCode</FONT></TT> to
find out how or if a process has exited.
<H3><A NAME="TheWaitTimeoutFunction">The <TT><FONT SIZE=4 FACE="Courier">Wait($Timeout)</FONT></TT><FONT SIZE=4>
Function</FONT></A></H3>
<P>
The calling process can wait for a process to terminate with the
<TT><FONT FACE="Courier">Wait</FONT></TT> function. The syntax
for this call is
<BLOCKQUOTE>
<TT><FONT FACE="Courier">Wait($Timeout)</FONT></TT>
</BLOCKQUOTE>
<P>
The <TT><FONT FACE="Courier">$Timeout</FONT></TT> variable is
set to the number of milliseconds to wait for the process to end.
If the process wants to wait forever, use the keyword <TT><FONT FACE="Courier">INFINITE</FONT></TT>
for <TT><FONT FACE="Courier">$Timeout</FONT></TT>. The returned
value of <TT><FONT FACE="Courier">Wait()</FONT></TT> is <TT><FONT FACE="Courier">FALSE</FONT></TT>
if it times out and the error code in <TT><FONT FACE="Courier">$!</FONT></TT>
is set to <TT><FONT FACE="Courier">WAIT_FAILED</FONT></TT>. A
returned value of <TT><FONT FACE="Courier">TRUE</FONT></TT> indicates
that the process terminated before the <TT><FONT FACE="Courier">$Timeout</FONT></TT>
value.
<H3><A NAME="ClassPriority">Class Priority</A></H3>
<P>
The <TT><FONT FACE="Courier">GetPriorityClass</FONT></TT> and
<TT><FONT FACE="Courier">SetPriorityClass</FONT></TT> methods
provide control over the priority of the process. The priority
class of the process can be retrieved with a call to this method:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">GetPriorityClass($Priority )</FONT></TT>
</BLOCKQUOTE>
<P>
The returned value of <TT><FONT FACE="Courier">$Priority</FONT></TT>
is set to the priority class of the object this method is being
called on. <TT><FONT FACE="Courier">SetPriorityClass($Priority
)</FONT></TT><I> </I>is the opposite of the <TT><FONT FACE="Courier">GetPriorityClass()</FONT></TT>
function.
<P>
The <TT><FONT FACE="Courier">$Priority</FONT></TT> variable is
set to the priority to set the <TT><FONT FACE="Courier">Process</FONT></TT>
to. The value can be set to one of the following:<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD WIDTH=244><TT><FONT FACE="Courier">IDLE_PRIORITY_CLASS</FONT></TT>
</TD><TD WIDTH=346>Indicates a process whose threads run only when the system is idle.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=244><TT><FONT FACE="Courier">NORMAL_PRIORITY_CLASS</FONT></TT>
</TD><TD WIDTH=346>Standard process scheduling.</TD></TR>
<TR VALIGN=TOP><TD WIDTH=244><TT><FONT FACE="Courier">HIGH_PRIORITY_CLASS</FONT></TT>
</TD><TD WIDTH=346>Runs at a higher priority level.</TD></TR>
<TR VALIGN=TOP><TD WIDTH=244><TT><FONT FACE="Courier">REALTIME_PRIORITY_CLASS</FONT></TT>
</TD><TD WIDTH=346>Runs the process at the highest available priority. Be careful when using this value because you can lock up the machine with a runaway process.
</TD></TR>
</TABLE></CENTER>
<P>
<P>
Listing 10.2 shows you how to use the <TT><FONT FACE="Courier">Win32::Process</FONT></TT>
module.
<HR>
<BLOCKQUOTE>
<B>Listing 10.2. Using the </B><TT><B><FONT FACE="Courier">Win32::Process</FONT></B></TT><B>
module.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 use Win32;<BR>
2 use Win32::Process;<BR>
3<BR>
4 Win32::Process::Create($pobj,<BR>
5 "c:/dos/winzip32.exe",
<BR>
6 "", #
no command line arguments<BR>
7 0,
<BR>
8 DETAchED_PROCESS, #
run by yourself.<BR>
9 ".")
|| die "oops";<BR>
10<BR>
11 $pobj->SetPriorityClass(NORMAL_PRIORITY_CLASS) || die $!;
<BR>
12<BR>
13 $pobj->Wait(INFINITE) || die $!; # wait forever</FONT></TT>
</BLOCKQUOTE>
<HR>
<H2><A NAME="TheWin32SemaphoreUtilities"><FONT SIZE=5 COLOR=#FF0000>The
</FONT><TT><FONT SIZE=5 COLOR=#FF0000 FACE="Courier">Win32::Semaphore</FONT></TT><FONT SIZE=5 COLOR=#FF0000>
Utilities</FONT></A></H2>
<P>
The <TT><FONT FACE="Courier">Win32::Semaphore</FONT></TT> lets
a Perl script access the semaphore facility in Windows NT. The
<TT><FONT FACE="Courier">Create</FONT></TT> call in a <TT><FONT FACE="Courier">Semaphore</FONT></TT>
module lets you create a semaphore object. The semaphore in an
NT machine works in the same manner as in UNIX. For more information
on how to use semaphores, see <A HREF="ch13.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/ch13.htm" >Chapter 13</A>,
"Messaging Facilities: The System V Ipc Functions."
The syntax for the <TT><FONT FACE="Courier">Create</FONT></TT>
function is
<BLOCKQUOTE>
<TT><FONT FACE="Courier">Create ($semaphoreObj,$initialCount,$maxCount,$name);</FONT></TT>
</BLOCKQUOTE>
<P>
The returned value from the <TT><FONT FACE="Courier">Create</FONT></TT>
call is placed to what the <TT><FONT FACE="Courier">$semaphoreObj</FONT></TT>
references. The <TT><FONT FACE="Courier">$initialCount</FONT></TT>
variable is used to set the initial count of the semaphore object.
The maximum count of the semaphore is set in <TT><FONT FACE="Courier">$maxCount</FONT></TT>.
The semaphore can be identified in the NT system by the value
in <TT><FONT FACE="Courier">$name</FONT></TT>.
<P>
The <TT><FONT FACE="Courier">DESTROY</FONT></TT> method removes
a semaphore object specified in the argument to itself. The syntax
is
<BLOCKQUOTE>
<TT><FONT FACE="Courier">DESTROY( $SemaphoreObj );</FONT></TT>
</BLOCKQUOTE>
<P>
The <TT><FONT FACE="Courier">DESTROY</FONT></TT> method is automatically
called by Perl when the <TT><FONT FACE="Courier">$SemaphoreObject</FONT></TT>
scalar goes out of scope.
<P>
The <TT><FONT FACE="Courier">Wait( $TimeOut )</FONT></TT> function
is used to wait for a timeout for the value in <TT><FONT FACE="Courier">$TimeOut</FONT></TT>.
The value in <TT><FONT FACE="Courier">$Timeout</FONT></TT> is
specified in milliseconds.
<P>
The <TT><FONT FACE="Courier">Wait</FONT></TT> method makes the
calling process wait on the semaphore. If the <TT><FONT FACE="Courier">Semaphore</FONT></TT>
is not released in <TT><FONT FACE="Courier">$TimeOut</FONT></TT>
milliseconds, the call returns, and the return value should be
checked. For no timeout value, use the predefined constant <TT><FONT FACE="Courier">INFINITE</FONT></TT>.
<P>
Once you are done with a semaphore, you should release it to let
other processes access it. To release a semaphore, you have to
make a call to
<BLOCKQUOTE>
<TT><FONT FACE="Courier">Release( $ReleaseCount, $lastCount);</FONT></TT>
</BLOCKQUOTE>
<P>
The value in <TT><FONT FACE="Courier">$ReleaseCount</FONT></TT>
is incremented by the call to <TT><FONT FACE="Courier">Release</FONT></TT>.
The <TT><FONT FACE="Courier">Release</FONT></TT> method releases
a semaphore after it increments the count. The <TT><FONT FACE="Courier">$LastCount</FONT></TT>
variable contains the last value.
<P>
Listing 10.3 illustrates how to use the <TT><FONT FACE="Courier">Semaphore</FONT></TT>
module in NT. Note how the <TT><FONT FACE="Courier">ErrorMsg</FONT></TT>
subroutine displays the readable message from the <TT><FONT FACE="Courier">GetLastError()</FONT></TT>
call.
<HR>
<BLOCKQUOTE>
<B>Listing 10.3. Using the </B><TT><B><FONT FACE="Courier">Win32::Semaphore</FONT></B></TT><B>
object.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 use Win32::Semaphore;<BR>
2 use Win32;<BR>
3<BR>
4 my $sObject;<BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -