Recent

Author Topic: Console application from Visual program  (Read 899 times)

Jake012345

  • Full Member
  • ***
  • Posts: 249
  • 'The knowledge is the key.'
    • FPCProjectShare
Console application from Visual program
« on: May 21, 2020, 06:59:51 pm »
Hello!

how can I run a console program from Visual application?

(I want it just for effect, but 3 procedure have to write in this.)

Thanks!
« Last Edit: May 21, 2020, 07:04:40 pm by Jake012345 »
Check my ProjectShare site and share your content: FPCShare

Bart

  • Hero Member
  • *****
  • Posts: 3821
    • Bart en Mariska's Webstek
Re: Console application from Visual program
« Reply #1 on: May 21, 2020, 09:15:04 pm »

Jake012345

  • Full Member
  • ***
  • Posts: 249
  • 'The knowledge is the key.'
    • FPCProjectShare
Re: Console application from Visual program
« Reply #2 on: May 26, 2020, 03:20:04 pm »
So I have to write separately programs If I want to show console messages?
Check my ProjectShare site and share your content: FPCShare

rvk

  • Hero Member
  • *****
  • Posts: 4189
Re: Console application from Visual program
« Reply #3 on: May 26, 2020, 03:50:33 pm »
So I have to write separately programs.
You asked to run a console program from your visual application, so, yes... that means a separate program which you (or someone else) need to write.

If I want to show console messages?
You'll need to clarify. What console? On Windows or Linux? You can have multiple consoles on both platforms. So what exactly do you mean.

Normally a visual program isn't connected to any console. So if you wan't to show a console from your visual program, you'll first need to create it before you can write to it.

Before that... is there any special need for a console? Can't you just do this with a form and a tmemo on it?

A very small example. This can be run from a GUI-program and it will create a console.

Code: Pascal  [Select][+][-]
  1. uses windows;
  2.  
  3. function fpAllocConsole: boolean;
  4. begin
  5.   Result := AllocConsole;
  6.   {$ifdef FPC}
  7.   if Result then
  8.   begin
  9.     IsConsole := True;
  10.     StdInputHandle  := 0;
  11.     StdOutputHandle := 0;
  12.     StdErrorHandle  := 0;
  13.     SysInitStdIO;
  14.   end;
  15.   {$endif FPC}
  16. end;
  17.  
  18. procedure DoConsole();
  19. var s: string;
  20. begin
  21.  fpAllocConsole;
  22.  writeln('Enter something or Q to Quit') ;
  23.  repeat
  24.     readln(s);
  25.     writeln('You typed: ' + s);
  26.  until uppercase(s)='Q';
  27.  FreeConsole;
  28. end;
« Last Edit: May 26, 2020, 04:00:12 pm by rvk »

Jake012345

  • Full Member
  • ***
  • Posts: 249
  • 'The knowledge is the key.'
    • FPCProjectShare
Re: Console application from Visual program
« Reply #4 on: May 26, 2020, 05:45:37 pm »
And what are the paramters?

c:\...\asd.exe -fast

For example:
I can make to my program a -fast parameter, and if I use it the program can run with more or less funcions?
Check my ProjectShare site and share your content: FPCShare

lucamar

  • Hero Member
  • *****
  • Posts: 2930
Re: Console application from Visual program
« Reply #5 on: May 26, 2020, 06:16:45 pm »
For example:
I can make to my program a -fast parameter, and if I use it the program can run with more or less funcions?

Your program will have all the functions programmed into it, whether it uses them or not.

If what you want is to be able to load a leaner, though less "powerful", version then you should move the "extra" functionality to a dynamic library which you would then load (or not) depending on the parameters passed.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Jake012345

  • Full Member
  • ***
  • Posts: 249
  • 'The knowledge is the key.'
    • FPCProjectShare
Re: Console application from Visual program
« Reply #6 on: May 26, 2020, 08:27:11 pm »
That's what I'm thinking.

But how can I do it?
Check my ProjectShare site and share your content: FPCShare

rvk

  • Hero Member
  • *****
  • Posts: 4189
