2

We have a legacy VB6 application that runs a separate C# EXE. The VB6 application relies on the C# application to perform a particular task. When the task is done successfully it will return with exit code 0, or 1 if failure.

This works well on our development machines (yes, we tried on more than 1 machine). But, when we tried it on the client machine it always returns a 0 exit code no matter the result of the task.

The really strange part is that this scenario has been working perfectly for about 8 months until this first happenned.

We even tried to make a simple C# 'runner app' that only calls an exe and catches its exit code. Again, it worked fine on our development machine but always return 0 on the client machine. So, we concluded that the problem is not in the VB6 program.

This is the C# 'runner app' code snippet that call the .exe program:

System.Diagnostics.Process installProcess = new System.Diagnostics.Process();
installProcess.StartInfo.FileName = this.textBox1.Text;
installProcess.StartInfo.Arguments = this.textBox2.Text;
installProcess.Start();
installProcess.WaitForExit();
MessageBox.Show("Exit code : " + installProcess.ExitCode.ToString());

And this is the C# code snippet that exit the program with custom exit code:

Environment.Exit(1);

Well, we suspect there's some configuration on the client machine OS that causes this strange behavior on the client machine.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
caesardo
  • 348
  • 6
  • 15
  • Have you tried directly running the other application from command prompt with invalid arguments and verifying return code is 1? – Tanveer Badar Apr 03 '18 at 07:24
  • can you please describe how to verify it? – caesardo Apr 03 '18 at 07:43
  • `echo %errorlevel%` is one way. – Tanveer Badar Apr 03 '18 at 07:47
  • `Environment.Exit(1);` is *not* usually the preferred way to exit an exe with a return code; usually you should unwind to your `static int Main()` method and simply `return 0;` or `return 1;` (perhaps this one inside a `catch` block) - hard to know if that is the reason without trying it. – Marc Gravell Apr 03 '18 at 08:09
  • we solved this by setting the `Environment` exit code property `Environment.ExitCode = 1` , is this an acceptable way of doing it? – caesardo Apr 04 '18 at 01:25

1 Answers1

2

Based on the comment from Michael Gosselin on this question, it is critical to compile your project as a "console application" and not as a "windows application".

Phil Jollans
  • 3,605
  • 2
  • 37
  • 50