To detect whether a console handle is being redirected, use the function in the following manner:

  case GetTypeOfConsoleHandle(TTextRec(Input).Handle) of
    tchUnknown: // redirected handle
  else
    // it's either an input or output handle to the console
  end;

Alternatively, pass a handle to GetTypeOfConsoleHandle where the parameter can be a THandle assigned any of the following:

  H1 := GetStdHandle(STD_INPUT_HANDLE);
  H2 := GetStdHandle(STD_OUTPUT_HANDLE);
  H3 := CreateFile('CONIN$', GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING,
    0, 0);
  H4 := CreateFile('CONOUT$', GENERIC_WRITE, FILE_SHARE_WRITE, nil, OPEN_ALWAYS,
    0, 0);

I've previously noticed that code posted on my blog has been copied elsewhere, so I won't be explaining the code. 


type
  TConsoleHandle = (tchUnknown, tchInput, tchOutput);

function GetTypeOfConsoleHandle(Handle: THandle): TConsoleHandle;
var
  LMode: Cardinal;
  LSBI: TConsoleScreenBufferInfo;
  LSize: COORD;
begin
  if GetConsoleMode(Handle, LMode) then
    begin
      if GetConsoleScreenBufferInfo(Handle, LSBI) then
         Result := tchOutput else
         Result := tchInput;
    end else
    begin
      if GetConsoleScreenBufferInfo(Handle, LSBI) then
        Result := tchOutput else
        begin
          LSize := GetLargestConsoleWindowSize(Handle);
          if (LSize.X=0) and (LSize.Y=0) then
            Result := tchUnknown else
            Result := tchOutput;
        end;
    end;
end;