This is an old revision of the document!
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.
