简体   繁体   English

是否可以使用 perl 处理退出代码 > 255?

[英]Is it possible to process exit codes > 255 with perl?

First, find a little background about exit code in perl ( also here )and on Windows .首先, 在 perl也在此处)和Windows中找到有关退出代码的一些背景知识。

Now - when I execute another process from a perl script (I'm open as to the method, qx / open / system / exec / IPC::Run , etc.) on Windows :现在 - 当我从 Windows 上的perl脚本(我对方法、 qx / open / system / exec / IPC::Run等)执行另一个进程时:

is it possible to capture exit codes outside the range of 0 - 255 ?是否可以捕获0 - 255范围之外的退出代码?

On Windows, a process can return a full (signed) 32bit exit code, and it's not so uncommon to have something return 0x8...0... , that is, some COM error code or somesuch.在 Windows 上,一个进程可以返回一个完整的(签名的)32 位退出代码,并且返回0x8...0...的东西并不少见,也就是说,一些 COM 错误代码或类似的东西。

Yes, Win32::Process can return the full signed 32-bit exit code.是的, Win32::Process可以返回完整的签名 32 位退出代码。 Use the GetExitCode method.使用GetExitCode方法。 But it's a little tricky, because the return value is not the exit code (it's the return value of the Windows GetExitCodeProcess function, which indicates success or failure of the function).但是有点棘手,因为返回值不是退出码(是Windows GetExitCodeProcess function的返回值,表示函数成功或失败)。 The exit code gets stored into the variable you pass to the method.退出代码存储在您传递给方法的变量中。

use Win32::Process;
use Win32;

sub ErrorReport{
    print Win32::FormatMessage( Win32::GetLastError() );
}

my $ProcessObj;
Win32::Process::Create($ProcessObj,
                       "C:\\winnt\\system32\\notepad.exe",
                       "notepad temp.txt",
                       0,
                       NORMAL_PRIORITY_CLASS,
                       ".") or die ErrorReport();

$ProcessObj->Wait(INFINITE);
my $exitCode;
$ProcessObj->GetExitCode($exitCode) or die ErrorReport();

It's possible but it's not simple.这是可能的,但并不简单。

The Win32::API module can expose the Windows API to Perl scripts. Win32::API模块可以将 Windows API 暴露给 Z0114AD06D728F1834E36FE1A397 脚本。 Use it to create a code reference for the GetExitCodeProcess function , invoke it with the process identifier of the dead program, and unpack the result.使用它为GetExitCodeProcess function创建代码引用,使用死程序的进程标识符调用它,然后解压缩结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM