Can a Windows GUI program written in Lazarus create a console and write to it at runtime?

StackOverflow https://stackoverflow.com/questions/20134421

  •  03-08-2022
  •  | 
  •  

Domanda

When writing a Lazarus program, you have the option of using $APPTYPE console or deselecting the Win32 GUI Application (-WG option) in the Project Options .

I noticed that without these options DebugLn and WriteLn raise exceptions. Is there a way to create a console even if the program is not compiled above mentioned options and output to it with DebugLn and WriteLn afterwards?

È stato utile?

Soluzione

In windows it is a little more tricky compared to Delphi. In Delphi all you need to do is to call AllocConsole. Using Lazarus/FreePascal you need to do a little extra work:

uses
  Windows;
begin
  AllocConsole;      // in Windows unit
  IsConsole := True; // in System unit
  SysInitStdIO;      // in System unit
  // Now you can do Writeln, DebugLn, ...
end.

Unfortunately I cannot help you on other platforms. Although iirc in linux console is always present for a program even if not visible. So it should work without extra code. However I cannot test this atm so take it with a grain of salt.

Altri suggerimenti

Well, obviously setting project as gui application rather then program is much better for programming. All needed uses clauses are alredy present.

So that kind of problem, getting power of lazarus, but working as console application I solve with adding Application.ShowMainForm:=False; before

Application.CreateForm(TForm1, Form1);

in .lpr file.

Everything works fine, even showmessage (everything) can be used.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top