我想开始一个cmd
进程,并继续向它提供一些不同的命令。我想在文本输出后立即输出所有文本cmd
。到目前为止,我有这个:
function GetDosOutput(const ACommandLine: string;
AWorkDir: string = 'C:\'): string;
var
_SA: TSecurityAttributes;
_SI: TStartupInfo;
_PI: TProcessInformation;
_StdOutPipeRead, StdOutPipeWrite: THandle;
_WasOK: boolean;
_Buffer: array [0 .. 255] of AnsiChar;
_BytesRead: Cardinal;
_Handle: boolean;
begin
Result := '';
_SA.nLength := SizeOf(_SA);
_SA.bInheritHandle := True;
_SA.lpSecurityDescriptor := nil;
CreatePipe(_StdOutPipeRead, StdOutPipeWrite, @_SA, 0);
try
FillChar(_SI, SizeOf(_SI), 0);
_SI.cb := SizeOf(_SI);
_SI.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
_SI.wShowWindow := SW_HIDE;
_SI.hStdInput := GetStdHandle(STD_INPUT_HANDLE);
_SI.hStdOutput := StdOutPipeWrite;
_SI.hStdError := StdOutPipeWrite;
_Handle := CreateProcess(nil, PChar('cmd.exe /C ' + ACommandLine), nil, nil,
True, 0, nil, PChar(AWorkDir), _SI, _PI);
CloseHandle(StdOutPipeWrite);
if _Handle then
try
repeat
_WasOK := ReadFile(_StdOutPipeRead, _Buffer, 255, _BytesRead, nil);
if _BytesRead > 0 then
begin
_Buffer[_BytesRead] := #0;
Result := Result + string(_Buffer);
Application.ProcessMessages;
end;
until not _WasOK or (_BytesRead = 0);
WaitForSingleObject(_PI.hProcess, INFINITE);
finally
CloseHandle(_PI.hThread);
CloseHandle(_PI.hProcess);
end;
finally
CloseHandle(_StdOutPipeRead);
end;
end;
如何保持相同的cmd.exe
进程并为其分配不同的命令,例如
1)ping stackoverflow.com,打印一行后立即查看其内容
2)ipconfig /all
吗?