home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC / src / oleinit.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  5.7 KB  |  213 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 = ::WCE_FCTN(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 !defined(_WIN32_WCE)
  85.     if (pThread->m_pMessageFilter == NULL)
  86.     {
  87.         pThread->m_pMessageFilter = new COleMessageFilter;
  88.         ASSERT(AfxOleGetMessageFilter() != NULL);
  89.         AfxOleGetMessageFilter()->Register();
  90.     }
  91. #endif // _WIN32_WCE
  92.     return TRUE;
  93.  
  94. InitFailed:
  95.     AfxOleTerm();
  96.     return FALSE;
  97. }
  98.  
  99. void AFXAPI AfxOleTerm(BOOL bJustRevoke)
  100. {
  101.     // release clipboard ownership
  102. #if !defined(_WIN32_WCE)
  103.     COleDataSource::FlushClipboard();
  104.  
  105.     // revoke all class factories
  106.     COleObjectFactory::RevokeAll();
  107. #endif // _WIN32_WCE
  108.  
  109. #ifndef _AFX_NO_OCC_SUPPORT
  110.     AfxOleUnlockAllControls();
  111. #endif
  112.  
  113.     if (!bJustRevoke)
  114.     {
  115. #if !defined(_WIN32_WCE)
  116.         CWinThread* pThread = AfxGetThread();
  117.         if (pThread != NULL)
  118.         {
  119.             // destroy message filter (may be derived class)
  120.             delete pThread->m_pMessageFilter;
  121.             pThread->m_pMessageFilter = NULL;
  122.         }
  123. #endif // _WIN32_WCE
  124.  
  125.         // terminate OLE last
  126.         _AFX_THREAD_STATE* pState = AfxGetThreadState();
  127.         // -1 is special case, so need to compare against TRUE
  128.         if (pState->m_bNeedTerm == TRUE)
  129.         {
  130.             CoFreeUnusedLibraries();
  131.             ::WCE_FCTN(OleUninitialize)();
  132.             pState->m_bNeedTerm = FALSE;
  133.         }
  134.     }
  135. }
  136.  
  137. AFX_STATIC_DATA DWORD _afxTickCount = (DWORD)-1;
  138. AFX_STATIC_DATA BOOL _afxTickInit = FALSE;
  139.  
  140. void AFXAPI AfxOleTermOrFreeLib(BOOL bTerm, BOOL bJustRevoke)
  141. {
  142.     if (bTerm)
  143.     {
  144.         AfxOleTerm(bJustRevoke);
  145.     }
  146.     else
  147.     {
  148.         // initialize _afxTickCount if necessary
  149.         if (!_afxTickInit)
  150.         {
  151.             _afxTickCount = ::GetTickCount();
  152.             ++_afxTickInit;
  153.         }
  154.  
  155.         // only call CoFreeUnusedLibraries if one minute has gone by
  156.         if (GetTickCount() - _afxTickCount > 60000)
  157.         {
  158.             CoFreeUnusedLibraries();
  159.             _afxTickCount = ::GetTickCount();
  160.         }
  161.     }
  162. }
  163.  
  164. /////////////////////////////////////////////////////////////////////////////
  165. // CWinApp support for parsing OLE command line
  166.  
  167. AFX_STATIC BOOL AFXAPI _AfxParseOption(LPTSTR lpszCmdLine, LPCTSTR lpszOption)
  168. {
  169.     int nLen = lstrlen(lpszOption);
  170.     while (*lpszCmdLine != 0)
  171.     {
  172.         if ((*lpszCmdLine == '-' || *lpszCmdLine == '/') &&
  173.             _tcsncmp(lpszOption, lpszCmdLine+1, nLen) == 0)
  174.         {
  175.             // remove the option from the command line
  176.             int nCmdLen = lstrlen(lpszCmdLine);
  177.             memmove(lpszCmdLine, lpszCmdLine + nLen + 1,
  178.                 (nCmdLen - nLen) * sizeof(TCHAR));
  179.             return TRUE;
  180.         }
  181.         lpszCmdLine++;
  182.     }
  183.     return FALSE;
  184. }
  185.  
  186. BOOL CWinApp::RunEmbedded()
  187. {
  188.     ASSERT(m_lpCmdLine != NULL);
  189.  
  190.     // hard coded non-localized name
  191.     if (_AfxParseOption(m_lpCmdLine, _T("Embedding")))
  192.     {
  193.         AfxOleSetUserCtrl(FALSE);
  194.         return TRUE;
  195.     }
  196.     return FALSE;   // not run with /Embedding
  197. }
  198.  
  199. BOOL CWinApp::RunAutomated()
  200. {
  201.     ASSERT(m_lpCmdLine != NULL);
  202.  
  203.     // hard coded non-localized name
  204.     if (_AfxParseOption(m_lpCmdLine, _T("Automation")))
  205.     {
  206.         AfxOleSetUserCtrl(FALSE);
  207.         return TRUE;
  208.     }
  209.     return FALSE;   // not run with /Automation
  210. }
  211.  
  212. /////////////////////////////////////////////////////////////////////////////
  213.