How to use System.Console class?

Hi… a new user here. FYI, I’m not a russian and I don’t speak russian either. So, please reply it in english (or indonesian if you could).

I’d like to use CRT unit in PascalABC. Unfortunately,I found some problems and errors.

  1. If I use CRT unit, the IDE refuses to debug my program and forces me to run it without IDE (shift+F9). But if I did it, the compiled program raises runtime error such as this image below. Though I could click ‘Continue’, it’s still annoying. And the most important thing is I couldn’t debug my code.

Cuplikan layar 2020-09-19 185443

  1. I found an old issue thread saying we should not use CRT unit because it’s a legacy unit and use the System.Console class instead. But if I put the module in the “uses” section, the IDE complains that it’s unable to find the “System.Console” namespace. If CRT unit is just a wrapper of Console class, then why the IDE is unable to debug it?

  2. An answer on PascalABC’s github said I could simply call the Console class’ methods. But a call to Console’s methods is always producing runtime exception saying ‘the handle is invalid’. So, how to initiate a console window and use to it as Console’s output?

So, if I use the CRT unit then I can’t do debugging, but I can’t use the Console class either. What should I do?

I’m using PascalABC v.3.7.1 (the latest version as I write this issue) on Windows 10.

Thank you.

I would prefer to leave a link to there, for future reference.
Also you should close that issue if you are transferring discussion here…


This means you don’t have console window. As i said, things that change console window, like Console.Clear, would still require you to start in Shift+F9 mode (or by executing .exe file).

any link to it in PascalABC’s web site (the english one)

I hope you mean this one?
http://pascalabc.net/en/
The fact that you call it PascalABC confuses me, because it’s a different language.

But yeah, it’s english version of that site is pretty restricted, there are many things missing.
In you switch language to Russian - you’ll see “Форум” button on bottom of left panel.

Ok. Done.

Hence, the question number 3. How to enable debugging and get rid of the error?

Do you mean there is another PascalABC beside PascalABC.Net? I call it PascalABC just for simplicity e.g. shorter word. I even want to write it as pabc to make it shorter, like fpc. Of course it still refers to PascalABC.Net.

There are LOTS of things missing. I just promoted pascalABC on Indonesian Pascal Community. Their first complain is the documentation is in Russian. Sure, there’s google translate but the translation is not perfect and can be misleading in some particular cases. I wish I knew russian, I’d like to help translate the doc into english. Yes, pascalABC is so good that it worth the effort.

1 лайк

Guess i wasn’t clear enough…

{ $define UseConsole}

begin
  Writeln('abc');
  Readln;
  {$ifdef UseConsole}
  Console.Clear;
  {$endif UseConsole}
  Writeln('def');
  Readln;
end.

You can debug this program because Console.Clear is disabled by $ifdef.
Then, when you make “Release” version of your program - you can remove whitespace before $define UseConsole to enable it.

You can also use {$ifdef RELEASE}, but it will be active only if you both disable Generate debug information in Options>>Compiler options AND start in Shift+F9 mode (or, again, from folder).

There was, and at least lead dev was same as for this pascal. It’s dead now, because they made whole new language on .Net platform. And all official references were deleted, probably to not have confusion between PascalABC and PascalABC.Net.

But i still see many school tasks for it on forums like cyberforum. And it’s not hard to find bootleg downloads for last version of PascalABC installer on unofficial sites, so i wanted to make sure you didn’t confuse one of those sites with official PascalABC.Net site.

It’s clear enough. But I don’t think it’s a correct solution because I can’t debug anything that’s calling Console’s methods. For example, I’m making a text animation that moving some chars all around the screen. The char position calculation clearly can’t be debugged using your way.

Is there a way to create the console window first than assign the handle to Console? Is it even possible? Because I found many topics related to this on stackoverflow, I just don’t know how to do it in pascalABC.

Really? Thank God it’s dead already. *kidding

No need to confuse. I know nothing about other pascalABC. So, I assure you, everytime I said pascalABC or pabc for short, I mean it refers to PascalABC.Net. It’s too long to type it over and over, you know. :grin:

You can try WinAPI AllocConsole, but you would probably first need to somehow break the redirected output.

Though i personally wouldn’t recommend doing complex animations with console windows, you have graphical units for that. Console window is mostly for simple text output.

Yes, I read it’s pretty complicated.

For professional or commercial works, yes I agree with you. But for educational purpose it’s still quite interesting, especially using unicode letters and emojis. Here’s some program samples I made using FPC and CRT unit to teach some newbies in Indonesian Pascal Community.

Pascal racing:
pascal-racing

Fancy text input:
fancy input crt

Text particle animation:
text_animation

2 лайка

Not really, from all the unmanaged sub-programs it’s one of easiest to call.
You don’t need anything like explicit memory management, not even string encoding conversion.

First open microsoft docs to find out parameters and .dll that contains entry point.

So, there is no arguments and BOOL return value, which in C++ is a 32-bit number that can only be assigned 1 (true) or 0 (false). You can use just integer for it. Or, if you want to make it extra right - you can define own record with 1 private integer field and operator implicit's for converting to- and from boolean.

function AllocConsole: integer;
external 'Kernel32.dll';
//You can also do this, if you want to change function name in your code:
// external 'Kernel32.dll' name 'AllocConsole';

begin
  if AllocConsole=0 then
    // Usually you should call WinAPI like GetLastError here,
    // to understand exactly what went wrong
    raise new System.InvalidOperationException;
  Readln;
end.

Aaand… It at least fails for me. But i never actually researched into how you can fix it, it may also be easy. As i said earlier - my guess is that it fails because IDE set’s ProcessStartInfo.RedirectStandardOutput when starting your .exe .

Well, i’m kinda too busy rn to research/give more info, so for now you are on your own with this.

Yes, this is the restriction for debugging in PascalABC.NET

Strange… This program works on my computer.

You can debug a program with the Console class:

But you cannot debug a program in the mode “Run without IDE”

It’s strange/ Give me the screenshort

You must use the Console class. The question is: why it doesn’t work on your computer

The CRT Unit in PascalABC.NET is only for compatibility purposes. For some more complex things use Console class only

Some other comments. We use GraphWPF and Controls units for the same purposes: the education of newbies. In PascalABC.NET there are many restrictions for using Console.

Take note that Console is a class - not a namespace, so to use it you can use the following code:

uses System;

begin
  Console.WriteLine('abc');
end.

Here is my example:

uses System;

begin
  var x := 5;
  Console.ForegroundColor := ConsoleColor.Green;
  Console.CursorLeft := x;
  Console.CursorTop := 20;
  Console.Write('abc');
  loop 100 do
  begin
    Sleep(100);
    Console.SetCursorPosition(x,20);
    Console.Write('   ');
    x += 1;
    Console.SetCursorPosition(x,20);
    Console.Write('abc');
    Console.SetCursorPosition(0,0);
  end;
  Console.ForegroundColor := ConsoleColor.Black;
end.

You cannot debug it due to the technical reasons!

1 лайк

Yeah, I’ve tried something similar to that as well, based on some code samples from stackoverflow. And I failed too. So, it’s alright for now. I’m still looking and playing around with pascalABC. Nothing serious.

A simple WriteLine does work but a call to any methods that manipulate cursor or text will produces an error. You have no option but to run the program without IDE and debugger. But what is the point of programming without debugging? Especially for learners or students.

Is it possible to overcome the technical reasons?

Use Ctrl-F9 for running

No