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 / intface.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-31  |  5.3 KB  |  197 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. **  intface.cpp
  14. **
  15. **  CInterface 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.  * CInterface::Create
  34.  *
  35.  * Purpose:
  36.  *  Creates an instance of the Interface automation object and initializes it.
  37.  *
  38.  * Parameters:       
  39.  *  ptinfo         TypeInfo of interface.
  40.  *  ppInterface    Returns Interface automation object.
  41.  *
  42.  * Return Value:
  43.  *  HRESULT
  44.  *
  45.  */
  46. HRESULT 
  47. CInterface::Create(LPTYPEINFO ptinfo, CInterface FAR* FAR* ppInterface) 
  48. {   
  49.     HRESULT hr;
  50.     CInterface FAR* pInterface = NULL;
  51.      
  52.     *ppInterface = NULL;
  53.     
  54.     // Create object.
  55.     pInterface = new CInterface();
  56.     if (pInterface == NULL)
  57.     {
  58.         hr = E_OUTOFMEMORY; 
  59.         goto error;
  60.     }    
  61.     // Load type information for the object from type library. 
  62.     hr = pInterface->LoadTypeInfo(IID_IInterface);
  63.     if (FAILED(hr))
  64.         goto error;
  65.     // Intialize base class, CTypeInfo
  66.     hr = pInterface->_InitTypeInfo(ptinfo);
  67.     if (FAILED(hr))
  68.         goto error;
  69.     
  70.     ptinfo->AddRef();
  71.     pInterface->m_ptinfo = ptinfo;
  72.  
  73. #ifdef _DEBUG  
  74.     lstrcpyn(pInterface->m_szClassName, TEXT("Interface"), 100);
  75. #endif 
  76.         
  77.     *ppInterface = pInterface;
  78.     return NOERROR;
  79.     
  80. error:
  81.     if (pInterface == NULL) return E_OUTOFMEMORY;
  82.     if (pInterface->m_ptinfo) pInterface->m_ptinfo->Release();
  83.          
  84.     // Set to NULL to prevent destructor from attempting to free again  
  85.     pInterface->m_ptinfo = NULL;
  86.     
  87.     delete pInterface;
  88.     return hr;
  89. }
  90.  
  91. /*
  92.  * CInterface::CInterface
  93.  *
  94.  * Purpose:
  95.  *  Constructor for CInterface object. Initializes members to NULL.
  96.  *
  97.  */
  98. CInterface::CInterface()
  99.     m_pdispFunctions = NULL;
  100.     m_pdispBaseInterface = NULL;
  101.     m_ptinfo = NULL;
  102. }
  103.  
  104. /*
  105.  * CInterface::~CInterface
  106.  *
  107.  * Purpose:
  108.  *  Destructor for CInterface object. 
  109.  *
  110.  */
  111. CInterface::~CInterface()
  112. {
  113.     if (m_pdispFunctions) m_pdispFunctions->Release();
  114.     if (m_pdispBaseInterface) m_pdispBaseInterface->Release();
  115.     if (m_ptinfo) m_ptinfo->Release();
  116.  
  117. STDMETHODIMP_(REFCLSID)
  118. CInterface::GetInterfaceID()
  119. {
  120.     return IID_IInterface;
  121. }
  122.  
  123. STDMETHODIMP_(ICollection FAR*)
  124. CInterface::get_Functions()     
  125. {  
  126.     HRESULT hr;
  127.     CFunction FAR* pFunction;
  128.     CCollection FAR* pCollection = NULL;
  129.     LPDISPATCH pdisp;
  130.     LPTYPEATTR ptypeattr = NULL;
  131.     unsigned short n;
  132.     
  133.     if (m_pdispFunctions == NULL)
  134.     {   
  135.         // Create collection of functions in interface.
  136.         hr = m_ptinfo->GetTypeAttr(&ptypeattr); 
  137.         if (FAILED(hr))
  138.             {RaiseException(IDS_Unexpected); return NULL;}   
  139.         hr = CCollection::Create(ptypeattr->cFuncs, 0, &pCollection);
  140.         if (FAILED(hr))
  141.             {RaiseException(IDS_Unexpected); goto error;}      
  142.         for (n=0; n<ptypeattr->cFuncs; n++)
  143.         {                
  144.             hr = CFunction::Create(m_ptinfo, n, &pFunction); 
  145.             if (FAILED(hr))
  146.                 {RaiseException(IDS_Unexpected); goto error;}
  147.             pFunction->QueryInterface(IID_IDispatch, (void FAR* FAR*)&pdisp);
  148.             pCollection->Add(pdisp);  
  149.             pdisp->Release();
  150.         }
  151.         pCollection->QueryInterface(IID_IDispatch, (void FAR* FAR*)&pdisp);
  152.         m_pdispFunctions = pdisp; 
  153.         m_ptinfo->ReleaseTypeAttr(ptypeattr);
  154.     }
  155.     m_pdispFunctions->AddRef();
  156.     return (ICollection FAR*)m_pdispFunctions;     
  157.     
  158. error:    
  159.     if (ptypeattr) m_ptinfo->ReleaseTypeAttr(ptypeattr);   
  160.     if (pCollection) delete pCollection;  
  161.     return NULL;
  162. }
  163.  
  164. STDMETHODIMP_(IInterface FAR*)
  165. CInterface::get_BaseInterface()
  166. {
  167.     HRESULT hr;
  168.     CInterface FAR* pInterface = NULL;
  169.     LPTYPEINFO ptinfoInterface = NULL;
  170.     HREFTYPE hreftype;
  171.  
  172.     if(m_pdispBaseInterface == NULL)
  173.     {
  174.         hr = m_ptinfo->GetRefTypeOfImplType(0, &hreftype);
  175.         if (FAILED(hr))
  176.             {RaiseException(IDS_Unexpected); return NULL;}
  177.         hr = m_ptinfo->GetRefTypeInfo(hreftype, &ptinfoInterface);
  178.         if (FAILED(hr))
  179.             {RaiseException(IDS_Unexpected); return NULL;}
  180.         hr = CInterface::Create(ptinfoInterface, &pInterface); 
  181.         if (FAILED(hr))
  182.             {RaiseException(IDS_Unexpected); goto error;}
  183.         pInterface->QueryInterface(IID_IDispatch, (void FAR* FAR*)&m_pdispBaseInterface);
  184.         ptinfoInterface->Release();
  185.     }
  186.     m_pdispBaseInterface->AddRef();
  187.     return (IInterface FAR*)m_pdispBaseInterface;
  188.  
  189. error:
  190.     if (ptinfoInterface) ptinfoInterface->Release();
  191.     if (pInterface) delete pInterface;
  192.     return NULL;
  193.     
  194. }
  195.