home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / inole2 / chap02 / query / object2.h < prev    next >
C/C++ Source or Header  |  1995-05-03  |  4KB  |  122 lines

  1. /*
  2.  * OBJECT2.H
  3.  *
  4.  * Definition of the CObject1 class that uses contained
  5.  * classes to provide ISampleOne and ISampleTwo.
  6.  *
  7.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Microsoft
  10.  * Internet  :  kraigb@microsoft.com
  11.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  12.  */
  13.  
  14.  
  15. #ifndef _OBJECT2_H_
  16. #define _OBJECT2_H_
  17.  
  18.  
  19. //Creation function
  20. BOOL CreateObject2(IUnknown **);
  21.  
  22.  
  23. /*
  24.  * The object we want to provide in OLE supports the IUnknown,
  25.  * ISampleOne, and ISampleTwo interfaces.
  26.  *
  27.  * The C++ class, CObject2, implements these interfaces with
  28.  * contained classes where each contained class inherits singly
  29.  * from an interface.  This is a little different from interface
  30.  * implementations shown in Object1 because contained classes
  31.  * are automatically instantiated along with CObject2.
  32.  */
  33.  
  34.  
  35.  
  36. //The C++ class that manages the actual object.
  37. class CObject2 : public IUnknown
  38.     {
  39.     /*
  40.      * Declare the contained classes, which should be friends.
  41.      * As with interface implementations, these need back
  42.      * pointers to the outer object as well as the IUnknown to
  43.      * which to delegate, which are the constructor parameters.
  44.      */
  45.     class CImpISampleOne : public ISampleOne
  46.         {
  47.         private:
  48.             DWORD       m_cRef;         //For debugging
  49.             CObject2   *m_pObj;         //Back pointer for delegation
  50.  
  51.         public:
  52.             CImpISampleOne(CObject2 *pObj)
  53.                 { m_cRef=0; m_pObj=pObj; }
  54.  
  55.             ~CImpISampleOne(void)
  56.                 { }
  57.  
  58.             //IUnknown members
  59.             STDMETHODIMP         QueryInterface(REFIID, PPVOID);
  60.             STDMETHODIMP_(DWORD) AddRef(void);
  61.             STDMETHODIMP_(DWORD) Release(void);
  62.  
  63.             //ISampleOne members
  64.             STDMETHODIMP         GetMessage(LPTSTR, UINT);
  65.         };
  66.  
  67.     class CImpISampleTwo : public ISampleTwo
  68.         {
  69.         private:
  70.             DWORD       m_cRef;         //For debugging
  71.             CObject2   *m_pObj;         //Back pointer for delegation
  72.  
  73.         public:
  74.             CImpISampleTwo(CObject2 *pObj)
  75.                 { m_cRef=0; m_pObj=pObj; }
  76.             ~CImpISampleTwo(void)
  77.                 { }
  78.  
  79.             //IUnknown members
  80.             STDMETHODIMP         QueryInterface(REFIID, PPVOID);
  81.             STDMETHODIMP_(DWORD) AddRef(void);
  82.             STDMETHODIMP_(DWORD) Release(void);
  83.  
  84.             //ISampleTwo members
  85.             STDMETHODIMP         GetString(LPTSTR, UINT);
  86.         };
  87.  
  88.  
  89.     friend CImpISampleOne;
  90.     friend CImpISampleTwo;
  91.  
  92.  
  93.     private:
  94.         DWORD           m_cRef;         //Object reference count
  95.  
  96.         /*
  97.          * In this technique I still use "ImpI" prefixes to
  98.          * differentiate contained classes.  The difference here
  99.          * from Object1 is that we declare objects instead of
  100.          * pointers to objects, so instantiating CObject2 will
  101.          * automatically instantiate CImpI*.  Destroying CObject2
  102.          * will automatically destroy CImp*.
  103.          */
  104.         CImpISampleOne  m_ImpISampleOne;
  105.         CImpISampleTwo  m_ImpISampleTwo;
  106.  
  107.     public:
  108.         CObject2(void);
  109.         ~CObject2(void);
  110.  
  111.         //IUnknown members
  112.         STDMETHODIMP         QueryInterface(REFIID, PPVOID);
  113.         STDMETHODIMP_(DWORD) AddRef(void);
  114.         STDMETHODIMP_(DWORD) Release(void);
  115.     };
  116.  
  117.  
  118. typedef CObject2 *PCObject2;
  119.  
  120.  
  121. #endif _OBJECT2_H_
  122.