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 / alias.cpp next >
Encoding:
C/C++ Source or Header  |  1997-07-31  |  3.3 KB  |  138 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. **  alias.cpp
  14. **
  15. **  CAlias 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.  * CAlias::Create
  34.  *
  35.  * Purpose:
  36.  *  Creates an instance of the Alias automation object and initializes it.
  37.  *
  38.  * Parameters:       
  39.  *  ptinfo     Typeinfo of Alias.
  40.  *  ppAlias    Returns Alias automation object.
  41.  *
  42.  * Return Value:
  43.  *  HRESULT
  44.  *
  45.  */
  46. HRESULT
  47. CAlias::Create(LPTYPEINFO ptinfo, CAlias FAR* FAR* ppAlias) 
  48. {   
  49.     HRESULT hr;
  50.     CAlias FAR* pAlias = NULL;       
  51.     LPTYPEATTR ptypeattr = NULL; 
  52.     CTypeDesc FAR* pTypeDesc;
  53.      
  54.     *ppAlias = NULL;
  55.     
  56.     // Create alias object.
  57.     pAlias = new CAlias();
  58.     if (pAlias == NULL)
  59.     {
  60.         hr = E_OUTOFMEMORY; 
  61.         goto error;
  62.     }   
  63.     // Load type information for the object from type library. 
  64.     hr = pAlias->LoadTypeInfo(IID_IAlias);
  65.     if (FAILED(hr))
  66.         goto error;  
  67.     
  68.     // Ask base class (CTypeInfo) to initialize    
  69.     hr = pAlias->_InitTypeInfo(ptinfo);
  70.     if (FAILED(hr))
  71.         goto error;        
  72.     
  73.     // Get base type of this alias.
  74.     hr = ptinfo->GetTypeAttr(&ptypeattr); 
  75.     if (FAILED(hr))
  76.         return NULL;     
  77.     hr = CTypeDesc::Create(ptinfo, &ptypeattr->tdescAlias, &pTypeDesc);
  78.     if (FAILED(hr))
  79.         goto error;
  80.     pTypeDesc->QueryInterface(IID_IDispatch, (LPVOID FAR*)&pAlias->m_pdispTypeDescBase);
  81.     ptinfo->ReleaseTypeAttr(ptypeattr);
  82.     
  83. #ifdef _DEBUG  
  84.     lstrcpyn(pAlias->m_szClassName, TEXT("Alias"), 100);
  85. #endif
  86.         
  87.     *ppAlias = pAlias;
  88.     return NOERROR;
  89.     
  90. error:
  91.     if (pAlias == NULL) return E_OUTOFMEMORY;
  92.     if (pAlias->m_pdispTypeDescBase) pAlias->m_pdispTypeDescBase->Release();    
  93.     if (ptypeattr) ptinfo->ReleaseTypeAttr(ptypeattr); 
  94.          
  95.     // Set to NULL to prevent destructor from attempting to free again
  96.     pAlias->m_pdispTypeDescBase = NULL;
  97.     
  98.     delete pAlias;
  99.     return hr;
  100. }
  101.  
  102. /*
  103.  * CAlias::CAlias
  104.  *
  105.  * Purpose:
  106.  *  Constructor for CAlias object. Initializes members to NULL.
  107.  *
  108.  */
  109. CAlias::CAlias()
  110. {
  111.     m_pdispTypeDescBase = NULL;
  112. }
  113.  
  114. /*
  115.  * CAlias::~CAlias
  116.  *
  117.  * Purpose:
  118.  *  Destructor for CAlias object. 
  119.  *
  120.  */
  121. CAlias::~CAlias()
  122. {
  123.     if (m_pdispTypeDescBase) m_pdispTypeDescBase->Release();
  124. }  
  125.  
  126. STDMETHODIMP_(REFCLSID)
  127. CAlias::GetInterfaceID()
  128. {
  129.     return IID_IAlias;
  130. }
  131.  
  132. STDMETHODIMP_(ITypeDesc FAR*)
  133. CAlias::get_BaseType()
  134. {
  135.     m_pdispTypeDescBase->AddRef();
  136.     return (ITypeDesc FAR*)m_pdispTypeDescBase;
  137. }
  138.