Borland Online And The Cobb Group Present:


June, 1994 - Vol. 1 No. 6

Borland C++ 3.1 Windows Programming - Running a DOS program as a Windows program

When you're learning to create your first Windows program, you're dealing with several complex issues at once. First, you learn about Windows graphics architecture and Application Programming Interface (API). Shortly thereafter, you need to learn about event-driven programming, resources, and memory management.

If you're trying to convert a DOS program to run under Windows, you might change many aspects of the original code to try to create a true Windows program. Naturally, some programs are easier to convert than others, but even command-line interface programs can be difficult to move from DOS to Windows.

In this article, we'll show you the fastest way to convert a command-line interface program into a Windows program using Borland C++'s EasyWin feature. Let's begin by reviewing the basic elements of a DOS program and a Windows program, and then examining the components of an EasyWin program.

The main() differences

To create a DOS program in Borland C++, you'll have to do at least two things. First, you'll tell the compiler you're going to create a DOS program. When the compiler builds the program, it will generate the appropriate code for DOS to load your program and then run it by calling the program's main() function.

The second responsibility you have in creating a DOS program is providing the main() function that we just mentioned. By convention, all C or C++ DOS programs must have exactly one main() function.

To create a Windows program, you'll have to do some things a bit differently, but you'll still need to do at least two things. First, you'll tell the compiler you're going to create a Windows program. Windows loads programs in a different manner from DOS, so the compiler has to create the appropriate code for what Windows expects.

Second, you'll provide a special function called WinMain() that Windows calls to run your program. The WinMain() function is similar to the main() function for a DOS program, with a couple of exceptions: You must declare it to use the Pascal calling convention, and its arguments include more than just the command-line parameters.

Creating an EasyWin program falls somewhere between creating a DOS program and creating a Windows program. First, you'll tell the compiler you're going to create a Windows program. This tells the compiler to create the appropriate code for Windows to run this program.

However, unlike a traditional Windows program, you'll provide a main() function instead of a WinMain() function. When Borland C++ sees a main() function in a Windows program, it recognizes that the program should use EasyWin and will link the necessary code to provide a WinMain() function that calls your main() function.

As you can see, this means you can convert many DOS programs to Windows programs by reconfiguring the Borland C++ compiler to create a Windows program. To demonstrate this, let's create a simple EasyWin program.

Converting from DOS to Windows

To begin writing an EasyWin program, launch the Borland C++ 3.1 Integrated Development Environment (IDE) for Windows. When the IDE's main window appears, choose Close Project from the Project menu if a project file is open.

Next, choose Application... from the Options menu to display the Application Options dialog box. Click the Windows App button to tell the compiler that you want to create a Windows program, as shown in Figure A. Click OK to save this setting.


Figure A - In the Application Options dialog box, you tell the compiler to create a Windows program.

Open an editor window for a new source file by choosing New from the File menu. When the editor window appears, enter the code from Listing A.


Listing A: EASYWIN.CPP
#include <iostream.h>

main()
{
  char temp;
  while(temp != 'q')
    cin >> temp;
  cout << "done!" << endl;
  return 0;
}

When you finish entering the code, choose Save from the File menu. In the Save File As dialog box, enter EASYWIN.CPP in the File Name entry field. Click OK to save the file.

To compile and run this source file as a Windows program, choose Run from the Run menu. After the compiler builds and links the application (you'll see a warning about the compiler using the default Module Definition File), an empty window with the title EASYWIN.EXE will appear.

To test the application, enter the following characters one at a time: a, b, c, q (press [Enter] after each letter). When you finish, the word Inactive will appear in the title of the window, and the window will resemble the one shown in Figure B.


Figure B - The EASYWIN.EXE program has a simple command-line interface.

To close the EASYWIN.EXE window, double-click on its System menu icon. When the IDE reappears, choose Exit from the File menu to quit.

Conclusion

Converting a DOS program to a true Windows program can be a challenging task. By converting a DOS program to an EasyWin program, you can gradually move your command-line applications to the Windows environment.

Return to the Borland C++ Developer's Journal index

Subscribe to the Borland C++ Developer's Journal


Copyright (c) 1996 The Cobb Group, a division of Ziff-Davis Publishing Company. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis Publishing Company is prohibited. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Publishing Company.