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 / property.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-31  |  4.6 KB  |  187 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 Property Browse Helper Sample
  12. **
  13. **  property.cpp
  14. **
  15. **  CProperty 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.  * CProperty::Create
  34.  *
  35.  * Purpose:
  36.  *  Creates an instance of the Property automation object and initializes it.
  37.  *
  38.  * Parameters:   
  39.  *  ptinfo        TypeInfo in which this property/variable is contained.
  40.  *  pvardesc      VARDESC that descrribes this property/variable.
  41.  *  ppProperty    Returns Property automation object.
  42.  *
  43.  * Return Value:
  44.  *  HRESULT
  45.  *
  46.  */
  47. HRESULT 
  48. CProperty::Create(LPTYPEINFO ptinfo, LPVARDESC pvardesc, CProperty FAR* FAR* ppProperty) 
  49. {   
  50.     HRESULT hr;
  51.     CProperty FAR* pProperty = NULL;   
  52.     BSTR bstr;  
  53.     unsigned int cNames;
  54.     CTypeDesc FAR* pTypeDesc = NULL;
  55.      
  56.     *ppProperty = NULL;
  57.     
  58.     // Create object.
  59.     pProperty = new CProperty();
  60.     if (pProperty == NULL)
  61.     {
  62.         hr = E_OUTOFMEMORY; 
  63.         goto error;
  64.     }   
  65.     
  66.    // Load type information for the object from type library. 
  67.     hr = pProperty->LoadTypeInfo(IID_IProperty);
  68.     if (FAILED(hr))
  69.         goto error; 
  70.     
  71.     pProperty->m_memid = pvardesc->memid; 
  72.     ptinfo->GetNames(pvardesc->memid, &bstr, 1, &cNames);    
  73.     pProperty->m_bstrName = bstr;
  74.     
  75.     // Property type
  76.     hr = CTypeDesc::Create(ptinfo, &pvardesc->elemdescVar.tdesc, &pTypeDesc);
  77.     if (FAILED(hr))
  78.         goto error;
  79.     pTypeDesc->QueryInterface(IID_IDispatch, (LPVOID FAR*)&pProperty->m_pdispTypeDesc);
  80.     
  81.     hr = ptinfo->GetDocumentation(pvardesc->memid, NULL, &pProperty->m_bstrDocumentation,
  82.              &pProperty->m_ulHelpContext, &pProperty->m_bstrHelpFile);    
  83.     if (FAILED(hr))
  84.         goto error;
  85.  
  86. #ifdef _DEBUG  
  87.     lstrcpyn(pProperty->m_szClassName, TEXT("Property"), 100);
  88. #endif 
  89.         
  90.     *ppProperty = pProperty;
  91.     return NOERROR;
  92.     
  93. error:                        
  94.     if (pProperty == NULL) return E_OUTOFMEMORY;
  95.     if (pProperty->m_bstrName) SysFreeString(pProperty->m_bstrName);
  96.     if (pProperty->m_bstrDocumentation) SysFreeString(pProperty->m_bstrDocumentation);
  97.     if (pProperty->m_bstrHelpFile) SysFreeString(pProperty->m_bstrHelpFile);   
  98.     
  99.     // Set to NULL to prevent destructor from attempting to free again
  100.     pProperty->m_bstrName = NULL;
  101.     pProperty->m_bstrDocumentation = NULL;
  102.     pProperty->m_bstrHelpFile = NULL;    
  103.  
  104.     delete pProperty;
  105.     return hr;
  106. }
  107.  
  108. /*
  109.  * CProperty::CProperty
  110.  *
  111.  * Purpose:
  112.  *  Constructor for CProperty object. Initializes members to NULL.
  113.  *
  114.  */
  115. CProperty::CProperty()
  116. {   
  117.     m_bstrName = NULL;
  118.     m_bstrDocumentation = NULL;
  119.     m_bstrHelpFile = NULL;      
  120.     m_pdispTypeDesc = NULL;
  121. }
  122.  
  123. /*
  124.  * CProperty::~CProperty
  125.  *
  126.  * Purpose:
  127.  *  Destructor for CProperty object. 
  128.  *
  129.  */
  130. CProperty::~CProperty()
  131. {    
  132.      if (m_bstrName) SysFreeString(m_bstrName);
  133.      if (m_bstrDocumentation) SysFreeString(m_bstrDocumentation);
  134.      if (m_bstrHelpFile) SysFreeString(m_bstrHelpFile);    
  135.      if (m_pdispTypeDesc) m_pdispTypeDesc->Release();
  136. }
  137.  
  138. STDMETHODIMP_(REFCLSID)
  139. CProperty::GetInterfaceID()
  140. {
  141.     return IID_IProperty;
  142. }
  143.  
  144. STDMETHODIMP_(BSTR)
  145. CProperty::get_Name()
  146. {
  147.     return SysAllocString(m_bstrName);
  148. }   
  149.  
  150. STDMETHODIMP_(BSTR)
  151. CProperty::get_Documentation()     
  152. {
  153.     return SysAllocString(m_bstrDocumentation);
  154. }  
  155.  
  156. STDMETHODIMP_(long)
  157. CProperty::get_HelpContext()     
  158. {
  159.     return (long)m_ulHelpContext;
  160. }
  161.  
  162. STDMETHODIMP_(BSTR)
  163. CProperty::get_HelpFile()     
  164. {
  165.     return SysAllocString(m_bstrHelpFile);
  166. }   
  167.  
  168.  
  169. STDMETHODIMP_(ITypeDesc FAR*)
  170. CProperty::get_Type()
  171. {
  172.     m_pdispTypeDesc->AddRef();
  173.     return (ITypeDesc FAR*)m_pdispTypeDesc;
  174. }   
  175.  
  176. STDMETHODIMP_(MEMBERID)
  177. CProperty::get_Memberid()
  178. {
  179.     return m_memid;
  180.  
  181. STDMETHODIMP_(OBJTYPE)
  182. CProperty::get_Kind()
  183. {   
  184.     return TYPE_PROPERTY;
  185. }
  186.