db2trade
asked on
Capturing "Route print"
Is is possible to capture the output of MS-DOS command "Route print" using Delphi ?
Thanks
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
originally taken from here:
https://www.experts-exchange.com/questions/10105267/Capturing-dos-output.html
after the button is clicked, you have the output of the command in c:\output.txt
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen}
public
{ Public-Deklarationen}
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
type TRunThread = class(TThread)
private
processHandle : cardinal;
processReady : boolean;
waitingThread : cardinal;
procedure Execute; override;
end;
procedure TRunThread.Execute;
begin
WaitForSingleObject(proces
processReady:=true; // Set "processReady" flag for main thread
PostThreadMessage(waitingT
// If you call Application.HandleMessage (see below) in the
// main thread, the main thread is sleeping the most time in
// winAPI "waitMessage". So we send a "dummy" message in order
// to let the main thread return from Application.HandleMessage
end;
procedure TForm1.Button1Click(Sender
var si : TStartupInfo;
pi : TProcessInformation;
begin
caption:='start "route print"...';
ZeroMemory(@si,sizeOf(si))
si.dwFlags:=STARTF_USESHOW
if CreateProcess(nil,'cmd /c route print > c:\output.txt',nil,nil,fal
caption:='"route print" started...';
with TRunThread.Create(true) do // create the thread object, but do not start it now...
try
processHandle:=pi.hProcess
processReady:=false; // flag for the loop (see below)
waitingThread:=GetCurrentT
caption:='wait for "route print"...';
Resume; // now all information is prepared; so let's start the thread
repeat
Application.HandleMessage;
until Application.Terminated or processReady; // continue with normal program when either the
// started process has stopped or our program is closed
caption:='"route print"...';
finally Free end;
CloseHandle(pi.hThread); CloseHandle(pi.hProcess); // Never forget to close handles...
caption:='ready...';
end else caption:='could not start "route print" command...';
end;
end.