Jump to content
Sign in to follow this  
Mike Torrettinni

Problem with ExitCode

Recommended Posts

In short: Seems like ExitCode is not always reliable retrieved with %ERRORLEVEL% if Project is not a console project.

 

Long version:

I'm testing simple example of how ExitCode works. The problem is that I can't reliable retrieve it with %ERRORLEVEL%, when not running in batch file.

 

When executing example project in batch file, it works OK, always shows correct ExitCode as %ERRORLEVEL%. This is batch file - that always works:

project1.exe
echo %ERRORLEVEL%

echo always show correct ExitCode as set in project1.exe.

 

BUT:

When I try to execute the same project1.exe from cmd line, without batch file, it doesn't reliable returns ExitCode with echo %ERRORLEVEL%.

This is how I run it without batch file:

image.png.11cdba31c0a64c38661d9c5653a8acf1.png

 

This is simple VLC Forms project:

 

program Project1;

uses
  Vcl.Forms,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

begin
  Application.Initialize;

  ExitCode := 2;

//  Application.MainFormOnTaskbar := True;
//  Application.CreateForm(TForm1, Form1);
//  Application.Run;
end.

 

 

Then, I tried to do the same with Console project and in this case %ERRORLEVEL% always works correct - show correct ExitCode, with and without batch file.

 

So, am I missing something really obvious? Is non-console project the problem here?

  • Like 1

Share this post


Link to post
50 minutes ago, Mike Torrettinni said:

So, am I missing something really obvious? Is non-console project the problem here?

Yes. A GUI application will be detached from the console when it starts.

For example if you type "notepad.exe" on the command line and press enter, notepad is started and the command prompt immediately returns.

 

Even if your application sets ExitCode this is communicated back to the console because the console just launches the application and doesn't wait for it to terminate.

This is the same reason that GUI applications can't (reliably) write to the console.

Share this post


Link to post
1 minute ago, Anders Melander said:

Yes. A GUI application will be detached from the console when it starts.

For example if you type "notepad.exe" on the command line and press enter, notepad is started and the command prompt immediately returns.

 

Even if your application sets ExitCode this is communicated back to the console because the console just launches the application and doesn't wait for it to terminate.

This is the same reason that GUI applications can't (reliably) write to the console.

OK. Then why does it work 100% reliable in batch file? Same GUI project.

Share this post


Link to post
13 minutes ago, Mike Torrettinni said:

OK. Then why does it work 100% reliable in batch file? Same GUI project.

You can think a batch as its running line by line.

For cmd, you need to use start wait app.exe

START /WAIT VCLApp.exe
ECHO %ERRORLEVEL%

 

Share this post


Link to post
2 minutes ago, Mahdi Safsafi said:

You can think a batch as its running line by line.

For cmd, you need to use start wait app.exe


START /WAIT VCLApp.exe
ECHO %ERRORLEVEL%

 

Thank you, but I'm not seeing the connection to the issue I have... what am I missing here?

 

 

Share this post


Link to post
21 minutes ago, Mike Torrettinni said:

OK. Then why does it work 100% reliable in batch file? Same GUI project.

Strange question. Because batch file execution isn't the same as command line execution.

There are different ways to start a process. The command line uses one method, the batch file another.

Share this post


Link to post
11 minutes ago, Anders Melander said:

Strange question. Because batch file execution isn't the same as command line execution.

There are different ways to start a process. The command line uses one method, the batch file another.

Aha, now I understand Mahdi's post, probably not clear enough description. Perhaps I'm mixing terms like: cmd, command line and batch file run in cmd.

 

Lets see if this give better picture:

 

I run same project1.exe (VCL Forms project above, with just ExitCode set) in two different ways, both starting in cmd:

 

This is running run.bat in cmd - works OK:

 

image.png.91857673fdd46460d8d7aabf35f64f0f.png

 

image.png.a8b9a0c8e7be9374b1820908987980d8.png

 

This is running same lines, manually in same cmd - ExitCode is not correct:

 

image.png.dc9c0c8d283b31c44d27163170010e24.png

 

 

Is this showing my misunderstanding?

Edited by Mike Torrettinni

Share this post


Link to post
2 minutes ago, Mike Torrettinni said:

Is this showing my misunderstanding?

You are just repeating what you wrote in your OP.

 

It's really very simple. Just abandon your assumption that commands entered on the command prompt are treated the same way as commands in a batch file; They are not.

The behavior you are observing is as expected.

 

For console applications both cmd and bat will start the process, wait for its termination and set errorlevel to the process exit code.

For GUI applications bat will do the same as for console applications, but cmd will start the process without waiting for it to terminate. The value of errorlevel is irrelevant.

 

All this has nothing to do with Delphi BTW.

  • Like 1
  • Thanks 1

Share this post


Link to post
12 minutes ago, Mike Torrettinni said:

Thank you, but I'm not seeing the connection to the issue I have... what am I missing here?

 

 

When calling a console application from CMD, current CMD becomes a window for that application. And CMD will wait for that application to terminate in order to execute the next instruction. With that being said, CMD can have the correct ERRORLEVEL. But when calling a non-console application, CMD runs the application and returns immediately without waiting the application to be terminated. So it can't have a correct ERRORLEVEL.

using START /WAIT app.exe will create a new CMD2(console) that runs app.exe inside CMD2. and current CMD will wait CMD2 to terminate. So it would have the correct ERRORLEVEL.

  • Like 1
  • Thanks 1

Share this post


Link to post

OK, I got it now! START /WAIT does the trick.

Now it makes sense what you @Mahdi Safsafi and @Anders Melander are talking about.

 

To recap:

- running GUI application (no matter if it actually shows any GUI)

   - starting it in batch file, all commands are within same 'virtual' command window, so %ERRORLEVEL% will find correct ExitCode from project

   - starting it manually in cmd with just project.exe starts new 'virtual' command window and next manual command echo %ERRORLEVEL% will not find correct ExitCode from project, because it is not running in same command window.

      To fix this, we should use START /WAIT project.exe command.

 

- running console application:

   - all works OK in all situation (with or wihtout batch file), because console application always runs in same command window as next commands, like echo %ERRORLEVEL%

 

Right?

 

  • Like 1

Share this post


Link to post
1 hour ago, Mike Torrettinni said:

Right?

Not really. If you want the exit code you need to wait for the process to terminate. The exit code is only available after the exit. 

 

A GUI app doesn't attach to the console at all so it's about the consoles that are involved. 

Share this post


Link to post
7 minutes ago, David Heffernan said:

Not really. If you want the exit code you need to wait for the process to terminate. The exit code is only available after the exit. 

 

A GUI app doesn't attach to the console at all so it's about the consoles that are involved. 

You think start /wait project1.exe is not best solution for my problem? Better suggestion?

Share this post


Link to post
54 minutes ago, Mike Torrettinni said:

You think start /wait project1.exe is not best solution for my problem? Better suggestion?

No. It's exactly what I said. You have to wait for the process to terminate. I was commenting that your explanation was wide of the mark. 

Share this post


Link to post
Just now, David Heffernan said:

No. It's exactly what I said. You have to wait for the process to terminate. I was commenting that your explanation was wide of the mark. 

Aha, OK. English is not first language, so sometimes I can be a bit off the mark.

Share this post


Link to post
On 7/4/2020 at 1:46 AM, Mike Torrettinni said:

English is not first language, so sometimes I can be a bit off the mark.

Not a surprise on initially German forum ;)))

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×