古詩詞大全網 - 四字成語 - Delphi調用DOS並輸出結果集

Delphi調用DOS並輸出結果集

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls;

type

TForm1 = class(TForm)

Memo1:TMemo;

procedure FormClose(Sender:TObject; var Action:TCloseAction);

procedure FormCreate(Sender:TObject);

procedure ress(Sender:TObject; var Key:Char);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1 :TForm1;

ReadOut, WriteOut :THandle;

ReadIn, WriteIn :THandle;

ProcessInfo :TProcessInformation;

implementation

{$R *.dfm}

procedure InitConsole; {設置 console 啟動屬性}

var

Secu :TSecurityAttributes;

start :TStartUpInfo;

buf :array[0..MAX_PATH] of Char;

begin // MAX_PATH

with Secu do

begin

nlength := SizeOf(TSecurityAttributes);

binherithandle := true;

lpsecuritydescriptor := nil;

end;

Createpipe(ReadOut, WriteOut, @Secu, 0); {創建壹個命名管道用來捕獲console程序的輸出}

Createpipe(ReadIn, WriteIn, @Secu, 0); {創建第二個命名管道用來捕獲console程序的輸入}

GetEnvironmentVariable('comspec', buf, SizeOf(buf));

ZeroMemory(@start, SizeOf(start));

start.cb := SizeOf(start);

start.hStdOutput := WriteOut;

start.hStdInput := ReadIn;

start.hStdError := WriteOut;

start.dwFlags := STARTF_USESTDHANDLES +

STARTF_USESHOWWINDOW;

start.wShowWindow := SW_HIDE;

CreateProcess(nil, buf,

@Secu, @Secu, true,

NORMAL_PRIORITY_CLASS,

nil, nil, start, ProcessInfo);

end;

function ReadFromPipe:string;

var

buf :array[0..1024] of Char;

BytesRead :DWord;

begin

Result := '';

while

BytesRead > 0 do

begin //循環讀取數據。

if ReadFile(ReadOut, buf, SizeOf(buf), BytesRead, nil) then

begin

Result := Result + Copy(buf, 1, BytesRead);

WaitForSingleObject(ProcessInfo.hProcess, 10);

// Form1.Memo1.Lines.Add('a ' + inttostr(BytesRead));

end

else

break;

PeekNamedPipe(ReadOut, nil, 0, nil, @BytesRead, nil); //檢查管道裏是否有數據。

// Form1.Memo1.Lines.Add('b ' + inttostr(BytesRead));

end;

end;

procedure WriteCMD(Value:string);

var

len :integer;

BytesWrite :DWord;

Buffer :PChar;

begin

len := Length(Value) + 2;

Buffer := PChar(Value + #13#10); //命令與內容不出現在壹行。

WriteFile(WriteIn, Buffer[0], len, BytesWrite, nil);

end;

procedure CloseConsole;

begin

TerminateProcess(ProcessInfo.hProcess, 0);

CloseHandle(ProcessInfo.hProcess);

CloseHandle(ProcessInfo.hThread);

CloseHandle(ReadIn);

CloseHandle(WriteIn);

CloseHandle(ReadOut);

CloseHandle(WriteOut);

end;

procedure TForm1.FormClose(Sender:TObject; var Action:TCloseAction);

begin

CloseConsole;

end;

procedure TForm1.FormCreate(Sender:TObject);

begin

InitConsole;

end;

procedure TForm1.ress(Sender:TObject; var Key:Char);

var

s, ss :string;

i, j :integer;

begin

if Key = #13 then

begin

i := Memo1.Lines.Count - 1;

s := Memo1.Lines[i];

delete(s, 1, pos('>', s));

WriteCMD(TRIM(s));

//WaitForSingleObject(ProcessInfo.hProcess, 10000);//WAIT_TIMEOUT);

sleep(1);

// ss := TRIM(ReadFromPipe);

// for j = (Memo1.Lines.Count-1) downto 0 do

// begin

// if Memo1.Lines[j] = '' then Memo1.Lines.delete(j);

// end;

Memo1.Lines.Add(TRIM(ReadFromPipe));

end;

end;

end.