oletsvr.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. #include "stdafx.h"
  11. #ifdef AFX_OLE3_SEG
  12. #pragma code_seg(AFX_OLE3_SEG)
  13. #endif
  14. #ifdef _DEBUG
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. #define new DEBUG_NEW
  19. /////////////////////////////////////////////////////////////////////////////
  20. // COleTemplateServer
  21. COleTemplateServer::COleTemplateServer()
  22. : COleObjectFactory(CLSID_NULL, NULL, FALSE, NULL)
  23. {
  24. m_pDocTemplate = NULL;
  25. }
  26. BOOL COleTemplateServer::Register()
  27. {
  28. return COleObjectFactory::Register();
  29. }
  30. BOOL COleTemplateServer::OnCmdMsg(UINT nID, int nCode, void* pExtra,
  31. AFX_CMDHANDLERINFO* pHandlerInfo)
  32. {
  33. BOOL bReturn;
  34. if (nCode == CN_OLE_UNREGISTER)
  35. bReturn = Unregister();
  36. else
  37. bReturn = COleObjectFactory::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
  38. return bReturn;
  39. }
  40. BOOL COleTemplateServer::Unregister()
  41. {
  42. BOOL bReturn = COleObjectFactory::Unregister();
  43. if (!bReturn)
  44. return FALSE;
  45. // get registration info from doc template string
  46. CString strServerName;
  47. CString strLocalServerName;
  48. CString strLocalShortName;
  49. if (!m_pDocTemplate->GetDocString(strServerName,
  50. CDocTemplate::regFileTypeId) || strServerName.IsEmpty())
  51. {
  52. TRACE0("Error: not enough information in DocTemplate to unregister OLE server.\n");
  53. return FALSE;
  54. }
  55. if (!m_pDocTemplate->GetDocString(strLocalServerName,
  56. CDocTemplate::regFileTypeName))
  57. strLocalServerName = strServerName; // use non-localized name
  58. if (!m_pDocTemplate->GetDocString(strLocalShortName,
  59. CDocTemplate::fileNewName))
  60. strLocalShortName = strLocalServerName; // use long name
  61. ASSERT(strServerName.Find(' ') == -1); // no spaces allowed
  62. // place entries in system registry
  63. if (!AfxOleUnregisterServerClass(m_clsid, strServerName,
  64. strLocalShortName, strLocalServerName, (OLE_APPTYPE) m_bOAT))
  65. {
  66. bReturn = FALSE;
  67. }
  68. return bReturn;
  69. }
  70. void COleTemplateServer::ConnectTemplate(
  71. REFCLSID clsid, CDocTemplate* pDocTemplate, BOOL bMultiInstance)
  72. {
  73. ASSERT_VALID(this);
  74. ASSERT_VALID(pDocTemplate);
  75. ASSERT(pDocTemplate->m_pAttachedFactory == NULL);
  76. // setup initial state of underlying COleObjectFactory
  77. m_clsid = clsid;
  78. ASSERT(m_pRuntimeClass == NULL);
  79. m_bMultiInstance = bMultiInstance;
  80. // attach the doc template to the factory
  81. m_pDocTemplate = pDocTemplate;
  82. m_pDocTemplate->m_pAttachedFactory = this;
  83. }
  84. void COleTemplateServer::UpdateRegistry(OLE_APPTYPE nAppType,
  85. LPCTSTR* rglpszRegister, LPCTSTR* rglpszOverwrite)
  86. {
  87. ASSERT(m_pDocTemplate != NULL);
  88. ASSERT(m_bOAT == (BYTE) OAT_UNKNOWN);
  89. m_bOAT = (BYTE) nAppType;
  90. // get registration info from doc template string
  91. CString strServerName;
  92. CString strLocalServerName;
  93. CString strLocalShortName;
  94. CString strLocalFilterName;
  95. CString strLocalFilterExt;
  96. if (!m_pDocTemplate->GetDocString(strServerName,
  97. CDocTemplate::regFileTypeId) || strServerName.IsEmpty())
  98. {
  99. TRACE0("Error: not enough information in DocTemplate to register OLE server.\n");
  100. return;
  101. }
  102. if (!m_pDocTemplate->GetDocString(strLocalServerName,
  103. CDocTemplate::regFileTypeName))
  104. strLocalServerName = strServerName; // use non-localized name
  105. if (!m_pDocTemplate->GetDocString(strLocalShortName,
  106. CDocTemplate::fileNewName))
  107. strLocalShortName = strLocalServerName; // use long name
  108. if (!m_pDocTemplate->GetDocString(strLocalFilterName,
  109. CDocTemplate::filterName))
  110. ASSERT(nAppType != OAT_DOC_OBJECT_SERVER);
  111. if (!m_pDocTemplate->GetDocString(strLocalFilterExt,
  112. CDocTemplate::filterExt))
  113. ASSERT(nAppType != OAT_DOC_OBJECT_SERVER);
  114. ASSERT(strServerName.Find(' ') == -1); // no spaces allowed
  115. int nIconIndex = 0;
  116. POSITION pos = AfxGetApp()->GetFirstDocTemplatePosition();
  117. for (int nIndex = 1; pos != NULL; nIndex++)
  118. {
  119. CDocTemplate* pTemplate = AfxGetApp()->GetNextDocTemplate(pos);
  120. if (pTemplate == m_pDocTemplate)
  121. {
  122. nIconIndex = nIndex;
  123. pos = NULL; // set exit condition
  124. }
  125. }
  126. // place entries in system registry
  127. if (!AfxOleRegisterServerClass(m_clsid, strServerName,
  128. strLocalShortName, strLocalServerName, nAppType,
  129. rglpszRegister, rglpszOverwrite, nIconIndex,
  130. strLocalFilterName, strLocalFilterExt))
  131. {
  132. // not fatal (don't fail just warn)
  133. AfxMessageBox(AFX_IDP_FAILED_TO_AUTO_REGISTER);
  134. }
  135. }
  136. CCmdTarget* COleTemplateServer::OnCreateObject()
  137. {
  138. ASSERT_VALID(this);
  139. ASSERT_VALID(m_pDocTemplate);
  140. // save application user control status
  141. BOOL bUserCtrl = AfxOleGetUserCtrl();
  142. // create invisible doc/view/frame set
  143. CDocument* pDoc = m_pDocTemplate->OpenDocumentFile(NULL, FALSE);
  144. // restore application's user control status
  145. AfxOleSetUserCtrl(bUserCtrl);
  146. if (pDoc != NULL)
  147. {
  148. ASSERT_VALID(pDoc);
  149. ASSERT_KINDOF(CDocument, pDoc);
  150. // all new documents created by OLE start out modified
  151. pDoc->SetModifiedFlag();
  152. }
  153. return pDoc;
  154. }
  155. /////////////////////////////////////////////////////////////////////////////