Use a GUI console application?

How to create a console application that can work with or without a graphical interface?

For example, let's say if I had a console application. If I tried to run this console application from Windows Explorer, it won’t work, it just closes, but I can call it from my GUI application or the Windows command console (cmd. Exe) and pass some switches (options?) To it.

Thus, some useful functions can be used without even starting a GUI application, they can be called from the command line.

EDIT

I'm not sure how to create a console application, especially what will accept flags (switches, options?).

I saw some Applications that do something similar. For example, they may have a console application that converts bmp to png, and the GUI calls that console application and passes arguments to it, etc.

Hope this makes sense.

So how could I use something like this?

Thanks.

+4
source share
3 answers

For example, let's say if I had a console application. If I tried to run this console application from Windows Explorer, it won’t work, it just closes, but I can call it from my GUI application or the Windows command console (cmd. Exe) and pass some switches (options?) To it.

It will work. However, the console window will disappear as soon as your program exits. If you want to give the user the opportunity to read the results of their console application before the window closes, just start your program with one

Readln; 

or

 Writeln('Press Enter to exit.'); Readln; 

If you want to use the console window for output (or input) in a GUI application, you can try the AllocConsole and FreeConsole .

Command line arguments (for example, myapp.exe /OPEN "C:\some dir\file.txt" /THENEXIT ) can be used in all types of Windows applications, both graphical applications and console applications. Just use the ParamCount and ParamStr .

How to create a console application that takes command line arguments

In the Delphi IDE, select File / New / Console Application. Then write

 program Project1; {$APPTYPE CONSOLE} uses Windows, SysUtils; var freq: integer; begin if ParamCount = 0 then Writeln('No arguments passed.') else if ParamCount >= 1 then if SameText(ParamStr(1), '/msg') then begin if ParamCount = 1 then Writeln('No message to display!') else MessageBox(0, PChar(ParamStr(2)), 'My Console Application', MB_ICONINFORMATION); end else if SameText(ParamStr(1), '/beep') then begin freq := 400; if ParamCount >= 2 then if not TryStrToInt(ParamStr(2), freq) then Writeln('Invalid frequency: ', ParamStr(2)); Windows.Beep(freq, 2000); end; end. 

Compile the program. Then open the shell (CMD.EXE) and change to the directory where Project1.exe .

Then try

 C:\Users\Andreas Rejbrand\Documents\RAD Studio\Projects>project1 No arguments passed. C:\Users\Andreas Rejbrand\Documents\RAD Studio\Projects>project1 /msg No message to display! C:\Users\Andreas Rejbrand\Documents\RAD Studio\Projects>project1 /msg "This is a test." C:\Users\Andreas Rejbrand\Documents\RAD Studio\Projects>project1 /beep C:\Users\Andreas Rejbrand\Documents\RAD Studio\Projects>project1 /beep 600 C:\Users\Andreas Rejbrand\Documents\RAD Studio\Projects> 

How to pass three arguments

 if ParamCount >= 1 then begin if SameText(ParamStr(1), '/CONVERT') then begin // The user wants to convert if ParamCount <= 2 then begin Writeln('Too few arguments!'); Exit; end; FileName1 := ParamStr(2); FileName2 := ParamStr(3); DoConvert(FileName1, FileName2); end; end; 
+4
source

Thus, some useful functions can be used without even starting a GUI application, they can be called from the command line.

If you want the application to be a GUI, but you want to override the GUI by passing command line options, try the following:

 program Project1; uses Forms, SysUtils, Windows, Unit1 in 'Unit1.pas' {Form1}; {$R *.res} begin if ParamCount > 0 then Windows.MessageBox(GetDesktopWindow, PChar(ParamStr(1)), PChar('Test'), 0) else begin Application.Initialize; Application.CreateForm(TForm1, Form1); Application.Run; end; end. 
+1
source

I'm not sure how to do this in delphi, but in C # I just checked in the Main method if any command line arguments were passed, then if the application was launched from the console, if there was no GUI launch.

0
source

All Articles