Console Applications

From RAD Studio
Jump to: navigation, search

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:

{$APPTYPE CONSOLE}

in the code opens a console window of its own.

To create a new Delphi console application

  1. Choose File > New > Other > Delphi Projects.
  2. Double-click Console Application from the New Items dialog box.

The IDE then creates a project file for this type of source file and displays the Code Editor.

To create a new C++ console application

  1. Choose File > New > Other > C++Builder Projects and double-click Console Application in the New Items dialog box. The New Console Application (C++) wizard opens.
  2. Choose the source type (C or C++) for the main module of the project, or enable Specify project source and specify a preexisting file that contains a main or winmain function.
  3. If you want your C++ console application to use visuals, select the Target Framework: FireMonkey or VCL. 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. The default framework is None.
  4. Click the OK button.

The IDE then 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.

See Also