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 / lines / appcf.cpp < prev    next >
C/C++ Source or Header  |  1997-07-31  |  3KB  |  118 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 Simple InProc Automation Object.
  12. **
  13. **  Applicationcf.cpp
  14. **
  15. **  CApplicationCF (class factory) implementation
  16. **
  17. **  Written by Microsoft Product Support Services, Windows Developer Support
  18. **
  19. *************************************************************************/
  20. #include <windows.h>
  21. #include <windowsx.h>
  22. #ifdef WIN16   
  23.   #include <ole2.h>
  24.   #include <compobj.h>    
  25.   #include <dispatch.h> 
  26.   #include <variant.h>
  27.   #include <olenls.h>  
  28. #endif      
  29. #include "lines.h" 
  30.  
  31. CApplicationCF::CApplicationCF(void)
  32. {    
  33.     m_cRef = 0; 
  34. }
  35.  
  36. /*
  37.  * CApplicationCF::QueryInterface, AddRef, Release
  38.  *
  39.  * Purpose:
  40.  *  Implements IUnknown::QueryInterface, AddRef, Release
  41.  *
  42.  */
  43. STDMETHODIMP
  44. CApplicationCF::QueryInterface(REFIID iid, void FAR* FAR* ppv) 
  45. {   
  46.     *ppv = NULL;
  47.     
  48.     if (iid == IID_IUnknown || iid == IID_IClassFactory)
  49.         *ppv = this;
  50.     else 
  51.         return E_NOINTERFACE; 
  52.  
  53.     AddRef();
  54.     return NOERROR;    
  55. }
  56.  
  57.  
  58. STDMETHODIMP_(ULONG)
  59. CApplicationCF::AddRef(void)
  60. {
  61.     return ++m_cRef;
  62. }
  63.  
  64.  
  65. STDMETHODIMP_(ULONG)
  66. CApplicationCF::Release(void)
  67. {   
  68.     if(--m_cRef == 0)
  69.     {
  70.         delete this;
  71.         return 0;
  72.     }
  73.     return m_cRef;
  74. }
  75.  
  76. /*
  77.  * CApplicationCF::CreateInstance, LockServer
  78.  *
  79.  * Purpose:
  80.  *  Implements IClassFactory::CreateInstance, LockServer
  81.  *
  82.  */
  83. STDMETHODIMP
  84. CApplicationCF::CreateInstance(IUnknown FAR* punkOuter,
  85.                          REFIID riid,
  86.                          void FAR* FAR* ppv)
  87. {
  88.     HRESULT hr;
  89.     
  90.     *ppv = NULL;
  91.     
  92.     // This implementation does'nt allow aggregation
  93.     if (punkOuter)
  94.         return CLASS_E_NOAGGREGATION;
  95.     
  96.     // This is REGCLS_SINGLEUSE class factory, so CreateInstance will be
  97.     // called atmost once. An application objects has a REGCLS_SINGLEUSE class
  98.     // factory. The global application object has already been created when 
  99.     // CreateInstance is called. A REGCLS_MULTIPLEUSE class factory's 
  100.     // CreateInstance would be called multiple times and would create a new 
  101.     // object each time. An MDI application would have a REGCLS_MULTIPLEUSE 
  102.     // class factory for it's document objects.             
  103.     hr = g_pApplication->QueryInterface(riid, ppv);
  104.     if (FAILED(hr)) 
  105.     {
  106.         g_pApplication->Quit();
  107.         return hr;
  108.     }   
  109.     return NOERROR;
  110. }
  111.  
  112. STDMETHODIMP
  113. CApplicationCF::LockServer(BOOL fLock)
  114. {
  115.     CoLockObjectExternal(g_pApplication, fLock, TRUE); 
  116.     return NOERROR;     
  117. }
  118.