2.2 WinMain Function

Much like the main function in standard C-language applications, the WinMain function is the entry point for a Windows application. Every Windows application must have a WinMain function (it is always named WinMain); no Windows application can run without it. In most Windows applications, the WinMain function does the following:

Calls initialization functions that register window classes, create windows, and perform any other necessary initializing

Enters a message loop to process messages from the application queue

Terminates the application when the message loop retrieves a WM_QUIT message

The WinMain function 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 requires the PASCAL calling convention.

When the user starts an application, Windows passes the following four parameters to the application's WinMain function:

Parameter Value passed to application

hinstCurrent The instance handle of the application.
hinstPrevious The handle of another instance of the application, if one is running. If no other instances of this application are running, Windows sets this parameter to NULL.
lpszCmdLine A long pointer to a null-terminated command line.
nCmdShow An integer that specifies whether to display the application's window as a window or as an icon. The application passes this value to the ShowWindow function when calling that function to display the application's main window.

For more information about handles, see Section 2.2.2, “Handles.” For more information about the lpszCmdLine parameter, see Section 2.2.11, “Application Command-Line Parameter.”