Link to home
Start Free TrialLog in
Avatar of rbohac
rbohac

asked on

How to tell if running in debugger

Is there a compiler directive that I can use to omit certain code if I am running the application from the IDE.

specifically I have a splash screen that I would like to turn off when debugging.

something like

{$IF (not running in the IDE)}

    frmSplash := TfrmSplash.Create(nil);
    frmSplash.Show;
    frmSplash.Repaint;
    Sleep(3500);
{$ENDIF}
Avatar of shaneholmes
shaneholmes

function RunningFromIDE: boolean;
var
IdeHnd: THandle;
MyApp,Text: STring;
Begin
 MyApp := ExtractFileName(Application.ExeName);
 Delete(MyApp,pos('.',MyApp),4);
 ShowMessage(MyApp);
 IdeHnd := FindWindow('TAppBuilder', nil);
   If IDEhnd > 0 then
       begin
        SetLength (Text, 100);
        GetWindowText (IdeHnd, PChar (Text), 100);
        If pos(MyApp+' [Running]',Text) > 0 then
           Result := True
        else
           Result := False;
   end else result := False;
end;


procedure TForm1.FormActivate(Sender: TObject);
begin
 if RunningFromIDE then
  ShowMessage('Yes');
 esle
 ShowMessage('No');
end;

Shane
Avatar of Russell Libby

The compiler define option is just that; it defines at compile time what is included/excluded from the source. (your in the IDE when compiling it, right...). So at compile time, the compiler has no idea if you plan on running the program standalone or in debug mode (throught the ide).

If you want to go with a define, you can set one in the project options / Directories-Conditionals tab, but you will still need to remember to remove it when compiling a "release" version.

Or,if running on an NT based system, you could also use the following function to skip certain blocks of code.

if not(IsDebuggerPresent) then
begin

  .... do splashscreen

end;

---------

Regards,
Russell

ASKER CERTIFIED SOLUTION
Avatar of Ferruccio Accalai
Ferruccio Accalai
Flag of Italy 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

Nice one ;-)
I didn't realize that flag was there in the System unit.

Russell
Me either - <SMILE>

Shane
Well russell, i discovered that not so much time ago...Sometimes Delphi give's new surprises :))))
BTW i'd create the Splash Form without using Sleep and Repaint....

I'd use this method in your case:

program Project1;

uses
  Forms,

  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2};

{$R *.res}

begin
  Application.Initialize;
  If DebugHook = 0 then  Begin
  Form2 := TForm2.Create(Application);
  Form2.Show;
  Form2.Update;
  end;
  Application.CreateForm(TForm1, Form1);
  If DebugHook = 0 then  begin
  Form2.Hide;
  Form2.Free;
  end;
  Application.Run;
end.
Avatar of rbohac

ASKER

Wow. Thats a nice trick.  Russell, I guess that makes sense about it already being compiled.

I use the sleep because the application loads rather fast, and I want to display the splash screen slightly longer.

I have that code in a try...finally which looks like

try
  Application.Initialize;
  {Splash Screen}
    if DebugHook = 0 then  {Only run when not in the debugger}
      begin
       frmSplash := TfrmSplash.Create(nil);
       frmSplash.Show;
       frmSplash.Repaint;
       Sleep(3500);
      end;

Application.CreateForm(s)....

finally
  if(frmSplash <> nil) then
    FreeAndNil(frmSplash);
  Application.Run;
end;