Compartilhar via


InitInstance Member Function

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at InitInstance Member Function.

The Windows operating system allows you to run more than one copy, or "instance," of the same application. WinMain calls InitInstance every time a new instance of the application starts.

The standard InitInstance implementation created by the MFC Application Wizard performs the following tasks:

  • As its central action, creates the document templates that in turn create documents, views, and frame windows. For a description of this process, see Document Template Creation.

  • Loads standard file options from an .ini file or the Windows registry, including the names of the most recently used files.

  • Registers one or more document templates.

  • For an MDI application, creates a main frame window.

  • Processes the command line to open a document specified on the command line or to open a new, empty document.

You can add your own initialization code or modify the code written by the wizard.

Note

MFC applications must be initialized as single threaded apartment (STA). If you call CoInitializeEx in your InitInstance override, specify COINIT_APARTMENTTHREADED (rather than COINIT_MULTITHREADED). For more information, see PRB: MFC Application Stops Responding When You Initialize the Application as a Multithreaded Apartment (828643) at https://support.microsoft.com/default.aspxscid=kb;en-us;828643.

See Also

CWinApp: The Application Class