<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Hi All,<br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">I have got the following code from the wiki for creating and reading from an external process. Effectively, I want to capture all of the output from the process "handle.exe", into a StringList so that I can read it to make sure that everything went OK, however, when I run the following code, I am getting a "RunError(103) on the line: WriteLn('-- Going to run: ' + OurCommand);<br>
<br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Any ideas what I'm doing wrong please?<br><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Lazarus 1.0.12 with FPC 2.6.2 Windows XP<br>
</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br>procedure TfrmMain.Button1Click(Sender: TObject);<br>const<br> READ_BYTES = 2048;<br><br>var<br> OurCommand: String;<br> OutputLines: TStringList;<br>
MemStream: TMemoryStream;<br> OurProcess: TProcess;<br> NumBytes: LongInt;<br> BytesRead: LongInt;<br><br>begin<br> MemStream := TMemoryStream.Create;<br> BytesRead := 0;<br><br> OurProcess := TProcess.Create(nil);<br>
OurCommand:='C:\Documents and Settings\RichardMace\My Documents\Downloads\SysinternalsSuite\handle.exe';<br> WriteLn('-- Going to run: ' + OurCommand);<br> OurProcess.CommandLine := OurCommand;<br><br>
OurProcess.Options := [poUsePipes];<br> WriteLn('-- External program run started');<br><br> OurProcess.Execute;<br> while OurProcess.Running do<br> begin<br> // make sure we have room<br> MemStream.SetSize(BytesRead + READ_BYTES);<br>
<br> // try reading it<br> NumBytes := OurProcess.Output.Read((MemStream.Memory + BytesRead)^, READ_BYTES);<br> if NumBytes > 0 then<br> begin<br> Inc(BytesRead, NumBytes);<br> Write('.'); //Output progress to screen.<br>
end<br> else begin<br> // no data, wait 100 ms<br> Sleep(100);<br> end;<br> end;<br> // read last part<br> repeat<br> // make sure we have room<br> MemStream.SetSize(BytesRead + READ_BYTES);<br>
// try reading it<br> NumBytes := OurProcess.Output.Read((MemStream.Memory + BytesRead)^, READ_BYTES);<br> if NumBytes > 0 then<br> begin<br> Inc(BytesRead, NumBytes);<br> Write('.');<br>
end;<br> until NumBytes <= 0;<br> if BytesRead > 0 then WriteLn;<br> MemStream.SetSize(BytesRead);<br> memMain.Lines.Add('-- External program run complete');<br><br> OutputLines := TStringList.Create;<br>
OutputLines.LoadFromStream(MemStream);<br> memMain.Lines.Add('-- External program output line count = ' + IntToStr(OutputLines.Count) + ' --');<br> for NumBytes := 0 to OutputLines.Count - 1 do<br> begin<br>
WriteLn(OutputLines[NumBytes]);<br> end;<br> WriteLn('-- Program end');<br> OutputLines.Free;<br> OurProcess.Free;<br> MemStream.Free;<br>end;<br><br></div></div>