home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / OLELOCK.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-30  |  3.0 KB  |  108 lines

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