Solved

How do I execute a section of code only when running under the debugger

Posted on 2011-03-09
4
294 Views
Last Modified: 2012-05-11
I tried using
{$IFDEF DEBUG}
...
{$ENDIF}

but it appears to get fired in the compiled exe as well.

Thanks for your help

Daniel
0
Comment
Question by:DanielManchester
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 14

Expert Comment

by:systan
ID: 35085477
hi
try to read that link;
http://stackoverflow.com/questions/147719/delphi-ifdebug-equivalent
in D2007 the define DEBUG begins
0
 
LVL 24

Accepted Solution

by:
jimyX earned 500 total points
ID: 35085478
function IsDebuggerPresent: BOOL; stdcall; external 'kernel32.dll';

procedure TForm1.Button1Click(Sender: TObject);
begin
  if IsDebuggerPresent then ShowMessage('Exists')
  else ShowMessage('not Exists')
end;

http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_23199714.html
0
 
LVL 14

Expert Comment

by:systan
ID: 35085517
I tried this code from the link;
unit Unit1;
{$define debug}
interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    btn1: TButton;
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


procedure TForm1.btn1Click(Sender: TObject);
begin
 {$IFDEF DEBUG}
  ShowMessage('my conditional IS defined!');
  {$ELSE}
  ShowMessage('my conditional is NOT defined!');
  {$ENDIF}

  {$IFNDEF DEBUG}
  ShowMessage('My conditional is explicitly NOT defined');
  {$ENDIF}

end;

end.

Open in new window

0
 

Author Closing Comment

by:DanielManchester
ID: 35085825
Just what I was looking for.  Thanks!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

734 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