我使用下面的代码将命令发送到控制台应用程序并以字符串形式接收其输出。
除控制台应用程序没有关闭或退出命令外,一切正常。只能使用 Ctrl - C 来停止/关闭(完成作业并正确关闭)。我被困在这里,我可以将这些关键事件发送到控制台。
这是线程内的代码。
procedure DoWork.Execute;
const
MAX_CHUNK: dword = 4096;
var
Buffer: array [0..MaxBufferSize] of byte;
SecurityAttributes: SECURITY_ATTRIBUTES;
hiRead, hoRead, hiWrite, hoWrite: THandle;
StartupInfo: TSTARTUPINFO;
BytesRead, BytesWritten, ExitCode, PipeMode: dword;
ComSpec:array [0..MAX_PATH] of char;
ToSend, Tempstr,WorkDir,cmd2,fsize: string;
i,acc: integer;
c: cardinal;
begin
cmd2 := 'rtmpdump.exe --live -v -r "rtmp://"' ;
WorkDir := 'C:\Users\Administrator\Desktop\rtmpdump';
SecurityAttributes.nLength := SizeOf(SECURITY_ATTRIBUTES);
SecurityAttributes.lpSecurityDescriptor := nil;
SecurityAttributes.bInheritHandle := True;
CreatePipe(hiRead, hiWrite, @SecurityAttributes, 0);
CreatePipe(hoRead, hoWrite, @SecurityAttributes, 0);
GetStartupInfo(StartupInfo);
StartupInfo.hStdOutput := hoWrite;
StartupInfo.hStdError := hoWrite;
StartupInfo.hStdInput := hiRead;
StartupInfo.dwFlags := STARTF_USESHOWWINDOW + STARTF_USESTDHANDLES;
StartupInfo.wShowWindow := SW_HIDE;
// GetEnvironmentVariable('COMSPEC', ComSpec, sizeof(ComSpec));
CreateProcess(nil, PChar('C:\Users\Administrator\Desktop\rtmpdump\rtmpdump.exe ' + cmd2),
nil, nil, True,CREATE_NEW_CONSOLE, nil,
PChar(WorkDir), StartupInfo, ProcessInfo);
CloseHandle(hoWrite);
CloseHandle(hiRead);
PipeMode := PIPE_NOWAIT;
SetNamedPipeHandleState(hoRead, PipeMode , nil, nil);
while true do
begin
Sleep(10);
GetExitCodeProcess(ProcessInfo.hProcess, ExitCode);
if ExitCode <> STILL_ACTIVE then Break;
ReadFile(hoRead, Buffer, MAX_CHUNK, BytesRead, nil);
if BytesRead > 0 then
begin
for i := 0 to BytesRead - 1 do
TempStr := Tempstr + string(char(buffer[i]));
end else
begin
if TempStr <> '' then
begin
try
except
break;
end;
TempStr := '';
end;
end;
if comand <> '' then
begin
WriteFile(hiWrite, pchar(Comando)^, length(comando), BytesWritten, nil);
WriteFile(hiWrite, #13#10, 2, BytesWritten, nil);
comando := '';
end;
end;
GetExitCodeProcess(ProcessInfo.hProcess, ExitCode);
if ExitCode = STILL_ACTIVE then TerminateProcess(ProcessInfo.hProcess, 0);
CloseHandle(hoRead);
CloseHandle(hiWrite);
end;
PS:我试过了 - &gt; generateconsolectrlevent