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