如何检查我的 Delphi 控制台应用程序是否重定向到文件或管道?

发布于 2024-12-29 02:08:47 字数 115 浏览 6 评论 0原文

我有一个控制台应用程序,当输出重定向(外部)到文件或管道时(myapp.exe > Foo.bar),必须禁用或启用某些操作

我如何检查我的 Delphi 控制台应用程序是否重定向到文件或管道?

I have a console app that must disable or enable some operations when output is redirected (externally) to a file or pipe (myapp.exe > Foo.bar)

How I can check if my Delphi console app is redirected to a file or pipe?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

归属感 2025-01-05 02:08:47

您可以使用 GetStdHandle GetFileType 函数。

首先,使用 GetStdHandle 函数检索控制台输出句柄,然后可以使用 GetFileType 函数检查句柄的类型。

{$APPTYPE CONSOLE}

{$R *.res}

uses
  Windows,
  SysUtils;


function ConsoleRedirected: Boolean;
var
  FileType : DWORD;
begin
  FileType:= GetFileType(GetStdHandle(STD_OUTPUT_HANDLE));
  Result  := (FileType=FILE_TYPE_PIPE) or (FileType=FILE_TYPE_DISK);
end;


begin
  try
    if ConsoleRedirected then
      Writeln('Hello From File')
    else
      Writeln('Hello Console');
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

you can use the GetStdHandle and GetFileType functions.

first you retrieve the console output handle using the GetStdHandle function and then you can check the type of the handle with the GetFileType function.

{$APPTYPE CONSOLE}

{$R *.res}

uses
  Windows,
  SysUtils;


function ConsoleRedirected: Boolean;
var
  FileType : DWORD;
begin
  FileType:= GetFileType(GetStdHandle(STD_OUTPUT_HANDLE));
  Result  := (FileType=FILE_TYPE_PIPE) or (FileType=FILE_TYPE_DISK);
end;


begin
  try
    if ConsoleRedirected then
      Writeln('Hello From File')
    else
      Writeln('Hello Console');
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.
相思故 2025-01-05 02:08:47

我在这里介绍的方法感觉很老套,但我找不到更好的方法来检测标准输出是否已从屏幕控制台重定向。使用 GetFileType 的方法无法检测所有形式的重定向,因为某些重定向是针对 FILE_TYPE_CHAR 类型的设备。


调用 GetConsoleMode()传递标准输出句柄。如果 GetConsoleMode() 失败,则您的控制台已重定向。

program RedirectionDetection;
{$APPTYPE CONSOLE}
uses
  Windows;

function ConsoleRedirected: Boolean;
var
  Mode: DWORD;
begin
  Result := not GetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), Mode);
end;

begin
  if ConsoleRedirected then begin
    Writeln('I have been redirected');
  end else begin
    Writeln('I am a console');
    Readln;
  end;
end.

The approach I present here feels hacky but I can't find a better way to detect whether or not the standard output has been redirected away from a screen console. The approach using GetFileType cannot detect all forms of redirection since some redirections are to devices of type FILE_TYPE_CHAR.


Call GetConsoleMode() passing the standard output handle. If GetConsoleMode() fails then your console has been redirected.

program RedirectionDetection;
{$APPTYPE CONSOLE}
uses
  Windows;

function ConsoleRedirected: Boolean;
var
  Mode: DWORD;
begin
  Result := not GetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), Mode);
end;

begin
  if ConsoleRedirected then begin
    Writeln('I have been redirected');
  end else begin
    Writeln('I am a console');
    Readln;
  end;
end.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文