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