Re: Console application from Visual program
« Reply #7 on: May 26, 2020, 08:45:09 pm »
That's what I'm thinking.
But how can I do it?
Can you explain exactly what you want (not just repeat what others suggest).

Do you want a small .exe and load a dll (which is difficult for beginners)?

Do you want to read out a parameter -fast from your startup commands?
Which you can do with
If (paramcount > 0) and (paramstr(1) = '-fast') then .... ;

Do you want to run an external program (like you asked for in your opening post) and pass a parameter -fast?



440bx

  • Hero Member
  • *****
  • Posts: 1897
Re: Console application from Visual program
« Reply #8 on: May 26, 2020, 08:51:12 pm »
That's what I'm thinking.

But how can I do it?
What @rvk showed in a previous post is the simplest way to go about it.  You can find a complete working example attached to the following post: https://forum.lazarus.freepascal.org/index.php/topic,44828.msg315571.html#msg315571

As far as parameters or anything else, the simplest way is to have the GUI portion share (or pass) some of its data to the console part.  Since the console is part of the GUI program, sharing data is done just as if you were dealing with another GUI window in the program.

Also, the questions @rvk asked are good ones.  You need to be a little more precise as to what you want to do in order to get more precise help as well.

HTH.

FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Jake012345

  • Full Member
  • ***
  • Posts: 249
  • 'The knowledge is the key.'
    • FPCProjectShare
Re: Console application from Visual program
« Reply #9 on: May 27, 2020, 05:13:34 am »
My idea is that I write only two programs:

An Gui-program
and a console program

the console program doesn't do anything on simple start, but the Gui-program knows the parameter  s and execute it with theese for the necessary messages.
Check my ProjectShare site and share your content: FPCShare

rvk

  • Hero Member
  • *****
  • Posts: 4189
Re: Console application from Visual program
« Reply #10 on: May 27, 2020, 07:58:14 am »
the console program doesn't do anything on simple start, but the Gui-program knows the parameter  s and execute it with theese for the necessary messages.
Then why not use my suggestion from my first post to use allocconsole?

You can start your gui program and open a console for messages.

dbannon

  • Hero Member
  • *****
  • Posts: 1076
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Console application from Visual program
« Reply #11 on: May 27, 2020, 09:10:45 am »
My idea is that I write only two programs:

An Gui-program
and a console program

the console program doesn't do anything on simple start, but the Gui-program knows the parameter  s and execute it with theese for the necessary messages.

Jake012345, perhaps another way to think about it is to write one program that has at least two units.  The first unit is a console or command line unit.  It starts up first and if it finds the various things (such as command line parameters)  it needs to know to do its job, it goes and does it.  If it does not know what to do, it allows the GUI unit to startup and the user can click buttons and enter information to instruct the program to do its job.

In this model, its easier to have most of the working code in the command line unit or units, it makes sense for the GUI, if its started up, to be able to call them.  The way you are thinking, you always have to start up the GUI, the forms must be created etc, before the code in there can be called. Even if we don't need the forms.

Someone here on the forum (sorry, cannot remember who, but thanks!) recently pointed out to me that the lpr file can have things like this in it -

Code: Pascal  [Select][+][-]
  1.     ....
  2.     Application.Initialize;
  3.     if ContinueToGUI then begin
  4.         Application.CreateForm(TMainForm, MainForm);
  5.         Application.CreateForm(TSett, Sett);
  6.    ....

The ContinueToGUI() function does things like look at command line options and parameters and either triggers the program's purpose or decides to run the GUI.

Davo



Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

Leledumbo

  • Hero Member
  • *****
  • Posts: 8226
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Console application from Visual program
« Reply #12 on: May 29, 2020, 02:14:54 am »
Sounds like you're trying to make a GUI wrapper over a CLI app, but I'm not too sure. Feels like we have a language barrier here.
If I'm right, I have at least 3 projects that do so, 1 of them (1st on the list below) have both the CLI and GUI parts written in the same project folder:

 

TinyPortal © 2005-2018