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

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