Reputation: 73
In MFC DLL Why do I have to declare theApp using “CWinApp theApp;”,or else it will occur runtime exception. When theApp was instantiated?
Upvotes: 3
Views: 5535
Reputation: 4663
[From MSDN]
theApp is the application object. This object is constructed when other C++ global objects are constructed and should be already available when Windows calls the WinMain function. And it is necessary to declare your object at the global level.
[From Programming VisualC++]
Application startup—When the user starts the application, Windows calls the application framework's built-in WinMain function, and WinMain looks for your globally constructed application object of a class derived from CWinApp. Don't forget that in a C++ program global objects are constructed before the main program is executed.
The CMyApp::InitInstance member function—When the WinMain function finds the application object, it calls the virtual InitInstance member function, which makes the calls needed to construct and display the application's main frame window. You must override InitInstance in your derived application class because the CWinApp base class doesn't know what kind of main frame window you want.
The CWinApp::Run member function—The Run function is hidden in the base class, but it dispatches the application's messages to its windows, thus keeping the application running. WinMain calls Run after it calls InitInstance.
Upvotes: 6