home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / inole2 / chap19 / hcosmo / hcosmo.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-21  |  5.9 KB  |  300 lines

  1. /*
  2.  * HCOSMO.CPP
  3.  * Cosmo Handler Chapter 19
  4.  *
  5.  * DLL exports for an object handler as well as class factory.
  6.  *
  7.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Microsoft
  10.  * Internet  :  kraigb@microsoft.com
  11.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  12.  */
  13.  
  14.  
  15. #define INITGUIDS
  16. #include "hcosmo.h"
  17.  
  18.  
  19. //Count number of objects and number of locks.
  20. ULONG       g_cObj=0;
  21. ULONG       g_cLock=0;
  22.  
  23. //DLL Instance handle
  24. HINSTANCE   g_hInst=0;
  25.  
  26.  
  27. /*
  28.  * LibMain(32)
  29.  *
  30.  * Purpose:
  31.  *  Entry point for Win32 or Win16 DLLs.
  32.  */
  33.  
  34. #ifdef WIN32
  35. BOOL WINAPI LibMain32(HINSTANCE hInstance, ULONG ulReason
  36.     , LPVOID pvReserved)
  37.     {
  38.     if (DLL_PROCESS_DETACH==ulReason)
  39.         {
  40.         return TRUE;
  41.         }
  42.     else
  43.         {
  44.         if (DLL_PROCESS_ATTACH!=ulReason)
  45.             return TRUE;
  46.         }
  47.  
  48.     g_hInst=hInstance;
  49.     return TRUE;
  50.     }
  51.  
  52. #else
  53.  
  54. /*
  55.  * LibMain (also called from Win32 LibMain32)
  56.  *
  57.  * Purpose:
  58.  *  DLL-specific entry point called from LibEntry.
  59.  */
  60.  
  61. int PASCAL LibMain(HINSTANCE hInst, WORD wDataSeg
  62.     , WORD cbHeapSize, LPSTR lpCmdLine)
  63.     {
  64.     if (0!=cbHeapSize)
  65.         UnlockData(0);
  66.  
  67.     g_hInst=hInst;
  68.     return (int)hInst;
  69.     }
  70.  
  71. #endif
  72.  
  73.  
  74.  
  75.  
  76. /*
  77.  * DllGetClassObject
  78.  *
  79.  * Purpose:
  80.  *  Standard export for DLL component objects.
  81.  */
  82.  
  83. HRESULT APIENTRY DllGetClassObject(REFCLSID rclsid
  84.     , REFIID riid, PPVOID ppv)
  85.     {
  86.     if (CLSID_CosmoFigure!=rclsid)
  87.         return ResultFromScode(E_FAIL);
  88.  
  89.     //Check that we can provide the interface
  90.     if (IID_IUnknown!=riid && IID_IClassFactory!=riid)
  91.         return ResultFromScode(E_NOINTERFACE);
  92.  
  93.     //Return our IClassFactory for Figure objects
  94.     *ppv=new CFigureClassFactory;
  95.  
  96.     if (NULL==*ppv)
  97.         return ResultFromScode(E_OUTOFMEMORY);
  98.  
  99.     ((LPUNKNOWN)*ppv)->AddRef();
  100.     g_cObj++;
  101.     return NOERROR;
  102.     }
  103.  
  104.  
  105.  
  106.  
  107.  
  108. /*
  109.  * DllCanUnloadNow
  110.  *
  111.  * Purpose:
  112.  *  Answers if the DLL can be freed, that is, if there are no
  113.  *  references to anything this DLL provides.
  114.  *
  115.  * Parameters:
  116.  *  None
  117.  *
  118.  * Return Value:
  119.  *  BOOL            TRUE if nothing is using us, FALSE otherwise.
  120.  */
  121.  
  122. STDAPI DllCanUnloadNow(void)
  123.     {
  124.     SCODE   sc;
  125.  
  126.     //Our answer is whether there are any object or locks
  127.     sc=(0L==g_cObj && 0==g_cLock) ? S_OK : S_FALSE;
  128.     return ResultFromScode(sc);
  129.     }
  130.  
  131.  
  132.  
  133.  
  134. /*
  135.  * ObjectDestroyed
  136.  *
  137.  * Purpose:
  138.  *  Function for the Figure object to call when it gets destroyed.
  139.  *  Since we're in a DLL we only track the number of objects here
  140.  *  letting DllCanUnloadNow take care of the rest.
  141.  */
  142.  
  143. void ObjectDestroyed(void)
  144.     {
  145.     g_cObj--;
  146.     return;
  147.     }
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154. /*
  155.  * CFigureClassFactory::CFigureClassFactory
  156.  * CFigureClassFactory::~CFigureClassFactory
  157.  */
  158.  
  159. CFigureClassFactory::CFigureClassFactory(void)
  160.     {
  161.     m_cRef =0L;
  162.     return;
  163.     }
  164.  
  165. CFigureClassFactory::~CFigureClassFactory(void)
  166.     {
  167.     return;
  168.     }
  169.  
  170.  
  171.  
  172. /*
  173.  * CFigureClassFactory::QueryInterface
  174.  * CFigureClassFactory::AddRef
  175.  * CFigureClassFactory::Release
  176.  */
  177.  
  178. STDMETHODIMP CFigureClassFactory::QueryInterface(REFIID riid
  179.     , PPVOID ppv)
  180.     {
  181.     *ppv=NULL;
  182.  
  183.     //Any interface on this object is the object pointer.
  184.     if (IID_IUnknown==riid || IID_IClassFactory==riid)
  185.         *ppv=this;
  186.  
  187.     if (NULL!=*ppv)
  188.         {
  189.         ((LPUNKNOWN)*ppv)->AddRef();
  190.         return NOERROR;
  191.         }
  192.  
  193.     return ResultFromScode(E_NOINTERFACE);
  194.     }
  195.  
  196.  
  197. STDMETHODIMP_(ULONG) CFigureClassFactory::AddRef(void)
  198.     {
  199.     return ++m_cRef;
  200.     }
  201.  
  202.  
  203. STDMETHODIMP_(ULONG) CFigureClassFactory::Release(void)
  204.     {
  205.     if (0!=--m_cRef)
  206.         return m_cRef;
  207.  
  208.     delete this;
  209.     ObjectDestroyed();
  210.     return 0;
  211.     }
  212.  
  213.  
  214.  
  215.  
  216.  
  217. /*
  218.  * CFigureClassFactory::CreateInstance
  219.  *
  220.  * Purpose:
  221.  *  Instantiates a Figure object that supports the IFigure
  222.  *  and IUnknown interfaces.  If the caller asks for a different
  223.  *  interface than these two then we fail.
  224.  *
  225.  * Parameters:
  226.  *  pUnkOuter       LPUNKNOWN to the controlling IUnknown if we are
  227.  *                  being used in an aggregation.
  228.  *  riid            REFIID identifying the interface the caller
  229.  *                  desires to have for the new object.
  230.  *  ppvObj          PPVOID in which to store the desired interface
  231.  *                  pointer for the new object.
  232.  *
  233.  * Return Value:
  234.  *  HRESULT         NOERROR if successful, otherwise contains
  235.  *                  E_NOINTERFACE if we cannot support the requested
  236.  *                  interface.
  237.  */
  238.  
  239. STDMETHODIMP CFigureClassFactory::CreateInstance(LPUNKNOWN pUnkOuter
  240.     , REFIID riid, PPVOID ppvObj)
  241.     {
  242.     PCFigure            pObj;
  243.     HRESULT             hr;
  244.  
  245.     *ppvObj=NULL;
  246.     hr=ResultFromScode(E_OUTOFMEMORY);
  247.  
  248.     //Verify that a controlling unknown is asking for IUnknown
  249.     if (NULL!=pUnkOuter && IID_IUnknown!=riid)
  250.         return ResultFromScode(E_NOINTERFACE);
  251.  
  252.     //Create the object.  This also creates a window.
  253.     pObj=new CFigure(pUnkOuter, ObjectDestroyed, g_hInst);
  254.  
  255.     if (NULL==pObj)
  256.         return hr;
  257.  
  258.     if (pObj->Init())
  259.         hr=pObj->QueryInterface(riid, ppvObj);
  260.  
  261.     //Kill the object if initial creation or Init failed.
  262.     if (FAILED(hr))
  263.         delete pObj;
  264.     else
  265.         g_cObj++;
  266.  
  267.     return hr;
  268.     }
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275. /*
  276.  * CFigureClassFactory::LockServer
  277.  *
  278.  * Purpose:
  279.  *  Increments or decrements the lock count of the DLL.  If the lock
  280.  *  count goes to zero and there are no objects, the DLL is allowed
  281.  *  to unload.  See DllCanUnloadNow.
  282.  *
  283.  * Parameters:
  284.  *  fLock           BOOL specifying whether to increment or
  285.  *                  decrement the lock count.
  286.  *
  287.  * Return Value:
  288.  *  HRESULT         NOERROR always.
  289.  */
  290.  
  291. STDMETHODIMP CFigureClassFactory::LockServer(BOOL fLock)
  292.     {
  293.     if (fLock)
  294.         g_cLock++;
  295.     else
  296.         g_cLock--;
  297.  
  298.     return NOERROR;
  299.     }
  300.