Analyze OLE Server Code in InitInstance

Switch to Scribble.cpp and examine the OLE server code that AppWizard provided, beginning with CScribbleApp theApp. (Note that the default class names differ from those you customized for Scribble.)

All MFC OLE applications — whether container, server, or Automation server — must call AfxOleInit and the static function COleTemplateServer::RegisterAll from the application’s InitInstance to initialize framework support for OLE. For an application that is not an OLE server, the CDocTemplate object coordinates the creation of the frame window, view, and document object for the stand-alone application. The CDocTemplate uses menu, accelerator, and string resources passed to the constructor in InitInstance code, to determine the menu, accelerators, and Windows shell registration of the stand-alone application.

In the case of an OLE server application, additional information is needed. The InitInstance function passes this information as parameters to the CDocTemplate::SetServerInfo function, before it calls CWinApp::AddDocTemplate. Here is a description of the parameters:

pDocTemplate->SetServerInfo(
   IDR_SCRIBBTYPE_SRVR_EMB, IDR_SCRIBBTYPE_SRVR_IP,
   RUNTIME_CLASS(CInPlaceFrame));

The following code, from Scribble.cpp, defines the OLE class ID for the Scribble application and registers the application. AppWizard provides a default ID that is randomly generated. The call to COleTemplateServer::ConnectTemplate registers the class ID with Windows.

Note   The clsid below will differ from the unique one that is provided when you run AppWizard.

static const CLSID clsid =
{ 0x0002180f, 0x0, 0x0, { 0xC0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x46 } };

...

m_server.ConnectTemplate(clsid, pDocTemplate, FALSE);

If the application was spawned by OLE as an in-place server or Automation server, then InitInstance returns before performing additional initialization tasks that are appropriate only for stand-alone applications:

if (cmdInfo.m_bRunEmbedded || cmdInfo.m_bRunAutomated)
{
   return TRUE;
}