olelock.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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_CORE4_SEG
  12. #pragma code_seg(AFX_CORE4_SEG)
  13. #endif
  14. #ifdef _DEBUG
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. /////////////////////////////////////////////////////////////////////////////
  19. // Global functions which handle app shutdown
  20. // Note: these functions can be replaced by replacing this module in its
  21. // entirety (although doing so would be rare).
  22. void AFXAPI AfxOleOnReleaseAllObjects()
  23. {
  24. // don't shut down the application if user is in control of the app
  25. if (AfxOleGetUserCtrl())
  26. return;
  27. AfxOleSetUserCtrl(TRUE); // avoid re-entry
  28. // shut down the application
  29. CWinApp* pApp = AfxGetApp();
  30. if (pApp != NULL && pApp->m_pMainWnd != NULL)
  31. {
  32. // destroy the main window (only if enabled)
  33. if (pApp->m_pMainWnd->IsWindowEnabled())
  34. {
  35. // the main window will post WM_QUIT as part of its shutdown
  36. pApp->m_pMainWnd->DestroyWindow();
  37. }
  38. }
  39. else if (!afxContextIsDLL)
  40. {
  41. // no main window, so just post WM_QUIT.
  42. AfxPostQuitMessage(0);
  43. }
  44. }
  45. BOOL AFXAPI AfxOleCanExitApp()
  46. {
  47. AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
  48. return pModuleState->m_nObjectCount == 0;
  49. }
  50. void AFXAPI AfxOleLockApp()
  51. {
  52. AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
  53. InterlockedIncrement(&pModuleState->m_nObjectCount);
  54. }
  55. void AFXAPI AfxOleUnlockApp()
  56. {
  57. AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
  58. ASSERT(pModuleState->m_nObjectCount != 0);
  59. if (InterlockedDecrement(&pModuleState->m_nObjectCount) == 0)
  60. {
  61. // allow application to shut down when all the objects have
  62. // been released
  63. ::AfxOleOnReleaseAllObjects();
  64. }
  65. }
  66. /////////////////////////////////////////////////////////////////////////////
  67. // Access to "user-control" state
  68. void AFXAPI AfxOleSetUserCtrl(BOOL bUserCtrl)
  69. {
  70. AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
  71. #ifdef _DEBUG
  72. CWinApp* pApp = AfxGetApp();
  73. if (bUserCtrl && !pModuleState->m_bUserCtrl &&
  74. (pApp == NULL || pApp->m_pMainWnd == NULL ||
  75. !pApp->m_pMainWnd->IsWindowVisible()))
  76. {
  77. // If the user gets control while the application window is
  78. // not visible, the application may not shutdown when the object
  79. // count reaches zero.
  80. TRACE0("Warning: AfxOleSetUserCtrl(TRUE) called with application window hidden.\n");
  81. }
  82. #endif
  83. pModuleState->m_bUserCtrl = bUserCtrl;
  84. }
  85. BOOL AFXAPI AfxOleGetUserCtrl()
  86. {
  87. AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
  88. return pModuleState->m_bUserCtrl;
  89. }
  90. /////////////////////////////////////////////////////////////////////////////