[Lazarus] WriteLn back to the GUI

ListMember listmember at letterboxes.org
Wed Oct 23 21:01:33 CEST 2013

On 2013-10-23 18:58, Sven Barth wrote:
> Am 23.10.2013 17:50, schrieb ListMember:
>> This is far too (incredibly) elaborate.
>> It's a bit like giving me plans to set up an army to invade a country 
>> when all I want to do is to visit it as a tourist :)
> No, this is the officially supported way to interfere with Pascal's IO 
> system.

I gathered as much. But, at least for me, writing a 'textdriver' simply 
to redirect the output of WriteLn is too complicated.

>> As I mentioned in my first post, I am prepared to alter the compiler 
>> sources; and I had this in mind:
>> I would like to add these to a common unit --such as globals.pas

> What would adding a callback to "globals.pas" provide? The string that 
> is written by Writeln is created at runtime and not at compile time.

And, that's precisely what I want to do: Get/redirect the string (that 
Writeln would write to console) back to my application in run time.

>> type
>>   TMyWriteLnCallback = procedure(AText: String);
>> var
>>   GMyWriteLnCallback: TMyCallback = nil;
>> and then, wherever the 'compiler magic' for WriteLn is, use something 
>> like
>> if Assigned(GMyWriteLnCallback) then GMyWriteLnCallback(TextStuff)
>> else {do the usual console output}
>> So, could someone tell me where I have to inject these lines.
> I've already written you how you can do it. Write a text driver which 
> uses your callback function.
> This is much easier than trying to understand how the compiler handles 
> Writeln plus you don't need to manipulate the compiler or the RTL for 
> that.

Frankly, I agree that I should not have to modify the compiler or the 
RTL; especially for something like redirecting the WriteLn output from 
console to my own application.

But, doing it through text drivers etc. is --how can I put it-- almost 

I don't want to do it through

Procedure AssignStream(var F: Textfile; Stream: TStream);

as it exists in StreamIO unit because I don't want to fiddle around with 
streams as it doesn't have an OnChange event. It means I need to wait 
for the whole app to finish, or I need to keep polling for what has 
changed in the stream. Totally useless in my case.

I could use the sample code you posted a link to (and, thank you also 
for that), and I adapted it to use TMemo for my use.

But, why would I have to write 100+ lines of code just to capture the 
output of WriteLn.

Plus, I keep getting segfaults when I close the application --something 
is calling InOutFunc when the app closes and still tries to write to a 
destroyed TMemo.

I think I'll go with Leledumbo's solution --and yes, will alter the 
globals.pas in the compiler's source.

More information about the Lazarus mailing list