3

Alright so I have a program called WC_Error_Resolution, which was written in C# (C# Console Application with .NET 4.0). This program returns 0, 1, or 2 depending on what is done while running the program. If I call it from cmd.exe, everything works fine. However, here is where I get confused. I have a test batch script (the real one is much larger) that calls it like:

    ECHO Start
    WC_Error_Resolution.exe file1.xml file2.xml inifile.ini log1.log log2.log
    ECHO ERRORLEVEL=%ERRORLEVEL%

The problem here is that the second echo never gets executed. The second the script gets to the program, it calls it and exits the batch script. Then I decided to try:

    ECHO Start
    START /B /WAIT "" WC_Error_Resolution.exe file1.xml file2.xml inifile.ini log1.log log2.log
    ECHO ERRORLEVEL=%ERRORLEVEL%

This time, it works as expected. I have created tons of batch scripts that call .exe files like the first example, and have never run into a problem like this. What could cause the script to stop execution after the WC_Error_Resolution.exe program returns?

EDIT
-----------------------------------------------------
So now this is showing up with another program of mine. Both programs reference System.Xml.Linq. The programs can be scripted on Windows 7, Windows 8, & Windows Server 2012 R2. The programs fail to execute on Windows Server 2008 R2 and do not show any error. The script simply never executes the program or commands below the program call. A quick note, this is a 32-bit program being run in a 64-bit environment with WoW64 enabled. Is there some issue with .NET applications run on Windows Server 2008 R2 that I don't know about?

Disco Globeulon
  • 459
  • 1
  • 6
  • 16
  • Is your `WC_Error_Resolution.exe` application a 32-bit or 64-bit console application? Why do you use `/B` in second batch? Does your console application not exit correct with `return 0;` (or 1 or 2) in `main()`? Run your application in debug mode in Visual Studio and check how it really exits. – Mofi Oct 01 '14 at 21:29
  • The project's build properties specifies the target platform as "Any CPU". Should I change it to x86 instead? I use /B is because the call to the exe is part of a much larger script, so I didn't want a second window to appear during execution. The application runs normally, and exits with exit code 0 when nothing is altered, 1 when a specific file is altered, and 2 on error. Runs fine in VS. When I script it like the first example, the second echo won't get executed, but if I type in `ECHO ERRORLEVEL=%ERRORLEVEL%` on command line after the script returns, I get the correct exit code. – Disco Globeulon Oct 02 '14 at 12:50
  • See [What does the Visual Studio “Any CPU” target mean?](http://stackoverflow.com/questions/516730/) Build your console application explicitly with x86 configuration and test if that makes a difference. – Mofi Oct 02 '14 at 14:02
  • I'll refer you to http://stackoverflow.com/questions/516730/what-does-the-visual-studio-any-cpu-target-mean for what "Any CPU" means. Based on this, I don't expect there to be any difference in execution behavior, but will give it a shot and come back with results. – Disco Globeulon Oct 02 '14 at 14:55
  • `start /B /W ""` adds `CREATE_NEW_PROCESS_GROUP` to the process creation flags. In 64-bit Windows 7, that's the only major difference I see when I compare the arguments passed to `CreateProcessW` in both cases when running `C:\Windows\System32\where.exe` as a test case. Does it echo properly in a new window if you remove the `/B` option (add a `pause` at the end if necessary)? – Eryk Sun Oct 03 '14 at 00:45
  • You'll probably need to step through executing the batch file under a debugger. Put a breakpoint on `cmd!BatLoop`. This function implements the dispatch loop. In this case it's dispatching to either `cmd!ExtCom` or `cmd!Start`, and then `cmd!eEcho`. – Eryk Sun Oct 03 '14 at 00:47
  • So I tried scripting it, calling the exe without START, and it actually worked correctly with both "Any CPU" and "x86" as the target platform. I believe it might be an issue with my coworker's script now. Will try "debugging" the batch file on her machine. – Disco Globeulon Oct 03 '14 at 13:04

1 Answers1

0

This is answered (by use of a C++ example) in the answers to the question:

How do I get the application exit code from a Windows command line?

If the program detaches from the console then the batch file continues while the program runs and the START /B /WAIT is the correct work around to get the errorlevel. If you cannot see why the C# program detaches from the console you might want to to ask another question based around extracts from your C# program.

Community
  • 1
  • 1
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • This isn't an issue anymore since we've altered the process to work around it. However, I will say that the linked answer, while useful, doesn't necessarily apply since the called program is a console program. That is unless the same applies for c# console apps. – Disco Globeulon Jan 22 '15 at 20:59
  • Yes: Console Applications can detach the console, and may use windows, which may be the case with your application, which was why I made the note. – Brian Tompsett - 汤莱恩 Jan 22 '15 at 22:00
  • If that were the case though, do you think that issue would appear across machines? It only happens on my coworker's machine. – Disco Globeulon Feb 05 '15 at 17:55