Much like the main function in standard C programs, the WinMain function is the entry point for a Windows application. Every Windows application must have a WinMain function; 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 initializations
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(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
HANDLE hInstance;/* current instance */
HANDLE hPrevInstance;/* previous instance */
LPSTR lpCmdLine;/* command line */
int nCmdShow;/* whether to show window or 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:
hInstance
The instance handle of the application.
hPrevInstance
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.
lpCmdLine
A long pointer to a null-terminated command line.
nCmdShow
An integer value 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 on handles, see the next page. For more information on the lpCmdLine parameter, see “The Application Command-Line Parameter”.