Console Applications
Go Up to Creating Applications
Console applications are 32-bit programs that run without a graphical interface, in a console window, on a target platform. These applications typically do not require much user input and perform a limited set of functions. Although console applications cannot display visuals, you can use all the types and functions of the RTL (run-time library) in a console application.
Any application that contains the following in the code, opens a console window of its own.:
{$APPTYPE CONSOLE}
Contents
To create a new Delphi console application
- Click File > New > Other > Delphi.
- Double-click Console Application.
Then, the IDE creates a project file for this type of source file and displays the Code Editor.
To create a new C++ console application
- Click File > New > Other > C++Builder and double-click Console Application. The New Console Application (C++) dialog box opens.
- Select the source type (C or C++) for the main module of the project, or enable Specify project source and specify the preexisting file that contains a
main
orwinmain
function. - For C++, in the Target Framework group, specify the application type if you want your application to display visuals:
- For C++, in the Target Framework group, specify the application type if you want your application to display visuals:
- FireMonkey: to create a FireMonkey application
- VCL: to create a VCL application
- None: a default framework
- Note: Although a console application cannot display visuals, a C++ console application can process visual files from a library (such as icons, bitmaps, video, and so forth). For example, if you do not indicate in the wizard that you want to use the VCL, your application cannot use any of the VCL classes later. Trying to do so causes linker errors.
- Click OK.
Then, the IDE creates a project file for this type of source file and displays the Code Editor.
Your console application must handle all exceptions
Console applications should ensure that no exceptions escape from the program scope. Otherwise, when the program terminates, the operating system displays a dialog with exception information.
Your C++ console applications should handle all exceptions in order to prevent the target platform from attempting to display a dialog when you run the console application. For more information, see Exception Handling in C++Builder.
For example, your Delphi application should include exception handling such as shown in the following code:
program ConsoleExceptionHandling;
{$APPTYPE CONSOLE}
uses
SysUtils;
procedure ExecuteProgram;
begin
// Program does something
raise Exception.Create('Unforeseen exception');
end;
begin
try
ExecuteProgram;
except
// Handle error condition
WriteLn('Program terminated due to an exception');
// Set ExitCode <> 0 to flag error condition (by convention)
ExitCode := 1;
end;
end.
How your console application terminates
Users can terminate console applications in one of the following ways:
- Click the Close (X) button.
- Press
Ctrl+C
. - Press
Ctrl+Break
. - Log off.
To handle these user termination requests, use the options of the Windows API function SetConsoleCtrlHandler. Depending on the user's choice, the application might be terminated forcefully, the process might not shut down cleanly, and the finalization section might not run.