The Program Entry Point

With this global look at HELLOWIN.C out of the way, we can now begin the line-by-line dissection of the program. The code begins with an #include statement to include the WINDOWS.H header file:

#include <windows.h>

WINDOWS.H contains declarations of the Windows functions, the Windows structures, the new data types, and numeric constants.

This is followed by a forward declaration of the WndProc function:

long FAR PASCAL WndProc (HWND, WORD, WORD, LONG) ;

The declaration is required because WndProc is referenced by some code in the WinMain function.

In a C program written for a conventional environment, the entry point is a function called main. This is where the program begins execution. (Actually, the main function is the entry point to the part of the program written by the programmer. Usually the C compiler will insert some start-up code in the executable file. The start-up code then calls main.) The entry point of a Windows program is a function called WinMain. (As is the case with main, WinMain is actually called from some start-up code inserted into the executable file.) WinMain is always defined like this:

int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,

LPSTR lpszCmdParam, int nCmdShow)

This function uses the PASCAL calling sequence and returns an integer to the start-up code. The function must be named WinMain. It has four parameters.

The hInstance parameter is called the ”instance handle.“ This is a number that uniquely identifies the program when it is running under Windows. It could be that the user is running multiple copies of the same program under Windows. (For example, most Windows users at one time or another have loaded multiple versions of the CLOCK program to see what happens.) Each copy is called an ”instance,“ and each has a different hInstance value. The instance handle is comparable to a ”task ID“ or ”process ID“ number common in mulitasking operating systems.

The hPrevInstance (”previous instance“) parameter is the instance handle of the most recent previous instance of the same program that is still active. If no other copies of the program are currently loaded, then hPrevInstance will be 0 or NULL.

The lpszCmdParam parameter is a long (or far) pointer to a 0-terminated string that contains any command-line parameters passed to the program. It is possible to run a Windows program with a command-line parameter by typing the program name and the parameter into the Run dialog box invoked from either the Program Manager or the File Manager.

The nCmdShow parameter is a number indicating how the window is to be initially displayed in Windows. This number is assigned by whatever program executes the program to run under Windows. Programs do not often need to examine this number, but they can if they want. In most cases the number is either a 1 or a 7. But it's best not to think of the value as a 1 or a 7. Rather, think of the value as SW_SHOWNORMAL (defined in WINDOWS.H as 1) or SW_SHOWMINNOACTIVE (defined as 7). The SW prefix in these identifiers stands for ”show window.“ This indicates whether the user launched the program to be displayed as a normal window or to be initially minimized.