home *** CD-ROM | disk | FTP | other *** search
/ PC Press 1997 July / Sezamfile97_2.iso / windows / program / activex / axtsamp.exe / TSBRANCH.EXE / APTSERVE / SERVER.CPP < prev    next >
C/C++ Source or Header  |  1996-12-29  |  18KB  |  608 lines

  1. /*+==========================================================================
  2.   File:      SERVER.CPP
  3.  
  4.   Summary:   Implementation file for the CServer server control utility
  5.              C++ object.  This object encapsulates the server's internal
  6.              control of global server object and lock counts to govern
  7.              server lifetime.
  8.  
  9.              For a comprehensive tutorial code tour of this module's
  10.              contents and offerings see the tutorial APTSERVE.HTM file.
  11.              For more specific technical details on the internal workings
  12.              see the comments dispersed throughout the module's source code.
  13.  
  14.   Classes:   CServer.
  15.  
  16.   Functions: .
  17.  
  18.   Origin:    3-20-96: atrent - Editor-inheritance from SERVER.CPP in
  19.                the LOCSERVE Tutorial Code Sample.
  20.  
  21. ----------------------------------------------------------------------------
  22.   This file is part of the Microsoft ActiveX Tutorial Code Samples.
  23.  
  24.   Copyright (C) Microsoft Corporation, 1997.  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 make calls to the COM/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 MICARS.H and CARGUIDS.H for the common car-related Interface
  43.     class, GUID, and CLSID specifications.
  44.   We include SERVER.H for the object class declarations for the
  45.     C++ CServer server control object.
  46.   We include FACTORY.H because it has the necessary internal class factory
  47.     declarations for this component server.
  48. ---------------------------------------------------------------------------*/
  49. #include <windows.h>
  50. #include <ole2.h>
  51. #include <apputil.h>
  52. #include <micars.h>
  53. #include <carguids.h>
  54. #include "server.h"
  55. #include "factory.h"
  56.  
  57.  
  58. /*---------------------------------------------------------------------------
  59.   Implementation the internal CServer C++ object.  Used to encapsulate
  60.   global server data and the methods for Lock and Object count incrementing
  61.   and decrementing.
  62. ---------------------------------------------------------------------------*/
  63.  
  64. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  65.   Method:   CServer::CServer
  66.  
  67.   Summary:  CServer Constructor.
  68.  
  69.   Args:     void
  70.  
  71.   Modifies: lots-o-stuff.
  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 for this server.
  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 and Apartment thread references.
  86.   m_pCFCar = NULL;
  87.   m_pCFUtilityCar = NULL;
  88.   m_pCFCruiseCar = NULL;
  89.   m_paiAptCar = NULL;
  90.   m_paiAptUtilityCar = NULL;
  91.   m_paiAptCruiseCar = NULL;
  92.   m_dwAptCar = 0;
  93.   m_dwAptUtilityCar = 0;
  94.   m_dwAptCruiseCar = 0;
  95.  
  96.   // NULL the apartment handle array
  97.   for (UINT i = 0; i<NUM_APARTMENTS; i++)
  98.     m_hApts[i] = NULL;
  99.  
  100.   return;
  101. }
  102.  
  103.  
  104. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  105.   Method:   CServer::~CServer
  106.  
  107.   Summary:  CServer Destructor.
  108.  
  109.   Args:     void
  110.  
  111.   Modifies: .
  112.  
  113.   Returns:  void
  114. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  115. CServer::~CServer(void)
  116. {
  117.  
  118.   return;
  119. }
  120.  
  121.  
  122. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  123.   Method:   CServer::OwnThis
  124.  
  125.   Summary:  Wait for multithread exclusive ownership of this CServer object.
  126.             This definition overrides the virtual function defined in the
  127.             CThreaded base class to permit convenient trace logging here.
  128.  
  129.   Args:     void
  130.  
  131.   Modifies: m_bOwned.
  132.  
  133.   Returns:  BOOL
  134.               TRUE if success; FALSE if not.
  135. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  136. BOOL CServer::OwnThis(void)
  137. {
  138.   BOOL bOwned = FALSE;
  139.  
  140.   LOGF1("L: CServer::OwnThis. Thread <%X> waiting to own CServer.",TID);
  141.  
  142.   if (WAIT_OBJECT_0 == WaitForSingleObject(m_hOwnerMutex, INFINITE))
  143.   {
  144.     m_bOwned = bOwned = TRUE;
  145.     LOGF1("L: CServer::OwnThis. CServer now owned by Thread <%X>.",TID);
  146.   }
  147.  
  148.   return bOwned;
  149. }
  150.  
  151.  
  152. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  153.   Method:   CServer::UnOwnThis
  154.  
  155.   Summary:  Relinquish ownership of this CServer object.
  156.             This definition overrides the virtual function defined in the
  157.             CThreaded base class to permit convenient trace logging here.
  158.  
  159.   Args:     void
  160.  
  161.   Modifies: m_bOwned.
  162.  
  163.   Returns:  void
  164. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  165. void CServer::UnOwnThis(void)
  166. {
  167.   if (m_bOwned)
  168.   {
  169.     LOGF1("L: CServer::UnOwnThis. Ownership relinquished by <%X>.",TID);
  170.     m_bOwned = FALSE;
  171.     ReleaseMutex(m_hOwnerMutex);
  172.   }
  173.  
  174.   return;
  175. }
  176.  
  177.  
  178. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  179.   Method:   CServer::ObjectsUp
  180.  
  181.   Summary:  Increment the Server's living Object count.
  182.  
  183.   Args:     void
  184.  
  185.   Modifies: m_cObjects.
  186.  
  187.   Returns:  void
  188. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  189. void CServer::ObjectsUp(void)
  190. {
  191.   if (OwnThis())
  192.   {
  193.     m_cObjects += 1;
  194.     LOGF2("L<%X>: CServer::ObjectsUp. New cObjects=%i.",TID,m_cObjects);
  195.  
  196.     UnOwnThis();
  197.   }
  198.  
  199.   return;
  200. }
  201.  
  202.  
  203. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  204.   Method:   CServer::ObjectsDown
  205.  
  206.   Summary:  Decrement the Server's living object count. Trigger an unload
  207.             of this entire server if no more living components.
  208.  
  209.   Args:     void
  210.  
  211.   Modifies: m_cObjects.
  212.  
  213.   Returns:  void
  214. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  215. void CServer::ObjectsDown(void)
  216. {
  217.   if (OwnThis())
  218.   {
  219.     if (m_cObjects > 0)
  220.       m_cObjects -= 1;
  221.  
  222.     LOGF2("L<%X>: CServer::ObjectsDown. New cObjects=%i.",TID,m_cObjects);
  223.  
  224.     // If no more living objects and no locks then shut down the server.
  225.     if (0L == m_cObjects && 0L == m_cLocks && IsWindow(m_hWndServer))
  226.     {
  227.       LOGF1("L<%X>: CServer::ObjectsDown. Closing down APTSERVE server.",TID);
  228.  
  229.       // Post a message to this local server's message queue requesting
  230.       // a close of the application.
  231.       PostMessage(m_hWndServer, WM_CLOSE, 0, 0L);
  232.     }
  233.  
  234.     UnOwnThis();
  235.   }
  236.  
  237.   return;
  238. }
  239.  
  240.  
  241. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  242.   Method:   CServer::Lock
  243.  
  244.   Summary:  Increment the Server's Lock count.
  245.  
  246.   Args:     void
  247.  
  248.   Modifies: m_cLocks.
  249.  
  250.   Returns:  void
  251. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  252. void CServer::Lock(void)
  253. {
  254.   if (OwnThis())
  255.   {
  256.     m_cLocks += 1;
  257.  
  258.     LOGF2("L<%X>: CServer::Lock. New cLocks=%i.",TID,m_cLocks);
  259.  
  260.     UnOwnThis();
  261.   }
  262.  
  263.   return;
  264. }
  265.  
  266.  
  267. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  268.   Method:   CServer::Unlock
  269.  
  270.   Summary:  Decrement the Server's Lock count.
  271.  
  272.   Args:     void
  273.  
  274.   Modifies: m_cLocks.
  275.  
  276.   Returns:  void
  277. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  278. void CServer::Unlock(void)
  279. {
  280.   if (OwnThis())
  281.   {
  282.     m_cLocks -= 1;
  283.     if (m_cLocks < 0)
  284.       m_cLocks = 0;
  285.  
  286.     LOGF2("L<%X>: CServer::Unlock. New cLocks=%i.",TID,m_cLocks);
  287.  
  288.     // If no more living objects and no locks then shut down the server.
  289.     if (0L == m_cObjects && 0L == m_cLocks && IsWindow(m_hWndServer))
  290.     {
  291.       LOGF1("L<%X>: CServer::Unlock. Closing down APTSERVE server.",TID);
  292.       // Post a message to this local server's message queue requesting
  293.       // a close of the application. This will force a termination of
  294.       // all apartment threads.
  295.       PostMessage(m_hWndServer, WM_CLOSE, 0, 0L);
  296.     }
  297.  
  298.     UnOwnThis();
  299.   }
  300.  
  301.   return;
  302. }
  303.  
  304.  
  305. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  306.   Function: AptThreadProc
  307.  
  308.   Summary:  The common apartment model thread procedure for this server.
  309.  
  310.   Args:     LPARAM lparam
  311.               Standard Window Proc parameter.
  312.  
  313.   Modifies: .
  314.  
  315.   Returns:  DWORD
  316.               Thread procedure return (usually msg.wParam).
  317. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  318. DWORD WINAPI AptThreadProc(
  319.                LPARAM lparam)
  320. {
  321.   HRESULT hr;
  322.   MSG msg;
  323.   DWORD dwCFRegId;
  324.   APT_INIT_DATA* paid = (APT_INIT_DATA*) lparam;
  325.  
  326.   LOGF1("L: AptThreadProc. Starting Apartment Thread <%X>.",TID);
  327.  
  328.   // Initialize COM for this apartment thread. Default of apartment
  329.   // model is assumed.
  330.   hr = CoInitialize(NULL);
  331.  
  332.   // Now register the class factory with COM.
  333.   LOGF1("L: AptThreadProc. Registering class factory of apartment <%X>.",TID);
  334.   hr = CoRegisterClassObject(
  335.          paid->rclsid,
  336.          paid->pcf,
  337.          CLSCTX_LOCAL_SERVER,
  338.          REGCLS_MULTIPLEUSE,
  339.          &dwCFRegId);
  340.   LOGERROR("L:CoRegisterClassObject",hr);
  341.   if (SUCCEEDED(hr))
  342.   {
  343.     // Provide a message pump for this thread.
  344.     while (GetMessage(&msg, 0, 0, 0))
  345.       DispatchMessage(&msg);
  346.  
  347.     LOGF1("L: AptThreadProc. Revoking class factory of apartment <%X>.",TID);
  348.     // Unregister the class factory with COM when the thread dies.
  349.     CoRevokeClassObject(dwCFRegId);
  350.   }
  351.   else
  352.   {
  353.     LOGF2("L<%X>: AptThreadProc. RegisterClass failed. hr=0x%X.",TID,hr);
  354.   }
  355.  
  356.   // Uninitialize COM in the context of this apartment thread.
  357.   CoUninitialize();
  358.  
  359.   LOGF1("L: AptThreadProc. Apartment Thread <%X> Terminated.",TID);
  360.  
  361.   return msg.wParam;
  362. }
  363.  
  364.  
  365. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  366.   Method:   CServer::OpenFactories
  367.  
  368.   Summary:  Create and register all of this server's class factories.
  369.  
  370.   Args:     void
  371.  
  372.   Modifies: See below.
  373.  
  374.   Returns:  BOOL
  375.               TRUE if success; FALSE if not
  376. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  377. BOOL CServer::OpenFactories(void)
  378. {
  379.   BOOL bOk = FALSE;
  380.   HRESULT hr;
  381.  
  382.   LOGF1("L<%X>: CServer::OpenFactories. Begin.",TID);
  383.  
  384.   if (OwnThis())
  385.   {
  386.     // Create the ClassFactory C++ objects.
  387.     m_pCFCar = new CFCar(NULL, this);
  388.     m_pCFUtilityCar = new CFUtilityCar(NULL, this);
  389.     m_pCFCruiseCar = new CFCruiseCar(NULL, this);
  390.  
  391.     // Create Structures for Apartment initialization.
  392.     m_paiAptCar = new APT_INIT_DATA(CLSID_AptCar);
  393.     m_paiAptUtilityCar = new APT_INIT_DATA(CLSID_AptUtilityCar);
  394.     m_paiAptCruiseCar = new APT_INIT_DATA(CLSID_AptCruiseCar);
  395.  
  396.     // Create the Appartment for AptCar.
  397.     LOGF1("L<%X>: CServer::OpenFactories. AptCar.",TID);
  398.     if (NULL != m_pCFCar && NULL != m_paiAptCar)
  399.     {
  400.       // AddRef this cached pointer to the Class Factory.
  401.       m_pCFCar->AddRef();
  402.  
  403.       // Assign the ClassFactory in the apartment init data and AddRef.
  404.       m_paiAptCar->pcf = m_pCFCar;
  405.       m_paiAptCar->pcf->AddRef();
  406.  
  407.       // Create the Apartment Thread.
  408.       m_hApts[APTCAR] = CreateThread(
  409.                           0,
  410.                           0,
  411.                           (LPTHREAD_START_ROUTINE) AptThreadProc,
  412.                           (LPVOID) m_paiAptCar,
  413.                           0,
  414.                           &m_dwAptCar);
  415.  
  416.       bOk = (NULL != m_hApts[APTCAR]);
  417.       if (!bOk)
  418.       {
  419.         hr = GetLastError();
  420.         LOGF2("L<%X>: CServer::OpenFactories. AptCar failed. hr=0x%X.",TID,hr);
  421.         // If can't register factory then clean up for server exit.
  422.         m_pCFCar->Release();
  423.         m_paiAptCar->pcf->Release();
  424.         DELETE_POINTER(m_pCFCar);
  425.         DELETE_POINTER(m_paiAptCar);
  426.       }
  427.     }
  428.     else
  429.       bOk = FALSE;
  430.  
  431.     // Create the Appartment for AptUtiliytCar.
  432.     LOGF1("L<%X>: CServer::OpenFactories. AptUtilityCar.",TID);
  433.     if (bOk)
  434.     {
  435.       if (NULL != m_pCFUtilityCar && NULL != m_paiAptUtilityCar)
  436.       {
  437.         // AddRef this cached pointer to the Class Factory.
  438.         m_pCFUtilityCar->AddRef();
  439.  
  440.         // Assign the ClassFactory in the apartment init data and AddRef again.
  441.         m_paiAptUtilityCar->pcf = m_pCFUtilityCar;
  442.         m_paiAptUtilityCar->pcf->AddRef();
  443.  
  444.         // Create the Apartment Thread.
  445.         m_hApts[APTUTILITYCAR] = CreateThread(
  446.                                    0,
  447.                                    0,
  448.                                    (LPTHREAD_START_ROUTINE) AptThreadProc,
  449.                                    (LPVOID) m_paiAptUtilityCar,
  450.                                    0,
  451.                                    &m_dwAptUtilityCar);
  452.  
  453.         bOk = (NULL != m_hApts[APTUTILITYCAR]);
  454.         if (!bOk)
  455.         {
  456.           hr = GetLastError();
  457.           LOGF2("L<%X>: CServer::OpenFactories. AptUtilityCar failed. hr=0x%X.",TID,hr);
  458.           // If can't register factory then clean up for server exit.
  459.           m_pCFUtilityCar->Release();
  460.           m_paiAptUtilityCar->pcf->Release();
  461.           DELETE_POINTER(m_pCFUtilityCar);
  462.           DELETE_POINTER(m_paiAptUtilityCar);
  463.         }
  464.       }
  465.       else
  466.         bOk = FALSE;
  467.     }
  468.  
  469.     // Create the Appartment for AptCruiseCar.
  470.     LOGF1("L<%X>: CServer::OpenFactories. AptCruiseCar.",TID);
  471.     if (bOk)
  472.     {
  473.       if (NULL != m_pCFCruiseCar && NULL != m_paiAptCruiseCar)
  474.       {
  475.         // AddRef this cached pointer to the Class Factory.
  476.         m_pCFCruiseCar->AddRef();
  477.  
  478.         // Assign the ClassFactory in the apartment init data and AddRef again.
  479.         m_paiAptCruiseCar->pcf = m_pCFCruiseCar;
  480.         m_paiAptCruiseCar->pcf->AddRef();
  481.  
  482.         // Create the Apartment Thread.
  483.         m_hApts[APTCRUISECAR] = CreateThread(
  484.                                   0,
  485.                                   0,
  486.                                   (LPTHREAD_START_ROUTINE) AptThreadProc,
  487.                                   (LPVOID) m_paiAptCruiseCar,
  488.                                   0,
  489.                                   &m_dwAptCruiseCar);
  490.  
  491.         bOk = (NULL != m_hApts[APTCRUISECAR]);
  492.         if (!bOk)
  493.         {
  494.           hr = GetLastError();
  495.           LOGF2("L<%X>: CServer::OpenFactories. AptCruiseCar failed. hr=0x%X.",TID,hr);
  496.           // If can't register factory then clean up for server exit.
  497.           m_pCFCruiseCar->Release();
  498.           m_paiAptCruiseCar->pcf->Release();
  499.           DELETE_POINTER(m_pCFCruiseCar);
  500.           DELETE_POINTER(m_paiAptCruiseCar);
  501.         }
  502.       }
  503.       else
  504.         bOk = FALSE;
  505.     }
  506.  
  507.     UnOwnThis();
  508.   }
  509.  
  510.   LOGF1("L<%X>: CServer::OpenFactories. End.",TID);
  511.  
  512.   return bOk;
  513. }
  514.  
  515.  
  516. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  517.   Method:   CServer::CloseFactories
  518.  
  519.   Summary:  Shutdown the class factory apartments. Revoke (ie, unregister)
  520.             and delete all the server's class factories too.
  521.  
  522.   Args:     void
  523.  
  524.   Modifies: .
  525.  
  526.   Returns:  BOOL
  527.               TRUE if success; FALSE if not
  528. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  529. BOOL CServer::CloseFactories(void)
  530. {
  531.   BOOL bOk = TRUE;
  532.   HRESULT hr;
  533.  
  534.   LOGF1("L<%X>: CServer::CloseFactories. Begin.",TID);
  535.  
  536.   if (OwnThis())
  537.   {
  538.     // Shutdown the AptCar Apartment Thread.
  539.     if (0 != m_dwAptCar)
  540.     {
  541.       LOGF1("L<%X>: CServer::CloseFactories. Terminate AptCar Apartment.",TID);
  542.       bOk = PostThreadMessage(m_dwAptCar, WM_QUIT, 0, 0);
  543.       if (!bOk)
  544.       {
  545.         hr = GetLastError();
  546.         LOGF2("L<%X>: CServer::CloseFactories. AptCar failed. hr=0x%X.",TID,hr);
  547.       }
  548.     }
  549.  
  550.     // Shutdown the AptUtilityCar Apartment Thread.
  551.     if (0 != m_dwAptUtilityCar)
  552.     {
  553.       LOGF1("L<%X>: CServer::CloseFactories. Terminate AptUtilityCar Apartment.",TID);
  554.       bOk = PostThreadMessage(m_dwAptUtilityCar, WM_QUIT, 0, 0);
  555.       if (!bOk)
  556.       {
  557.         hr = GetLastError();
  558.         LOGF2("L<%X>: CServer::CloseFactories. AptUtilityCar failed. hr=0x%X.",TID,hr);
  559.       }
  560.     }
  561.  
  562.     // Shutdown the AptCruiseCar Apartment Thread.
  563.     if (0 != m_dwAptCruiseCar)
  564.     {
  565.       LOGF1("L<%X>: CServer::CloseFactories. Terminate AptCruiseCar Apartment.",TID);
  566.       bOk = PostThreadMessage(m_dwAptCruiseCar, WM_QUIT, 0, 0);
  567.       if (!bOk)
  568.       {
  569.         hr = GetLastError();
  570.         LOGF2("L<%X>: CServer::CloseFactories. AptCruiseCar failed. hr=0x%X.",TID,hr);
  571.       }
  572.     }
  573.  
  574.     if (m_pCFCar && m_pCFUtilityCar && m_pCFCruiseCar)
  575.     {
  576.       // Release any and all of the Class Factory interface pointers.
  577.       LOGF1("L<%X>: CServer::CloseFactories. Releasing all Classfactory interfaces.",TID);
  578.       RELEASE_INTERFACE(m_pCFCar);
  579.       RELEASE_INTERFACE(m_paiAptCar->pcf);
  580.       RELEASE_INTERFACE(m_pCFUtilityCar);
  581.       RELEASE_INTERFACE(m_paiAptUtilityCar->pcf);
  582.       RELEASE_INTERFACE(m_pCFCruiseCar);
  583.       RELEASE_INTERFACE(m_paiAptCruiseCar->pcf);
  584.       DELETE_POINTER(m_paiAptCar);
  585.       DELETE_POINTER(m_paiAptUtilityCar);
  586.       DELETE_POINTER(m_paiAptCruiseCar);
  587.  
  588.       // Give CServer back before waiting on threads to die.
  589.       UnOwnThis();
  590.  
  591.       // Wait for the threads to terminate before closing their
  592.       // thread handles.
  593.       WaitForMultipleObjects(NUM_APARTMENTS, m_hApts, TRUE, INFINITE);
  594.       for (UINT i = 0; i<NUM_APARTMENTS; i++)
  595.         if (NULL != m_hApts[i])
  596.           CloseHandle(m_hApts[i]);
  597.     }
  598.     else
  599.       UnOwnThis();
  600.   }
  601.  
  602.   LOGF1("L<%X>: CServer::CloseFactories. End.",TID);
  603.  
  604.   return bOk;
  605. }
  606.  
  607. // =============================== END ======================================
  608.