Recent

Author Topic: ShowMessage within console application, error 1407  (Read 6474 times)

mtanner

  • Sr. Member
  • ****
  • Posts: 287
ShowMessage within console application, error 1407
« on: August 30, 2016, 09:17:42 am »
I'm trying to get a console application working, but I have a problem with ShowMessage, which gives an error "Failed to create win32 control, error: 1407".  I have added FPC and included the interfaces unit.  I saw a post saying you need to do Application.Initialize, but Application is not available in a console app (is it?).How do I get ShowMessage to work in a console app?

Just as background, I have  large collection of scientific calculation units, which use ShowMessage for "shoul-not-occur" error. I have to use these units both in GUI apps (so console Write not an alternative) and in console apps.

mtanner

  • Sr. Member
  • ****
  • Posts: 287
Re: ShowMessage within console application, error 1407
« Reply #1 on: August 30, 2016, 09:25:53 am »
I think I've solved the problem. Seems you have to add the Forms unit, and call Application.Initialize, then ShowMessage works.

mig-31

  • Sr. Member
  • ****
  • Posts: 305
Re: ShowMessage within console application, error 1407
« Reply #2 on: August 30, 2016, 10:11:36 am »
Console is CLI application without GUI.
Lazarus 2.2.6 - OpenSuse Leap 15.4, Mageia 8, CentOS 7

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: ShowMessage within console application, error 1407
« Reply #3 on: August 30, 2016, 10:38:56 am »
Well, ShowMessage is for a GUI app. One would use write/writeln for Console apps.
Thaddy to the resque ;):
Code: Pascal  [Select][+][-]
  1. // showmessage for console apps
  2. procedure ShowMessage(const mess:String);
  3. begin
  4.   writeln(mess);
  5. end;
  6.  

But Normally you would simply use:
Code: Pascal  [Select][+][-]
  1. writeln(YourMessage);

If you really have a lot of showmessages you may want to use my first solution :) But only in a console app! otherwise it clashes with the showmessage in forms.pas
« Last Edit: August 30, 2016, 10:40:51 am by Thaddy »
Specialize a type, not a var.

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: ShowMessage within console application, error 1407
« Reply #4 on: August 30, 2016, 11:26:28 am »
Another solution:
Code: Pascal  [Select][+][-]
  1. program smwithmacro;
  2. {$IFDEF WINDOWS}{$APPTYPE CONSOLE}{$ENDIF}
  3. {$MACRO ON}
  4. {$DEFINE ShowMessage:=WriteLn}
  5.  
  6. begin
  7.   ShowMessage('Hello, World');
  8. end

This has the advantage that you have all the formatting capabilities of writeln available. Is NOT Delphi compatible.
« Last Edit: August 30, 2016, 11:31:04 am by Thaddy »
Specialize a type, not a var.

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: ShowMessage within console application, error 1407
« Reply #5 on: August 30, 2016, 11:35:14 am »
I think I've solved the problem. Seems you have to add the Forms unit, and call Application.Initialize, then ShowMessage works.

Better look at my solutions... Adding forms will blow up your executable size beyond any acceptible size for a console application.
But it works..

BTW: writeln can be redirected to write anywhere, including GUI app memo's.
If there needs to be console output use one of my examples with a single ifdef.
« Last Edit: August 30, 2016, 02:39:57 pm by Thaddy »
Specialize a type, not a var.

mtanner

  • Sr. Member
  • ****
  • Posts: 287
Re: ShowMessage within console application, error 1407
« Reply #6 on: October 23, 2016, 10:57:48 am »
I'm revisiting this just to document what I have found about making ShowMessage work in a console app. Note my approach is largely trial-and-error with whatever I have have been able to learn from this forum. So the following is what works, not why it works, it's a cookbook solution.

First, I want to use ShowMessage for compatibility with Delphi. Redefining ShowMessage as a Writeln would possibly be confusing later on, at least to me. And then there is the issue that we "should" be able to make it work.

Using LCL/LCLbase increases the size of the console app. For me this is not important. The console apps I am writing are test rigs, for DLLs and some numerical algorithms. A large exe is of no consequence. What matter most is to get things working straightforwardly so I can concentrate on the algorithm development. Others may have different programming priorities.

So to make ShowMessage work I found I needed to do the following.
(1) Add LCL and LCLbase via the project Inspector
(2) Add the units: dialogs, interfaces, forms
(3) Do Application.Initialize at the start of the program

It seems tidy to me to then do Application.Terminate just before the program closes, but I suspect this is redundant.

mtanner

  • Sr. Member
  • ****
  • Posts: 287
Re: ShowMessage within console application, error 1407
« Reply #7 on: October 23, 2016, 10:58:51 am »
BTW, I'm now trying to figure out how to make ShowMessage work inside a DLL. I will post my conclusions here when I have some.

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: ShowMessage within console application, error 1407
« Reply #8 on: October 23, 2016, 11:03:16 am »
If it is windows only, just use messagebox. It is already inside a dll ;) kernel32.dll
But there are so many solutions for this basic problem that I am sure you will get others....
Specialize a type, not a var.

mtanner

  • Sr. Member
  • ****
  • Posts: 287
Re: ShowMessage within console application, error 1407
« Reply #9 on: October 23, 2016, 11:19:51 am »
I could use MessageBox instead of ShowMessage. What would I need in a console app and in a dll to make that work?

Eventually I want to port the dll to Linux. What is the Linux equivalkent to ShowMessahe/MessageBox, and does that change what is the best choice?

 

TinyPortal © 2005-2018