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 / chap05 / polyline / dllpoly.cpp next >
C/C++ Source or Header  |  1996-05-21  |  7KB  |  341 lines

  1. /*
  2.  * DLLPOLY.CPP
  3.  * Polyline Component Chapter 5
  4.  *
  5.  * Polyline component object used in CoCosmo that supports a custom
  6.  * interface IPolyline.  Contains DLL entry code and the component
  7.  * object exports DllGetClassObject and DllCanUnloadNow and the
  8.  * class factory object.
  9.  *
  10.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  11.  *
  12.  * Kraig Brockschmidt, Microsoft
  13.  * Internet  :  kraigb@microsoft.com
  14.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  15.  */
  16.  
  17.  
  18. #define INITGUIDS
  19. #include "polyline.h"
  20.  
  21.  
  22. //Count number of objects and number of locks.
  23. ULONG       g_cObj=0;
  24. ULONG       g_cLock=0;
  25.  
  26. //DLL Instance handle
  27. HINSTANCE   g_hInst=0;
  28.  
  29.  
  30. /*
  31.  * LibMain32 (Win32)
  32.  *
  33.  * Purpose:
  34.  *  Entry point for Win32 DLLs that calls the Win16 LibMain.
  35.  */
  36.  
  37. #ifdef WIN32
  38. BOOL WINAPI LibMain32(HINSTANCE hInstance, ULONG ulReason
  39.     , LPVOID pvReserved)
  40.     {
  41.     if (DLL_PROCESS_DETACH==ulReason)
  42.         {
  43.         return TRUE;
  44.         }
  45.     else
  46.         {
  47.         if (DLL_PROCESS_ATTACH!=ulReason)
  48.             return TRUE;
  49.         }
  50.  
  51.     return (0!=LibMain(hInstance, 0,  0, NULL));
  52.     }
  53. #endif
  54.  
  55.  
  56.  
  57. /*
  58.  * LibMain (also called from Win32 LibMain32)
  59.  *
  60.  * Purpose:
  61.  *  DLL-specific entry point called from LibEntry.
  62.  */
  63.  
  64. int PASCAL LibMain(HINSTANCE hInst, WORD wDataSeg
  65.     , WORD cbHeapSize, LPSTR lpCmdLine)
  66.     {
  67.     WNDCLASS    wc;
  68.  
  69.     if (GetClassInfo(hInst, SZCLASSPOLYLINE, &wc))
  70.         return (int)hInst;
  71.  
  72.    #ifndef WIN32
  73.     if (0!=cbHeapSize)
  74.         UnlockData(0);
  75.    #endif
  76.  
  77.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  78.     wc.hInstance     = hInst;
  79.     wc.cbClsExtra    = 0;
  80.     wc.lpfnWndProc   = PolylineWndProc;
  81.     wc.cbWndExtra    = CBPOLYLINEWNDEXTRA;
  82.     wc.hIcon         = NULL;
  83.     wc.hCursor       = LoadCursor(NULL, IDC_CROSS);
  84.     wc.hbrBackground = NULL;
  85.     wc.lpszMenuName  = NULL;
  86.     wc.lpszClassName = SZCLASSPOLYLINE;
  87.  
  88.     if (!RegisterClass(&wc))
  89.         return 0;
  90.  
  91.     g_hInst=hInst;
  92.     return (int)hInst;
  93.     }
  94.  
  95.  
  96.  
  97.  
  98.  
  99. /*
  100.  * DllGetClassObject
  101.  *
  102.  * Purpose:
  103.  *  Provides an IClassFactory for a given CLSID that this DLL is
  104.  *  registered to support.  This DLL is placed under the CLSID
  105.  *  in the registration database as the InProcServer.
  106.  *
  107.  * Parameters:
  108.  *  clsID           REFCLSID that identifies the class factory
  109.  *                  desired.  Since this parameter is passed this
  110.  *                  DLL can handle any number of objects simply
  111.  *                  by returning different class factories here
  112.  *                  for different CLSIDs.
  113.  *
  114.  *  riid            REFIID specifying the interface the caller wants
  115.  *                  on the class object, usually IID_ClassFactory.
  116.  *
  117.  *  ppv             PPVOID in which to return the interface
  118.  *                  pointer.
  119.  *
  120.  * Return Value:
  121.  *  HRESULT         NOERROR on success, otherwise an error code.
  122.  */
  123.  
  124. HRESULT APIENTRY DllGetClassObject(REFCLSID rclsid
  125.     , REFIID riid, PPVOID ppv)
  126.     {
  127.     if (CLSID_Polyline5!=rclsid)
  128.         return ResultFromScode(E_FAIL);
  129.  
  130.     //Check that we can provide the interface
  131.     if (IID_IUnknown!=riid && IID_IClassFactory!=riid)
  132.         return ResultFromScode(E_NOINTERFACE);
  133.  
  134.     //Return our IClassFactory for Polyline objects
  135.     *ppv=new CPolylineClassFactory;
  136.  
  137.     if (NULL==*ppv)
  138.         return ResultFromScode(E_OUTOFMEMORY);
  139.  
  140.     //AddRef the object through any interface we return
  141.     ((LPUNKNOWN)*ppv)->AddRef();
  142.     g_cObj++;
  143.     return NOERROR;
  144.     }
  145.  
  146.  
  147.  
  148.  
  149.  
  150. /*
  151.  * DllCanUnloadNow
  152.  *
  153.  * Purpose:
  154.  *  Answers if the DLL can be freed, that is, if there are no
  155.  *  references to anything this DLL provides.
  156.  *
  157.  * Parameters:
  158.  *  None
  159.  *
  160.  * Return Value:
  161.  *  BOOL            TRUE if nothing is using us, FALSE otherwise.
  162.  */
  163.  
  164. STDAPI DllCanUnloadNow(void)
  165.     {
  166.     SCODE   sc;
  167.  
  168.     //Our answer is whether there are any object or locks
  169.     sc=(0L==g_cObj && 0L==g_cLock) ? S_OK : S_FALSE;
  170.     return ResultFromScode(sc);
  171.     }
  172.  
  173.  
  174.  
  175.  
  176. /*
  177.  * ObjectDestroyed
  178.  *
  179.  * Purpose:
  180.  *  Function for the Polyline object to call when it gets destroyed.
  181.  *  Since we're in a DLL we only track the number of objects here
  182.  *  letting DllCanUnloadNow take care of the rest.
  183.  */
  184.  
  185. void ObjectDestroyed(void)
  186.     {
  187.     g_cObj--;
  188.     return;
  189.     }
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196. /*
  197.  * CPolylineClassFactory::CPolylineClassFactory
  198.  * CPolylineClassFactory::~CPolylineClassFactory
  199.  */
  200.  
  201. CPolylineClassFactory::CPolylineClassFactory(void)
  202.     {
  203.     m_cRef=0L;
  204.     return;
  205.     }
  206.  
  207. CPolylineClassFactory::~CPolylineClassFactory(void)
  208.     {
  209.     return;
  210.     }
  211.  
  212.  
  213.  
  214.  
  215. /*
  216.  * CPolylineClassFactory::QueryInterface
  217.  * CPolylineClassFactory::AddRef
  218.  * CPolylineClassFactory::Release
  219.  */
  220.  
  221. STDMETHODIMP CPolylineClassFactory::QueryInterface(REFIID riid
  222.     , PPVOID ppv)
  223.     {
  224.     *ppv=NULL;
  225.  
  226.     if (IID_IUnknown==riid || IID_IClassFactory==riid)
  227.         *ppv=this;
  228.  
  229.     if (NULL!=*ppv)
  230.         {
  231.         ((LPUNKNOWN)*ppv)->AddRef();
  232.         return NOERROR;
  233.         }
  234.  
  235.     return ResultFromScode(E_NOINTERFACE);
  236.     }
  237.  
  238.  
  239. STDMETHODIMP_(ULONG) CPolylineClassFactory::AddRef(void)
  240.     {
  241.     return ++m_cRef;
  242.     }
  243.  
  244.  
  245. STDMETHODIMP_(ULONG) CPolylineClassFactory::Release(void)
  246.     {
  247.     if (0L!=--m_cRef)
  248.         return m_cRef;
  249.  
  250.     delete this;
  251.     ObjectDestroyed();
  252.     return 0L;
  253.     }
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261. /*
  262.  * CPolylineClassFactory::CreateInstance
  263.  *
  264.  * Purpose:
  265.  *  Instantiates a Polyline object.
  266.  *
  267.  * Parameters:
  268.  *  pUnkOuter       LPUNKNOWN to the controlling IUnknown if we
  269.  *                  are being used in an aggregation.
  270.  *  riid            REFIID identifying the interface the caller
  271.  *                  desires to have for the new object.
  272.  *  ppvObj          PPVOID in which to store the desired
  273.  *                  interface pointer for the new object.
  274.  *
  275.  * Return Value:
  276.  *  HRESULT         NOERROR if successful, otherwise E_NOINTERFACE
  277.  *                  if we cannot support the requested interface.
  278.  */
  279.  
  280. STDMETHODIMP CPolylineClassFactory::CreateInstance
  281.     (LPUNKNOWN pUnkOuter, REFIID riid, PPVOID ppvObj)
  282.     {
  283.     PCPolyline          pObj;
  284.     HRESULT             hr;
  285.  
  286.     *ppvObj=NULL;
  287.     hr=ResultFromScode(E_OUTOFMEMORY);
  288.  
  289.     //Verify that a controlling unknown asks for IUnknown
  290.     if (NULL!=pUnkOuter && IID_IUnknown!=riid)
  291.         return ResultFromScode(E_NOINTERFACE);
  292.  
  293.     //Create the object.  This also creates a window.
  294.     pObj=new CPolyline(pUnkOuter, ObjectDestroyed, g_hInst);
  295.  
  296.     if (NULL==pObj)
  297.         return hr;
  298.  
  299.     if (pObj->Init())
  300.         hr=pObj->QueryInterface(riid, ppvObj);
  301.  
  302.     //Kill the object if initial creation or Init failed.
  303.     if (FAILED(hr))
  304.         delete pObj;
  305.     else
  306.         g_cObj++;
  307.  
  308.     return hr;
  309.     }
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316. /*
  317.  * CPolylineClassFactory::LockServer
  318.  *
  319.  * Purpose:
  320.  *  Increments or decrements the lock count of the DLL.  If the lock
  321.  *  count goes to zero and there are no objects, the DLL is allowed
  322.  *  to unload.  See DllCanUnloadNow.
  323.  *
  324.  * Parameters:
  325.  *  fLock           BOOL specifying whether to increment or
  326.  *                  decrement the lock count.
  327.  *
  328.  * Return Value:
  329.  *  HRESULT         NOERROR always.
  330.  */
  331.  
  332. STDMETHODIMP CPolylineClassFactory::LockServer(BOOL fLock)
  333.     {
  334.     if (fLock)
  335.         g_cLock++;
  336.     else
  337.         g_cLock--;
  338.  
  339.     return NOERROR;
  340.     }
  341.