如何从命令行启动的GUI应用程序写入StdOut?

Tob*_*len 5 delphi

我正在Delphi 7中编写一个标准的Windows应用程序.

如果我正在编写控制台应用程序,我可以调用以下内容输出到cmd行或输出文件.

writeln('Some info');
Run Code Online (Sandbox Code Playgroud)

如果我从我从命令行启动的标准GUI应用程序执行此操作,则会出现错误.

I/O Error 105
Run Code Online (Sandbox Code Playgroud)

必须有一个简单的解决方案来解决这个问题.基本上我希望我的应用程序有两种模式,GUI模式和非GUI模式.如何正确设置以便我可以回写cmd窗口?

Tad*_*ams 11

这个问题与我试图完成的事情非常相似(如果不完全相同).我想检测我的应用程序是否是从cmd.exe执行并将输出发送到父控制台,否则它将显示一个gui.这里的答案帮助我解决了我的问题.这是我作为实验提出的代码:

ParentChecker.dpr

program ParentChecker;

uses
  Vcl.Forms,
  SysUtils,
  PsAPI,
  Windows,
  TLHelp32,
  Main in 'Main.pas' {frmParentChecker};

{$R *.res}

function AttachConsole(dwProcessID: Integer): Boolean; stdcall; external 'kernel32.dll';
function FreeConsole(): Boolean; stdcall; external 'kernel32.dll';

function GetParentProcessName(): String;
const
  BufferSize = 4096;
var
  HandleSnapShot: THandle;
  EntryParentProc: TProcessEntry32;
  CurrentProcessId: THandle;
  HandleParentProc: THandle;
  ParentProcessId: THandle;
  ParentProcessFound: Boolean;
  ParentProcPath: String;
begin
  ParentProcessFound:=False;
  HandleSnapShot:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
  if HandleSnapShot<>INVALID_HANDLE_VALUE then
  begin
    EntryParentProc.dwSize:=SizeOf(EntryParentProc);
    if Process32First(HandleSnapShot,EntryParentProc) then
    begin
      CurrentProcessId:=GetCurrentProcessId();
      repeat
        if EntryParentProc.th32ProcessID=CurrentProcessId then
        begin
          ParentProcessId:=EntryParentProc.th32ParentProcessID;
          HandleParentProc:=OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,False,ParentProcessId);
          if HandleParentProc<>0 then
          begin
            ParentProcessFound:=True;
            SetLength(ParentProcPath,BufferSize);
            GetModuleFileNameEx(HandleParentProc,0,PChar(ParentProcPath),BufferSize);
            ParentProcPath:=PChar(ParentProcPath);
            CloseHandle(HandleParentProc);
          end;
          Break;
        end;
      until not Process32Next(HandleSnapShot,EntryParentProc);
    end;
    CloseHandle(HandleSnapShot);
  end;
  if ParentProcessFound then Result:=ParentProcPath
  else Result:='';
end;

function IsPrime(n: Integer): Boolean;
var
  i: Integer;
begin
  Result:=False;
  if n<2 then Exit;
  Result:=True;
  if n=2 then Exit;
  i:=2;
  while i<(n div i + 1) do
  begin
    if (n mod i)=0 then
    begin
      Result:=False;
      Exit;
    end;
    Inc(i);
  end;
end;

var
  i: Integer;
  ParentName: String;

begin
  ParentName:=GetParentProcessName().ToLower;
  Delete(ParentName,1,ParentName.LastIndexOf('\')+1);
  if ParentName='cmd.exe' then
  begin
    AttachConsole(-1);
    Writeln('');
    for i:=1 to 100 do if IsPrime(i) then Writeln(IntToStr(i)+' is prime');
    FreeConsole();
  end
  else
  begin
    Application.Initialize;
    Application.MainFormOnTaskbar:=True;
    Application.CreateForm(TfrmParentChecker, frmParentChecker);
    frmParentChecker.Label1.Caption:='Executed from '+ParentName;
    Application.Run;
  end;
end.
Run Code Online (Sandbox Code Playgroud)

Main.pas(带标签的表格):

unit Main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, RzLabel;

type
  TfrmParentChecker = class(TForm)
    Label1: TLabel;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmParentChecker: TfrmParentChecker;

implementation

{$R *.dfm}

end.
Run Code Online (Sandbox Code Playgroud)

这允许我从命令提示符运行我的GUI应用程序并将输出显示到我的应用程序启动的同一控制台.否则,它将运行应用程序的完整GUI部分.

控制台窗口的输出示例:

I:\Delphi\Tests and Demos\ParentChecker\Win32\Debug>start /wait ParentChecker.exe

2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
23 is prime
29 is prime
31 is prime
37 is prime
41 is prime
43 is prime
47 is prime
53 is prime
59 is prime
61 is prime
67 is prime
71 is prime
73 is prime
79 is prime
83 is prime
89 is prime
97 is prime

I:\Delphi\Tests and Demos\ParentChecker\Win32\Debug>
Run Code Online (Sandbox Code Playgroud)


Ond*_*lle 6

调用AllocConsole避免错误105.


Dav*_*nan 5

GUI子系统应用程序没有可靠的方法来附加到其父进程的控制台.如果您尝试这样做,最终会有两个共享同一控制台的活动进程.这导致了麻烦.

根据bummi的建议,替代方案虽然只保留了一个可执行文件,但是如果要求它以GUI模式运行,则可以使用一个控制台应用程序释放其控制台.这是一种更好的方法,但是当您想要在GUI模式下运行时,会导致控制台窗口闪烁,然后关闭.

我在Stack Overflow上遇到的关于这个主题的最佳讨论是Rob Kennedy的极好答案:一个可执行文件可以同时是控制台和GUI应用程序吗?

我相信,根据您在评论中的说法,您最好的选择是创建两个单独的可执行文件.一个用于GUI子系统,另一个用于控制台子系统.这是采取的方法:

  • Java:java.exe,javaw.exe.
  • Python:python.exe,pythonw.exe.
  • Visual Studio:devenv.com,devenv.exe.

是的,你必须运送多个可执行文件.但这样做可以为用户提供最佳体验.