How to? Get the return code from an external program...

May 24, 2010
855
0
Northlake, Il
I can't help but feel I'm missing something obvious here, but I don't know what that is. As usual, a TCC session transcript.
Code:
   Mon  Jan 14, 2013  10:00:06p

TCC  14.03.53 x64   Windows 7 [Version 6.1.7601]
Copyright 2012 JP Software Inc.  All Rights Reserved
Registered to Daniel Mathews

[Z:\]CD "Development\C, C++\Utilities\DuplicateRemover\Release"

[Z:\Development\C, C++\Utilities\DuplicateRemover\Release]DuplicateRemover /A /B
Fatal Error: Invalid switch "/A"
Fatal Error: Invalid switch "/B"
Program is being terminated due to error(s).
[Z:\Development\C, C++\Utilities\DuplicateRemover\Release]Echo %_? %?
0 0

[Z:\Development\C, C++\Utilities\DuplicateRemover\Release]Echo %@ExecStr[DuplicateRemover /A /B] >NUL:
Fatal Error: Invalid switch "/A"
Fatal Error: Invalid switch "/B"
Program is being terminated due to error(s).
[Z:\Development\C, C++\Utilities\DuplicateRemover\Release]Echo %_ExecStr
8

[Z:\Development\C, C++\Utilities\DuplicateRemover\Release]
The first time I just ran it directly from the command line with invalid arguments and it printed an error message and exited, presumably with a return code of 8. However, both %_?, which you would expect, and %?, which you would not, are zero.

So I ran it again with exactly the same arguments through @ExecStr and then checked %_ExecStr, and it was 8 as expected.

Unfortunately, it would be really nice for a calling batch file to have access to the return code to detect error(s), and @ExecStr and @ExecArray are both not satisfactory because the program is able to output an arbitrarily large number of lines.

The questions, therefore, are simple. What am I doing wrong when executing it directly from the command line? What do I misunderstand? And if I am misunderstanding something, how do I accomplish what I am trying to accomplish?
 
May 20, 2008
11,400
99
Syracuse, NY, USA
Not a clue, Dan. %? seems to be working correctly (see far below). Is DuplicateRemover.exe a subsystem:console application? TCC doesn't, by default, wait for subsystem:windows apps to end. Can you pare it down to something small that misbehaves?
Code:
l:\projects\return\release> type ..\rv.cpp
#include <windows.h>
#include <stdio.h>
 
INT wmain ( INT argc, WCHAR **argv )
{
        if ( argc < 2 )
                return 666;
        INT rv = _wtoi(argv[1]);
        wprintf(L"%d\n", rv);
        return rv;
}
 
l:\projects\rv\release> do i=1 to 5 ( rv %i & echo %? )
1
1
2
2
3
3
4
4
5
5
 
May 24, 2010
855
0
Northlake, Il
Vince, it's just a pure single-threaded console-only app, nothing at all Windows GUI related, not even message boxes. Tomorrow I'm going to cut out almost all of the code and add back it step-by-step until I find the point where it breaks. (But I have a hard time even understanding how this could happen at all given that @ExecStr catches the condition code correctly.)
 
May 24, 2010
855
0
Northlake, Il
I found the problem! And it was just a stupid mistake on my part (so stupid I'm embarrassed to post it here) so never mind! (If there's any demand to see what the problem was I will post it here; however the chances of anyone else making this mistake a quite low.)
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,461
88
Albuquerque, NM
prospero.unm.edu
I found the problem! And it was just a stupid mistake on my part (so stupid I'm embarrassed to post it here) so never mind! (If there's any demand to see what the problem was I will post it here; however the chances of anyone else making this mistake a quite low.)

Running a program in PRE_INPUT / PRE_EXEC / POST_EXEC / your prompt?
 
Aug 2, 2011
258
4
Berlin, Germany
... If there's any demand to see what the problem was I will post it here ...
Come on, let us know. I have made stupid errors as well. Perhaps I can learn something from it.
 
May 24, 2010
855
0
Northlake, Il
All right, all right, I've given in to the pressure. I've had a PRE_INPUT alias for quite a long time and it (originally!) worked OK because it saved the return code on entry and quit with it when it was done processing. I made a change to the batch file where I wiped out the return code because I forgot about it.
 
Similar threads
Thread starter Title Forum Replies Date
A Inconsistent return code for TCC scripts Support 4
vefatica Is EOF an implied RETURN? Support 3
Joe Caverly Why does TCCRT return VIEW as an internal command? Support 6
D Can the argument to RETURN be a function expansion? Support 3
J How to? Speedup of gosub/return Support 8
vefatica Can a subroutine return a 64-bit integer? Support 4
S Return of FTP copy bug in 16.02.48 Support 12
A WAD function return values with parentheses prevent evaluation of additional functions Support 31
Stefano Piccardi How to? return values from @python Support 2
A How to? return to completion mask after auto-completion Support 14
ehab aboudaya How to? 2 GUI questions and return HWND using TakeCommandIPC Support 3
M echo %@index[1234,.,-1] return -21 in new version Support 3
Jay Sage Function to Return Selected Text Support 2
rconn Do NOT request return receipt! Support 7
rconn No return receipt!! Support 10
M Goto fails when a text endtext block precedes the code Support 5
rconn News Update: Code Signing Certificate Status Support 16
Peter Murschall UNICODE mixed with ANSI Code Support 11
D What is wrong with this code? Support 7
D v20 code page behavior changes Support 2
vefatica Code page? Support 1
vefatica Exit code of a batch file? Support 4
rconn News Take Command code signing certificate Support 0
Phileosophos Documentation TPIPE /string type code confusion Support 4
M What am I doing wrong in this (very simple!) code? Support 3
M Why does this code produce these results? Support 2
M Why is the exit code zero? Support 6
S WAD _exit returns invalid code under some conditions Support 13
T @ping: response code, data size and documentation incorrect Support 2
C plugin or code to verify email addresses ? Support 7
vefatica Code page? Support 0
B BB code is Off? Support 2
D bug in ftp code Support 19
vefatica IDE external command syntax color? Support 1
gentzel Bug launching external commands in 20.11.43 Support 11
D Fixed 20.10 strangeness with external commands Support 9
Stefano Piccardi list of TCC commands for external editors Support 4
A How to? Run a TCC command from external program Support 2
T How to? Logging Output from External Programs Support 4
H Ruby - Running External Ruby script crashes TCC Support 11
nikbackm Alias - launch external program - batch file Support 16
R Don't wake up external drives on startup Support 2

Similar threads