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;
In 2017, with the release of Delphi 10.2 Tokyo, Embarcadero introduced a specialized implementation of the Observer pattern into the System.Classes unit. While it has been in the wild for 9 years, it remains a "hidden" architecture for many, primarily because it serves as the invisible engine behind LiveBindings. Other than live bindings, you can also use the Observer pattern as a way to update component settings to the Windows registry, an .ini file, or persist it elsewhere.
System.Classes