Board index » delphi » Graceful quit on start-up failure

Graceful quit on start-up failure

My application requires the presence of database files. If the user
neglects to ensure these are installed, my program tries to show a
message to that effect and quit gracefully.

However, I get GPF's. I assume the reason is that the functions I call
to display the warning and to test for "FileExists()" are spread in
other units which get created only later. I thought one cannot put the
mainform at the end of the auto-create list. I wonder is there a
graceful way.

Email appreciated. I respond to every email.
Matthew

 

Re:Graceful quit on start-up failure


Quote
> My application requires the presence of database files. If the user
> neglects to ensure these are installed, my program tries to show a
> message to that effect and quit gracefully.

> However, I get GPF's. I assume the reason is that the functions I call
> to display the warning and to test for "FileExists()" are spread in
> other units which get created only later. I thought one cannot put the
> mainform at the end of the auto-create list. I wonder is there a
> graceful way.

Hello  Matthew

        I required a similar functionality for an application we designed to aid
in background checks on potential {*word*2} employees.  The tricky part (and
it IS tricky) is to do all your checks in the Application startup code.  To
see this code use View|Project Source.
        There should be several lines of FormCreate and finally a line
"Application.Run".  By instantiating (creating) the units required to
verify startup requirements (in our case password verification, in yours
database presence) and placing a check in the project source you can put
the subsequent FormCreate and Application.Run lines in an if . . then
condition.
        A word of warning.  It is NOT common practice nor is it recommended to
monkey around with the project source.  This can be the cause of many
headaches and late hours.  On the other hand, the benefit of a small, well
thought-out additional amount of code can really flesh out an application.

        If you would like some proven, sample source code, do not hesitate to ask.
 I would have posted some here but I do not have it available to me just
now.

--
Rob Tanner B.E.,B.Sc.
Design Engineer
WestBau Technologies, Inc.

Re:Graceful quit on start-up failure


