Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling exe files from pascal
02-18-2010, 12:32 AM
Post: #1
Calling exe files from pascal
I am sure that is not a rare case when you need to call some external program or CMD from pascal... So, today i have for you one little program which will (i hope) clear you the way how are programs or CMD being called in pascal... In the next example we will call cmd, notepad and tweakUI... for more info read coments in code or ask here Smile


Code:
Program RunExe;

{$M 16000, 0, 0}
{$S+,D+,R+,B-}

{ For runing a program we need to setup ram first }

Uses
   { Use dos unit because functions Exec and GetEnv }
   Dos;

Type
   { the most common is string of 79 charrs }
   TStrCmd = String [ 79 ];

{ Main program for runing command line }
Procedure ExecCmd ( sCmdLine : TStrCmd );
Var
   nErr : LongInt;      { Value of function DosError    }
   sCmd : String;       { path to Command.com which is showing GetEnv }
Begin
   { step 1. need to get path to Command.Com file                    }
   sCmd := GetEnv ( 'COMSPEC' );

   { step 2. IF string of comand line is not empty we need  }
   {     parameter,  swich /C which is neccessery      }
   {     Command.Com /C                                        }
   If sCmdLine <> ''
   Then
      sCmdLine := '/C ' + sCmdLine;

   { step 3. Saving pointer for part of ram memory (at the moment)         }
   SwapVectors;

   { step 4. Running command line Command.com /C CmdLine     }
   Exec ( sCmd, sCmdLine );

   { step 5. rollback pointer          }
   SwapVectors;

   { K6. checking rollback value of running command        }
   nErr := DosError;
   If nErr <> 0
   Then
      WriteLn ( 'Mistake: ', nErr, ' - cant be done ', sCmdLine );
End;

Procedure RunExe_TEST;
Var
   sCmdLine : TStrCmd;
Begin

   { step 1. Test order DIR                                     }
   sCmdLine := 'Dir';
   ExecCmd ( sCmdLine );

   { step 2. Test calling protram NOTEPAD                      }
   sCmdLine := 'NOTEPAD.EXE';
   ExecCmd ( sCmdLine );

   { step 3. Test calling program TweakUI from Control Panel   }
   sCmdLine := 'Rundll32.exe shell32.dll, Control_RunDLL TweakUI.cpl';
   ExecCmd ( sCmdLine );

   { step 4. Notification about end of TEST example                     }
   WriteLn  ('End of test example');
   ReadLn;

End;

BEGIN
   { Connecting TEST from subprogram RunExe_TEST        }
   RunExe_TEST;
END.

Read rules Smile
[Image: legislator.png]
Find all posts by this user
Quote this message in a reply
02-18-2010, 02:21 AM
Post: #2
RE: Calling exe files from pascal
This is interesting. Using command lines as i can see. Also good use of compiler directives !
Very good job !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
02-18-2010, 03:42 AM
Post: #3
RE: Calling exe files from pascal
yes, in both of cases it is using a command line through pascal to call or run something! Tongue

Read rules Smile
[Image: legislator.png]
Find all posts by this user
Quote this message in a reply
02-22-2010, 05:51 PM
Post: #4
RE: Calling exe files from pascal
Code:
{$M 16000, 0, 0}
So thats setting up RAM?

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
02-22-2010, 08:43 PM
Post: #5
RE: Calling exe files from pascal
y, that are properties of memory.

Read rules Smile
[Image: legislator.png]
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: