Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2008-07

writeln to stdout in windows?

9 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Seth Grover

I'm confused... it seems like this used to work.

In a Linux (GTK1) application I create in lazarus (0.9.25 rev 15904), I can have something like this:

procedure TForm1.Button1Click(Sender: TObject);
begin
  writeln('hello');
end;

and when I click the button, I will see the message written to standard out. In Windows (0.9.25 rev 15839 snapshot) the same thing will cause a runtime 103 error ("file not open"). I've tried running it from the command line in windows as well as just double-clicking the executable in the file explorer, with no difference in behavior.

-SG

========================
Computer over. Virus = very yes.

Seth Grover
sethdgrover[at]gmail[dot]com

Reply by email

Seth Grover
to Seth Grover

hm. if i compile with -WC, then it opens up a separate console window (even though I also have "windows gui application", -WG, checked) and the writeln goes to that console window.

that's not what i want, though... if i run from the command line, i want to see the messages written to the console. if i just run the executable from the windows gui, i don't want to see a console, but i don't want the writeln's to raise an exception.

-sg

========================
Computer over. Virus = very yes.

Seth Grover
sethdgrover[at]gmail[dot]com

Reply by email

Felipe Monteiro de Carvalho
to Seth Grover: I'm confused... it seems like this used to work. In a Linux (GTK1) application I create…

Unlike Linux, GUI software in Windows have by default no console, and therefore no StdOut (or you can think that StdOut is nil). This a platform characteristic.

To add a console to your program add this to your program file:

{$apptype console}

This is explained in the upcoming Lazarus book in the Input/Output subchapter.

Felipe Monteiro de Carvalho

Reply by email

Seth Grover
to Felipe Monteiro de Carvalho

Okay, I understand. Thanks for the response.

-SG

Felipe Monteiro de Carvalho wrote 12 lines
Unlike Linux, GUI software in Windows have by default no console, and therefore no StdOut (or you can think that StdOut is nil). This a platform characteristic. To add a console to your program add this to your program file: {$apptype console} This is explained in the upcoming Lazarus book in the Input/Output subchapter. -- Felipe Monteiro de Carvalho

========================
Computer over. Virus = very yes.

Seth Grover
sethdgrover[at]gmail[dot]com
http://grovers.us/seth

Reply by email

Henry Vermaak
to Seth Grover: hm. if i compile with -WC, then it opens up a separate console window (even though I also…

2008/7/29 Seth Grover :

Show quoted text 8 lines
hm. if i compile with -WC, then it opens up a separate console window (even though I also have "windows gui application", -WG, checked) and the writeln goes to that console window. that's not what i want, though... if i run from the command line, i want to see the messages written to the console. if i just run the executable from the windows gui, i don't want to see a console, but i don't want the writeln's to raise an exception.

you can find out whether you program is run from the console by using the GetStdHandle function. it returns 0 if it's a gui app.

see: http://msdn.microsoft.com/en-us/library/ms683231(VS.85).aspx

it's tricky to do, but you can detect and attach to the console an app is run from, but you can't use writeln. look at this code (not tested on fpc):

program guiconsole;

uses
Forms, Windows, SysUtils,

  main in 'main.pas' {Form1};
{$R *.res}
const
  ATTACH_PARENT_PROCESS = DWORD(-1);
function AttachConsole(dwProcessId: DWORD): BOOL; stdcall; external
kernel32 name 'AttachConsole';
var
  h: THandle;
  dum: Cardinal;
  msg1: String = 'You''re running this from a command window!';
begin
      h := GetStdHandle(STD_OUTPUT_HANDLE);
      if h = 0
        then begin
          Application.Initialize;
          Application.CreateForm(TForm1, Form1);
          Application.Run;
        end
        else if h  INVALID_HANDLE_VALUE
          then begin
            AttachConsole(ATTACH_PARENT_PROCESS);
            h := GetStdHandle(STD_OUTPUT_HANDLE);
            if (h = 0) or (h = INVALID_HANDLE_VALUE)
              then MessageBox(0, PChar(Format('GetStdHandle error!
%d', [h])), 'Error', MB_OK);
            sleep(1000);
            if not WriteConsole(h, pchar(msg1), length(msg1), dum, nil)
              then MessageBox(0, PChar(Format('WriteConsole error!
%d', [h])), 'Error', MB_OK);
{            AllocConsole();
            Writeln('You''re running this from a command window!'); }
          end
          else MessageBox(0, PChar(Format('GetStdHandle error! %d',
[h])), 'Error', MB_OK);
end.

using writeconsole is a pain, though. i don't know if there's any way of making fpc use the attached console as stdout. i'll let you experiment...

henry

Reply by email

Martin Friebe
to Felipe Monteiro de Carvalho: The usual solution for this is doing like this: {$ifdef Debug} {$apptype console} {$endif}
debugln('foo');

from LCLProc may be your friend. It, is used in lazarus itself, too.

it does first look for a param "'--debug-log='", then enviroment, then <STDOUT>.
And it just keeps silent, if no output at all is avail.

For more info go to the LCLProc unit and check the initialization section at the end of file.

Regards
Martin

Felipe Monteiro de Carvalho wrote 13 lines
The usual solution for this is doing like this: {$ifdef Debug} {$apptype console} {$endif} {$ifdef Debug} WriteLn(....); {$endif} If you want to see the writelns you build with Debug defined.

Reply by email