program MY_console; {$APPTYPE CONSOLE} uses SysUtils, Windows, shellapi; var ReadOut, WriteOut, ReadIn, WriteIn: THandle; ProcessInfo: TProcessInformation; Security: TSecurityAttributes; StartUpInfo: TStartUpInfo; s: string; label start; //----------------------------------------------------读取管道 function ReadFromPipe(Pipe: THandle): string; var Buffer: PChar; BytesRead: DWord; ReadBuffer: Cardinal; begin Result := ''; if GetFileSize(Pipe, nil) = 0 then Exit; Buffer := AllocMem(ReadBuffer + 1); repeat BytesRead := 0; ReadFile(Pipe, Buffer[0], ReadBuffer, BytesRead, nil); if BytesRead > 0 then begin Buffer[BytesRead] := #0; OemToAnsi(Buffer, Buffer); Result := string(Buffer); end; until (BytesRead < ReadBuffer); FreeMem(Buffer); end; //---------------------------------------------------写管道 procedure WriteToPipe(Pipe: THandle; Value: string); var len: integer; BytesWrite: DWord; Buffer: PChar; begin len := Length(Value) + 1; Buffer := PChar(Value + #10); WriteFile(Pipe, Buffer[0], len, BytesWrite, nil); end; //--------------------------------------------------- begin { TODO -oUser -cConsole Main : Insert code here } begin with Security do begin nLength := SizeOf(TSecurityAttributes); bInheritHandle := true; lpSecurityDescriptor := nil; end; Createpipe(ReadOut, WriteOut, @Security, 0); Createpipe(ReadIn, WriteIn, @Security, 0); FillChar(StartUpInfo, Sizeof(StartUpInfo), #0); StartUpInfo.cb := SizeOf(StartUpInfo); with StartUpInfo do begin hStdOutput := WriteOut; hStdInput := ReadIn; hStdError := WriteOut; dwFlags := STARTF_USESTDHANDLES + STARTF_USESHOWWINDOW; wShowWindow := SW_HIDE; end; CreateProcess(nil, PChar('cmd'), @Security, @Security, true, NORMAL_PRIORITY_CLASS, nil, nil, StartUpInfo, ProcessInfo); {CreateProcess(nil, PChar('cmd'), @Security, @Security, true, DETACHED_PROCESS, nil, nil, StartUpInfo, ProcessInfo); 英文提示} //--------------------------------------------------------------------控制台输入 begin start: readln(s); WriteToPipe(WriteIn, s); Writeln(ReadFromPipe(ReadOut)); //readln; s := ''; goto start; end; end; //------------------------------------ end.
lose