Article ID: 140591
Article Last Modified on 11/21/2006
// CMyTemplateServer used to override OnCreateObject.
class CMyTemplateServer : public COleTemplateServer
{
// Constructors / Destructors
public:
CMyTemplateServer(){}
virtual ~CMyTemplateServer(){}
// Overrides
virtual CCmdTarget* OnCreateObject();
};
// 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;
}
class CMyApp : public CWinApp
{
public:
CMyApp();
// Overrides
// ClassWizard generated virtual function overrides
// {{AFX_VIRTUAL(CMyApp)
public:
virtual BOOL InitInstance();
// }}AFX_VIRTUAL
// Implementation
CMyTemplateServer m_server; // <== change to derived class
...
...
};
// 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.Keywords: kbautomation kbhowto KB140591