1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'Support' started by mathewsdw, Jan 14, 2013.

  1. mathewsdw

    Joined:
    May 24, 2010
    Messages:
    855
    Likes Received:
    0
    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?
     
  2. vefatica

    Joined:
    May 20, 2008
    Messages:
    8,076
    Likes Received:
    30
    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
     
  3. mathewsdw

    Joined:
    May 24, 2010
    Messages:
    855
    Likes Received:
    0
    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.)
     
  4. mathewsdw

    Joined:
    May 24, 2010
    Messages:
    855
    Likes Received:
    0
    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.)
     
  5. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,358
    Likes Received:
    39
    Running a program in PRE_INPUT / PRE_EXEC / POST_EXEC / your prompt?
     
  6. Frank

    Joined:
    Aug 2, 2011
    Messages:
    258
    Likes Received:
    4
    Come on, let us know. I have made stupid errors as well. Perhaps I can learn something from it.
     
  7. mathewsdw

    Joined:
    May 24, 2010
    Messages:
    855
    Likes Received:
    0
    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.
     
  8. Frank

    Joined:
    Aug 2, 2011
    Messages:
    258
    Likes Received:
    4
    :)
     
  9. mathewsdw

    Joined:
    May 24, 2010
    Messages:
    855
    Likes Received:
    0
    Actually, an embarrassed icon would be more appropriate for me, but there doesn't seem to be one...
     

Share This Page