Microsoft KB Archive/140591

= How To Display an MFC Automation Document Automatically =

Article ID: 140591

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.51

 Microsoft Visual C++ 1.52 Professional Edition

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.1</li></ul>

 Microsoft Visual C++ 2.2</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 4.1 Subscription</li></ul>

 Microsoft Visual C++ 4.2 Enterprise Edition</li></ul>

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

<ul> <li>Microsoft Visual C++ 6.0 Enterprise Edition</li></ul>

<ul> <li>Microsoft Visual C++ 6.0 Professional Edition</li></ul>

<ul> <li>Microsoft Visual C++ 6.0 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q140591

<div class="summary_section">

SUMMARY
Some OLE Automation servers, such as Microsoft Excel, automatically display a worksheet when it is created through OLE Automation. OLE Automation servers implemented in MFC do not display the document window by default when the document object is created through OLE Automation. This article explains how to add this behavior to a generic MFC OLE Automation server created using AppWizard.

<div class="moreinformation_section">

MORE INFORMATION
The creation process for a document object created through OLE Automation slightly differs from the process involved when the server's File.New menu item is used. The document object is created using a class factory, which is represented by the application object's COleTemplateServer data member (m_server).

When a new document object is created through OLE Automation, COleTemplateServer::OnCreateObject is called, which calls CDocTemplate::OpenDocumentFile with the last parameter set to FALSE to create an invisible document window. By replacing this call to CDocTemplate::OpenDocumentFile with the last parameter set to TRUE, a visible document window will be created.

Additionally, the code in the application's InitInstance, which checks if the server is running embedded or automated, should be removed. The main window of the application will not be displayed if this code isn't removed and the server isn't already running.

Step-by-Step Procedure
The following steps illustrate how to add this behavior to a generic MFC OLE Automation server created using AppWizard:

<ol> <li>Using AppWizard, create an SDI or MDI application called MyApp with OLE Automation support.</li> <li> Add the following class definition before the project's CWinApp-derived class's definition in the application object's header file (MyApp.h). // CMyTemplateServer used to override OnCreateObject. class CMyTemplateServer : public COleTemplateServer {     // Constructors / Destructors public: CMyTemplateServer{} virtual ~CMyTemplateServer{}

// Overrides virtual CCmdTarget* OnCreateObject; };                       </li> <li> Add the following implementation of OnCreateObject to the CWinApp- derived class's implementation file (MyApp.cpp). // Identical to COleTemplateServer::OnCreateObject except for // the call to OpenDocumentFile with the last parameter set to  // TRUE instead of FALSE. CCmdTarget* CMyTemplateServer::OnCreateObject {     ASSERT_VALID(this); ASSERT_VALID(m_pDocTemplate);

// Save application user control status BOOL bUserCtrl = AfxOleGetUserCtrl;

// Create visible doc/view/frame set CDocument* pDoc; pDoc = m_pDocTemplate->OpenDocumentFile(NULL, TRUE);

// Restore application's user control status AfxOleSetUserCtrl(bUserCtrl);

if (pDoc != NULL) {        ASSERT_VALID(pDoc); ASSERT_KINDOF(CDocument, pDoc);

// All new documents created by OLE start out modified pDoc->SetModifiedFlag; }     return pDoc; }                       </li> <li> Change the CWinApp-derived object's m_server member from type COleTemplateServer to CMyTemplateServer. For example: class CMyApp : public CWinApp {      public: CMyApp;

// Overrides // ClassWizard generated virtual function overrides // AFX_VIRTUAL

// Implementation CMyTemplateServer m_server; // <== change to derived class ...      ...    };                        </li> <li> Modify the check for running embedded or automated from the application's InitInstance member function. For example, modify the AppWizard provided code to match the following lines from InitInstance: // Check to see if launched as OLE server if (cmdInfo.m_bRunEmbedded || cmdInfo.m_bRunAutomated) {        // Register all OLE server (factories) as running. This enables // the OLE libraries to create objects from other applications. COleTemplateServer::RegisterAll;

// Application was run with /Embedding or /Automation. Don't        // show the main window in this case. //     return TRUE; } else {

// When a server application is launched stand-alone, it        // is a good idea to update the system registry in case // it has been damaged. m_server.UpdateRegistry(OAT_DISPATCH_OBJECT); COleObjectFactory::UpdateRegistryAll; } NOTE: This code is specific to Visual C++ version 4.0; it varies with the earlier versions. </li> <li>Compile the application, and run it stand-alone.</li> <li>Create an OLE Automation client that creates an instance of your document type. Upon creation of the document object, the view should automatically be displayed.</li></ol>

Keywords: kbautomation kbhowto KB140591

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.