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 / perdraw / server.cpp < prev    next >
C/C++ Source or Header  |  1997-08-05  |  6KB  |  218 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 threads.
  9.  
  10.              For a comprehensive tutorial code tour of this module's
  11.              contents and offerings see the tutorial PERDRAW.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-20-97: atrent - Editor-inheritance from SERVER.CPP in
  21.                the STOSERVE 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 class declarations for the C++ CServer
  45.     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.   Returns:  void
  67. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  68. CServer::CServer(void)
  69. {
  70.   // Initially zero the Object and Lock counts for this attached process.
  71.   m_cObjects = 0;
  72.   m_cLocks = 0;
  73.  
  74.   return;
  75. }
  76.  
  77.  
  78. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  79.   Method:   CServer::~CServer
  80.  
  81.   Summary:  CServer Destructor.
  82.  
  83.   Args:     void
  84.  
  85.   Returns:  void
  86. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  87. CServer::~CServer(void)
  88. {
  89.   return;
  90. }
  91.  
  92.  
  93. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  94.   Method:   CServer::Lock
  95.  
  96.   Summary:  Increment the Server's Lock count.
  97.  
  98.   Args:     void
  99.  
  100.   Returns:  void
  101. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  102. void CServer::Lock(void)
  103. {
  104.   LONG cLocks;
  105.  
  106.   if (OwnThis())
  107.   {
  108.     cLocks = ++m_cLocks;
  109.  
  110.     UnOwnThis();
  111.   }
  112.  
  113.   return;
  114. }
  115.  
  116.  
  117. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  118.   Method:   CServer::Unlock
  119.  
  120.   Summary:  Decrement the Server's Lock count.
  121.  
  122.   Args:     void
  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.   LONG cLocks;
  129.  
  130.   if (OwnThis())
  131.   {
  132.     cLocks = --m_cLocks;
  133.  
  134.     UnOwnThis();
  135.   }
  136.  
  137.   return;
  138. }
  139.  
  140.  
  141. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  142.   Method:   CServer::ObjectsUp
  143.  
  144.   Summary:  Increment the Server's living Object count.
  145.  
  146.   Args:     void
  147.  
  148.   Returns:  void
  149. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  150. void CServer::ObjectsUp(void)
  151. {
  152.   LONG cObjects;
  153.  
  154.   if (OwnThis())
  155.   {
  156.     cObjects = ++m_cObjects;
  157.  
  158.     UnOwnThis();
  159.   }
  160.  
  161.   return;
  162. }
  163.  
  164.  
  165. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  166.   Method:   CServer::ObjectsDown
  167.  
  168.   Summary:  Decrement the Server's living object count.
  169.  
  170.   Args:     void
  171.  
  172.   Returns:  void
  173. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  174. void CServer::ObjectsDown(void)
  175. {
  176.   LONG cObjects;
  177.  
  178.   if (OwnThis())
  179.   {
  180.     cObjects = --m_cObjects;
  181.  
  182.     UnOwnThis();
  183.   }
  184.  
  185.   return;
  186. }
  187.  
  188.  
  189. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  190.   Method:   CServer::CanUnloadNow
  191.  
  192.   Summary:  Check if the server can be can unloaded (ie, if there are no
  193.             longer any living COM objects and no locks).
  194.  
  195.   Args:     void
  196.  
  197.   Returns:  HRESULT
  198.               S_OK if this server can be unloaded.
  199.               S_FALSE if this server can not be unloaded.
  200. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  201. HRESULT CServer::CanUnloadNow(void)
  202. {
  203.   HRESULT hr = S_FALSE;
  204.   LONG cObjects, cLocks;
  205.  
  206.   if (OwnThis())
  207.   {
  208.     cObjects = m_cObjects;
  209.     cLocks = m_cLocks;
  210.  
  211.     hr = (0L==cObjects && 0L==cLocks) ? S_OK : S_FALSE;
  212.  
  213.     UnOwnThis();
  214.   }
  215.  
  216.   return hr;
  217. }
  218.