jimcadd has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I'm in a perl script trying to get a return code from an .exe in cygwin.
All I can get is an 8bit return code (-128) --> (127) a signed byte.

$output = `$bcr_exe $parameters`;
$rc = ($? >> 8);

Perl gets the return code back through the special variable "$?" which
is 16bits but gets shifted 8bits (>>8) to retrieve the return code.

Is there a way that anyone knows of that I can get a larger return code
(ie. a short 16bit or a long 32bit)?

This perl script is a converted batch program that used %ERRORLEVEL%.
Apparently it could return values greater than 8 bits.

Any help is greatly appreciated!

Replies are listed 'Best First'.
Re: Need a 16 bit returncode from .exe
by BrowserUk (Patriarch) on Mar 18, 2008 at 20:11 UTC

    Here's a hackish, possible solution. From perl, invoke a small .CMD wrapper that calls the program and then echoes %ERRORLEVEL% when the command terminates.

    In your perl code, assign the return from the backticks to an array. When the command completes, the return code will be the last line of that array:

    The .cmd wrapper:

    rem I called this errorlevel.cmd @%1 %2 %3 %4 %5 %6 %7 %8 %9 @echo %ERRORLEVEL%

    An example use:

    my @out = `errorlevel.cmd perl -le"print for 1 .. 10; exit 12345;"`;; print pop @out;; 12345 print join '', @out;; 1 2 3 4 5 6 7 8 9 10

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Need a 16 bit returncode from .exe
by samtregar (Abbot) on Mar 18, 2008 at 17:54 UTC
    You might try running the process with Win32::Process and using GetExitCode() to get the exit code. The docs don't state the range returned but I think there's a good chance it's 16-bit if that's what Windows usually returns.

    -sam

Re: Need a 16 bit returncode from .exe
by cdarke (Prior) on Mar 18, 2008 at 18:33 UTC
    I just tried samtregar's suggestion with a gash C .exe that exits with the return code of the argument (don't ask). Thought it might be helpful if I posted the code:
    use warnings; use strict; use Win32::Process; use Win32; my $code = 'C:\\Perl Modules\\Win32\\ExitCode\\Debug\\FileClose.exe'; my $ProcessHandle; Win32::Process::Create ( $ProcessHandle, $code, qq("$code" 1289), # Note syntax because path has an embedded space + 0, NORMAL_PRIORITY_CLASS, ".") || die "Oops: $^E"; $ProcessHandle->Wait(INFINITE); my $ExitCode; $ProcessHandle->GetExitCode($ExitCode); print "Exit code: $ExitCode\n";
    The perl program correctly reports an exit code of 1289. By the way, I tried the C program using ExitProcess and exit() (C RTL) and both worked. The MSDN gives the return type in ExitProcess as UINT (unsigned int), which is 32 bits.
        Yes, you are right BrowserUk. It should be easy to change the std file handles in the STARTUPINFO block to CreateProcess, but Win32::Process::Create does not expose that.
        BrowserUk, you kind-o tailed off on your post...
        Are you saying this solution won't work?
        I do need the output and the rc.
        Thanks.
Re: Need a 16 bit returncode from .exe
by jimcadd (Novice) on Mar 18, 2008 at 21:11 UTC

    Thanks to everyone for their help with this issue!!!

    Sam for getting the ball rolling...

    cdarke for the good example code. I got this to work however with the problem
    of the output.

    Finally BrowserUk...Your little wrapper worked like a charm!
    It may be a hack, but it's a working solution!!!

    Thanks Again Guys! Monks Rule!
Re: Need a 16 bit returncode from .exe
by jimcadd (Novice) on Mar 18, 2008 at 19:31 UTC
    I do need the output from the .exe (as well as the retcode)... So... you're saying this solution won't work for me?