Solved

How to tell if running in debugger

Posted on 2004-04-08
8
175 Views
Last Modified: 2010-04-05
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}
0
Comment
Question by:rbohac
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10784779
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
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 10784824

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

0
 
LVL 22

Accepted Solution

by:
Ferruccio Accalai earned 50 total points
ID: 10784904
you can use DebugHook: if its value > 0 then it's running from IDE

so

If DebugHook = 0 then begin
    frmSplash := TfrmSplash.Create(nil);
    frmSplash.Show;
    frmSplash.Repaint;
    Sleep(3500);
end;

0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 10784944

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

Russell
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 11

Expert Comment

by:shaneholmes
ID: 10784984
Me either - <SMILE>

Shane
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 10784994
Well russell, i discovered that not so much time ago...Sometimes Delphi give's new surprises :))))
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 10785186
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.
0
 
LVL 6

Author Comment

by:rbohac
ID: 10785516
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;
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Activex loadlibrary and show parented form issue 6 286
delphi custom sort exception 6 137
How to use Hashing sha1 in Delphi 2010 4 206
Virtuailstring tree compare node issue 14 105
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now