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 / object1.h < prev    next >
C/C++ Source or Header  |  1995-05-03  |  4KB  |  138 lines

  1. /*
  2.  * OBJECT1.H
  3.  *
  4.  * Definition of the CObject1 class that uses interface
  5.  * implementations 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 _OBJECT1_H_
  16. #define _OBJECT1_H_
  17.  
  18.  
  19. //Creation function
  20. BOOL CreateObject1(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, CObject1, implements these interfaces with
  28.  * "interface implementations" where the C++ class itself inherits
  29.  * from and implements IUnknown members and then contains
  30.  * other C++ classes that each separately inherit from the other
  31.  * interfaces.  The other classes are the "interface implementations."
  32.  */
  33.  
  34.  
  35. /*
  36.  * In this technique you'll generally need forward references
  37.  * like this for use in declaring the object class.
  38.  */
  39. class CImpISampleOne;
  40. typedef CImpISampleOne *PCImpISampleOne;
  41.  
  42. class CImpISampleTwo;
  43. typedef CImpISampleTwo *PCImpISampleTwo;
  44.  
  45. //The C++ class that manages the actual object.
  46. class CObject1 : public IUnknown
  47.     {
  48.     /*
  49.      * Usually interface implementations will need back pointers
  50.      * to the object itself since this object usually manages
  51.      * the important data members.  In that case, make the
  52.      * interface implementation classes friends of the object.
  53.      */
  54.  
  55.     friend CImpISampleOne;
  56.     friend CImpISampleTwo;
  57.  
  58.  
  59.     private:
  60.         DWORD           m_cRef;         //Object reference count
  61.  
  62.         /*
  63.          * I use "m_pImpI" as a prefix to differentiate interface
  64.          * implementations for this object from other interface
  65.          * pointer variables I might hold to other objects, whic
  66.          * would be prefixed with "m_pI".
  67.          */
  68.         PCImpISampleOne  m_pImpISampleOne;
  69.         PCImpISampleTwo  m_pImpISampleTwo;
  70.  
  71.     public:
  72.         CObject1(void);
  73.         ~CObject1(void);
  74.  
  75.         BOOL Init(void);
  76.  
  77.         //IUnknown members
  78.         STDMETHODIMP         QueryInterface(REFIID, PPVOID);
  79.         STDMETHODIMP_(DWORD) AddRef(void);
  80.         STDMETHODIMP_(DWORD) Release(void);
  81.     };
  82.  
  83.  
  84. typedef CObject1 *PCObject1;
  85.  
  86.  
  87. /*
  88.  * Interface implementation classes are C++ classes that
  89.  * each singly inherit from an interface.  Their IUnknown
  90.  * members delegate calls to CObject1's IUnknown members--
  91.  * since IUnknown members affect the entire *object*, and
  92.  * since these interfaces are not the object itself, we must
  93.  * delegate to implement the correct behavior.
  94.  */
  95.  
  96. class CImpISampleOne : public ISampleOne
  97.     {
  98.     private:
  99.         DWORD       m_cRef;         //For debugging
  100.         PCObject1   m_pObj;         //Back pointer for delegation
  101.  
  102.     public:
  103.         CImpISampleOne(PCObject1);
  104.         ~CImpISampleOne(void);
  105.  
  106.         //IUnknown members
  107.         STDMETHODIMP         QueryInterface(REFIID, PPVOID);
  108.         STDMETHODIMP_(DWORD) AddRef(void);
  109.         STDMETHODIMP_(DWORD) Release(void);
  110.  
  111.         //ISampleOne members
  112.         STDMETHODIMP         GetMessage(LPTSTR, UINT);
  113.     };
  114.  
  115.  
  116.  
  117. class CImpISampleTwo : public ISampleTwo
  118.     {
  119.     private:
  120.         DWORD       m_cRef;         //For debugging
  121.         PCObject1   m_pObj;         //Back pointer for delegation
  122.  
  123.     public:
  124.         CImpISampleTwo(PCObject1);
  125.         ~CImpISampleTwo(void);
  126.  
  127.         //IUnknown members
  128.         STDMETHODIMP         QueryInterface(REFIID, PPVOID);
  129.         STDMETHODIMP_(DWORD) AddRef(void);
  130.         STDMETHODIMP_(DWORD) Release(void);
  131.  
  132.         //ISampleTwo members
  133.         STDMETHODIMP         GetString(LPTSTR, UINT);
  134.     };
  135.  
  136.  
  137. #endif _OBJECT1_H_
  138.