Why does a non-interactive batch script think I've pressed control-C?

前端 未结 1 1617
清歌不尽
清歌不尽 2020-12-05 16:17

So my batch script is ticking along nicely when suddenly this appears in the output log:

21:27:13.99 c:\\apps\\w7lab-scripting>some-command
Error 32212257         


        
相关标签:
1条回答
  • 2020-12-05 16:44

    The magic here is in exit code 3221225786, aka 0xC000013A or STATUS_CONTROL_C_EXIT.

    The interactive application received a control-C, and didn't catch it, so as expected, it was aborted with STATUS_CONTROL_C_EXIT. The some-command application correctly reported this as the remote application's exit code, and passed it back to the batch script.

    What I hadn't realized was that cmd.exe detects control-C in a batch script in exactly this way, by checking whether a child process returns STATUS_CONTROL_C_EXIT. So by returning this error code I was inadvertently stopping the batch script.

    This can be demonstrated with a simple batch script:

    cmd /c exit 3221225786
    echo hello
    

    which, when run, produces

    C:\working\test>cmd /c exit 3221225786
    ^CTerminate batch job (Y/N)?
    
    0 讨论(0)
提交回复
热议问题