How do I write to StdOut from a GUI app started from the command line?

后端 未结 8 2158
一向
一向 2021-02-09 05:52

I am writing a standard windows app in Delphi 7.

If I was writing a console app, I can call the following to output to the cmd line or output file.

write         


        
8条回答
  •  逝去的感伤
    2021-02-09 06:26

    FWIW, I played around with this problem and happened upon AttachConsole which seems to do the trick. The only problem I ran into with my code is that the program won't give the console up without an extra ENTER key or two. It's not real polished since I was trying to fix that problem and (kind of) gave up. Perhaps someone here will see it?

    program writecon; uses windows, dialogs;
    
      function AttachConsole(dwProcessID: DWord): BOOL; stdcall; external 'kernel32.dll';
    
      function load_attach_console: boolean;
        begin
          Result := AttachConsole(-1);
        end;
    
      begin
        // the function requires XP or greater, you might want to check for that here.
        if load_attach_console = true then
          begin
            writeln;
            writeln('This is running in the console.');
            write('Press ENTER to continue.');
            readln;
            // from the linked page, you have to detach yourself from the console
            // when you're done, this is probably where the problem is.
            Flush(Output);
            Flush(Input);
            FreeConsole;
          end
        else
          MessageDlg('This is not running in the console.', mtInformation, [mbOk], 0);
      end.
    

提交回复
热议问题