Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set ExitCode in a VCL Forms Application

I can't get ExitCode to work for a VCL forms application. Here is my test application. It was created from the File / New menu in the Delphi 2007 IDE. The only change is that I added the line ExitCode := 42; at the end.

program Test;

uses
  Forms,
  Unit27 in 'Unit27.pas' {Form27};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm27, Form27);
  Application.Run;

  ExitCode := 42;
end.

Now, when I run it from the command line, %ERRORLEVEL% doesn't get set:

>.\Test.exe

>echo %ERRORLEVEL%
0

I was expected the value in %ERRORLEVEL% to be 42, but it isn't being updated.

I tried the same experiment in a console application, and that worked fine. Why isn't it working for my GUI application?

like image 642
dan-gph Avatar asked Feb 28 '13 04:02

dan-gph


1 Answers

You can establish the exit code like you're doing, but on the console you have to test the %errorlevel% variable in the same batch to get the value.

Instead of running your commands in the command prompt, create a simple bat like this:

REM calltest.bat

.\Test.exe
echo %ERRORLEVEL%

and then, invoke your test:

>calltest

I got this in my test:

>calltest.bat
>project3.exe
>echo 47

For both, setting directly the ExitCode variable or calling Halt.

My OS is Win7 64, if it makes any difference. Printing the %errorlevel% directly from the command line prints 0.

like image 178
jachguate Avatar answered Oct 13 '22 00:10

jachguate