Show: Delphi C++
Display Preferences

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 the following in the code, opens a console window of its own.:

{$APPTYPE CONSOLE}

To create a new Delphi console application

  1. Click File > New > Other > Delphi Projects.
  2. 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

  1. Click File > New > Other > C++Builder Projects and double-click Console Application. The New Console Application (C++) dialog box opens.
  2. 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 or winmain function.
  3. For C++, in the Target Framework group, specify the application type if you want your application to display visuals:
  4. 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.
    New Console Application
  5. 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.

See Also

Personal tools
RAD Studio 10.2 Tokyo
In other languages
Previous Versions
Assistance