title: How in Delphi 2009 redirect console (stin, sterr)?
body: <p>I try several samples in the internet and none of them work - the scripts are not executed- (maybe because are for pre Delphi 2009 unicode?).</p>
  
  <p>I need to run some python scripts and pass arguments to them, like:</p>
  
  <pre><code>python "..\Plugins\RunPlugin.py" -a login -u Test -p test
  </code></pre>
  
  <p>And capture the output to a string &amp; the errors to other.</p>
  
  <p>This is what I have now:</p>
  
  <pre><code>procedure RunDosInMemo(DosApp:String; var OutData: String);
  var
    SA: TSecurityAttributes;
    SI: TStartupInfo;
    PI: TProcessInformation;
    StdOutPipeRead, StdOutPipeWrite: THandle;
    WasOK: Boolean;
    Buffer: array[0..255] of Char;
    BytesRead: Cardinal;
    WorkDir: string;
    Handle: Boolean;
  begin
    OutData := '';
    with SA do begin
      nLength := SizeOf(SA);
      bInheritHandle := True;
      lpSecurityDescriptor := nil;
    end;
    CreatePipe(StdOutPipeRead, StdOutPipeWrite, @SA, 0);
    try
      with SI do
      begin
        FillChar(SI, SizeOf(SI), 0);
        cb := SizeOf(SI);
        dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES or CREATE_UNICODE_ENVIRONMENT;
        wShowWindow := SW_HIDE;
        hStdInput := GetStdHandle(STD_INPUT_HANDLE); // don't redirect stdin
        hStdOutput := StdOutPipeWrite;
        hStdError := StdOutPipeWrite;
      end;
      WorkDir := 'C:\';
      Handle := CreateProcess(nil, PChar(DosApp),
                              nil, nil, True, 0, nil,
                              PChar(WorkDir), SI, PI);
      CloseHandle(StdOutPipeWrite);
      if Handle then
      begin
        try
          repeat
            WasOK := ReadFile(StdOutPipeRead, Buffer, 255, BytesRead, nil);
            if BytesRead &gt; 0 then
            begin
              Buffer[BytesRead] := #0;
              OutData := OutData + String(Buffer);
            end;
          until not WasOK or (BytesRead = 0);
          WaitForSingleObject(PI.hProcess, INFINITE);
        finally
          CloseHandle(PI.hThread);
          CloseHandle(PI.hProcess);
        end;
      end else begin
        raise Exception.Create('Failed to load python plugin');
      end;
    finally
      CloseHandle(StdOutPipeRead);
    end;
  end;
  </code></pre>