Most of you probably didn't buy C++Builder to write console applications. In fact, many of you don't even know what a console application is. Let's take care of that right away: A console application is a 32-bit program that runs in a DOS-prompt dialog box under Windows. If you prefer, you can think of a Win32 console application as the 32-bit cousin of the DOS program.
Like an old DOS program, a console application differs from the traditional Windows application in several ways. For example, it doesn't have a main window, a menu, dialog boxes, or any of the other graphical goodies that mark a Windows GUI application. Also, console applications have no Window procedure, meaning you can write console applications with less fuss than is necessary with GUI applications.
For example,Figure A shows the source code for the traditional Hello World! console application. This program displays the text Hello World! in a command prompt box, then waits for the user to press a key before terminating. (Without the getch() function, Windows would open a command prompt box, display the text, and then immediately close the command prompt box.) That's the entire program.
So what's a console application good for? Such applications are convenient in specialty situations like servers, utility programs, or quick-test programs. They're particularly useful for learning C++ programming techniques without all the distractions of a GUI application.
Console applications are more widely used than you might think. Many of the command-line tools that come with C++Builder are Win32 console applications, as are many Windows utilities. You probably have quite a few console applications on your hard drive--you can think of them as the workhorses of the Windows world.
A console application generally involves a minimum amount of user interaction. Still, a console application can benefit from a little GUI help now and then. For example, let's say you have a server application that logs incoming mail messages. You could write a custom file-selection screen for your console application, but that would take a lot of work. It would be much more convenient to let the user choose the log file via the Windows Open File dialog box. In this case, you'd like to leverage some of Windows' GUI aspects in a console application.
Or, maybe you have a specialty application that crunches numbers in the background and displays its progress when requested. In this case, you might like to use a VCL form to display the requested information. The good news is, as we explain in the accompanying article, that you can use the common dialog boxes and VCL forms from a console application.
Figure A: Here's the code for the Hello World! console application.
//-------------------------------------------- #include <vcl\condefs.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> #pragma hdrstop
//--------------------------------------------
USERES("Project1.res");
//--------------------------------------------int main(int argc, char **argv)
{
printf("Hello World!");
getch();
return 0;
}
//--------------------------------------------
Kent Reisdorph is a editor of the C++Builder Developer's Journal as well as director of systems and services at TurboPower Software Company, and a member of TeamB, Borland's volunteer online support group. He's the author of Teach Yourself C++Builder in 21 Days and Teach Yourself C++Builder in 14 Days. You can contact Kent at editor@bridgespublishing.com.