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 / struct.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-31  |  4.4 KB  |  170 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. **  struct.cpp
  14. **
  15. **  CStruct 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.  * CStruct::Create
  34.  *
  35.  * Purpose:
  36.  *  Creates an instance of the Struct automation object and initializes it.
  37.  *
  38.  * Parameters:       
  39.  *  ptinfo      TypeInfo of struct..
  40.  *  ppStruct    Returns Struct automation object.
  41.  *
  42.  * Return Value:
  43.  *  HRESULT
  44.  *
  45.  */
  46. HRESULT 
  47. CStruct::Create(LPTYPEINFO ptinfo, CStruct FAR* FAR* ppStruct) 
  48. {   
  49.     HRESULT hr;
  50.     CStruct FAR* pStruct = NULL;
  51.      
  52.     *ppStruct = NULL;
  53.     
  54.     // Create object.
  55.     pStruct = new CStruct();
  56.     if (pStruct == NULL)
  57.     {
  58.         hr = E_OUTOFMEMORY; 
  59.         goto error;
  60.     }   
  61.     // Load type information for the object from type library. 
  62.     hr = pStruct->LoadTypeInfo(IID_IStruct);
  63.     if (FAILED(hr))
  64.         goto error;  
  65.     // Initialize base class, CTypeInfo    
  66.     hr = pStruct->_InitTypeInfo(ptinfo);
  67.     if (FAILED(hr))
  68.         goto error;
  69.     
  70.     ptinfo->AddRef();
  71.     pStruct->m_ptinfo = ptinfo;
  72.  
  73. #ifdef _DEBUG  
  74.     lstrcpyn(pStruct->m_szClassName, TEXT("Struct"), 100);
  75. #endif
  76.         
  77.     *ppStruct = pStruct;
  78.     return NOERROR;
  79.     
  80. error:
  81.     if (pStruct == NULL) return E_OUTOFMEMORY;
  82.     if (pStruct->m_ptinfo) pStruct->m_ptinfo->Release();
  83.          
  84.     // Set to NULL to prevent destructor from attempting to free again  
  85.     pStruct->m_ptinfo = NULL;
  86.     
  87.     delete pStruct;
  88.     return hr;
  89. }
  90.  
  91. /*
  92.  * CStruct::CStruct
  93.  *
  94.  * Purpose:
  95.  *  Constructor for CStruct object. Initializes members to NULL.
  96.  *
  97.  */
  98. CStruct::CStruct()
  99. {
  100.     m_pdispMembers = NULL;      
  101.     m_ptinfo = NULL;
  102. }
  103.  
  104. /*
  105.  * CStruct::~CStruct
  106.  *
  107.  * Purpose:
  108.  *  Destructor for CStruct object. 
  109.  *
  110.  */
  111. CStruct::~CStruct()
  112. {
  113.     if (m_pdispMembers) m_pdispMembers->Release();
  114.     if (m_ptinfo) m_ptinfo->Release();
  115. }  
  116.  
  117. STDMETHODIMP_(REFCLSID)
  118. CStruct::GetInterfaceID()
  119. {
  120.     return IID_IStruct;
  121. }
  122.  
  123. STDMETHODIMP_(ICollection FAR*)
  124. CStruct::get_Members()     
  125. {    
  126.     HRESULT hr;
  127.     CProperty FAR* pProperty;
  128.     CCollection FAR* pCollection = NULL;
  129.     LPDISPATCH pdisp;
  130.     LPVARDESC pvardesc = NULL;   
  131.     LPTYPEATTR ptypeattr = NULL;
  132.     unsigned short n;
  133.     
  134.     if (m_pdispMembers == NULL)
  135.     {   
  136.         // Create a collection of structure members 
  137.         hr = m_ptinfo->GetTypeAttr(&ptypeattr);
  138.         if (FAILED(hr))
  139.             {RaiseException(IDS_Unexpected); return NULL;}       
  140.         hr = CCollection::Create(ptypeattr->cVars, 0, &pCollection);  
  141.         if (FAILED(hr))
  142.             {RaiseException(IDS_Unexpected); goto error;}   
  143.         for (n=0; n<ptypeattr->cVars; n++)
  144.         {       
  145.             hr = m_ptinfo->GetVarDesc(n, &pvardesc);   
  146.             if (FAILED(hr))
  147.                 {RaiseException(IDS_Unexpected); goto error;}   
  148.             hr = CProperty::Create(m_ptinfo, pvardesc, &pProperty);
  149.             if (FAILED(hr))
  150.                 {RaiseException(IDS_Unexpected); goto error;}    
  151.             m_ptinfo->ReleaseVarDesc(pvardesc); 
  152.             pvardesc = NULL;
  153.             pProperty->QueryInterface(IID_IDispatch, (void FAR* FAR*)&pdisp);
  154.             pCollection->Add(pdisp);   
  155.             pdisp->Release();
  156.         }
  157.         pCollection->QueryInterface(IID_IDispatch, (void FAR* FAR*)&pdisp);
  158.         m_pdispMembers = pdisp;    
  159.         m_ptinfo->ReleaseTypeAttr(ptypeattr); 
  160.     }
  161.     m_pdispMembers->AddRef();
  162.     return (ICollection FAR*)m_pdispMembers;
  163.  
  164. error:  
  165.     if (ptypeattr) m_ptinfo->ReleaseTypeAttr(ptypeattr);   
  166.     if (pCollection) delete pCollection;   
  167.     if (pvardesc) m_ptinfo->ReleaseVarDesc(pvardesc);   
  168.     return NULL;
  169. }    
  170.