Windows allows several copies of the same program to be running at the same time. Thus, application initialization is conceptually divided into two sections: one-time initialization that is done the first time the program runs, and instance initialization that runs each time a copy of the program runs, including the first time.
The one-time application initialization of a typical Windows program calls RegisterClass for each of the unique Windows registration classes used by the program. Windows programs that use the Microsoft Foundation Class Library do not normally need to call RegisterClass, so there is typically no need to do one-time initialization in a Microsoft Foundation-based Windows program.
The Microsoft Foundation class CWinApp provides the member function InitApplication, which you can override to perform any one-time application initialization tasks for your application. The Microsoft Foundation classes already provide much of the default initialization (such as creating Windows registration classes) that is normally found in traditional Windows initialization functions. If you do not need to do any other special one-time initialization, you do not have to override InitApplication.
How to Initialize Each Application Instance
Each instance (copy) of a Windows program is given a chance to perform instance-specific application initialization. Typically, each instance must at least create a main window. The Microsoft Foundation class CWinApp provides the member function InitInstance that you will normally override to perform instance-specific application initialization.
For example, the sample program in HELLO.CPP overrides InitInstance to create a main window from the derived window class CMainWindow, and assigns the window pointer to the CWinApp member variable m_pMainWnd. The overriding function InitInstance then shows the Window and updates it, as shown here:
//in .H file
class CTheApp : public CWinApp
{
public:
BOOL InitInstance();
};
// in .CPP file
BOOL CTheApp::InitInstance()
{
m_pMainWnd = new CMainWindow();
m_pMainWnd->ShowWindow( m_nCmdShow );
m_pMainWnd->UpdateWindow();
return TRUE;
}
The member variable m_nCmdShow is passed to the ShowWindow function to specify whether the window is visible or not. m_nCmdShow is the value passed to WinMain in the nCmdShow argument.
In your derived application class, you will typically override InitInstance in much the same way as shown, substituting your own window class for CMainWindow. You may also use InitInstance to perform additional initialization tasks (buffer allocation, etc.) as well.