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 / chap20 / lnkassis / dllassis.cpp next >
Encoding:
C/C++ Source or Header  |  1996-05-21  |  5.1 KB  |  268 lines

  1. /*
  2.  * DLLASSIS.CPP
  3.  * Links Assistant Chapter 20
  4.  *
  5.  * DLL housing for the LinksAssistant component object for
  6.  * container applications that use the Links dialog.
  7.  *
  8.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Microsoft
  11.  * Internet  :  kraigb@microsoft.com
  12.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  13.  */
  14.  
  15.  
  16. #define INITGUIDS
  17. #include "dllassis.h"
  18.  
  19.  
  20. //Count number of objects and number of locks.
  21. ULONG       g_cObj=0;
  22. ULONG       g_cLock=0;
  23.  
  24.  
  25.  
  26. /*
  27.  * LibMain(32)
  28.  *
  29.  * Purpose:
  30.  *  Entry point conditionally compiled for Win32 and Windows
  31.  *  3.1.  Provides the proper structure for each environment.
  32.  */
  33.  
  34. #ifdef WIN32
  35. BOOL WINAPI LibMain32(HINSTANCE hInstance, ULONG ulReason
  36.     , LPVOID pvReserved)
  37.     {
  38.     if (DLL_PROCESS_DETACH==ulReason)
  39.         {
  40.         return TRUE;
  41.         }
  42.     else
  43.         {
  44.         if (DLL_PROCESS_ATTACH!=ulReason)
  45.             return TRUE;
  46.         }
  47.  
  48.     return TRUE;
  49.     }
  50. #else
  51. int PASCAL LibMain(HINSTANCE hInstance, WORD wDataSeg
  52.     , WORD cbHeapSize, LPSTR lpCmdLine)
  53.     {
  54.     if (0!=cbHeapSize)
  55.         UnlockData(0);
  56.  
  57.     return (int)hInstance;
  58.     }
  59. #endif
  60.  
  61.  
  62.  
  63.  
  64. /*
  65.  * DllGetClassObject
  66.  * DllCanUnloadNow
  67.  *
  68.  * Purpose:
  69.  *  Standard exports for DLL component objects.
  70.  */
  71.  
  72. HRESULT APIENTRY DllGetClassObject(REFCLSID rclsid
  73.     , REFIID riid, PPVOID ppv)
  74.     {
  75.     if (CLSID_LinksAssistant!=rclsid)
  76.         return ResultFromScode(E_FAIL);
  77.  
  78.     if (IID_IUnknown!=riid && IID_IClassFactory!=riid)
  79.         return ResultFromScode(E_NOINTERFACE);
  80.  
  81.     *ppv=new CLinksClassFactory();
  82.  
  83.     if (NULL==*ppv)
  84.         return ResultFromScode(E_OUTOFMEMORY);
  85.  
  86.     ((LPUNKNOWN)*ppv)->AddRef();
  87.     g_cObj++;
  88.     return NOERROR;
  89.     }
  90.  
  91.  
  92. STDAPI DllCanUnloadNow(void)
  93.     {
  94.     SCODE   sc;
  95.  
  96.     sc=(0L==g_cObj && 0L==g_cLock) ? S_OK : S_FALSE;
  97.     return ResultFromScode(sc);
  98.     }
  99.  
  100.  
  101.  
  102.  
  103. /*
  104.  * ObjectDestroyed
  105.  *
  106.  * Purpose:
  107.  *  Function for the object to call when it gets destroyed.
  108.  *  Since we're in a DLL we only track the number of objects here
  109.  *  letting DllCanUnloadNow take care of the rest.
  110.  */
  111.  
  112. void ObjectDestroyed(void)
  113.     {
  114.     g_cObj--;
  115.     return;
  116.     }
  117.  
  118.  
  119.  
  120.  
  121.  
  122. /*
  123.  * CLinksClassFactory::CLinksClassFactory
  124.  * CLinksClassFactory::~CLinksClassFactory
  125.  */
  126.  
  127. CLinksClassFactory::CLinksClassFactory(void)
  128.     {
  129.     m_cRef=0L;
  130.     return;
  131.     }
  132.  
  133.  
  134. CLinksClassFactory::~CLinksClassFactory(void)
  135.     {
  136.     return;
  137.     }
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144. /*
  145.  * CLinksClassFactory::QueryInterface
  146.  * CLinksClassFactory::AddRef
  147.  * CLinksClassFactory::Release
  148.  */
  149.  
  150. STDMETHODIMP CLinksClassFactory::QueryInterface(REFIID riid
  151.     , PPVOID ppv)
  152.     {
  153.     *ppv=NULL;
  154.  
  155.     if (IID_IUnknown==riid || IID_IClassFactory==riid)
  156.         *ppv=this;
  157.  
  158.     if (NULL!=*ppv)
  159.         {
  160.         ((LPUNKNOWN)*ppv)->AddRef();
  161.         return NOERROR;
  162.         }
  163.  
  164.     return ResultFromScode(E_NOINTERFACE);
  165.     }
  166.  
  167.  
  168. STDMETHODIMP_(ULONG) CLinksClassFactory::AddRef(void)
  169.     {
  170.     return ++m_cRef;
  171.     }
  172.  
  173.  
  174. STDMETHODIMP_(ULONG) CLinksClassFactory::Release(void)
  175.     {
  176.     if (0L!=--m_cRef)
  177.         return m_cRef;
  178.  
  179.     delete this;
  180.     ObjectDestroyed();
  181.     return 0;
  182.     }
  183.  
  184.  
  185.  
  186.  
  187.  
  188. /*
  189.  * CLinksClassFactory::CreateInstance
  190.  *
  191.  * Purpose:
  192.  *  Instantiates a Links Assistant object that supports the
  193.  *  IOleUILinkContainer interface for helping container do the
  194.  *  Links Dialog.
  195.  *
  196.  * Parameters:
  197.  *  pUnkOuter       LPUNKNOWN to the controlling IUnknown if we
  198.  *                  are being used in an aggregation.
  199.  *  riid            REFIID identifying the interface the caller
  200.  *                  desires to have for the new object.
  201.  *  ppvObj          PPVOID in which to store the desired
  202.  *                  interface pointer for the new object.
  203.  *
  204.  * Return Value:
  205.  *  HRESULT         NOERROR if successful, otherwise contains
  206.  *                  E_NOINTERFACE if we cannot support the
  207.  *                  requested interface.
  208.  */
  209.  
  210. STDMETHODIMP CLinksClassFactory::CreateInstance(LPUNKNOWN pUnkOuter
  211.     , REFIID riid, PPVOID ppvObj)
  212.     {
  213.     PCLinks             pObj;
  214.     HRESULT             hr;
  215.  
  216.     *ppvObj=NULL;
  217.     hr=ResultFromScode(E_OUTOFMEMORY);
  218.  
  219.     //We don't support aggregation
  220.     if (NULL!=pUnkOuter)
  221.         return ResultFromScode(CLASS_E_NOAGGREGATION);
  222.  
  223.     pObj=new CLinks(ObjectDestroyed);
  224.  
  225.     if (NULL==pObj)
  226.         return hr;
  227.  
  228.     hr=pObj->QueryInterface(riid, ppvObj);
  229.  
  230.     if (FAILED(hr))
  231.         delete pObj;
  232.     else
  233.         g_cObj++;
  234.  
  235.     return hr;
  236.     }
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243. /*
  244.  * CLinksClassFactory::LockServer
  245.  *
  246.  * Purpose:
  247.  *  Increments or decrements the lock count of the DLL.  If the lock
  248.  *  count goes to zero and there are no objects, the DLL is allowed
  249.  *  to unload.  See DllCanUnloadNow.
  250.  *
  251.  * Parameters:
  252.  *  fLock           BOOL specifying whether to increment or
  253.  *                  decrement the lock count.
  254.  *
  255.  * Return Value:
  256.  *  HRESULT         NOERROR always.
  257.  */
  258.  
  259. STDMETHODIMP CLinksClassFactory::LockServer(BOOL fLock)
  260.     {
  261.     if (fLock)
  262.         g_cLock++;
  263.     else
  264.         g_cLock--;
  265.  
  266.     return NOERROR;
  267.     }
  268.