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 / dcdserve / server.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-30  |  9.5 KB  |  378 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 DCDSERVE.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:    8-23-97: atrent - Editor-inheritance from SERVER.CPP in
  21.                the LOCSERVE Tutorial Code Sample. [Revised]
  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 EXE using
  42.     the convenient Virtual Window and Dialog classes and other
  43.     utility functions in the APPUTIL Library (ie, APPUTIL.LIB).
  44.   We include PAPINT.H and PAPGUIDS.H for the common paper-related
  45.     Interface class, GUID, and CLSID specifications.
  46.   We include SERVER.H for the class declarations for the C++ CServer
  47.     server control object.
  48.   We include FACTORY.H because it has the necessary internal class factory
  49.     declarations for this component server.
  50. ---------------------------------------------------------------------------*/
  51. #include <windows.h>
  52. #include <ole2.h>
  53. #include <apputil.h>
  54. #include <papint.h>
  55. #include <papguids.h>
  56. #include "server.h"
  57. #include "factory.h"
  58.  
  59.  
  60. /*---------------------------------------------------------------------------
  61.   Implementation the internal CServer C++ object.  Used to encapsulate
  62.   some server data and the methods for Lock and Object count incrementing
  63.   and decrementing.
  64. ---------------------------------------------------------------------------*/
  65.  
  66. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  67.   Method:   CServer::CServer
  68.  
  69.   Summary:  CServer Constructor.
  70.  
  71.   Args:     void
  72.  
  73.   Returns:  void
  74. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  75. CServer::CServer(void)
  76. {
  77.   // Zero the Object and Lock counts.
  78.   m_cObjects = 0;
  79.   m_cLocks = 0;
  80.  
  81.   // Zero the cached handles.
  82.   m_hInstServer = NULL;
  83.   m_hWndServer = NULL;
  84.  
  85.   // Zero the Factory references.
  86.   m_pCFPaper = NULL;
  87.   m_dwCFPaper = 0;
  88.  
  89.   // NULL the shared single object pointers.
  90.   m_pCOPaper = NULL;
  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::~CServer
  98.  
  99.   Summary:  CServer Destructor.
  100.  
  101.   Args:     void
  102.  
  103.   Returns:  void
  104. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  105. CServer::~CServer(void)
  106. {
  107.   return;
  108. }
  109.  
  110.  
  111. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  112.   Method:   CServer::ObjectsUp
  113.  
  114.   Summary:  Increment the Server's living Object count.
  115.  
  116.   Args:     void
  117.  
  118.   Modifies: m_cObjects.
  119.  
  120.   Returns:  void
  121. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  122. void CServer::ObjectsUp(void)
  123. {
  124.   if (OwnThis())
  125.   {
  126.     ++m_cObjects;
  127.  
  128.     UnOwnThis();
  129.   }
  130.  
  131.   return;
  132. }
  133.  
  134.  
  135. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  136.   Method:   CServer::ObjectsDown
  137.  
  138.   Summary:  Decrement the Server's living object count.
  139.  
  140.   Args:     void
  141.  
  142.   Modifies: m_cObjects.
  143.  
  144.   Returns:  void
  145. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  146. void CServer::ObjectsDown(void)
  147. {
  148.   if (OwnThis())
  149.   {
  150.     --m_cObjects;
  151.  
  152.     // If no more living objects and no locks then shut the server down.
  153.     if (0L == m_cObjects && 0L == m_cLocks && IsWindow(m_hWndServer))
  154.     {
  155.       // Post a message to this local server's message queue requesting
  156.       // a close of the application.
  157.       PostMessage(m_hWndServer, WM_CLOSE, 0, 0L);
  158.     }
  159.  
  160.     UnOwnThis();
  161.   }
  162.  
  163.   return;
  164. }
  165.  
  166.  
  167. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  168.   Method:   CServer::Lock
  169.  
  170.   Summary:  Increment the Server's Lock count.
  171.  
  172.   Args:     void
  173.  
  174.   Modifies: m_cLocks.
  175.  
  176.   Returns:  void
  177. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  178. void CServer::Lock(void)
  179. {
  180.   LONG cLocks;
  181.  
  182.   if (OwnThis())
  183.   {
  184.     cLocks = ++m_cLocks;
  185.  
  186.     UnOwnThis();
  187.   }
  188.  
  189.   return;
  190. }
  191.  
  192.  
  193. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  194.   Method:   CServer::Unlock
  195.  
  196.   Summary:  Decrement the Server's Lock count.
  197.  
  198.   Args:     void
  199.  
  200.   Modifies: m_cLocks.
  201.  
  202.   Returns:  void
  203. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  204. void CServer::Unlock(void)
  205. {
  206.   if (OwnThis())
  207.   {
  208.     --m_cLocks;
  209.  
  210.     // Use ObjectsDown to force a server shutdown if this unlock warrants it.
  211.     ++m_cObjects;
  212.     ObjectsDown();
  213.  
  214.     UnOwnThis();
  215.   }
  216.  
  217.   return;
  218. }
  219.  
  220.  
  221. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  222.   Method:   CServer::ObjectFirst
  223.  
  224.   Summary:  Returns TRUE if this is the first object created.
  225.  
  226.   Args:     void
  227.  
  228.   Returns:  BOOL
  229.               TRUE => First object created; FALSE => Object already exists.
  230. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  231. BOOL CServer::ObjectFirst(void)
  232. {
  233.   BOOL bFirst = TRUE;
  234.  
  235.   if (OwnThis())
  236.   {
  237.     bFirst = (NULL == m_pCOPaper);
  238.  
  239.     UnOwnThis();
  240.   }
  241.  
  242.   return bFirst;
  243. }
  244.  
  245.  
  246. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  247.   Method:   CServer::ObjectSet
  248.  
  249.   Summary:  Tells server that the shared single object has been created.
  250.  
  251.   Args:     void
  252.  
  253.   Modifies: m_pCOPaper.
  254.  
  255.   Returns:  HRESULT
  256.               Standard result code. NOERROR for success.
  257. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  258. HRESULT CServer::ObjectSet(IUnknown* pCob)
  259. {
  260.   HRESULT hr = NOERROR;
  261.  
  262.   if (OwnThis())
  263.   {
  264.     m_pCOPaper = pCob;
  265.  
  266.     UnOwnThis();
  267.   }
  268.  
  269.   return hr;
  270. }
  271.  
  272.  
  273. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  274.   Method:   CServer::ObjectQI
  275.  
  276.   Summary:  QueryInterface on the singleton object.
  277.  
  278.   Args:     void
  279.  
  280.   Returns:  HRESULT
  281.               Standard result code. NOERROR for success.
  282. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  283. HRESULT CServer::ObjectQI(REFIID riid, PPVOID ppv)
  284. {
  285.   HRESULT hr = E_FAIL;
  286.  
  287.   if (OwnThis())
  288.   {
  289.     hr = m_pCOPaper->QueryInterface(riid, ppv);
  290.  
  291.     UnOwnThis();
  292.   }
  293.  
  294.   return hr;
  295. }
  296.  
  297.  
  298. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  299.   Method:   CServer::OpenFactories
  300.  
  301.   Summary:  Create and register all of this server's class factories.
  302.  
  303.   Args:     void
  304.  
  305.   Modifies: m_pCFPaper, m_dwCFPaper.
  306.  
  307.   Returns:  BOOL
  308.               TRUE if success; FALSE if not
  309. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  310. BOOL CServer::OpenFactories(void)
  311. {
  312.   BOOL bOk = FALSE;
  313.   HRESULT hr;
  314.  
  315.   // Build and register the SharePaper factory.
  316.   m_pCFPaper = new CFPaper(NULL, this);
  317.   if (NULL != m_pCFPaper)
  318.   {
  319.     // AddRef this cached pointer to the Class Factory.
  320.     m_pCFPaper->AddRef();
  321.  
  322.     // Now register this class factory with COM.
  323.     hr = CoRegisterClassObject(
  324.            CLSID_SharePaper,
  325.            m_pCFPaper,
  326.            CLSCTX_LOCAL_SERVER,
  327.            REGCLS_MULTIPLEUSE,
  328.            &m_dwCFPaper);
  329.  
  330.     bOk = SUCCEEDED(hr);
  331.     if (!bOk)
  332.     {
  333.       // If can't register factory then clean up for server exit.
  334.       m_pCFPaper->Release();
  335.       DELETE_POINTER(m_pCFPaper);
  336.     }
  337.   }
  338.   else
  339.     bOk = FALSE;
  340.  
  341.   return bOk;
  342. }
  343.  
  344.  
  345. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  346.   Method:   CServer::CloseFactories
  347.  
  348.   Summary:  Revoke (ie, unregister) and delete all the server's class
  349.             factories.
  350.  
  351.   Args:     void
  352.  
  353.   Modifies: m_dwCFPaper.
  354.  
  355.   Returns:  BOOL
  356.               TRUE if success; FALSE if not
  357. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  358. BOOL CServer::CloseFactories(void)
  359. {
  360.   BOOL bOk = TRUE;
  361.   HRESULT hr;
  362.  
  363.   // Unregister the SharePaper class factory with COM.
  364.   if (0 != m_dwCFPaper)
  365.   {
  366.     hr = CoRevokeClassObject(m_dwCFPaper);
  367.     if (FAILED(hr))
  368.       bOk = FALSE;
  369.   }
  370.  
  371.   // Release any and all of the Class Factory interface pointers.
  372.   RELEASE_INTERFACE(m_pCFPaper);
  373.  
  374.   return bOk;
  375. }
  376.  
  377. // =============================== END ======================================
  378.