2

I have a perl file that calls a batch file to do an installation of a 3rd party program.

Main.pl

system ("Installer.bat");
print "Error code is $? \n";

Installer.bat

@echo off
installer.exe
echo errorlevel is %errorlevel% > logfile
exit %errorlevel%

The error code 3010 is returned by the batch file and it suggests that a restart is required. However, the perl module prints 49664. I thought it was supposed to print 3010. Could someone please explain how this works? I want to get the error code for a restart required in my perl code and then do some cleanup work and restart the machine from the perl module.

The following related points are also not clear. - Windows batch files only have 255 error codes, so how can 3010 be returned as a error code? - This place suggests that we need to right shift the error code by 8 bits to get the native error code. So if I right shift 49664 by 8, I get 194 (which is still not the same as 3010). However I also note that 3010 Mod 256 = 194

Santhosh
  • 6,547
  • 15
  • 56
  • 63
  • Have you tried [${^CHILD_ERROR_NATIVE}](http://perldoc.perl.org/perlvar.html) or [Win32::Process](http://search.cpan.org/~jdb/Win32-Process-0.14/Process.pm)? – Mikel Feb 25 '11 at 10:56
  • 1
    You're comment about 3010 mod 256 has mostly answered your own question. You're getting 8 bits of the answer back at the moment. The answer by Oesor therefore is probably what you need to do to get the full value back. – Colin Newell Feb 25 '11 at 15:19

2 Answers2

3

Per http://search.cpan.org/perldoc?IPC::System::Simple:

As of IPC::System::Simple v0.06, the run subroutine when called with multiple arguments will make available the full 32-bit exit value on Win32 systems. This is different from the previous versions of IPC::System::Simple and from Perl's in-build system() function, which can only handle 8-bit return values.

DVK
  • 126,886
  • 32
  • 213
  • 327
Oesor
  • 6,632
  • 2
  • 29
  • 56
0

Exit codes in batch files are broken, exit %errorlevel% will set the exit code for the batch file, but not the process!

@echo off
set err=3010
@%COMSPEC% /C exit %err% >nul

This will set the exit code of the process (cmd.exe probably) to 3010.

Community
  • 1
  • 1
Anders
  • 97,548
  • 12
  • 110
  • 164