I am trying to use some Windows command line tools from the short Pascal program. To keep things simple, I write a DoShell function that takes a command line as an argument and returns a record type called ShellResult, with one field for the process termination code and one field for the output of the process.
I am having serious problems with some standard library functions that do not work properly. The DOS Exec () function does not actually execute the command that I pass to it. The Reset () procedure gives me a RunError (2) runtime error if I did not set the compiler mode {I-}. In this case, I do not get errors at runtime, but the Readln () functions that I use in this file do not actually read anything, and in addition, the Writeln () functions used after this point in the code execution are also nothing do not do.
Here is the source code of my program. I am using Lazarus 0.9.28.2 beta, with Free Pascal Compiler 2.24
program project1; {$mode objfpc}{$H+} uses Classes, SysUtils, StrUtils, Dos { you can add units after this }; {$IFDEF WINDOWS}{$R project1.rc}{$ENDIF} type ShellResult = record output : AnsiString; exitcode : Integer; end; function DoShell(command: AnsiString): ShellResult; var exitcode: Integer; output: AnsiString; exepath: AnsiString; exeargs: AnsiString; splitat: Integer; F: Text; readbuffer: AnsiString; begin //Initialize variables exitcode := 0; output := ''; exepath := ''; exeargs := ''; splitat := 0; readbuffer := ''; Result.exitcode := 0; Result.output := ''; //Split command for processing splitat := NPos(' ', command, 1); exepath := Copy(command, 1, Pred(splitat)); exeargs := Copy(command, Succ(splitat), Length(command)); //Run command and put output in temporary file Exec(FExpand(exepath), exeargs + ' >__output'); exitcode := DosExitCode(); //Get output from file Assign(F, '__output'); Reset(F); Repeat Readln(F, readbuffer); output := output + readbuffer; readbuffer := ''; Until Eof(F); //Set Result Result.exitcode := exitcode; Result.output := output; end; var I : AnsiString; R : ShellResult; begin Writeln('Enter a command line to run.'); Readln(I); R := DoShell(I); Writeln('Command Exit Code:'); Writeln(R.exitcode); Writeln('Command Output:'); Writeln(R.output); end.
source share