📄 ch22_14.htm
字号:
<html><head><title>Win32::Process (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch22_13.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch22_15.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">22.14. Win32::Process</h2><p><a name="INDEX-3340" /><a name="INDEX-3341" /><a name="INDEX-3342" />This module provides access to extendedWin32 process creation and management abilities. Process objects arecreated with the <tt class="literal">Create</tt> method (the constructor).Additional methods can be used on objects to kill, suspend, resume,and set the priorities of processes.</p><p>The <tt class="literal">Create</tt> constructor has the following syntax: </p><blockquote><pre class="code">Win32::Process->Create($<em class="replaceable"><tt>Proc</em>, <em class="replaceable">app</em>, <em class="replaceable">cmnd</em>, <em class="replaceable">hndls</em>, <em class="replaceable">opts</em>, <em class="replaceable">dir</tt></em>)</pre></blockquote><p>The arguments to <tt class="literal">Create</tt> are as follows:</p><dl><dt><b><tt class="literal">$</tt><em class="replaceable">Proc</em></b></dt><dd>Name of the reference for the created process object.</p></dd><dt><i><em class="replaceable"><tt>app</tt></em></i></dt><dd>Full pathname of the executable.</p></dd><dt><i><em class="replaceable"><tt>cmnd</tt></em></i></dt><dd>Command line for executable.</p></dd><dt><i><em class="replaceable"><tt>hndls</tt></em></i></dt><dd>Determines handle inheritance. A value of <tt class="literal">1</tt> turnson inheritance; a <tt class="literal">0</tt> value turns it off.</p></dd><dt><i><em class="replaceable"><tt>opts</tt></em></i></dt><dd>Sets options to implement when the process is created. The availableoptions for this argument are listed below.</p></dd><dt><i><em class="replaceable"><tt>dir</tt></em></i></dt><dd>The working directory for the executable.</p></dd></dl><p>The process is created by passing the command line in<em class="replaceable"><tt>cmnd</tt></em> to the executable named in<em class="replaceable"><tt>app</tt></em>. For example, a process object for atext file running in Notepad is created like this:</p><blockquote><pre class="code">use Win32::Process;Win32::Process->Create($proc, 'C:\\windows\\Notepad.exe', "Notepad perlnut.txt", 1, DETACHED_PROCESS, ".");</pre></blockquote><p>The process creation options given by the<em class="replaceable"><tt>opts</tt></em> argument to <tt class="literal">Create</tt>are:</p><dl><dt><b><tt class="literal">CREATE_DEFAULT_ERROR_MODE</tt> </b></dt><dd>Gives the process the default error mode.</p></dd><dt><b><tt class="literal">CREATE_NEW_CONSOLE</tt> </b></dt><dd>Creates a new console for the process. Can't be usedwith <tt class="literal">DETACHED_PROCESS</tt>.</p></dd><dt><b><tt class="literal">CREATE_NO_CONSOLE</tt> </b></dt><dd>Creates a new process, but without running the process in a new andvisible window. That is, a new process will be created, but in thebackground.</p></dd><dt><b><tt class="literal">CREATE_NEW_PROCESS_GROUP</tt> </b></dt><dd>Creates process as root of a new process group. </p></dd><dt><b><tt class="literal">CREATE_SEPARATE_WOW_VDM</tt> </b></dt><dd>Runs process in its own Virtual DOS Machine (VDM). Only applicable to16-bit applications.</p></dd><dt><b><tt class="literal">CREATE_SUSPENDED</tt> </b></dt><dd>Starts process in a suspended state. The process can be started withthe <tt class="literal">Resume</tt> method.</p></dd><dt><b><tt class="literal">CREATE_UNICODE_ENVIRONMENT</tt> </b></dt><dd>Uses Unicode characters in the environment block of the new process.</p></dd><dt><b><tt class="literal">DEBUG_PROCESS</tt> </b></dt><dd>Debugs the new process with the calling process.</p></dd><dt><b><tt class="literal">DEBUG_ONLY_THIS_PROCESS</tt> </b></dt><dd>Doesn't debug the new process if calling process isbeing debugged.</p></dd><dt><b><tt class="literal">DETACHED_PROCESS</tt> </b></dt><dd>Creates a process with no access to the console of the callingprocess.</p></dd></dl><a name="perlnut2-CHP-22-SECT-14.1" /><div class="sect2"><h3 class="sect2">22.14.1. Win32::Process Methods</h3><p>The following methods are provided for objects of created byWin32::Process.</p><a name="INDEX-3343" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>GetExitCode</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">proc</em>->GetExitCode($<em class="replaceable">ref</em>)</pre><p><a name="INDEX-3343" />Gets the exit code of aprocess and saves it to<tt class="literal">$</tt><em class="replaceable"><tt>ref</tt></em>.</p></div><a name="INDEX-3344" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>GetPriorityClass</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">proc</em>->GetPriorityClass($<em class="replaceable">ref</em>)</pre><p><a name="INDEX-3344" />Gets the priority class ofthe process and stores it in<tt class="literal">$</tt><em class="replaceable"><tt>ref</tt></em>.</p></div><a name="INDEX-3345" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>Kill</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">proc</em>->Kill(<em class="replaceable">exitcode</em>)</pre><p><a name="INDEX-3345" />Kills the process with thegiven <em class="replaceable"><tt>exitcode</tt></em>, which is returned by theprocess.</p></div><a name="INDEX-3346" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>Resume</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">proc</em>->Resume</pre><p><a name="INDEX-3346" />Resumes a suspended process.This method can also be used on processes created with the<tt class="literal">CREATE_SUSPENDED</tt> flag.</p></div><a name="INDEX-3347" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>SetPriorityClass</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">proc</em>->SetPriorityClass($<em class="replaceable">priority</em>)</pre><p><a name="INDEX-3347" />Sets the priority class ofthe object to<tt class="literal">$</tt><em class="replaceable"><tt>priority</tt></em>. The prioritycan be one of the following:</p><dl><dt><b><tt class="literal">IDLE_PRIORITY_CLASS</tt></b></dt><dd>A process whith threads that run only when the system is idle</p></dd><dt><b><tt class="literal">NORMAL_PRIORITY_CLASS</tt></b></dt><dd>A process with normal scheduling</p></dd><dt><b><tt class="literal">HIGH_PRIORITY_CLASS</tt></b></dt><dd>A process that performs time-critical tasks that must be executedimmediately</p></dd><dt><b><tt class="literal">REALTIME_PRIORITY_CLASS</tt></b></dt><dd>The highest-priority process, even preempts operating-system threads</p></dd></dl></div><a name="INDEX-3348" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>Suspend</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">proc</em>->Suspend</pre><p><a name="INDEX-3348" />Suspends the process.</p></div><a name="INDEX-3349" /><a name="INDEX-3350" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>Wait</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">proc</em>->Wait(<em class="replaceable">n</em>)</pre><p><a name="INDEX-3349" />Waits<em class="replaceable"><tt>n</tt></em> milliseconds for the process to exit. Ifthe process times out, the method returns false and sets<tt class="literal">$!</tt> to <tt class="literal">WAIT_FAILED</tt>. For notimeout, sets <em class="replaceable"><tt>n</tt></em> to<tt class="literal">INFINITE</tt>.<a name="INDEX-3350" /> </p></div></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch22_13.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch22_15.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">22.13. Win32::Pipe</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">22.15. Win32::Registry</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -