To build a Windows application, follow these steps:
1.Create C-language or assembly-language source files that contain the WinMain function, window procedures, and other application code.
2.Use the resource editors (Image Editor and Font Editor) to create any cursor, icon, bitmap, and font resources the application will require.
3.Create a resource-definition (.RC) file that defines the application's resources. This file lists and names the resources you created in the preceding step. It also defines menus, dialog boxes, and other resources.
4.Create the module-definition (.DEF) file, which defines the attributes of the application modules, such as segment attributes, stack size, and heap size.
5.Compile and link all C-language source files; assemble all assembly-language source files.
6.Use RC to compile the resource-definition file and add it to the executable file.
Some programming practices that work well for C-language or assembly-language applications will not work at all in the Windows environment. For detailed information about using C and assembly language to write Windows applications, see Chapter 14, “C and Assembly Language.”
In general, when writing Windows applications, remember the following rules:
Do not take exclusive control of the CPU—it is a shared resource. Although Windows is a multitasking system, it is nonpreemptive. This means it cannot take control back from an application until the application releases control. A cooperative application carefully manages access to the CPU and gives other applications ample opportunity to run.
Do not attempt to directly access memory or hardware devices such as the keyboard, mouse, timer, screen, and serial and parallel ports. Windows requires absolute control of these resources to ensure equal, uninterrupted access for all applications that are running.
Within the application, all functions that Windows can call must be defined with the PASCAL keyword; this ensures that the function accesses arguments correctly. Functions that Windows can call are the WinMain function, callback functions, and window procedures.
Every application must have a WinMain function. This function is the entry point, or starting point, for the application. It contains statements and functions that create windows and that read and dispatch input intended for the application. The function definition has the following form:
int PASCAL WinMain(hinstCurrent, hinstPrevious, lpszCmdLine,
nCmdShow)
HINSTANCE hinstCurrent; /* handle of current instance */
HINSTANCE hinstPrevious; /* handle of previous instance */
LPSTR lpszCmdLine; /* address of command line */
int nCmdShow; /* show-window type (open/icon) */
{
.
.
.
}
The WinMain function must be declared with the PASCAL keyword. Although Windows calls the function directly, WinMain must not be defined with the FAR keyword, since it is called from linked-in startup code.
When using Windows functions, be sure to check the return values. Do not ignore these return values, because unusual conditions sometimes occur when a function fails.
Do not use C run-time functions for console input and output. These functions include getchar, putchar, scanf, and printf.
Do not use C run-time file-input-and-output functions to access serial and parallel ports. Instead, use the communications functions, which are described in detail in the Microsoft Windows Programmer's Reference, Volume 2.
You can use the C run-time file-input-and-output functions to access disk files. In particular, use the Windows OpenFile function and the low-level, C run-time input-and-output functions. Although you can use the C run-time stream-input-and-output functions, you do not get the advantages that OpenFile provides.
You can use the C run-time memory-management functions malloc, calloc, realloc, and free, but be aware that Windows translates these functions to its own local-heap functions, LocalAlloc, LocalReAlloc, and LocalFree. Since local-heap functions do not always operate exactly as do C run-time memory-management functions, you may get unexpected results.