Delphi 如何一次执行多条CMD命令
答案:1 悬赏:0 手机版
解决时间 2021-02-13 14:01
- 提问者网友:謫仙
- 2021-02-13 06:28
Delphi 如何一次执行多条CMD命令
最佳答案
- 五星知识达人网友:野味小生
- 2021-02-13 07:45
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.追问
但是按照cmd上输入 无法运行
这个该怎么解决?
追答在DOS命令下方输入后,按“运 行"键(可以自己加入回车运行)。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