Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 error code or somesuch.

like image 546
Martin Ba Avatar asked Dec 27 '22 17:12

Martin Ba


1 Answers

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();
like image 156
cjm Avatar answered Jan 09 '23 12:01

cjm