home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / atl / beeper / beepobj.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  2KB  |  85 lines

  1. // beepobj.cpp : Implementation of CBeeperApp and DLL registration.
  2. //
  3. // This is a part of the Active Template Library.
  4. // Copyright (C) 1996-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Active Template Library Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Active Template Library product.
  12.  
  13. #include "prebeep.h"
  14. #include "beeper.h"
  15. #include "beepobj.h"
  16.  
  17.  
  18. LPCOLESTR arr[] = {OLESTR("Hello"), OLESTR("World"),
  19.     OLESTR("From"), OLESTR("Beeper")};
  20.  
  21. /////////////////////////////////////////////////////////////////////////////
  22. //
  23.  
  24. CBeeper::CBeeper()
  25. {
  26.     m_nCnt = 0;
  27.     for (int i = 0; i < 4; i++)
  28.         m_var[i] = arr[i];
  29. }
  30.  
  31. STDMETHODIMP CBeeper::Beep()
  32. {
  33.     if (m_nCnt++ == 5)
  34.     {
  35.         m_nCnt = 0;
  36.         return Error(L"Too many beeps");
  37.     }
  38.     MessageBeep(0);
  39.     return S_OK;
  40. }
  41.  
  42. STDMETHODIMP CBeeper::get_Count(long* retval)
  43. {
  44.     if (retval == NULL)
  45.         return E_POINTER;
  46.     *retval = 4;
  47.     return S_OK;
  48. }
  49.  
  50. STDMETHODIMP CBeeper::get_Item(long Index, BSTR* pbstr)
  51. {
  52.     if (pbstr == NULL)
  53.         return E_POINTER;
  54.     if (Index > 4)
  55.         *pbstr = NULL;
  56.     *pbstr = SysAllocString(arr[Index-1]);
  57.     return S_OK;
  58. }
  59.  
  60. STDMETHODIMP CBeeper::get__NewEnum(IUnknown** retval)
  61. {
  62.     if (retval == NULL)
  63.         return E_POINTER;
  64.     *retval = NULL;
  65.     typedef CComObject<CComEnum<IEnumVARIANT, &IID_IEnumVARIANT, VARIANT,
  66.         _Copy<VARIANT> > > enumvar;
  67.     HRESULT hRes = S_OK;
  68.     enumvar* p = new enumvar;
  69.     if (p == NULL)
  70.         hRes = E_OUTOFMEMORY;
  71.     else
  72.     {
  73.         hRes = p->FinalConstruct();
  74.         if (hRes == S_OK)
  75.         {
  76.             hRes = p->Init(&m_var[0], &m_var[4], NULL, AtlFlagCopy);
  77.             if (hRes == S_OK)
  78.                 hRes = p->QueryInterface(IID_IUnknown, (void**)retval);
  79.         }
  80.     }
  81.     if (hRes != S_OK)
  82.         delete p;
  83.     return hRes;
  84. }
  85.