home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winui / shell / bandobjs / clsfact.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-12-11  |  4.8 KB  |  189 lines

  1. /**************************************************************************
  2.    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  3.    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  4.    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  5.    PARTICULAR PURPOSE.
  6.  
  7.    Copyright 1997 - 1998 Microsoft Corporation.  All Rights Reserved.
  8. **************************************************************************/
  9.  
  10. /**************************************************************************
  11.  
  12.    File:          ClsFact.cpp
  13.    
  14.    Description:   Implements CClassFactory.
  15.  
  16. **************************************************************************/
  17.  
  18. /**************************************************************************
  19.    #include statements
  20. **************************************************************************/
  21.  
  22. #include "ClsFact.h"
  23. #include "Guid.h"
  24.  
  25. /**************************************************************************
  26.    private function prototypes
  27. **************************************************************************/
  28.  
  29. /**************************************************************************
  30.    global variables
  31. **************************************************************************/
  32.  
  33. ///////////////////////////////////////////////////////////////////////////
  34. //
  35. // IClassFactory implementation
  36. //
  37.  
  38. /**************************************************************************
  39.  
  40.    CClassFactory::CClassFactory
  41.  
  42. **************************************************************************/
  43.  
  44. CClassFactory::CClassFactory(CLSID clsid)
  45. {
  46. m_clsidObject = clsid;
  47. m_ObjRefCount = 1;
  48. g_DllRefCount++;
  49. }
  50.  
  51. /**************************************************************************
  52.  
  53.    CClassFactory::~CClassFactory
  54.  
  55. **************************************************************************/
  56.  
  57. CClassFactory::~CClassFactory()
  58. {
  59. g_DllRefCount--;
  60. }
  61.  
  62. /**************************************************************************
  63.  
  64.    CClassFactory::QueryInterface
  65.  
  66. **************************************************************************/
  67.  
  68. STDMETHODIMP CClassFactory::QueryInterface(REFIID riid, LPVOID *ppReturn)
  69. {
  70. *ppReturn = NULL;
  71.  
  72. if(IsEqualIID(riid, IID_IUnknown))
  73.    {
  74.    *ppReturn = this;
  75.    }
  76.    
  77. else if(IsEqualIID(riid, IID_IClassFactory))
  78.    {
  79.    *ppReturn = (IClassFactory*)this;
  80.    }   
  81.  
  82. if(*ppReturn)
  83.    {
  84.    (*(LPUNKNOWN*)ppReturn)->AddRef();
  85.    return S_OK;
  86.    }
  87.  
  88. return E_NOINTERFACE;
  89. }                                             
  90.  
  91. /**************************************************************************
  92.  
  93.    CClassFactory::AddRef
  94.  
  95. **************************************************************************/
  96.  
  97. STDMETHODIMP_(DWORD) CClassFactory::AddRef()
  98. {
  99. return ++m_ObjRefCount;
  100. }
  101.  
  102.  
  103. /**************************************************************************
  104.  
  105.    CClassFactory::Release
  106.  
  107. **************************************************************************/
  108.  
  109. STDMETHODIMP_(DWORD) CClassFactory::Release()
  110. {
  111. if(--m_ObjRefCount == 0)
  112.    {
  113.    delete this;
  114.    return 0;
  115.    }
  116.    
  117. return m_ObjRefCount;
  118. }
  119.  
  120. /**************************************************************************
  121.  
  122.    CClassFactory::CreateInstance
  123.  
  124. **************************************************************************/
  125.  
  126. STDMETHODIMP CClassFactory::CreateInstance(  LPUNKNOWN pUnknown, 
  127.                                              REFIID riid, 
  128.                                              LPVOID *ppObject)
  129. {
  130. HRESULT  hResult = E_FAIL;
  131. LPVOID   pTemp = NULL;
  132.  
  133. *ppObject = NULL;
  134.  
  135. if(pUnknown != NULL)
  136.    return CLASS_E_NOAGGREGATION;
  137.  
  138. //create the proper object
  139. if(IsEqualCLSID(m_clsidObject, CLSID_SampleDeskBand))
  140.    {
  141.    CDeskBand *pDeskBand = new CDeskBand();
  142.    if(NULL == pDeskBand)
  143.       return E_OUTOFMEMORY;
  144.    
  145.    pTemp = pDeskBand;
  146.    }
  147.   
  148. if(IsEqualCLSID(m_clsidObject, CLSID_SampleExplorerBar))
  149.    {
  150.    CExplorerBar *pExplorerBar = new CExplorerBar();
  151.    if(NULL == pExplorerBar)
  152.       return E_OUTOFMEMORY;
  153.    
  154.    pTemp = pExplorerBar;
  155.    }
  156.   
  157. if(IsEqualCLSID(m_clsidObject, CLSID_SampleCommBand))
  158.    {
  159.    CCommBand *pCommBand = new CCommBand();
  160.    if(NULL == pCommBand)
  161.       return E_OUTOFMEMORY;
  162.    
  163.    pTemp = pCommBand;
  164.    }
  165.   
  166. if(pTemp)
  167.    {
  168.    //get the QueryInterface return for our return value
  169.    hResult = ((LPUNKNOWN)pTemp)->QueryInterface(riid, ppObject);
  170.  
  171.    //call Release to decement the ref count
  172.    ((LPUNKNOWN)pTemp)->Release();
  173.    }
  174.  
  175. return hResult;
  176. }
  177.  
  178. /**************************************************************************
  179.  
  180.    CClassFactory::LockServer
  181.  
  182. **************************************************************************/
  183.  
  184. STDMETHODIMP CClassFactory::LockServer(BOOL)
  185. {
  186. return E_NOTIMPL;
  187. }
  188.  
  189.