February 20, 2001


From notes:

ENTRY POINT of any program:

In Windows programming (C/C++) the main() function is replaced by WinMain() which forms the Entry Point for the windows program.

This WinMain() function gets some parameters for initialization.
Normally when programming in VC++, this entry point WinMain() is hidden from the programmer/user. It is present in the directory “msvc\mfc\src\” and the file name is “winmain.cpp”. This is called by all the programs while it is executed.

Parameters of WinMain()

There are 4 important parameters of WinMain() function First 2 Parameters are of type HINSTANCE and third parameter is of type LPTSTR (basically char * type) type and last parameter is of “int” type.

The first 2 parameter finds the existance of previous instance and current instance of the window.

Third argument is a Char * type which is normal string to the function displayed on the screen as title of the window.

Fourth argument is int type which tells that how window is to be displayed ie., whether as icon,maximised or normal etc., this argument is basically sent to ShowWindow() function of CWnd class.

WinMain() calls basically 4 functions

WinMain() (Sequence)







calls +----------------->




1. InitApplication()
2. InitInstance()
3. ExitInstance()
4. Run()

All the above functions are of CWinApp class.


Function of CWinApp class calls create function, show window function and update windows. This is the function where we write code for creating an application window.

This is the function belongs to CWinApp class which is used to clean up the procedure. Basically the procedures are written in the constructor.

This function is used to set the MESSAGE_MAPs for all the message handlers.
This function consists of an infinite loop


#check 1


checks for the user input

#check 2


checks for the user QUIT message


till the user input is received or the quit message is received this will be in the loop.

The Following code shows the Run function will call the CWinThread::Run() function :

int CWinApp::Run() ( Available in APPCORE.CPP )
if (m_pMainWnd == NULL && AfxOleGetUserCtrl())
// Not launched /Embedding or /Automation,
//but has no main window!
TRACE0("Warning: m_pMainWnd is NULL in CWinApp::
Run - quitting application.\n");
return CWinThread::Run();



int CWinThread::Run() ( Available in THRDCORE.CPP )

// for tracking the idle time state
BOOL bIdle = TRUE;
LONG lIdleCount = 0;

// acquire and dispatch messages
//until a WM_QUIT message is received.
for (;;)
// phase1: check to see if we can do idle work
while (bIdle &&
!::PeekMessage(&m_msgCur, NULL, NULL, NULL,
// call OnIdle while in bIdle state
if (!OnIdle(lIdleCount++))
bIdle = FALSE; // assume "no idle"

// phase2: pump messages while available
// pump message, but quit on WM_QUIT
if (!PumpMessage())
return ExitInstance();

// reset "no idle" state after pumping
//"normal" message
if (IsIdleMessage(&m_msgCur))
bIdle = TRUE;
lIdleCount = 0;

} while (::PeekMessage(&m_msgCur, NULL, NULL, NULL, PM_NOREMOVE));
ASSERT(FALSE); // not reachable