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 / conserve / server.cpp < prev    next >
C/C++ Source or Header  |  1997-08-05  |  6KB  |  232 lines

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