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

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