Here’s a version of the Delphi application that checks for command-line parameters before the form is created. If the parameter /scan is passed, the form will not be created, and the program will terminate. If no parameters are passed, or the parameter is something other than /scan, the form will be created and displayed. Full Delphi Code:
Main Program Block (Project1.dpr):
program Project1;
uses
Forms, Dialogs, SysUtils, Unit1 in 'Unit1.pas';
{$R *.RES}
begin
Application.Initialize;
// Check for command-line parameters
if ParamCount > 0 then
begin
if SameText(ParamStr(1), '/scan') then
begin
// If the parameter is "/scan", do not create the form and exit the program
MessageBox(0, 'Scan mode executed. No form will be created.', 'Info', MB_ICONINFORMATION or MB_OK);
Halt; // Terminate the program
end;
end;
// If no parameters or parameter is not "/scan", create and display the form
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
Unit1 (Unit1.pas):
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TForm1 = class(TForm)
private
{ Private declarations }
public
constructor Create(AOwner: TComponent); override;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
{ TForm1 }
constructor TForm1.Create(AOwner: TComponent);
begin
inherited Create(AOwner); // Call the inherited constructor
// Additional initialization code (if needed)
ShowMessage('Form created successfully!');
end;
end.
How It Works:
- Parameter Check in Main Block:- Before calling Application.CreateForm, the program checks if any parameters are provided using ParamCount.
- If /scan is passed as a parameter (case-insensitive), it shows a message and terminates the application using Halt.
- If no parameters or a different parameter is passed, it proceeds to create and display the form.
- Form Creation:- The form's constructor (Create) is overridden, but in this case, it doesn’t perform parameter checking (since it’s already handled in the main program block).
- Graceful Exit with Message:- When /scan is detected, a message box informs the user, and the program exits without showing the form.
Example Scenarios: - Running Project1.exe will create and display the form. - Running Project1.exe /scan will not create the form. Instead, it will display an informational message and exit the program.
Additional option is what if no form will be created and just display a message in console, just add the $APPTYPE like this:
{$APPTYPE CONSOLE}
program NetworkDownAlarm;
uses
Forms,
mainnetdownalarm in 'mainnetdownalarm.pas' {formmain_networkdown};
{$R *.res}
begin
Application.Initialize;
if ParamCount > 0 then begin
if ParamStr(1)='/scan' then
begin
// If the parameter is "/scan", do not create the form and exit the program
writeln('Scan mode executed. No form will be created.');
Halt; // Terminate the program
end;
end;
Application.CreateForm(Tformmain_networkdown, formmain_networkdown);
Application.Run;
end.
Another is to create different form base on the parameter:
program Project1;
uses
Forms, Dialogs, SysUtils, Unit1 in 'Unit1.pas', Unit2 in 'Unit2.pas';
{$R *.RES}
const
ProgramVersion = '1.0.0'; // Define the program version
begin
Application.Initialize;
// Check for command-line parameters
if ParamCount > 0 then
begin
if SameText(ParamStr(1), '/scan') then
begin
// If the parameter is "/scan", create and display TScanForm (Unit2)
Application.CreateForm(TScanForm, ScanForm);
Application.Run;
Exit;
end
else if SameText(ParamStr(1), '/help') then
begin
// If the parameter is "/help", show help text and exit
MessageBox(0, PChar('Program Version: ' + ProgramVersion + sLineBreak +
'Usage:' + sLineBreak +
'/scan - Run scan mode (no default form).' + sLineBreak +
'/help - Display this help message.'),
'Help', MB_ICONINFORMATION or MB_OK);
Halt; // Terminate the program
end;
end;
// If no parameters or unrecognized parameter, create and display TForm1
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
NOTE: just make sure each of the form will have:
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
constructor Tformmain.Create(AOwner: TComponent);
begin
inherited Create(AOwner); // Call the inherited constructor
end;
