Recent

Author Topic: FPC throws a runtime error with working Delphi code  (Read 8459 times)

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: FPC throws a runtime error with working Delphi code
« Reply #15 on: March 28, 2019, 01:45:58 am »
It seems to me it is possible to create and destroy consoles building on Akira's code:
Code: Pascal  [Select][+][-]
  1. function fpAllocConsole: boolean;
  2. begin
  3.   Result := AllocConsole;
  4.   {$ifdef FPC}
  5.   if Result then
  6.   begin
  7.     IsConsole := True;
  8.     SysInitStdIO;
  9.   end;
  10.   {$endif FPC}
  11. end;

Changing your code to test:
Code: Pascal  [Select][+][-]
  1.   case Msg of
  2.     WM_CREATE:
  3.     begin
  4.     if not fpAllocConsole then
  5.           begin
  6.             MessageBox(Wnd,
  7.                        'Failed to allocate a console',
  8.                        'AllocConsole error',
  9.                        MB_ICONERROR or MB_OK);
  10.             exit;
  11.           end;
  12.  
  13.             writeln(Output, '1  outputting text now that we have a console is ' +
  14.                     'possible.');
  15.             ReadLn;
  16.             FreeConsole;
  17.  
  18.             fpAllocConsole;
  19.             writeln(Output, '2  outputting text now that we have a console is ' +
  20.                     'possible.');
  21.             ReadLn;

440bx

  • Hero Member
  • *****
  • Posts: 3921
Re: FPC throws a runtime error with working Delphi code
« Reply #16 on: March 28, 2019, 04:11:32 am »
@Engkin:

I tried what you and Akira suggested and didn't get any output from writeln, not even on the first console allocation.




(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: FPC throws a runtime error with working Delphi code
« Reply #17 on: March 28, 2019, 05:12:38 pm »
Code: Pascal  [Select][+][-]
  1. ...
  2. uses Windows;
  3.  
  4. function InitConsole: Boolean;
  5. begin
  6.   Result := AllocConsole;
  7.   if not Result then
  8.     Exit;
  9.   StdInputHandle := 0;
  10.   StdOutputHandle := 0;
  11.   StdErrorHandle := 0;
  12.   IsConsole := True;
  13.   SysInitStdIO;
  14.   IsConsole := False;
  15. end;
  16.  
  17. procedure DoneConsole;
  18. begin
  19.   SysFlushStdIO;
  20.   FreeConsole;
  21.   SysInitStdIO;
  22. end;
  23.  
  24. procedure TForm1.Button1Click(Sender: TObject);
  25. begin
  26.   if InitConsole then
  27.   try
  28.     Writeln('OK');
  29.     Readln;
  30.   finally
  31.     DoneConsole;
  32.   end;
  33. end;

ASBzone

  • Hero Member
  • *****
  • Posts: 678
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: FPC throws a runtime error with working Delphi code
« Reply #18 on: March 28, 2019, 07:59:37 pm »
@Engkin: I tried what you and Akira suggested and didn't get any output from writeln, not even on the first console allocation.
What Akiira provided (yesterday) worked for me.
-ASB: https://www.BrainWaveCC.com/

Lazarus v2.2.7-ada7a90186 / FPC v3.2.3-706-gaadb53e72c
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

Thaddy

  • Hero Member
  • *****
  • Posts: 14157
  • Probably until I exterminate Putin.
Re: FPC throws a runtime error with working Delphi code
« Reply #19 on: March 28, 2019, 08:10:08 pm »
What Akiira provided (yesterday) worked for me.
The code by ASerge is more complete. I suggest you use that.
Specialize a type, not a var.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: FPC throws a runtime error with working Delphi code
« Reply #20 on: March 28, 2019, 08:49:53 pm »
@Engkin:

I tried what you and Akira suggested and didn't get any output from writeln, not even on the first console allocation.

You are right, you are running the code under Lazarus. Can you try again with this:
Code: Pascal  [Select][+][-]
  1. function fpAllocConsole: boolean;
  2. begin
  3.   Result := AllocConsole;
  4.   {$ifdef FPC}
  5.   if Result then
  6.   begin
  7.     IsConsole := True;
  8.     StdInputHandle  := 0;
  9.     StdOutputHandle := 0;
  10.     StdErrorHandle  := 0;
  11.     SysInitStdIO;
  12.   end;
  13.   {$endif FPC}
  14. end;

Edit:
ASerge figured it before I did.
« Last Edit: March 28, 2019, 09:10:50 pm by engkin »

Thaddy

  • Hero Member
  • *****
  • Posts: 14157
  • Probably until I exterminate Putin.
Re: FPC throws a runtime error with working Delphi code
« Reply #21 on: March 28, 2019, 09:42:59 pm »
Yes. and all such things should go in the wiki. I'll see if I have time, otherwise feel free to add it  yourself.
Specialize a type, not a var.

440bx

  • Hero Member
  • *****
  • Posts: 3921
Re: FPC throws a runtime error with working Delphi code
« Reply #22 on: March 29, 2019, 12:49:07 am »
@Akira, @Engkin and @Serge

A heartfelt thank you to the three of you for your efforts to solve the problem.  This solution saves me a lot of time and hassle. :)

Engkin, Serge... my hat off to you guys.  I owe you one.

For anyone who might benefit from seeing "applicability", I attached a small program that works with Delphi 2, Delphi 10 and FPC in 32 and 64bit.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018