DBEEPER.CPP
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:4k
源码类别:

Windows编程

开发平台:

Visual C++

  1. /*
  2.  * DBEEPER.CPP
  3.  * Beeper Automation Object Chapter 14
  4.  *
  5.  * Server module code for the Beeper object.
  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. #define INITGUIDS
  14. #include "dbeeper.h"
  15. //Count number of objects and number of locks.
  16. ULONG       g_cObj=0;
  17. ULONG       g_cLock=0;
  18. /*
  19.  * LibMain(32)
  20.  *
  21.  * Purpose:
  22.  *  Entry point conditionally compiled for Win32 and Windows
  23.  *  3.1.  Provides the proper structure for each environment.
  24.  */
  25. #ifdef WIN32
  26. BOOL WINAPI LibMain32(HINSTANCE hInstance, ULONG ulReason
  27.     , LPVOID pvReserved)
  28.     {
  29.     if (DLL_PROCESS_DETACH==ulReason)
  30.         {
  31.         return TRUE;
  32.         }
  33.     else
  34.         {
  35.         if (DLL_PROCESS_ATTACH!=ulReason)
  36.             return TRUE;
  37.         }
  38.     return TRUE;
  39.     }
  40. #else
  41. int PASCAL LibMain(HINSTANCE hInstance, WORD wDataSeg
  42.     , WORD cbHeapSize, LPSTR lpCmdLine)
  43.     {
  44.     if (0!=cbHeapSize)
  45.         UnlockData(0);
  46.     return (int)hInstance;
  47.     }
  48. #endif
  49. /*
  50.  * DllGetClassObject
  51.  * DllCanUnloadNow
  52.  * Standard COM exports for DLL servers.
  53.  */
  54. HRESULT APIENTRY DllGetClassObject(REFCLSID rclsid, REFIID riid
  55.     , PPVOID ppv)
  56.     {
  57.     CBeeperFactory *pBF;
  58.     HRESULT         hr;
  59.     if (CLSID_Beeper!=rclsid)
  60.         return ResultFromScode(E_FAIL);
  61.     //Check that we can provide the interface
  62.     if (IID_IUnknown!=riid && IID_IClassFactory!=riid)
  63.         return ResultFromScode(E_NOINTERFACE);
  64.     //Return our beeper factory's IClassFactory
  65.     pBF=new CBeeperFactory();
  66.     if (NULL==pBF)
  67.         return ResultFromScode(E_OUTOFMEMORY);
  68.     //If the factory hasn't the interface, delete it
  69.     hr=pBF->QueryInterface(riid, ppv);
  70.     if (FAILED(hr))
  71.         delete pBF;
  72.     return hr;
  73.     }
  74. STDAPI DllCanUnloadNow(void)
  75.     {
  76.     SCODE   sc;
  77.     //Our answer is whether there are any object or locks
  78.     sc=(0L==g_cObj && 0L==g_cLock) ? S_OK : S_FALSE;
  79.     return ResultFromScode(sc);
  80.     }
  81. /*
  82.  * ObjectDestroyed
  83.  *
  84.  * Purpose:
  85.  *  Function for the object to call when it gets destroyed.  Since
  86.  *  we're in a DLL we only track the number of objects here, letting
  87.  *  DllCanUnloadNow take care of the rest.
  88.  */
  89. void ObjectDestroyed(void)
  90.     {
  91.     g_cObj--;
  92.     return;
  93.     }
  94. /*
  95.  * CBeeperFactory::CBeeperFactory
  96.  * CBeeperFactory::~CBeeperFactory
  97.  */
  98. CBeeperFactory::CBeeperFactory(void)
  99.     {
  100.     m_cRef=0L;
  101.     return;
  102.     }
  103. CBeeperFactory::~CBeeperFactory(void)
  104.     {
  105.     return;
  106.     }
  107. /*
  108.  * CBeeperFactory::QueryInterface
  109.  * CBeeperFactory::AddRef
  110.  * CBeeperFactory::Release
  111.  */
  112. STDMETHODIMP CBeeperFactory::QueryInterface(REFIID riid, PPVOID ppv)
  113.     {
  114.     *ppv=NULL;
  115.     if (IID_IUnknown==riid || IID_IClassFactory==riid)
  116.         *ppv=this;
  117.     if (NULL!=*ppv)
  118.         {
  119.         ((LPUNKNOWN)*ppv)->AddRef();
  120.         return NOERROR;
  121.         }
  122.     return ResultFromScode(E_NOINTERFACE);
  123.     }
  124. STDMETHODIMP_(ULONG) CBeeperFactory::AddRef(void)
  125.     {
  126.     return ++m_cRef;
  127.     }
  128. STDMETHODIMP_(ULONG) CBeeperFactory::Release(void)
  129.     {
  130.     if (0!=--m_cRef)
  131.         return m_cRef;
  132.     delete this;
  133.     ObjectDestroyed();
  134.     return 0;
  135.     }
  136. /*
  137.  * CBeeperFactory::CreateInstance
  138.  * CBeeperFactory::LockServer
  139.  */
  140. STDMETHODIMP CBeeperFactory::CreateInstance(LPUNKNOWN pUnkOuter
  141.     , REFIID riid, PPVOID ppvObj)
  142.     {
  143.     PCBeeper            pObj;
  144.     HRESULT             hr;
  145.     *ppvObj=NULL;
  146.     hr=ResultFromScode(E_OUTOFMEMORY);
  147.     //Verify that a controlling unknown asks for IUnknown
  148.     if (NULL!=pUnkOuter && IID_IUnknown!=riid)
  149.         return ResultFromScode(CLASS_E_NOAGGREGATION);
  150.     //Create the object passing function to notify on destruction.
  151.     pObj=new CBeeper(pUnkOuter, ObjectDestroyed);
  152.     if (NULL==pObj)
  153.         return hr;
  154.     if (pObj->Init())
  155.         hr=pObj->QueryInterface(riid, ppvObj);
  156.     //Kill the object if initial creation or Init failed.
  157.     if (FAILED(hr))
  158.         delete pObj;
  159.     else
  160.         g_cObj++;
  161.     return hr;
  162.     }
  163. STDMETHODIMP CBeeperFactory::LockServer(BOOL fLock)
  164.     {
  165.     if (fLock)
  166.         g_cLock++;
  167.     else
  168.         g_cLock--;
  169.     return NOERROR;
  170.     }