home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / oleinit.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-16  |  5.6 KB  |  207 lines

  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.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef AFX_INIT_SEG
  14. #pragma code_seg(AFX_INIT_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. #define new DEBUG_NEW
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // OLE OLE_DATA init structure
  26.  
  27. OLE_DATA _oleData;
  28.  
  29. OLE_DATA::OLE_DATA()
  30. {
  31.     // OLE 1.0 Clipboard formats
  32.     cfNative = ::RegisterClipboardFormat(_T("Native"));
  33.     cfOwnerLink = ::RegisterClipboardFormat(_T("OwnerLink"));
  34.     cfObjectLink = ::RegisterClipboardFormat(_T("ObjectLink"));
  35.  
  36.     // OLE 2.0 Clipboard formats
  37.     cfEmbeddedObject = ::RegisterClipboardFormat(_T("Embedded Object"));
  38.     cfEmbedSource = ::RegisterClipboardFormat(_T("Embed Source"));
  39.     cfLinkSource = ::RegisterClipboardFormat(_T("Link Source"));
  40.     cfObjectDescriptor = ::RegisterClipboardFormat(_T("Object Descriptor"));
  41.     cfLinkSourceDescriptor = ::RegisterClipboardFormat(_T("Link Source Descriptor"));
  42.     cfFileName = ::RegisterClipboardFormat(_T("FileName"));
  43.     cfFileNameW = ::RegisterClipboardFormat(_T("FileNameW"));
  44.     cfRichTextFormat = ::RegisterClipboardFormat(_T("Rich Text Format"));
  45.     cfRichTextAndObjects = ::RegisterClipboardFormat(_T("RichEdit Text and Objects"));
  46. }
  47.  
  48. /////////////////////////////////////////////////////////////////////////////
  49. // OLE initialization & termination
  50.  
  51. BOOL AFXAPI AfxOleInit()
  52. {
  53.     _AFX_THREAD_STATE* pState = AfxGetThreadState();
  54.     ASSERT(!pState->m_bNeedTerm);    // calling it twice?
  55.  
  56.     // Special case DLL context to assume that the calling app initializes OLE.
  57.     // For DLLs where this is not the case, those DLLs will need to initialize
  58.     // OLE for themselves via OleInitialize.  This is done since MFC cannot provide
  59.     // automatic uninitialize for DLLs because it is not valid to shutdown OLE
  60.     // during a DLL_PROCESS_DETACH.
  61.     if (afxContextIsDLL)
  62.     {
  63.         pState->m_bNeedTerm = -1;  // -1 is a special flag
  64.         return TRUE;
  65.     }
  66.  
  67.     // first, initialize OLE
  68.     SCODE sc = ::OleInitialize(NULL);
  69.     if (FAILED(sc))
  70.     {
  71.         // warn about non-NULL success codes
  72.         TRACE1("Warning: OleInitialize returned scode = %s.\n",
  73.             AfxGetFullScodeString(sc));
  74.         goto InitFailed;
  75.     }
  76.     // termination required when OleInitialize does not fail
  77.     pState->m_bNeedTerm = TRUE;
  78.  
  79.     // hook idle time and exit time for required OLE cleanup
  80.     CWinThread* pThread; pThread = AfxGetThread();
  81.     pThread->m_lpfnOleTermOrFreeLib = AfxOleTermOrFreeLib;
  82.  
  83.     // allocate and initialize default message filter
  84.     if (pThread->m_pMessageFilter == NULL)
  85.     {
  86.         pThread->m_pMessageFilter = new COleMessageFilter;
  87.         ASSERT(AfxOleGetMessageFilter() != NULL);
  88.         AfxOleGetMessageFilter()->Register();
  89.     }
  90.     return TRUE;
  91.  
  92. InitFailed:
  93.     AfxOleTerm();
  94.     return FALSE;
  95. }
  96.  
  97. void AFXAPI AfxOleTerm(BOOL bJustRevoke)
  98. {
  99.     // release clipboard ownership
  100.     COleDataSource::FlushClipboard();
  101.  
  102.     // revoke all class factories
  103.     COleObjectFactory::RevokeAll();
  104.  
  105. #ifndef _AFX_NO_OCC_SUPPORT
  106.     AfxOleUnlockAllControls();
  107. #endif
  108.  
  109.     if (!bJustRevoke)
  110.     {
  111.         CWinThread* pThread = AfxGetThread();
  112.         if (pThread != NULL)
  113.         {
  114.             // destroy message filter (may be derived class)
  115.             delete pThread->m_pMessageFilter;
  116.             pThread->m_pMessageFilter = NULL;
  117.         }
  118.  
  119.         // terminate OLE last
  120.         _AFX_THREAD_STATE* pState = AfxGetThreadState();
  121.         // -1 is special case, so need to compare against TRUE
  122.         if (pState->m_bNeedTerm == TRUE)
  123.         {
  124.             CoFreeUnusedLibraries();
  125.             ::OleUninitialize();
  126.             pState->m_bNeedTerm = FALSE;
  127.         }
  128.     }
  129. }
  130.  
  131. AFX_STATIC_DATA DWORD _afxTickCount = (DWORD)-1;
  132. AFX_STATIC_DATA BOOL _afxTickInit = FALSE;
  133.  
  134. void AFXAPI AfxOleTermOrFreeLib(BOOL bTerm, BOOL bJustRevoke)
  135. {
  136.     if (bTerm)
  137.     {
  138.         AfxOleTerm(bJustRevoke);
  139.     }
  140.     else
  141.     {
  142.         // initialize _afxTickCount if necessary
  143.         if (!_afxTickInit)
  144.         {
  145.             _afxTickCount = ::GetTickCount();
  146.             ++_afxTickInit;
  147.         }
  148.  
  149.         // only call CoFreeUnusedLibraries if one minute has gone by
  150.         if (GetTickCount() - _afxTickCount > 60000)
  151.         {
  152.             CoFreeUnusedLibraries();
  153.             _afxTickCount = ::GetTickCount();
  154.         }
  155.     }
  156. }
  157.  
  158. /////////////////////////////////////////////////////////////////////////////
  159. // CWinApp support for parsing OLE command line
  160.  
  161. AFX_STATIC BOOL AFXAPI _AfxParseOption(LPTSTR lpszCmdLine, LPCTSTR lpszOption)
  162. {
  163.     int nLen = lstrlen(lpszOption);
  164.     while (*lpszCmdLine != 0)
  165.     {
  166.         if ((*lpszCmdLine == '-' || *lpszCmdLine == '/') &&
  167.             _tcsncmp(lpszOption, lpszCmdLine+1, nLen) == 0)
  168.         {
  169.             // remove the option from the command line
  170.             int nCmdLen = lstrlen(lpszCmdLine);
  171.             memmove(lpszCmdLine, lpszCmdLine + nLen + 1,
  172.                 (nCmdLen - nLen) * sizeof(TCHAR));
  173.             return TRUE;
  174.         }
  175.         lpszCmdLine++;
  176.     }
  177.     return FALSE;
  178. }
  179.  
  180. BOOL CWinApp::RunEmbedded()
  181. {
  182.     ASSERT(m_lpCmdLine != NULL);
  183.  
  184.     // hard coded non-localized name
  185.     if (_AfxParseOption(m_lpCmdLine, _T("Embedding")))
  186.     {
  187.         AfxOleSetUserCtrl(FALSE);
  188.         return TRUE;
  189.     }
  190.     return FALSE;   // not run with /Embedding
  191. }
  192.  
  193. BOOL CWinApp::RunAutomated()
  194. {
  195.     ASSERT(m_lpCmdLine != NULL);
  196.  
  197.     // hard coded non-localized name
  198.     if (_AfxParseOption(m_lpCmdLine, _T("Automation")))
  199.     {
  200.         AfxOleSetUserCtrl(FALSE);
  201.         return TRUE;
  202.     }
  203.     return FALSE;   // not run with /Automation
  204. }
  205.  
  206. /////////////////////////////////////////////////////////////////////////////
  207.