home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / com / connect / mdrive / driver.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-03  |  3.5 KB  |  134 lines

  1. // Driver.h : Declaration of the CRandomEvent
  2.  
  3.  
  4. #include "mdrivres.h"       // main symbols
  5. #include <afxtempl.h>
  6. /////////////////////////////////////////////////////////////////////////////
  7. // Drive
  8. struct foo
  9. {
  10.     foo() {nPos = 0;nDir=1;}
  11.     int nPos;
  12.     int nDir;
  13. };
  14.  
  15. class CCriticalSection
  16. {
  17. public:
  18.     void Lock() { EnterCriticalSection(&m_sec); }
  19.     void Unlock() { LeaveCriticalSection(&m_sec); }
  20.     CCriticalSection() { InitializeCriticalSection(&m_sec); } 
  21.     ~CCriticalSection() { DeleteCriticalSection(&m_sec); }
  22.     CRITICAL_SECTION m_sec;
  23. };
  24.  
  25. extern LONG g_cObjCnt;
  26. class CRandomEvent : public IRandomEvent
  27. {
  28. public:
  29.     CRandomEvent() { m_cnt = 0L; m_nID = 0; }
  30.  
  31.     STDMETHOD(GetTypeInfoCount)(UINT*) { return E_NOTIMPL; }
  32.     STDMETHOD(GetTypeInfo)(UINT, LCID, ITypeInfo**) { return E_NOTIMPL; }
  33.     STDMETHOD(GetIDsOfNames)(REFIID, LPOLESTR*, UINT, LCID, DISPID*) { return E_NOTIMPL; }
  34.     STDMETHOD(Invoke)(DISPID, REFIID, LCID, WORD, DISPPARAMS*, VARIANT*, EXCEPINFO*, UINT*) { return E_NOTIMPL; }
  35.     STDMETHOD(QueryInterface)(REFIID iid, LPVOID* ppv)
  36.     { 
  37.         if ((iid == __uuidof(IRandomEvent)) ||
  38.         (iid == __uuidof(IDispatch)) ||
  39.         (iid == __uuidof(IUnknown)))
  40.             *ppv = this;
  41.         else
  42.         {
  43.             *ppv = 0;
  44.             return E_NOINTERFACE;
  45.         }
  46.         AddRef();
  47.         return S_OK;
  48.     }
  49.     STDMETHOD_(ULONG,AddRef)() { return InterlockedIncrement(&m_cnt); }
  50.     STDMETHOD_(ULONG,Release)()
  51.     { 
  52.     InterlockedDecrement(&m_cnt);
  53.     if (m_cnt != 0)
  54.         return m_cnt;
  55.     InterlockedDecrement(&g_cObjCnt);
  56.     delete this;
  57.     return 0;
  58.     }
  59.         
  60. // IRandomEvent
  61.     STDMETHOD(put_Fire)(long l);
  62.     STDMETHOD(put_ID)(int n) { m_nID = n; return S_OK; }
  63.  
  64. private:
  65. // data
  66.     LONG m_cnt;    
  67.     int m_nID;
  68.     CMap<long, long, foo, foo> m_mapPos;
  69.     CCriticalSection m_cs;
  70. };
  71.  
  72. // NOTE: CRandomEventClassFactory is not necessary for this example, one can just use new
  73. extern LONG g_cLockCnt;
  74. interface CRandomEventClassFactory : public IClassFactory
  75. {
  76. public:
  77.     CRandomEventClassFactory() { m_cnt = 0L; }
  78.  
  79.     STDMETHOD(CreateInstance)(IUnknown* pUnkOuter, REFIID riid, void** ppvObject)
  80.     {
  81.         *ppvObject=NULL;
  82.         if (pUnkOuter != 0)
  83.             return CLASS_E_NOAGGREGATION;
  84.  
  85.         CRandomEvent* pRandomEvent = new CRandomEvent;
  86.         if (pRandomEvent == 0)
  87.             return E_OUTOFMEMORY;
  88.  
  89.         HRESULT hr = pRandomEvent->QueryInterface(riid, ppvObject);
  90.         if (FAILED(hr))
  91.             delete pRandomEvent;
  92.         else
  93.             InterlockedIncrement(&g_cObjCnt);
  94.  
  95.         return hr;
  96.     }
  97.         
  98.     STDMETHOD(LockServer)(BOOL fLock)
  99.     {
  100.         if (fLock)
  101.             InterlockedIncrement(&g_cLockCnt);
  102.         else
  103.             InterlockedDecrement(&g_cLockCnt);
  104.         return S_OK;
  105.     }     
  106.     STDMETHOD(QueryInterface)(REFIID iid, LPVOID* ppv)
  107.     { 
  108.         if ((iid == __uuidof(IClassFactory)) ||
  109.         (iid == __uuidof(IUnknown)))
  110.             *ppv = this;
  111.         else
  112.         {
  113.             *ppv = 0;
  114.             return E_NOINTERFACE;
  115.         }
  116.         AddRef();
  117.         return S_OK;
  118.     }
  119.     STDMETHOD_(ULONG,AddRef)() { return InterlockedIncrement(&m_cnt); }
  120.     STDMETHOD_(ULONG,Release)()
  121.     {  
  122.     InterlockedDecrement(&m_cnt);
  123.     if (m_cnt != 0)
  124.         return m_cnt;
  125.     delete this;
  126.     return 0;
  127.     } 
  128. private:
  129.     LONG m_cnt;
  130. };
  131. /////////////////////////////////////////////////////////////////////////////
  132.  
  133.  
  134.