Here's how I've handled a similar need to check for the existence of
key files. The first line in my project source calls a procedure in a
formless error-checking unit. The core code for that unit is as
follows:

  FileList[1] := 'assist96.dat';
  FileList[2] := 'agytot96.dat';
  FileList[3] := 'afdc96.dat';
  FileList[4] := 'census90.dat';
  FileList[5] := 'house96.dat';
  FileList[6] := 'metro.dat';
  FileList[7] := 'metro.cgf';
  FileList[8] := 'metro.cgi';
  FileList[9] := 'metro.cgr';
  GetDir(0,CurDir); {returns the current drive and directory}
  if Length(CurDir) = 3 then  {check if current directory is root
directory}
    CurDirTest := CurDir
  else
    CurDirTest := CurDir + '\';

  for q := 1 to 9 do
    if not FileExists(CurDirTest + FileList[q]) then
    begin
      MessageBeep(mb_ok);
      ShellReturn := MessageDlg('File ' + FileList[q] + ' is
missing.', mtError,[mbOk], 0);
      while GetModuleUsage(ShellReturn) > 0 do
        Application.ProcessMessages;
      Application.Terminate;
      Halt;
    end;

Mark Shapiro
info...@swbell.net

Re:Graceful quit on start-up failure


Quote
Matthew wrote:

> However, I get GPF's. I assume the reason is that the functions I call
> to display the warning and to test for "FileExists()" are spread in
> other units which get created only later. I thought one cannot put the
> mainform at the end of the auto-create list. I wonder is there a
> graceful way.

  Put the code to search for the db's in the 'OnActivate' form, and if
they're not there, show the message, and close....
  If you want it in the DPR, you should be able to do something like
this:

  If FileExists('whatever.db') then Application.Run
  else Exit;  // possibly Application.Terminate;

--
Jason
Dark...@SLSoftware.reno.nv.us

Re:Graceful quit on start-up failure


--
Rob Tanner B.E.,B.Sc.
Design Engineer
WestBau Technologies, Inc.

Jason Wallace <Dark...@SLSoftware.reno.nv.us> wrote in article
<333B983A.2...@SLSoftware.reno.nv.us>...

Quote
>   If you want it in the DPR, you should be able to do something like
> this:

>   If FileExists('whatever.db') then Application.Run
>   else Exit;  // possibly Application.Terminate;

        You do not need the 'else' here.  Assume that FileExists is available in a
unit called DM.  The .DPR should look a lot like this.

        Application.Initialize;
        Application.Title := 'Insert Title Here';
        Application.CreateForm(TDM, DM);        
        if DM.FileExists('whatever.db' then begin
                Application.CreateForm(TMainForm, MainForm);
                { Create Any Other Forms that are required at startup )
                Application.Run;
        end;

Quote
> Jason
> Dark...@SLSoftware.reno.nv.us

Re:Graceful quit on start-up failure


In article <01bc3e23$d80c6640$84c5a...@eweston.wbm.ca> "Rob Tanner" <westbau_technolog...@eagle.wbm.ca> writes:

Quote
>Jason Wallace <Dark...@SLSoftware.reno.nv.us> wrote in article
><333B983A.2...@SLSoftware.reno.nv.us>...
>>   If you want it in the DPR, you should be able to do something like
>> this:

>>   If FileExists('whatever.db') then Application.Run
>>   else Exit;  // possibly Application.Terminate;
>        You do not need the 'else' here.  Assume that FileExists is available in a
>unit called DM.  The .DPR should look a lot like this.
>        Application.Initialize;
>        Application.Title := 'Insert Title Here';
>        Application.CreateForm(TDM, DM);        
>        if DM.FileExists('whatever.db' then begin
>                Application.CreateForm(TMainForm, MainForm);
>                { Create Any Other Forms that are required at startup )
>                Application.Run;
>        end;

One serious problem that users should be aware of has to do with what happens
when a Delphi 1.x application is started on a machine when BDE is not present.

As-written, the application dies most ungracefully.  In a DLL routine it can
bring down Windows (GPF in DLL).  The problem is that the initialization-code
in the "DB" unit attempts to create a TSession object... blithely assuming
that it will be successful and with no provision to trap an EDBEngineError
exception.

I coded around this (e.g. in ChimneySweep) by creating a local modification to
the DB unit.

Re:Graceful quit on start-up failure


Quote
Sundial Services wrote:

> In article <01bc3e23$d80c6640$84c5a...@eweston.wbm.ca> "Rob Tanner" <westbau_technolog...@eagle.wbm.ca> writes:

> >Jason Wallace <Dark...@SLSoftware.reno.nv.us> wrote in article
> ><333B983A.2...@SLSoftware.reno.nv.us>...
> >>   If you want it in the DPR, you should be able to do something like
> >> this:

> >>   If FileExists('whatever.db') then Application.Run
> >>   else Exit;  // possibly Application.Terminate;

> >        You do not need the 'else' here.  Assume that FileExists is available in a
> >unit called DM.  The .DPR should look a lot like this.

> >        Application.Initialize;
> >        Application.Title := 'Insert Title Here';
> >        Application.CreateForm(TDM, DM);
> >        if DM.FileExists('whatever.db' then begin
> >                Application.CreateForm(TMainForm, MainForm);
> >                { Create Any Other Forms that are required at startup )
> >                Application.Run;
> >        end;

> One serious problem that users should be aware of has to do with what happens
> when a Delphi 1.x application is started on a machine when BDE is not present.

> As-written, the application dies most ungracefully.  In a DLL routine it can
> bring down Windows (GPF in DLL).  The problem is that the initialization-code
> in the "DB" unit attempts to create a TSession object... blithely assuming
> that it will be successful and with no provision to trap an EDBEngineError
> exception.

> I coded around this (e.g. in ChimneySweep) by creating a local modification to
> the DB unit.

Is there any way to do this kind "BDE Exists" checkup without having the
VCL sources? Some dBi.. call perhaps ?

Re:Graceful quit on start-up failure


With care, you could try putting some test code in the PROJECT SOURCE.
Granted, I've heard that Borland doesn't recommend putting your own code
in the Project source, but I've done it successfully.

Method A)

program Gridtest;

uses
  Forms, sysutils, dialogs,
  Gridtst1 in 'GRIDTST1.PAS' {Form1};

{$R *.RES}

begin
  if not fileexists('c:\idapi\idapi.cfg') then
  begin
   showmessage('You must install the'+#13+
               'Borland Database Engine'+#13+
               'to run this application');
   application.terminate;
  end;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

Method B)

If the BDE might not be in the standard location (different drive,
etc.), you could check the WIN.INI using INIFILES calls.  There is a
Windows API call to identify for the Windows directory in case it is
non-standard.  Anyway, look in the WIN.INI for the appropriate
sections...

[IDAPI]
DLLPATH=C:\IDAPI
CONFIGFILE01=C:\IDAPI\IDAPI.CFG

[Borland Language Drivers]
LDPath=C:\IDAPI\LANGDRV

Quote
Markku Nevalainen wrote:

> Sundial Services wrote:

> > In article <01bc3e23$d80c6640$84c5a...@eweston.wbm.ca> "Rob Tanner" <westbau_technolog...@eagle.wbm.ca> writes:

> > >Jason Wallace <Dark...@SLSoftware.reno.nv.us> wrote in article
> > ><333B983A.2...@SLSoftware.reno.nv.us>...
> > >>   If you want it in the DPR, you should be able to do something like
> > >> this:

> > >>   If FileExists('whatever.db') then Application.Run
> > >>   else Exit;  // possibly Application.Terminate;

> > >        You do not need the 'else' here.  Assume that FileExists is available in a
> > >unit called DM.  The .DPR should look a lot like this.

> > >        Application.Initialize;
> > >        Application.Title := 'Insert Title Here';
> > >        Application.CreateForm(TDM, DM);
> > >        if DM.FileExists('whatever.db' then begin
> > >                Application.CreateForm(TMainForm, MainForm);
> > >                { Create Any Other Forms that are required at startup )
> > >                Application.Run;
> > >        end;

> > One serious problem that users should be aware of has to do with what happens
> > when a Delphi 1.x application is started on a machine when BDE is not present.

> > As-written, the application dies most ungracefully.  In a DLL routine it can
> > bring down Windows (GPF in DLL).  The problem is that the initialization-code
> > in the "DB" unit attempts to create a TSession object... blithely assuming
> > that it will be successful and with no provision to trap an EDBEngineError
> > exception.

> > I coded around this (e.g. in ChimneySweep) by creating a local modification to
> > the DB unit.

> Is there any way to do this kind "BDE Exists" checkup without having the
> VCL sources? Some dBi.. call perhaps ?

Other Threads