📄 ch22_19.htm
字号:
<html><head><title>Win32 Extensions (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_18.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="ch23_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">22.19. Win32 Extensions</h2><p>In addition to the modules listed previously, Perl for Win32 can usean additional set of functions from the Win32 extension. Thesefunctions provide useful tools for some Windows-specific tasks thatdon't require their own modules. They are exportedfrom the Win32 package with:</p><blockquote><pre class="code">use Win32;</pre></blockquote><p>Many of these functions do not take arguments and return the value(or values) of the requested information, unless otherwise noted.</p><dl><dt><b><tt class="literal">Win32::CopyFile(</tt><em class="replaceable">from</em><tt class="literal">,</tt> <em class="replaceable">to</em><tt class="literal">,</tt> <em class="replaceable">overwrite</em><tt class="literal">)</tt></b></dt><dd><a name="INDEX-3395" />Copiesan existing file to a new file, while preserving all attributes ofthe original file, except the security information. If<em class="replaceable"><tt>to</tt></em> exists, overwriting will take placeonly if <em class="replaceable"><tt>overwrite</tt></em> is true. Read-only fileswill never be overwritten; they will need to be<tt class="literal">unlink</tt> ed first.</p></dd><dt><b><tt class="literal">Win32::FreeLibrary(</tt><em class="replaceable">handle</em><tt class="literal">)</tt></b></dt><dd><a name="INDEX-3396" />Unloadsa previously loaded DLL. <em class="replaceable"><tt>handle</tt></em> is nolonger valid after <tt class="literal">FreeLibrary( )</tt> is called.</p></dd><dt><b><tt class="literal">Win32::GetArchName( )</tt></b></dt><dd><a name="INDEX-3397" />Hasbeen deprecated in favor of<tt class="literal">$ENV{PROCESSOR_ARCHITECTURE}</tt>.</p></dd><dt><b><tt class="literal">Win32::GetChipName( )</tt></b></dt><dd><a name="INDEX-3398" />Returnsthe processor type of the machine under which the perl process isrunning. This string will be represented as 386, 486, 586, etc.</p></dd><dt><b><tt class="literal">Win32::GetFullPathName(</tt><em class="replaceable">filename</em><tt class="literal">)</tt></b></dt><dd><a name="INDEX-3399" />Combines<em class="replaceable"><tt>filename</tt></em> with drive number and directoryname. In a list context, <tt class="literal">GetFulllPathName( )</tt>returns <em class="replaceable"><tt>path</tt></em> and<em class="replaceable"><tt>file</tt></em>.</p></dd><dt><b><tt class="literal">Win32::GetLastError( )</tt></b></dt><dd><a name="INDEX-3400" />Returnsthe last error value generated by a call to a Win32 API function.</p></dd><dt><b><tt class="literal">Win32::GetLongPathName(</tt><em class="replaceable">pathname</em><tt class="literal">)</tt></b></dt><dd><a name="INDEX-3401" />Returns<em class="replaceable"><tt>pathname</tt></em> as composed of longnamecomponents.</p></dd><dt><b><tt class="literal">Win32::GetProcAddress(</tt><em class="replaceable">instance</em><tt class="literal">,</tt> <em class="replaceable">procname</em><tt class="literal">)</tt></b></dt><dd><a name="INDEX-3402" />Returnsthe address of a function inside of a loaded library. Since<tt class="literal">GetProcAddress( )</tt> is deprecated, you should useWin32::API instead.</p></dd><dt><b><tt class="literal">Win32::LoadLibrary(</tt><em class="replaceable">libname</em><tt class="literal">)</tt></b></dt><dd><a name="INDEX-3403" />Loadsa DLL into memory and returns its module handle. You can then use thereturned module handle with Win32::GetProcAddress andWin32::FreeLibrary.</p></dd><dt><b><tt class="literal">Win32::MsgBox(</tt><em class="replaceable">message</em><tt class="literal">, [,</tt> <em class="replaceable">flags</em><tt class="literal">, [,</tt> <em class="replaceable">title</em><tt class="literal">]])</tt></b></dt><dd><a name="INDEX-3404" />Createsa dialog box that contains <em class="replaceable"><tt>message</tt></em>.<em class="replaceable"><tt>title</tt></em> specifies an optional window title.<tt class="literal">FLAGS</tt> specifies the required icons according tothe following:</p><blockquote><pre class="code">0 = OK1 = OK and Cancel2 = Abort, Retry, and Ignore3 = Yes, No and Cancel4 = Yes and No5 = Retry and CancelMB_ICONSTOP creates an "X" in a red circleMB_ICONQUESTION creates a question mark in a bubbleMB_ICONEXCLAMATION creates an exclamation mark in a yellow triangleMB_ICONINFORMATION creates an "i" in a bubble</pre></blockquote><p><tt class="literal">MsgBox( )</tt> returns the menu ID of the selectedpushbutton:</p><blockquote><pre class="code">0 Error1 OK2 Cancel3 Abort4 Retry5 Ignore6 Yes7 No</pre></blockquote></dd><dt><b><tt class="literal">Win32::OLELastError( )</tt></b></dt><dd><a name="INDEX-3405" />Returnsthe last error value generated by a call to a Win32 OLE API function.</p></dd><dt><b><tt class="literal">Win32::BuildNumber( )</tt></b></dt><dd><a name="INDEX-3406" />Returnsthe build number of Perl for Win32.</p></dd><dt><b><tt class="literal">Win32::LoginName( )</tt></b></dt><dd><a name="INDEX-3407" />Returnsthe username of the owner of the current Perl process.</p></dd><dt><b><tt class="literal">Win32::NodeName( )</tt></b></dt><dd><a name="INDEX-3408" />Returnsthe Microsoft network node name of the current machine.</p></dd><dt><b><tt class="literal">Win32::DomainName( )</tt></b></dt><dd><a name="INDEX-3409" />Returnsthe name of the Microsoft network domain that the owner of thecurrent Perl process is logged into.</p></dd><dt><b><tt class="literal">Win32::FsType( )</tt></b></dt><dd><a name="INDEX-3410" />Returnsa string naming the filesystem type of the currently active drive.</p></dd><dt><b><tt class="literal">Win32::GetCwd( )</tt></b></dt><dd><a name="INDEX-3411" />Returnsthe current active drive and directory. This function does not returna UNC path, since the functionality required for such a feature isnot available under Windows 95.</p></dd><dt><b><tt class="literal">Win32::SetCwd(</tt><em class="replaceable">newdir</em>)</b></dt><dd><a name="INDEX-3412" />Setsthe current active drive and directory to<em class="replaceable"><tt>newdir</tt></em>. This function does not work withUNC paths, since the functionality required for such a feature is notavailable under Windows 95.</p></dd><dt><b><tt class="literal">Win32::SetLastError(</tt><em class="replaceable">error</em><tt class="literal">)</tt></b></dt><dd><a name="INDEX-3413" />Setsthe value of the last error encountered to<em class="replaceable"><tt>error</tt></em>. Does not work under Windows 95.</p></dd><dt><b><tt class="literal">Win32::GetOSVersion( )</tt></b></dt><dd><a name="INDEX-3414" />Returnsa list of elements describing the version of the operating system.The elements of the list are an arbitrary descriptive string; themajor version number of the operating system; the minor versionnumber; the build number; and a digit indicating the actual operatingsystem, which will be <tt class="literal">0</tt> for Win32s,<tt class="literal">1</tt> for Windows 95, and <tt class="literal">2</tt> forWindows NT. For example:</p><blockquote><pre class="code">use Win32;($string, $major, $minor, $build, $id) = Win32::GetOSVersion( );</pre></blockquote></dd><dt><b><tt class="literal">Win32::FormatMessage(</tt><em class="replaceable">error</em>)</b></dt><dd><a name="INDEX-3415" />Convertsthe Win32 error number supplied by <em class="replaceable"><tt>error</tt></em>into a descriptive string. The error number can be retrieved using<tt class="literal">Win32::GetLastError</tt> or<tt class="literal">Win32::OLELastError</tt>.</p></dd><dt><b><tt class="literal">Win32::Spawn(</tt><em class="replaceable">command</em>, <em class="replaceable">args</em>, <tt class="literal">$</tt><em class="replaceable">pid</em>)</b></dt><dd><a name="INDEX-3416" />Spawnsa new process for the given <em class="replaceable"><tt>command</tt></em>,passing the arguments in <em class="replaceable"><tt>args</tt></em>. The ID ofthe new process is saved in the variable named by<em class="replaceable"><tt>pid</tt></em>.</p></dd><dt><b><tt class="literal">Win32::LookupAccountName(</tt><em class="replaceable">sys</em>, <em class="replaceable">acct</em>, <tt class="literal">$</tt><em class="replaceable">domain</em>, <tt class="literal">$</tt><em class="replaceable">sid</em>, <tt class="literal">$</tt><em class="replaceable">type</em>)</b></dt><dd><a name="INDEX-3417" />Returnsthe domain name, SID, and SID type to the specified variables for theaccount <em class="replaceable"><tt>acct</tt></em> on system<em class="replaceable"><tt>sys</tt></em>.</p></dd><dt><b><tt class="literal">Win32::LookupAccountSID(</tt><em class="replaceable">sys</em>, <em class="replaceable">sid</em>, <tt class="literal">$</tt><em class="replaceable">acct</em>, <tt class="literal">$</tt><em class="replaceable">domain</em>, <tt class="literal">$</tt><em class="replaceable">type</em>)</b></dt><dd><a name="INDEX-3418" />Returnsthe account name, domain name, and SID type to the specifiedvariables for the SID <em class="replaceable"><tt>sid</tt></em> on system<em class="replaceable"><tt>sys</tt></em>.</p></dd><dt><b><tt class="literal">Win32::InitiateSystemShutdown(</tt><em class="replaceable">machine</em>, <em class="replaceable">message</em>, <em class="replaceable">timeout</em>, <em class="replaceable">forceclose</em>, <em class="replaceable">reboot</em>)</b></dt><dd><a name="INDEX-3419" />Shutsdown the specified <em class="replaceable"><tt>machine</tt></em> in thespecified <em class="replaceable"><tt>timeout</tt></em> interval.<em class="replaceable"><tt>message</tt></em> is broadcast to all users. If<em class="replaceable"><tt>forceclose</tt></em> is true, all documents areclosed (forcefully) without prompting the user. If<em class="replaceable"><tt>reboot</tt></em> is true, the machine is rebooted.</p></dd><dt><b><tt class="literal">Win32::AbortSystemShutdown(</tt><em class="replaceable">machine</em>)</b></dt><dd><a name="INDEX-3420" />Abortsa shutdown on the specified <em class="replaceable"><tt>machine</tt></em>.</p></dd><dt><b><tt class="literal">Win32::GetTickCount( )</tt></b></dt><dd><a name="INDEX-3421" />Returnsthe Win32 tick count.</p></dd><dt><b><tt class="literal">Win32::IsWinNT( )</tt></b></dt><dd><a name="INDEX-3422" />Returnstrue (nonzero) if the Win32 subsystem is Windows NT.</p></dd><dt><b><tt class="literal">Win32::IsWin95( )</tt></b></dt><dd><a name="INDEX-3423" />Returnstrue (nonzero) if the Win32 subsystem is Windows 95.</p></dd><dt><b><tt class="literal">Win32::ExpandEnvironmentStrings(</tt><em class="replaceable">envstring</em>)</b></dt><dd><a name="INDEX-3424" />Returnsa string in which any environment variables in the given<em class="replaceable"><tt>envstring</tt></em> are replaced with their values.</p></dd><dt><b><tt class="literal">Win32::GetShortPathName(</tt><em class="replaceable">longpathname</em>)</b></dt><dd><a name="INDEX-3425" />Returnsthe short (8.3) pathname of <em class="replaceable"><tt>longpathname</tt></em>.</p></dd><dt><b><tt class="literal">Win32::GetNextAvailDrive( )</tt></b></dt><dd><a name="INDEX-3426" />Returnsa string in the form of <em class="replaceable"><tt>d:\</tt></em>, in which<em class="replaceable"><tt>d</tt></em> is the first available drive letter.</p></dd><dt><b><tt class="literal">Win32::RegisterServer(</tt><em class="replaceable">libraryname</em>)</b></dt><dd><a name="INDEX-3427" />Loadsthe DLL <em class="replaceable"><tt>libraryname</tt></em> and calls the function<tt class="literal">DllRegisterServer</tt>.</p></dd><dt><b><tt class="literal">Win32::UnregisterServer(</tt><em class="replaceable">libraryname</em>)</b></dt><dd><a name="INDEX-3428" />Loadsthe DLL <em class="replaceable"><tt>libraryname</tt></em> and calls the function<tt class="literal">DllUnregisterServer</tt>.</p></dd><dt><b><tt class="literal">Win32::Sleep(</tt><em class="replaceable">time</em>)</b></dt><dd><a name="INDEX-3429" />Pausesfor the number of milliseconds specified by<em class="replaceable"><tt>time</tt></em>.</p></dd></dl><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch22_18.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="ch23_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">22.18. Win32::Shortcut</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">23. OLE Automation</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 + -