home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / olelock.cpp < prev    next >
C/C++ Source or Header  |  1998-06-16  |  3KB  |  106 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_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.     if (pApp != NULL && pApp->m_pMainWnd != NULL)
  39.     {
  40.         // destroy the main window (only if enabled)
  41.         if (pApp->m_pMainWnd->IsWindowEnabled())
  42.         {
  43.             // the main window will post WM_QUIT as part of its shutdown
  44.             pApp->m_pMainWnd->DestroyWindow();
  45.         }
  46.     }
  47.     else if (!afxContextIsDLL)
  48.     {
  49.         // no main window, so just post WM_QUIT.
  50.         AfxPostQuitMessage(0);
  51.     }
  52. }
  53.  
  54. BOOL AFXAPI AfxOleCanExitApp()
  55. {
  56.     AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
  57.     return pModuleState->m_nObjectCount == 0;
  58. }
  59.  
  60. void AFXAPI AfxOleLockApp()
  61. {
  62.     AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
  63.     InterlockedIncrement(&pModuleState->m_nObjectCount);
  64. }
  65.  
  66. void AFXAPI AfxOleUnlockApp()
  67. {
  68.     AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
  69.     ASSERT(pModuleState->m_nObjectCount != 0);
  70.     if (InterlockedDecrement(&pModuleState->m_nObjectCount) == 0)
  71.     {
  72.         // allow application to shut down when all the objects have
  73.         //  been released
  74.         ::AfxOleOnReleaseAllObjects();
  75.     }
  76. }
  77.  
  78. /////////////////////////////////////////////////////////////////////////////
  79. // Access to "user-control" state
  80.  
  81. void AFXAPI AfxOleSetUserCtrl(BOOL bUserCtrl)
  82. {
  83.     AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
  84. #ifdef _DEBUG
  85.     CWinApp* pApp = AfxGetApp();
  86.     if (bUserCtrl && !pModuleState->m_bUserCtrl &&
  87.         (pApp == NULL || pApp->m_pMainWnd == NULL ||
  88.         !pApp->m_pMainWnd->IsWindowVisible()))
  89.     {
  90.         // If the user gets control while the application window is
  91.         //  not visible, the application may not shutdown when the object
  92.         //  count reaches zero.
  93.         TRACE0("Warning: AfxOleSetUserCtrl(TRUE) called with application window hidden.\n");
  94.     }
  95. #endif
  96.     pModuleState->m_bUserCtrl = bUserCtrl;
  97. }
  98.  
  99. BOOL AFXAPI AfxOleGetUserCtrl()
  100. {
  101.     AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
  102.     return pModuleState->m_bUserCtrl;
  103. }
  104.  
  105. /////////////////////////////////////////////////////////////////////////////
  106.