home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / tutsamp / perserve / server.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-05  |  5.7 KB  |  203 lines

  1. /*+==========================================================================
  2.   File:      SERVER.CPP
  3.  
  4.   Summary:   Implementation file for the CServer server control C++
  5.              object.  This object encapsulates the server's internal
  6.              control of global server object and lock counts. The
  7.              CThreaded OwnThis mechanism is used to ensure mutually
  8.              exclusive access to this CServer object by multiple
  9.              contending threads.
  10.  
  11.              For a comprehensive tutorial code tour of this module's
  12.              contents and offerings see the tutorial PERSERVE.HTM
  13.              file. For more specific technical details on the internal
  14.              workings see the comments dispersed throughout the module's
  15.              source code.
  16.  
  17.   Classes:   CServer.
  18.  
  19.   Functions: .
  20.  
  21.   Origin:    2-4-97: atrent - Editor-inheritance from SERVER.CPP in
  22.                the STOSERVE Tutorial Code Sample.
  23.  
  24. ----------------------------------------------------------------------------
  25.   This file is part of the Microsoft COM Tutorial Code Samples.
  26.  
  27.   Copyright (C) Microsoft Corporation, 1997.  All rights reserved.
  28.  
  29.   This source code is intended only as a supplement to Microsoft
  30.   Development Tools and/or on-line documentation.  See these other
  31.   materials for detailed information regarding Microsoft code samples.
  32.  
  33.   THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  34.   KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  35.   IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  36.   PARTICULAR PURPOSE.
  37. ==========================================================================+*/
  38.  
  39. /*---------------------------------------------------------------------------
  40.   We include WINDOWS.H for all Win32 applications.
  41.   We include OLE2.H because we will be calling the COM/OLE Libraries.
  42.   We include APPUTIL.H because we will be building this DLL using
  43.     the convenient Virtual Window and Dialog classes and other
  44.     utility functions in the APPUTIL Library (ie, APPUTIL.LIB).
  45.   We include SERVER.H for the class declarations for the C++ CServer
  46.     server control object.
  47. ---------------------------------------------------------------------------*/
  48. #include <windows.h>
  49. #include <ole2.h>
  50. #include <apputil.h>
  51. #include "server.h"
  52.  
  53.  
  54. /*---------------------------------------------------------------------------
  55.   Implementation the internal CServer C++ object.  Used to encapsulate
  56.   some server data and the methods for Lock and Object count incrementing
  57.   and decrementing.
  58. ---------------------------------------------------------------------------*/
  59.  
  60. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  61.   Method:   CServer::CServer
  62.  
  63.   Summary:  CServer Constructor.
  64.  
  65.   Args:     void
  66.  
  67.   Modifies: m_cObjects, m_cLocks.
  68.  
  69.   Returns:  void
  70. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  71. CServer::CServer(void)
  72. {
  73.   // Zero the Object and Lock counts for this attached process.
  74.   m_cObjects = 0;
  75.   m_cLocks = 0;
  76.  
  77.   return;
  78. }
  79.  
  80.  
  81. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  82.   Method:   CServer::~CServer
  83.  
  84.   Summary:  CServer Destructor.
  85.  
  86.   Args:     void
  87.  
  88.   Returns:  void
  89. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  90. CServer::~CServer(void)
  91. {
  92.   return;
  93. }
  94.  
  95.  
  96. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  97.   Method:   CServer::Lock
  98.  
  99.   Summary:  Increment the Server's Lock count.
  100.  
  101.   Args:     void
  102.  
  103.   Modifies: m_cLocks.
  104.  
  105.   Returns:  void
  106. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  107. void CServer::Lock(void)
  108. {
  109.   InterlockedIncrement((PLONG) &m_cLocks);
  110.  
  111.   return;
  112. }
  113.  
  114.  
  115. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  116.   Method:   CServer::Unlock
  117.  
  118.   Summary:  Decrement the Server's Lock count.
  119.  
  120.   Args:     void
  121.  
  122.   Modifies: m_cLocks.
  123.  
  124.   Returns:  void
  125. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  126. void CServer::Unlock(void)
  127. {
  128.   InterlockedDecrement((PLONG) &m_cLocks);
  129.  
  130.   return;
  131. }
  132.  
  133.  
  134. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  135.   Method:   CServer::ObjectsUp
  136.  
  137.   Summary:  Increment the Server's living Object count.
  138.  
  139.   Args:     void
  140.  
  141.   Modifies: m_cObjects.
  142.  
  143.   Returns:  void
  144. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  145. void CServer::ObjectsUp(void)
  146. {
  147.   InterlockedIncrement((PLONG) &m_cObjects);
  148.  
  149.   return;
  150. }
  151.  
  152.  
  153. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  154.   Method:   CServer::ObjectsDown
  155.  
  156.   Summary:  Decrement the Server's living object count.
  157.  
  158.   Args:     void
  159.  
  160.   Modifies: m_cObjects.
  161.  
  162.   Returns:  void
  163. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  164. void CServer::ObjectsDown(void)
  165. {
  166.   InterlockedDecrement((PLONG) &m_cObjects);
  167.  
  168.   return;
  169. }
  170.  
  171.  
  172. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  173.   Method:   CServer::CanUnloadNow
  174.  
  175.   Summary:  Check if we can unload the server (ie, if there are no longer
  176.             any living COM objects and no locks.
  177.  
  178.   Args:     void
  179.  
  180.   Modifies: .
  181.  
  182.   Returns:  HRESULT
  183.               S_OK if this server can be unloaded.
  184.               S_FALSE if this server can not be unloaded.
  185. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  186. HRESULT CServer::CanUnloadNow(void)
  187. {
  188.   HRESULT hr = S_FALSE;
  189.   LONG cObjects, cLocks;
  190.  
  191.   if (OwnThis())
  192.   {
  193.     cObjects = m_cObjects;
  194.     cLocks = m_cLocks;
  195.  
  196.     hr = (0L==cObjects && 0L==cLocks) ? S_OK : S_FALSE;
  197.  
  198.     UnOwnThis();
  199.   }
  200.  
  201.   return hr;
  202. }
  203.