oleinit.cpp 5.6 KB

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