Delphi 如何一次执行多条CMD命令
我想用delphi做一个像cmd的命令输入框并获取内容。输入命令后判断是否符合要求,然后再输入命令。命令不是独立的,需要第一条输入返回后才能输入第二条。这个怎么做呢?...
我想用delphi 做一个像cmd的命令输入框并获取内容。输入命令后判断是否符合要求,然后再输入命令。命令不是独立的,需要第一条输入返回后才能输入第二条。
这个怎么做呢? 展开
这个怎么做呢? 展开
1个回答
展开全部
unit uDOS;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Edit1: TEdit;
btnRun: TButton;
Label1: TLabel;
lblCount: TLabel;
btnSave: TButton;
SaveDialog1: TSaveDialog;
procedure btnRunClick(Sender: TObject);
procedure btnSaveClick(Sender: TObject);
private
procedure RunDosCommand(Command: String; Output: TStrings);
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btnRunClick(Sender: TObject);
var
cm: String;
sl: TStrings;
begin
cm := Edit1.Text;
if cm = '' then exit;
sl := TStringList.Create;
cm := 'cmd /c ' + cm;
RunDosCommand(cm, sl);
lblCount.Caption := '0';
Memo1.Lines.Clear;
Memo1.Lines.AddStrings(sl);
lblCount.Caption := IntToStr(sl.Count) + ' ÐмǼ';
sl.Free;
end;
procedure TForm1.RunDosCommand(Command: String; Output: TStrings);
var
hReadPipe: THandle;
hWritePipe: THandle;
SI: TStartUpInfo;
PI: TProcessInformation;
SA: TSecurityAttributes;
BytesRead: DWORD;
Avail,ExitCode,wrResult: DWORD;
Dest: Array[0..1023] of Char;
CmdLine: Array[0..512] of Char;
TmpList: TStringList;
osVer: TOSVERSIONINFO;
tmpstr: AnsiString;
begin
osVer.dwOSVersionInfoSize := Sizeof(TOSVERSIONINFO);
GetVersionEX(osVer);
if osVer.dwPlatformId = VER_PLATFORM_WIN32_NT then begin
SA.nLength := SizeOf(SA);
SA.lpSecurityDescriptor := nil;//@SD;
SA.bInheritHandle := True;
CreatePipe(hReadPipe, hWritePipe, @SA, 0);
end else CreatePipe(hReadPipe, hWritePipe, nil, 1024);
try
Screen.Cursor := crHourglass;
FillChar(SI, SizeOf(SI), 0);
SI.cb := SizeOf(TStartUpInfo);
SI.wShowWindow := SW_HIDE;
SI.dwFlags := STARTF_USESHOWWINDOW;
SI.dwFlags := SI.dwFlags or STARTF_USESTDHANDLES;
SI.hStdOutput := hWritePipe;
SI.hStdError := hWritePipe;
StrPCopy(CmdLine, Command);
if CreateProcess(nil, CmdLine, nil, nil, True,
NORMAL_PRIORITY_CLASS, nil, nil, SI, PI) then
begin
ExitCode := 0;
while ExitCode = 0 do begin
wrResult := WaitForSingleObject(PI.hProcess, 500);
if PeekNamedPipe(hReadPipe, @Dest[0], 1024, @Avail,
nil, nil) then begin
if Avail > 0 then begin
TmpList := TStringList.Create;
try
FillChar(Dest, SizeOf(Dest), 0);
ReadFile(hReadPipe, Dest[0], Avail, BytesRead, nil);
TmpStr := Copy(Dest, 0, BytesRead-1);
TmpList.Text := TmpStr;
Output.AddStrings(TmpList);
finally
TmpList.Free;
end;
end;
end;
if wrResult <> WAIT_TIMEOUT then ExitCode := 1;
end;
GetExitCodeProcess(PI.hProcess, ExitCode);
CloseHandle(PI.hProcess);
CloseHandle(PI.hThread);
end;
finally
CloseHandle(hReadPipe);
CloseHandle(hWritePipe);
Screen.Cursor := crDefault;
end;
end;
procedure TForm1.btnSaveClick(Sender: TObject);
begin
if Memo1.Lines.Count = 0 then exit;
if SaveDialog1.Execute then begin
Memo1.Lines.SaveToFile(SaveDialog1.Filename);
end;
end;
end.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询