home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / oleaut / browseh / union.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-31  |  4.4 KB  |  178 lines

  1. /*************************************************************************
  2. **
  3. **  This is a part of the Microsoft Source Code Samples.
  4. **
  5. **  Copyright (C) 1992-1997 Microsoft Corporation. All rights reserved.
  6. **
  7. **  This source code is only intended as a supplement to Microsoft Development
  8. **  Tools and/or WinHelp documentation.  See these sources for detailed
  9. **  information regarding the Microsoft samples programs.
  10. **
  11. **  OLE Automation TypeLibrary Browse Helper Sample
  12. **
  13. **  union.cpp
  14. **
  15. **  CUnion implementation
  16. **
  17. **  Written by Microsoft Product Support Services, Windows Developer Support
  18. **
  19. *************************************************************************/
  20.  
  21. #include <windows.h>
  22. #include <windowsx.h>
  23. #ifdef WIN16   
  24.   #include <ole2.h>
  25.   #include <compobj.h>    
  26.   #include <dispatch.h> 
  27.   #include <variant.h>
  28.   #include <olenls.h>  
  29. #endif 
  30. #include "browseh.h"  
  31.  
  32. /*
  33.  * CUnion::Create
  34.  *
  35.  * Purpose:
  36.  *  Creates an instance of the Union automation object and initializes it.
  37.  *
  38.  * Parameters:       
  39.  *  ptinfo     TypeInfo of Union.
  40.  *  ppUnion    Returns Union automation object.
  41.  *
  42.  * Return Value:
  43.  *  HRESULT
  44.  *
  45.  */
  46. HRESULT 
  47. CUnion::Create(LPTYPEINFO ptinfo, CUnion FAR* FAR* ppUnion) 
  48. {   
  49.     HRESULT hr;
  50.     CUnion FAR* pUnion = NULL;
  51.      
  52.     *ppUnion = NULL;
  53.     
  54.     // Create application object.
  55.     pUnion = new CUnion();
  56.     if (pUnion == NULL)
  57.     {
  58.         hr = E_OUTOFMEMORY; 
  59.         goto error;
  60.     }   
  61.     // Load type information for the object from type library. 
  62.     hr = pUnion->LoadTypeInfo(IID_IUnion);
  63.     if (FAILED(hr))
  64.         goto error;  
  65.     // Initialise base class, CTypeInfo.    
  66.     hr = pUnion->_InitTypeInfo(ptinfo);
  67.     if (FAILED(hr))
  68.         goto error;
  69.     
  70.     ptinfo->AddRef();
  71.     pUnion->m_ptinfo = ptinfo;
  72.  
  73. #ifdef _DEBUG  
  74.     lstrcpyn(pUnion->m_szClassName, TEXT("Union"), 100);
  75. #endif
  76.         
  77.     *ppUnion = pUnion;
  78.     return NOERROR;
  79.     
  80. error:
  81.     if (pUnion == NULL) return E_OUTOFMEMORY;
  82.     if (pUnion->m_ptinfo) pUnion->m_ptinfo->Release();
  83.          
  84.     // Set to NULL to prevent destructor from attempting to free again  
  85.     pUnion->m_ptinfo = NULL;
  86.     
  87.     delete pUnion;
  88.     return hr;
  89. }
  90.  
  91. /*
  92.  * CUnion::CUnion
  93.  *
  94.  * Purpose:
  95.  *  Constructor for CUnion object. Initializes members to NULL.
  96.  *
  97.  */
  98. CUnion::CUnion()
  99. {
  100.     m_pdispMembers = NULL;      
  101.     m_ptinfo = NULL;
  102. }
  103.  
  104. /*
  105.  * CUnion::~CUnion
  106.  *
  107.  * Purpose:
  108.  *  Destructor for CUnion object. 
  109.  *
  110.  */
  111. CUnion::~CUnion()
  112. {
  113.     if (m_pdispMembers) m_pdispMembers->Release();
  114.     if (m_ptinfo) m_ptinfo->Release();
  115. }  
  116.  
  117. STDMETHODIMP_(REFCLSID)
  118. CUnion::GetInterfaceID()
  119. {
  120.     return IID_IUnion;
  121. }
  122.  
  123. /*
  124.  * CUnion::
  125.  *
  126.  * Purpose:
  127.  *  
  128.  *
  129.  */
  130.  
  131. STDMETHODIMP_(ICollection FAR*)
  132. CUnion::get_Members()     
  133. {    
  134.     HRESULT hr;
  135.     CProperty FAR* pProperty;
  136.     CCollection FAR* pCollection = NULL;
  137.     LPDISPATCH pdisp;
  138.     LPVARDESC pvardesc = NULL;   
  139.     LPTYPEATTR ptypeattr = NULL;
  140.     unsigned short n;
  141.     
  142.     if (m_pdispMembers == NULL)
  143.     {   
  144.         // Create collection of union memebers. 
  145.         hr = m_ptinfo->GetTypeAttr(&ptypeattr);
  146.         if (FAILED(hr))
  147.             {RaiseException(IDS_Unexpected); return NULL;}       
  148.         hr = CCollection::Create(ptypeattr->cVars, 0, &pCollection);  
  149.         if (FAILED(hr))
  150.             {RaiseException(IDS_Unexpected); goto error;}   
  151.         for (n=0; n<ptypeattr->cVars; n++)
  152.         {       
  153.             hr = m_ptinfo->GetVarDesc(n, &pvardesc);   
  154.             if (FAILED(hr))
  155.                 {RaiseException(IDS_Unexpected); goto error;}   
  156.             hr = CProperty::Create(m_ptinfo, pvardesc, &pProperty);
  157.             if (FAILED(hr))
  158.                 {RaiseException(IDS_Unexpected); goto error;}    
  159.             m_ptinfo->ReleaseVarDesc(pvardesc); 
  160.             pvardesc = NULL;
  161.             pProperty->QueryInterface(IID_IDispatch, (void FAR* FAR*)&pdisp);
  162.             pCollection->Add(pdisp);   
  163.             pdisp->Release();
  164.         }
  165.         pCollection->QueryInterface(IID_IDispatch, (void FAR* FAR*)&pdisp);
  166.         m_pdispMembers = pdisp;    
  167.         m_ptinfo->ReleaseTypeAttr(ptypeattr); 
  168.     }
  169.     m_pdispMembers->AddRef();
  170.     return (ICollection FAR*)m_pdispMembers;
  171.  
  172. error:  
  173.     if (ptypeattr) m_ptinfo->ReleaseTypeAttr(ptypeattr);   
  174.     if (pCollection) delete pCollection;   
  175.     if (pvardesc) m_ptinfo->ReleaseVarDesc(pvardesc);   
  176.     return NULL;
  177. }    
  178.