Incode Systems Logo Incode Systems, Inc.
www.incodesystems.com
Please E-mail: webmaster [at] incodesystems.com if you have information to contribute.
(Last modified: Monday, September 4, 2017 2:24 pm)

Delphi Programming

ERRORLEVEL from GUI Windows Program


(top)

ERRORLEVEL from GUI Windows Program

Symptoms:
With a command line program, you can easily set the ERRORLEVEL returned by the program using:
Halt(1);

However, with a GUI (Graphical User Interface) program (in other words, a program with windows), this method does not work.

Solution:
Here is a program named SetERRORLEVEL that sets the ERRORLEVEL. The form has a single button named CloseBtn. When CloseBtn is clicked, the program exits with ERRORLEVEL 1.

The ERRORLEVEL may be returned in a batch file like this:

START /W SetERRORLEVEL
IF ERRORLEVEL 1 GOTO Error
unit Main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TMainForm = class(TForm)
    CloseBtn: TButton;
    procedure CloseBtnClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;
  ErrorLevel: integer;

implementation

{$R *.DFM}

procedure TMainForm.CloseBtnClick(Sender: TObject);
begin
  ErrorLevel:=1;
  MainForm.Close;
end;

initialization
finalization
  System.ExitCode:=ErrorLevel;
end.