Recent

Author Topic: [Solved] MessageBox fails in Initialization  (Read 472 times)

Edson

  • Hero Member
  • *****
  • Posts: 1135
[Solved] MessageBox fails in Initialization
« on: December 15, 2020, 07:36:57 pm »
Hi.

Recently I was testing a program in a PC different from my system with not success.

Investigating, I arrive to this sample code:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs;
  6. type
  7.   TForm1 = class(TForm)
  8.   end;
  9.  
  10. var
  11.   Form1: TForm1;
  12.  
  13. implementation
  14. {$R *.lfm}
  15.  
  16. initialization
  17.   Application.MessageBox(PChar('Hello'), PChar('Hello'));
  18.  
  19. end.
  20.  

It fails. The message box never appears.

It was compilated in a Intel Core i7, x64 Windows 10 Enterprise 2015, using Lazarus 2.0.6 - FPC 3.0.4

« Last Edit: December 16, 2020, 03:57:01 am by Edson »
Lazarus 2.0.10 - FPC 3.2.0 - x86_64-win64 on Windows 8

winni

  • Hero Member
  • *****
  • Posts: 2219
Re: MessageBox fails in Initialization
« Reply #1 on: December 15, 2020, 08:09:11 pm »
Hi!

The initialization section is no place for GUI user interaction.
Not even the Form.create is a place for that.

The first moment when everything is initialized an correct shown is Form.activate

Astonishing enough: sometime your code works.

Winni

lucamar

  • Hero Member
  • *****
  • Posts: 3609
Re: MessageBox fails in Initialization
« Reply #2 on: December 15, 2020, 09:13:52 pm »
Explanation: Unit initialization blocks are executed before the main program block, where the Application object is itself initialized.

The Application object itself exists: it's created in the initialization of the Forms unit (otherwise you'd get a nice exception box :)) but it's not yet initialized so while MessageBox() might work sometimes it won't normally do it, because it's "parent" (let's call it that) isn't fully set up yet.

Contrary to what winni says, you can move that code to the form's OnCreate handler and the message box will show up before the form does, because by that time Application is fully operative. Or you can wait to OnActivate and the message box will show up over the brand new form. ;)

HTH!
« Last Edit: December 15, 2020, 09:21:54 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

winni

  • Hero Member
  • *****
  • Posts: 2219
Re: MessageBox fails in Initialization
« Reply #3 on: December 15, 2020, 09:25:50 pm »
@lucamar

You can put the MessageBox in FormCreate - BUT

In the most cases it shows an empty MessageBox -  the string is missing.
I am talking about Linu64/gtk2

I don't know with other OS.

Winni

lucamar

  • Hero Member
  • *****
  • Posts: 3609
Re: MessageBox fails in Initialization
« Reply #4 on: December 15, 2020, 09:37:37 pm »
@lucamar

You can put the MessageBox in FormCreate - BUT

In the most cases it shows an empty MessageBox -  the string is missing.
I am talking about Linu64/gtk2

I don't know with other OS.

Winni

I am on Linux 64 bit and it never fails (not that I use it that much, though). There is no reason for it to fail: at that point the only thing missing is to end creating the form(s) and calling Application.Run but that shouldn't interfere with showing a message box. Indeed, it's the almost the only place to show one if something went wrong when initializing the form objects.

You should be able to use MessageBox() any time after Application.Initialize; whether it has created any form or not. In fact, this works alright too:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   Interfaces, // this includes the LCL widgetset
  10.   Forms, unit1
  11.   { you can add units after this };
  12.  
  13. {$R *.res}
  14.  
  15. begin
  16.   RequireDerivedFormResource := True;
  17.   Application.Scaled := True;
  18.   Application.Initialize;
  19.   Application.MessageBox('Ready to start rolling!!!', 'Very important!');
  20.   Application.CreateForm(TForm1, Form1);
  21.   Application.Run;
  22. end.

At least it does here (Xubuntu 18.04, Laz/FPC 2.0.10/3.2.0) - AMD64
« Last Edit: December 15, 2020, 09:41:38 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2101
Re: MessageBox fails in Initialization
« Reply #5 on: December 15, 2020, 10:03:06 pm »
You should be able to use MessageBox() any time after Application.Initialize; whether it has created any form or not.

Agreed, I use that fairly regularly to respond to e.g. an --about on the command line with a dialog(ue) box.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

Edson

  • Hero Member
  • *****
  • Posts: 1135
Re: MessageBox fails in Initialization
« Reply #6 on: December 16, 2020, 03:56:44 am »
Thanks all for the information. It's clear for me why the code it's failing.
Lazarus 2.0.10 - FPC 3.2.0 - x86_64-win64 on Windows 8

 

TinyPortal © 2005-2018