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;