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 / interfac / iclassf2.cpp < prev    next >
C/C++ Source or Header  |  1996-05-21  |  5KB  |  221 lines

  1. /*
  2.  * ICLASSF2.CPP
  3.  *
  4.  * Template implementation of a Class Factory object that
  5.  * supports licensing.
  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. #include "iclassf2.h"
  16.  
  17.  
  18. /*
  19.  * CClassFactory2::CClassFactory2
  20.  * CClassFactory2::~CClassFactory2
  21.  *
  22.  * Constructor Parameters:
  23.  *  None
  24.  */
  25.  
  26. CClassFactory2::CClassFactory2(void)
  27.     {
  28.     m_cRef=0L;
  29.     return;
  30.     }
  31.  
  32.  
  33. CClassFactory2::~CClassFactory2(void)
  34.     {
  35.     return;
  36.     }
  37.  
  38.  
  39.  
  40.  
  41.  
  42. /*
  43.  * CClassFactory2::QueryInterface
  44.  * CClassFactory2::AddRef
  45.  * CClassFactory2::Release
  46.  *
  47.  * Purpose:
  48.  *  Non-delegating IUnknown members for CClassFactory2.
  49.  */
  50.  
  51. STDMETHODIMP CClassFactory2::QueryInterface(REFIID riid
  52.     , LPVOID *ppv)
  53.     {
  54.     *ppv=NULL;
  55.  
  56.     if (IID_IUnknown==riid || IID_IClassFactory2==riid
  57.         || IID_IClassFactory2==riid)
  58.         *ppv=(LPVOID)this;
  59.  
  60.     if (NULL!=*ppv)
  61.         {
  62.         ((LPUNKNOWN)*ppv)->AddRef();
  63.         return NOERROR;
  64.         }
  65.  
  66.     return ResultFromScode(E_NOINTERFACE);
  67.     }
  68.  
  69.  
  70. STDMETHODIMP_(ULONG) CClassFactory2::AddRef(void)
  71.     {
  72.     return ++m_cRef;
  73.     }
  74.  
  75.  
  76. STDMETHODIMP_(ULONG) CClassFactory2::Release(void)
  77.     {
  78.     if (0L!=--m_cRef)
  79.         return m_cRef;
  80.  
  81.     //Free the object if the reference and lock counts are zero.
  82.     delete this;
  83.     return 0;
  84.     }
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92. /*
  93.  * CClassFactory2::CreateInstance
  94.  *
  95.  * Purpose:
  96.  *  Instantiates an object supported by this class factory.  That
  97.  *  object must at least support IUnknown.
  98.  *
  99.  *  Derived classes should override this function.
  100.  *
  101.  * Parameters:
  102.  *  pUnkOuter       LPUNKNOWN to the controlling IUnknown if we are
  103.  *                  being used in an aggregation.
  104.  *  riid            REFIID identifying the interface the caller
  105.  *                  desires to have for the new object.
  106.  *  ppvObj          LPVOID * in which to store the desired
  107.  *                  interface pointer for the new object.
  108.  *
  109.  * Return Value:
  110.  *  HRESULT         NOERROR if successful, otherwise E_NOINTERFACE
  111.  *                  if we cannot support the requested interface.
  112.  */
  113.  
  114. STDMETHODIMP CClassFactory2::CreateInstance(LPUNKNOWN pUnkOuter
  115.     , REFIID riid, LPVOID *ppvObj)
  116.     {
  117.     *ppvObj=NULL;
  118.     return ResultFromScode(E_OUTOFMEMORY);
  119.     }
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126. /*
  127.  * CClassFactory2::LockServer
  128.  *
  129.  * Purpose:
  130.  *  Increments or decrements the lock count of the serving
  131.  *  IClassFactory2 object.  When the lock count is zero and the
  132.  *  reference count is zero we get rid of this object.
  133.  *
  134.  *  DLL objects should override this to affect their DLL ref count.
  135.  *
  136.  * Parameters:
  137.  *  fLock           BOOL specifying whether to increment or
  138.  *                  decrement the lock count.
  139.  */
  140.  
  141. STDMETHODIMP CClassFactory2::LockServer(BOOL fLock)
  142.     {
  143.     return NOERROR;
  144.     }
  145.  
  146.  
  147.  
  148. /*
  149.  * CClassFactory2::GetLicInfo
  150.  *
  151.  * Purpose:
  152.  *  Fills a LICINFO structure with license information for
  153.  *  this class factory.
  154.  *
  155.  * Parameters:
  156.  *  pLicInfo        LPLICINFO to the structure to fill
  157.  */
  158.  
  159. STDMETHODIMP CClassFactory2::GetLicInfo(LPLICINFO pLicInfo)
  160.     {
  161.     if (NULL==pLicInfo)
  162.         return ResultFromScode(E_POINTER);
  163.  
  164.     return ResultFromScode(E_NOTIMPL);
  165.     }
  166.  
  167.  
  168.  
  169.  
  170. /*
  171.  * CClassFactory2::RequestLicKey
  172.  *
  173.  * Purpose:
  174.  *  Retrieves a license key from this class factory for use with
  175.  *  CreateInstanceLic.
  176.  *
  177.  * Parameters:
  178.  *  dwReserved      DWORD reserved
  179.  *  pbstrKey        BSTR * in which to return the key.
  180.  */
  181.  
  182. STDMETHODIMP CClassFactory2::RequestLicKey(DWORD dwReserved
  183.     , BSTR *pbstrKey)
  184.     {
  185.     *pbstrKey=NULL;
  186.     return ResultFromScode(E_NOTIMPL);
  187.     }
  188.  
  189.  
  190.  
  191. /*
  192.  * CClassFactory2::CreateInstanceLic
  193.  *
  194.  * Purpose:
  195.  *  Creates and instance of the object given a license key.
  196.  *  Same as CreateInstance, and implementations of this function
  197.  *  will typically just validate the key and call CreateInstance.
  198.  *
  199.  * Parameters:
  200.  *  pUnkOuter       LPUNKNOWN to the controlling IUnknown if we are
  201.  *                  being used in an aggregation.
  202.  *  pUnkReserved    LPUNKNOWN reserved.
  203.  *  riid            REFIID identifying the interface the caller
  204.  *                  desires to have for the new object.
  205.  *  bstrKey         BSTR key used to validate creation.
  206.  *  ppvObj          LPVOID * in which to store the desired
  207.  *                  interface pointer for the new object.
  208.  */
  209.  
  210. STDMETHODIMP CClassFactory2::CreateInstanceLic(LPUNKNOWN pUnkOuter
  211.     , LPUNKNOWN pUnkReserved, REFIID riid, BSTR bstrKey
  212.     , LPVOID *ppvObj)
  213.     {
  214.     *ppvObj=NULL;
  215.  
  216.     //Validate bstrKey
  217.  
  218.     //Create an instance once the key is validated.
  219.     return CreateInstance(pUnkOuter, riid, ppvObj);
  220.     }
  221.