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