home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / MFC / SRC / OLETSVR.CP_ / OLETSVR.CP
Encoding:
Text File  |  1993-02-08  |  5.6 KB  |  207 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library. 
  2. // Copyright (C) 1992 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 Microsoft 
  7. // QuickHelp and/or WinHelp documentation provided with the library. 
  8. // See these sources for detailed information regarding the 
  9. // Microsoft Foundation Classes product. 
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef AFX_OLE_SEG
  14. #pragma code_seg(AFX_OLE_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char BASED_CODE THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. /////////////////////////////////////////////////////////////////////////////
  23.  
  24. COleTemplateServer::COleTemplateServer()
  25.     : COleServer(FALSE)
  26. {
  27.     m_pDocTemplate = NULL;
  28.     ASSERT(!m_bLaunchEmbedded);     // will be set later in RunEmbedded
  29. }
  30.  
  31. BOOL COleTemplateServer::RunEmbedded(CDocTemplate* pDocTemplate,
  32.     BOOL bMultiInstance, LPCSTR lpszCmdLine)
  33. {
  34. #ifdef _DEBUG   // pre-conditions
  35.     ASSERT_VALID(this);
  36.     ASSERT(!IsOpen()); // only call once
  37.     ASSERT(m_pDocTemplate == NULL);
  38.  
  39.     // doc template must be valid and unattached
  40.     ASSERT_VALID(pDocTemplate);
  41.     ASSERT(pDocTemplate->m_pAttachedServer == NULL);
  42.     if (bMultiInstance)
  43.         ASSERT(pDocTemplate->IsKindOf(RUNTIME_CLASS(CSingleDocTemplate)));
  44.     else
  45.         ASSERT(pDocTemplate->IsKindOf(RUNTIME_CLASS(CMultiDocTemplate)));
  46. #endif //_DEBUG
  47.  
  48.     m_pDocTemplate = pDocTemplate;
  49.     m_pDocTemplate->m_pAttachedServer = this;
  50.  
  51.     // first register the server
  52.     CString strServerName;
  53.     CString strLocalServerName;
  54.  
  55.     if (!m_pDocTemplate->GetDocString(strServerName,
  56.        CDocTemplate::regFileTypeId) || strServerName.IsEmpty())
  57.     {
  58.         TRACE0("Error: not enough information in DocTemplate to"
  59.                     " register OLE server\n");
  60.         return FALSE;
  61.     }
  62.     if (!m_pDocTemplate->GetDocString(strLocalServerName,
  63.        CDocTemplate::regFileTypeName))
  64.         strLocalServerName = strServerName;     // use non-localized name
  65.  
  66.     ASSERT(strServerName.Find(' ') == -1);  // no spaces allowed
  67.  
  68.     // check if run with /Embedding or at least /E
  69.     BOOL bEmbedded = FALSE;     // OLE Embedding
  70.     BOOL bRun = FALSE;          // DDE -e or OLE Embedding
  71.  
  72.     // Hard coded non-localized name
  73.     static char BASED_CODE szEmbedding[] = "Embedding";
  74. #define EMBEDDING_LEN   9
  75.     ASSERT(lstrlen(szEmbedding) == EMBEDDING_LEN);
  76.  
  77.     while (*lpszCmdLine == ' ')
  78.         lpszCmdLine++;
  79.     if ((*lpszCmdLine == '-' || *lpszCmdLine == '/'))
  80.     {
  81.         lpszCmdLine++;
  82.         if (*lpszCmdLine == 'e' || *lpszCmdLine == 'E')
  83.         {
  84.             bRun = TRUE;        // OLE or DDE embedded launch
  85.             if (_fstrncmp(szEmbedding, lpszCmdLine, EMBEDDING_LEN) == 0)
  86.             {
  87.                 bEmbedded = TRUE;       // OLE embedded
  88.                 m_bLaunchEmbedded = TRUE;   // for cleanup logic
  89.                 lpszCmdLine += EMBEDDING_LEN;
  90.             }
  91.             else
  92.             {
  93.                 // just skip the 'e'
  94.                 lpszCmdLine++;
  95.             }
  96.         }
  97.     }
  98.  
  99.     if (!bEmbedded)
  100.     {
  101.         // not launched embedded - autoregister
  102.         if (!AfxOleRegisterServerName(strServerName, strLocalServerName))
  103.         {
  104.             // not fatal (don't fail just warn)
  105.             AfxMessageBox(AFX_IDP_FAILED_TO_AUTO_REGISTER);
  106.         }
  107.     }
  108.  
  109.     if (!Register(strServerName, bMultiInstance))
  110.     {
  111.         AfxMessageBox(AFX_IDP_FAILED_TO_REGISTER);
  112.         return FALSE;       // can't continue
  113.     }
  114.  
  115.     if (!bRun)
  116.         return FALSE;
  117.  
  118.     while (*lpszCmdLine == ' ')
  119.         lpszCmdLine++;
  120.     if (*lpszCmdLine != '\0')
  121.     {
  122.         // open the initial data file (may go to a different template)
  123.         CDocument* pDoc = AfxGetApp()->OpenDocumentFile(lpszCmdLine);
  124.         if (pDoc == NULL)
  125.         {
  126.             TRACE1("Error: failed to open embedded data file '%Fs'\n",  
  127.                 lpszCmdLine);
  128.             // Since launching failed, we will revoke the server now
  129.             BeginRevoke();
  130.             return FALSE;
  131.         }
  132.         else if (pDoc->GetDocTemplate() != pDocTemplate)
  133.         {
  134.             TRACE1("Warning: embedded data file '%Fs'"
  135.                 " opened by unexpected CDocTemplate\n", lpszCmdLine);
  136.         }
  137.     }
  138.  
  139.     return TRUE;        // we can run in embedded mode
  140. }
  141.  
  142. COleServerDoc* COleTemplateServer::OnOpenDoc(LPCSTR lpszDocName)
  143. {
  144.     ASSERT_VALID(this);
  145.     ASSERT_VALID(m_pDocTemplate);
  146.     ASSERT(lpszDocName != NULL);
  147.  
  148.     // detach during library create
  149.     ASSERT(m_pDocTemplate->m_pAttachedServer == this);
  150.     m_pDocTemplate->m_pAttachedServer = NULL;   // detach while create
  151.  
  152. #ifdef _NEARDATA
  153.     CString strT(lpszDocName);
  154.     CDocument* pDoc = m_pDocTemplate->OpenDocumentFile(strT);
  155. #else
  156.     CDocument* pDoc = m_pDocTemplate->OpenDocumentFile(lpszDocName);
  157. #endif
  158.  
  159.     // re-attach
  160.     ASSERT(m_pDocTemplate->m_pAttachedServer == NULL);
  161.     m_pDocTemplate->m_pAttachedServer = this;
  162.  
  163.     if (pDoc == NULL)
  164.         return NULL;
  165.     ASSERT(pDoc->IsKindOf(RUNTIME_CLASS(COleServerDoc)));
  166.     return (COleServerDoc*)pDoc;
  167. }
  168.  
  169. COleServerDoc* COleTemplateServer::OnCreateDoc(LPCSTR /*lpszTypeName*/,
  170.     LPCSTR lpszDocName)
  171. {
  172.     ASSERT_VALID(this);
  173.     ASSERT_VALID(m_pDocTemplate);
  174.     ASSERT(lpszDocName != NULL);
  175.  
  176.     // detach during library create
  177.     ASSERT(m_pDocTemplate->m_pAttachedServer == this);
  178.     m_pDocTemplate->m_pAttachedServer = NULL;   // detach while create
  179.  
  180.     CDocument* pDoc = m_pDocTemplate->OpenDocumentFile(NULL);
  181.  
  182.     // re-attach
  183.     ASSERT(m_pDocTemplate->m_pAttachedServer == NULL);
  184.     m_pDocTemplate->m_pAttachedServer = this;
  185.  
  186.     if (pDoc == NULL)
  187.         return NULL;
  188.     ASSERT(pDoc->IsKindOf(RUNTIME_CLASS(COleServerDoc)));
  189.     pDoc->SetModifiedFlag();
  190. #ifdef _NEARDATA
  191.     CString strT(lpszDocName);
  192.     pDoc->SetTitle(strT);
  193. #else
  194.     pDoc->SetTitle(lpszDocName);
  195. #endif
  196.     return (COleServerDoc*)pDoc;
  197. }
  198.  
  199. COleServerDoc* COleTemplateServer::OnEditDoc(LPCSTR lpszTypeName,
  200.     LPCSTR lpszDocName)
  201. {
  202.     ASSERT_VALID(this);
  203.     return OnCreateDoc(lpszTypeName, lpszDocName);
  204. }
  205.  
  206. /////////////////////////////////////////////////////////////////////////////
  207.