Link to home
Start Free TrialLog in
Avatar of ripcord
ripcord

asked on

Where do I put functions and procedures I want to run at startup?

Based on command line options I've set my main form to Application.ShowMainForm := False; in the OnCreate event. I now want to process the command line and access functions and procdures that are in the project without showing the main form. I tried to access these functions and procedures from within the OnCreate Event but I get access violations. I assume the errors are because program hasn't fully loaded. Where do I call these functions and procdures from? I tried OnActivate, but it only gets called when the mainform is not hidden.

Thanks
Avatar of erajoj
erajoj
Flag of Sweden image

Hi,
What procedures/functions?
If the procedures/functions you're talking about are in standalone units then make sure you have included them in your project source file's (*.dpr) uses statement and then simply do all the processing there.

Example:

Project source:

program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  MyUnit in 'MyUnit.pas'; // here's your unit!
{$R *.RES}

begin
  Application.ShowMainForm := False;
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  if ( MyUnit.CheckParameters ) then
  begin
    Application.ShowMainForm := True;
  end;
  Application.Run;
end.

Standalone unit:

unit MyUnit;

interface

function CheckParameters: Boolean;

implementation

uses
  SysUtils;

function CheckParameters: Boolean;
begin
  Result := CompareText( ParamStr( 1 ), '/hidden' ) <> 0
end;

end.

Explain further...!

/// John
Avatar of ripcord
ripcord

ASKER

I am calling functions and procedures from both the main form(unit) and standalone forms(units). They are the same functions and procdures I normally call when the form is not hidden, so all the units are in my uses statement. It looks like I get the error when one of the procdures attemts to close the application using Close; Is this not allowed from within the OnCreate event or should I be closing in another way?
Avatar of ripcord

ASKER

Also when calling a procedure from OnCreate that tries to access ParamCount, the value for ParamCount says "inacessable value".
Hi,
Use Halt. Close is not available in the OnCreate event.
The reason you can't see ParamCount when debugging is simply because it is a function and not an integer value. It still works (I have tested it).
Sometimes when you get "inacessable value" while debugging is because the awesome object pascal compiler has optimized it out since there is no need for that value at the moment.

procedure TForm1.FormCreate(Sender: TObject);
var
  cParam: Integer;
begin
  cParam := ParamCount;
  Caption := IntToStr( cParam );
  if ( MyUnit.CheckParameters ) then
  begin
    Application.ShowMainForm := True;
  end else
  begin
    Halt;
  end;
end;



Avatar of ripcord

ASKER

thanks, it's all working now. Please answer the question to get credit.
ASKER CERTIFIED SOLUTION
Avatar of erajoj
erajoj
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial