Use the CWinApp class to create a Windows application object. An application object provides member functions for initializing the application (and each instance of it) and for running the application.
Each application that uses the Foundation classes can only contain one CWinApp object. This object is constructed when other C++ global objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the Foundation class library. Declare your CWinApp object at the global level or statically.
Typically, you will derive an application class from CWinApp and override the InitInstance member function to create your application's main window object.
The Microsoft Foundation Class Library provides a number of global functions that you can use to access your CWinApp object, as shown by the following list. These functions are available from C or C++.
Function | Purpose |
AfxGetApp | Obtain a pointer to the CWinApp object. |
AfxGetInstanceHandle | Obtain a handle to the current application instance. |
AfxGetResourceHandle | Obtain a handle to the application's resources. |
AfxGetAppName | Obtain a pointer to a string containing the application's name. |
Note:
Most users of the Foundation classes will use the default WinMain provided by the Foundation class library. If you provide your own version of WinMain, you must call AfxWinInit before using a Foundation class to attach your CWinApp object to the Microsoft Foundation Class Library.