Solved

Include some lines of code using compiler statement

Posted on 2009-05-17
15
189 Views
Last Modified: 2012-05-07
title say all

just

{$  do5Lines of code }    ??
0
Comment
Question by:BdLm
  • 7
  • 5
  • 2
  • +1
15 Comments
 
LVL 12

Expert Comment

by:Hypo
ID: 24405780
Is this what you are after?

{$DEFINE do5LinesOfCode} // Remove this line if you don't want 5 lines of code...

{$IFDEF do5LinesOfCode}
  // your lines of code...
{$ENDIF}
0
 
LVL 8

Author Comment

by:BdLm
ID: 24405811
can I pass a Parameter to my lines of code ?
0
 
LVL 8

Author Comment

by:BdLm
ID: 24405816
The waek point id idef ...  I need these point 2000 x in my code   (profiler)
therfore only want I line for a piease of code to insert ...
0
 
LVL 12

Expert Comment

by:Hypo
ID: 24405837
ok... so you want it like a macro then... I know one way to do that is using Include-files... You can then type  {$I 'C:\MyCode.pas'} and the compiler will replace all those statement with the lines that are in that file. I'll have a look if there is any other ways aswell that don't require you to put the lines in a separate file.

for instance, MyCode.Pas could contain the following line:
TButton(Sender).Caption := 'Test';

and then if you have a button event like this:
procedure TForm1.Button1Click(Sender: TObject);
begin
  {$I 'C:\MyCode.pas'}
end;

The code will compile and the text on the button will change to Test when pressed.

regards
Hypo
0
 
LVL 12

Expert Comment

by:Hypo
ID: 24405897
I've looked around some but didn't find any other way to do this, so to me it seems that the include files are the way to do what you want. Some extra info: You don't have to quote the filename if it doesn't contain spaces in it, and the compiler assumes that the default extension for includes are .pas, so {$I My5LinesOfCode} is equivalent to {$I 'My5LinesOfCode.pas'}.

Just by curiosity, may I ask why you want to include the lines using a compilerdirective? It isn't possible for you to put the code you want to execute in a simple function?

/Hypo
0
 
LVL 31

Assisted Solution

by:moorhouselondon
moorhouselondon earned 100 total points
ID: 24406905
Trouble with using this technique is that you are going to end up with two versions of the program with different maps.  I think you are concerned about speed - putting the test code into a procedure is better if speed is not crucial.  

You can specify a parameter string when calling the program in "test" mode and use the paramstr(1) function to retrieve it.  If running in normal mode, leave the parameter blank.

http://www.delphibasics.co.uk/RTL.asp?Name=ParamStr

Doing this, you can setup Delphi to always use this "debug" string when you run the program from the IDE.  At the start of your prog have debugflag set depending on whether paramstr contains the correct keyword, and then test for this on entry to your test procedure.  If it is false, exit the test procedure immediately.
0
 
LVL 8

Author Comment

by:BdLm
ID: 24407311
{ifdef PROFILER}

   {$I 'C:\InitProfiler.pas'}
{endif}

     testCode

{$ifdef PROFILER}
 
        {$I 'C:\StopProfiler.pas'}
 {endif}

any better / shorter way in the source code ?





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 8

Author Comment

by:BdLm
ID: 24407327
PS:  I know http://www.prodelphi.de/indexpd.htm   but not invented here  :-)  
0
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 24407373
var
  debugflag:boolean;


procedure profiler(whocalledme:string);
begin
if debugflag then
  begin
 
  logging routine storing date time and whocalledme

  end;
end;


procedure main;
begin
debugflag:=(paramstr(1)='mysecretdebugswitch');
profiler('program started');
.
.
.

0
 
LVL 8

Author Comment

by:BdLm
ID: 24407387
here comes a sample with prodelphi , they include the lines of code in the $ifdef statement ...



procedure TForm1.Button3Click(Sender: TObject);

begin

{$IFDEF PROFILE}asm DW 310FH; call Profint.ProfStop; end; Try; asm mov edx,79 or $99EA0000; mov eax,self; call Profint.ProfEnter; mov ecx,eax; DW 310FH; add[ecx].0,eax; adc[ecx].4,edx; end; {$ENDIF}
 

     ACheck.Filename := edit2.Text;
 

     ACheck.Name  := ' Bla Bla';

     

     ACheck.StoreResults;

{$IFDEF PROFILE}finally; asm DW 310FH; mov ecx,79; call Profint.ProfExit; mov ecx,eax; DW 310FH; add[ecx].0,eax; adc[ecx].4,edx; end; end; {$ENDIF}

end;

Open in new window

0
 
LVL 12

Accepted Solution

by:
Hypo earned 400 total points
ID: 24407397
Yeah, you can put the defines in the inc-file as well...

procedure TForm1.Button1Click(Sender: TObject);
begin
  {$i C:\initProfiler}
  ...
  {$i C:\stopProfiler}
end;


And then in C:\InitProfiler.pas you have...
{$IFDEF PROFILER}
  // your code...
  TButton(Sender).Caption := 'Testing';
{$ENDIF}

And similar in StopProfiler.pas...
0
 
LVL 8

Author Comment

by:BdLm
ID: 24407399
@ Hypo :  just working on that solution,   :-))
0
 
LVL 12

Expert Comment

by:Hypo
ID: 24407423
Nice, I just tried it, putting those lines you have at the start and at the end in two seperate files, compiles... well I had to remove some of your asm-code since i don't have all the global variables you have defined... but I tried it with other code, so it should work with your code as well... :)
0
 
LVL 8

Author Comment

by:BdLm
ID: 24407574
Btw:  did you see my new question , because now I need to init the profiler  var in the begin end block of my unit    http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_24416082.html
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 24416694
when you use the profiler, it parses all your units and adds the entry and exit procs to the beginning resp end of each function or procedure.
Why do you have to do this manually ?
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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 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

13 Experts available now in Live!

Get 1:1 Help Now