Domanda

Why does the following output 0 and not 4?

use strict;
use warnings;
use feature qw( say );

use Win32::Process qw( NORMAL_PRIORITY_CLASS INFINITE );

Win32::Process::Create(my $ProcessObj,
   $ENV{COMSPEC},
   $ENV{COMSPEC}.' /c start /min perl -e"sleep 5; exit 4"',
   0,
   NORMAL_PRIORITY_CLASS,
   '.',
)
   or die $^E;

$ProcessObj->Wait(INFINITE);
$ProcessObj->GetExitCode(my $exit_code);
say $exit_code;  # 0
È stato utile?

Soluzione

The program you are executing is cmd. Its exit code is the exit code of the last command it executes.

The last program cmd executes is start, which launches a program and exits. Its exit code is not the exit code of the program it launched. How could it be? It might not even have started executing yet, much less exit.

To best way to get the script's exit code is if it's your own child. Don't use cmd and start; launch the program directly with the CREATE_NEW_CONSOLE flag. I don't know how to minimize it, though, but you'll have to search for that. (We can help you create a Perl solution if none exist and you find out how to do it in C.)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top