开发者

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.

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:

is it possible to capture exit codes outside the range of 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开发者_JAVA技巧 error code or somesuch.


Yes, Win32::Process can return the full signed 32-bit exit code. Use the GetExitCode method. 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). 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. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